Closed wally96334 closed 7 months ago
The errno
module is defined in IronPython.Modules.dll
so I'm guessing that's not getting loaded up properly in your app domain. Unfortunately I haven't been able to reproduce your issue. Could you perhaps provide a more complete example?
This is a stripped-down version of my code:
//##############################################
//##############################################
internal class CLS_app_domain_contexts
{
private CLS_Iron_Python_app_domain_context
V___Iron_Python_app_domain_context;
private AppDomain
V__IronPython_NET_AppDomain;
//==============================================
public CLS_app_domain_contexts ()
{
V___app_domain_names = new STR_app_domain_names (99);
V__IronPython_NET_AppDomain
= M_create_domain
(V___app_domain_names.G_IronPython_NET_AppDomain_name);
/** tried >>> M_create_instance_and_unwrap<CLS_Iron_Python_app_domain_context> */
V___Iron_Python_app_domain_context
= M_create_instance_and_unwrap<CLS_Iron_Python_app_domain_context>
(V__IronPython_NET_AppDomain);
/***/
/** also tried >>> M_create_IronPython_instance_and_unwrap */
/** * /
V___Iron_Python_app_domain_context
= M_create_IronPython_instance_and_unwrap
(V__IronPython_NET_AppDomain);
/***/
}
//==============================================
//==============================================
private AppDomain M_create_domain (string P_domain_name)
{
return AppDomain.CreateDomain(P_domain_name);
}
//==============================================
private T_class_type
M_create_instance_and_unwrap<T_class_type>
(AppDomain P_AppDomain)
{
Type L_class_type = typeof(T_class_type);
return
(T_class_type)
P_AppDomain.CreateInstanceAndUnwrap(
//Gets the display name of the assembly.
L_class_type.Assembly.FullName,
// Gets the fully qualified name of the type,
// including its namespace
// but not its assembly.
L_class_type.FullName);
}
//==============================================
//==============================================
private CLS_Iron_Python_app_domain_context
M_create_IronPython_instance_and_unwrap
(AppDomain P_AppDomain)
{
object
U_S_Instance_LIB_DLL
= Activator
.CreateInstance
(P_AppDomain,
"Iron_Python_3_4_DSKT_02, Version=1.0.0.0,"
+ " Culture=neutral, PublicKeyToken=null",
"Iron_Python_3_4_LIB_02.CLS_Iron_Python_app_domain_context")
.Unwrap();
P_AppDomain
.Load ("Iron_Python_3_4_DSKT_02, Version=1.0.0.0,"
+ " Culture=neutral, PublicKeyToken=null");
return U_S_Instance_LIB_DLL as CLS_Iron_Python_app_domain_context;
}
//==============================================
//==============================================
internal object [] M_AD_execute_script_Iron_Python_3
(cls_CSI_eval_01_NTRF [] P_ARY_CSI_eval_01_NTRF)
{
object [] L_return_values = null;
int L_return_value_last_ndx;
STR_exception_std_arg
L2_exception_std_arg,
L_exception_std_arg;
try
{
#if show_messages
MessageBox.Show ("M_AD_execute_script_Iron_Python_3 call ..." +
$" <{P_ARY_CSI_eval_01_NTRF[0].G_code}>");
#endif
V___Iron_Python_app_domain_context
.M_IPAD_execute_script_Iron_Python_3 (P_ARY_CSI_eval_01_NTRF);
// EXCEPTION THROWN on this call >
L_return_values
= V___Iron_Python_app_domain_context.M_IPAD_get_result_set ();
#if show_messages
MessageBox.Show ("M_AD_execute_script_Iron_Python_3 " +
$" L_return_values == null <{L_return_values == null}>");
#endif
if (L_return_values != null)
if ( (L_return_value_last_ndx = L_return_values.Length) > 1 )
if ( L_return_values[--L_return_value_last_ndx] is Exception )
{
Exception
L_xcpt = L_return_values[L_return_value_last_ndx] as Exception;
string [] L_ARY_mssgs = { };
L2_exception_std_arg = new STR_exception_std_arg ();
V___Iron_Python_app_domain_context
.M_IPAD_get_exception_std_arg (ref L2_exception_std_arg);
KwK_exception
L_KwK_exception = new KwK_exception (ref L2_exception_std_arg);
//MessageBox.Show ("U_XSA_link" + L2_exception_std_arg.U_XSA_link);
L_exception_std_arg
= new STR_exception_std_arg
(P_XSA_mssg : L_xcpt.Message,
P_XSA_ARY_mssgs : L_ARY_mssgs,
P_XSA_Exception : L_xcpt,
P_XSA_exception_type
: ENM_exception_type.e38_app_domain_execution_Exception
| ENM_exception_type.e12_fatal_error_Exception);
L_KwK_exception
.add_message_link_set (ref L_exception_std_arg);
throw L_KwK_exception;
}
return L_return_values;
}
catch (Exception L_xcpt)
{
string [] L2_ARY_mssgs = {
$" L_return_values is null "
+ $"<{L_return_values == null}>",
$" V___Iron_Python_app_domain_context is null"
+ $" <{V___Iron_Python_app_domain_context == null}>"
};
#if show_messages
MessageBox.Show ("M_AD_execute_script_Iron_Python_3 " +
$" V__IronPython_NET_AppDomain == null <{V__IronPython_NET_AppDomain == null}>\n"
+ "M_AD_execute_script_Iron_Python_3 " +
$" V___Iron_Python_app_domain_context == null <{V___Iron_Python_app_domain_context == null}>");
#endif
STR_exception_std_arg
L_xcpt_args = new STR_exception_std_arg
(P_XSA_mssg : L_xcpt.Message,
P_XSA_ARY_mssgs : L2_ARY_mssgs,
P_XSA_Exception : L_xcpt,
P_XSA_exception_type :
ENM_exception_type.e38_app_domain_execution_Exception
| ENM_exception_type.e12_fatal_error_Exception);
throw new KwK_exception (ref L_xcpt_args);
}
}
//==============================================
//==============================================
//##############################################
//##############################################
//##############################################
//##############################################
internal static class CLS_S_code_execution
{
//==============================================
internal static string
M_generate_call_code_execution (string P_code)
{
string L_code = string.Format("\"\"\"{0}\"\"\"", P_code);
return "GBL_CExeCtx_code_execution_facility"
+ $".EXECUTE_PYTHON_CODE({L_code})";
}
//==============================================
internal static readonly string
def_code_execution_class
//= @"#!/usr/bin/python3
//# latin-1
= @"
import sys
import traceback
import smtplib
import json
class CLS_CExeCtx_source_for_generic_objects(dict):
def __init__(self):
dict.__init__(self)
def to_JSON (self):
return json.dumps(self.__dict__, sort_keys=False, indent=4)
# global GBL_CExeCtx_Extern_Execution_Context_Object
GBL_CExeCtx_Extern_Execution_Context_Object = CLS_CExeCtx_source_for_generic_objects ()
class CLS_CExeCtx_code_execution_facility:
def __init__ (self):
pass
def gather_data_for_error (self, P_xcpt):
self.L_error_obj = CLS_CExeCtx_source_for_generic_objects ()
self.L_error_class = P_xcpt.__class__.__name__
self.L_error_obj.sys_error_repr = str(repr(P_xcpt))
self.L_error_obj.sys_error_class = str(self.L_error_class)
self.detail = P_xcpt.args[0]
self.cl, self.exc, self.tb = sys.exc_info()
self.line_number = traceback.extract_tb(self.tb)[-1][1]
self.L_error_obj.sys_error_message = str(self.detail)
self.L_error_obj.sys_error_line_number = self.line_number
self.L_error_obj.return_mssg_error_class = '*** error class is <' + self.L_error_class + '>'
self.L_error_obj.traceback = traceback.format_exc()
def EXECUTE_PYTHON_CODE (self, P_fn_to_ececute):
try:
exec (P_fn_to_ececute)
GBL_CExeCtx_Extern_Execution_Context_Object \
.sys_execution_successful = True
except Exception as L_xcpt:
GBL_CExeCtx_Extern_Execution_Context_Object \
.sys_execution_successful = False
self.gather_data_for_error (L_xcpt)
GBL_CExeCtx_Extern_Execution_Context_Object \
.XV_Returned_Value_System_Variable \
= self.L_error_obj.to_JSON ()
GBL_CExeCtx_code_execution_facility = CLS_CExeCtx_code_execution_facility()
VPY_returned_val = ' +++++++++++++++++++ class GBL_CExeCtx_code_execution_facility'
";
}
//##############################################
[Serializable]
public class CLS_Iron_Python_app_domain_context : MarshalByRefObject
{
private CLS_Iron_Python_3 V__Iron_Python_3;
private object [] V__ARY_resultSet;
//==============================================
public CLS_Iron_Python_app_domain_context ()
{
V__Iron_Python_3 = new CLS_Iron_Python_3 ();
}
//==============================================
//==============================================
public void M_IPAD_execute_script_Iron_Python_3
(NTF_CSI_eval [] p_ARY_CSI_eval_01_NTRF)
{
V__ARY_resultSet
= V__Iron_Python_3
.M_IP3_IronPython_execute_all_code (p_ARY_CSI_eval_01_NTRF);
#if M_IP3_execute_script_LOG
MessageBox.Show ("M_IPAD_execute_script_Iron_Python_3 " +
$" L_ARY_resultSet == null <{V__ARY_resultSet == null}>");
if (V__ARY_resultSet != null)
MessageBox.Show ("M_IPAD_execute_script_Iron_Python_3 " +
$" L_ARY_resultSet Length <{V__ARY_resultSet.Length}>");
#endif
}
//==============================================
public object [] M_IPAD_get_result_set ()
{
#if M_IP3_execute_script_LOG
MessageBox.Show ("M_IPAD_get_result_set " +
$" V_ARY_resultSet == null <{V__ARY_resultSet == null}>");
if (V__ARY_resultSet != null)
MessageBox.Show ("M_IPAD_get_result_set " +
$" V_ARY_resultSet Length <{V__ARY_resultSet.Length}>");
#endif
return V__ARY_resultSet;
}
//==============================================
public void M_IPAD_get_exception_std_arg
(ref STR_exception_std_arg P_exception_std_arg)
{
V__Iron_Python_3
.M_IP3_get_exception_std_arg (ref P_exception_std_arg);
}
//==============================================
//==============================================
}
//##############################################
internal class CLS_Iron_Python_3 : MarshalByRefObject
{
private const string
V_C_package_path
= @"C:\Dev_LT_02\Solution\packages\IronPython.3.4.1\lib";
private const string
V_C_package_std_lib_path
= @"C:\Dev_LT_02\Solution\packages\IronPython.StdLib.3.4.1";
private const string
V_C_lib_path
= @"C:\Dev_LT_02\Solution\Iron_Python_3_4_DSKT_02\lib";
private ScriptEngine V__script_engine;
private ScriptScope V__scope;
//==============================================
public CLS_Iron_Python_3 ()
{
V__script_engine = Python.CreateEngine();
V__scope = V__script_engine.CreateScope();
var paths = V__script_engine.GetSearchPaths();
paths.Add(V_C_lib_path);
paths.Add(V_C_package_std_lib_path);
paths.Add(V_C_package_path);
V__script_engine.SetSearchPaths(paths);
v_LIST_IP3_resultSet = new List<object> ();
}
//==============================================
//==============================================
public object [] M_IP3_IronPython_execute_all_code
(NTF_CSI_eval [] p_ARY_CSI_eval_01_NTRF)
{
bool L_success;
STR_output_OBJ L_output_OBJ = new STR_output_OBJ ();
v_LIST_IP3_resultSet.Clear ();
// EXCEPTION THROWN on this call >
if ( M_IP3_execute_script
(CLS_S_code_execution.def_code_execution_class)
)
foreach (NTF_CSI_eval L_CSI_eval in p_ARY_CSI_eval_01_NTRF)
{
#if M_IP3_execute_script_LOG
MessageBox.Show ("M_IP3_IronPython_execute_all_code >" + L_CSI_eval.G_code);
#endif
L_success
= M_IP3_IronPy_execute_code
(L_CSI_eval.G_code, ref L_output_OBJ);
#if M_IP3_execute_script_LOG
MessageBox.Show ("L_success >" + L_success);
#endif
if ( ! L_success )
return v_LIST_IP3_resultSet.ToArray ();
L_output_OBJ.GS_CSI_eval = L_CSI_eval;
M_IP3_iron_python_upload_data (ref L_output_OBJ);
}
#if M_IP3_execute_script_LOG
MessageBox.Show ("M_IP3_IronPython_execute_all_code " +
$" v_LIST_IP3_resultSet == null <{v_LIST_IP3_resultSet == null}>");
#endif
return v_LIST_IP3_resultSet.ToArray ();
}
//==============================================
//==============================================
private bool M_IP3_execute_script (string P_code)
{
bool L_success;
ScriptSource L_source = null;
// EXCEPTION THROWN HERE >
try
{
L_source
= V__script_engine.CreateScriptSourceFromString (P_code);
L_source.Execute(V__scope);
L_success = true;
}
catch (Exception L_xcpt)
{
L_success = false;
#if M_IP3_execute_script_LOG
string L_path = @"C:\KwiKKumpute\.KwiKKumpute_logs\a001_exceptions.txt";
string L_mssg = "\n\n >>> " + DateTime.Now
+ "\n >>> " + L_xcpt.Message + "\n" + L_xcpt.StackTrace;
File.WriteAllText (L_path, L_mssg);
MessageBox.Show ("M_IP3_execute_script <1> >>>" + L_xcpt.Message);
#endif
string [] L_ARY_mssgs
= { P_code,
$"V__script_engine is null"
+ $" <{V__script_engine == null}>",
$"L_source is null <{L_source == null}>",
$"V__scope is null <{V__scope == null}>" };
V___exception_std_arg
= new STR_exception_std_arg
(P_XSA_mssg : L_xcpt.Message,
P_XSA_ARY_mssgs : L_ARY_mssgs,
P_XSA_Exception : L_xcpt,
P_XSA_exception_type
: ENM_exception_type.e38_app_domain_execution_Exception
| ENM_exception_type.e12_fatal_error_Exception);
// The ides here is to add the custom exception object to the list.
// That did not work, as the attempt to get the List of objects
// throws an exception.
v_LIST_IP3_resultSet.Add (L_xcpt);
}
return L_success;
}
//==============================================
//==============================================
private void M_IP3_set_return_object_values
(ref STR_output_OBJ PR_output_OBJ)
{
dynamic L_VPY_returned_val;
object L_return_value_obj;
L_VPY_returned_val
= V__scope.GetVariable<object>
("GBL_CExeCtx_Extern_Execution_Context_Object");
L_return_value_obj = L_VPY_returned_val.XV_Returned_Value_System_Variable;
PR_output_OBJ.GS_is_good = L_VPY_returned_val.sys_execution_successful;
PR_output_OBJ.M_try_parse_values (L_return_value_obj.ToString ());
}
//==============================================
//==============================================
}
//##############################################
//##############################################
The code above was working before being moved into an app domain.
I believe that I have figured it out. It was a silly mistake on my part.
The code for both the calling code from the main app domain and the app domain that is running IronPython are in (separate) libraries. I installed IronPython in the library, which is why it was building and running (sort of), but did not install it within the main project in which the app is actually running.
I've since installed it, and everything is good.
SORRY for being a nuisance!
No worries. Glad you figured it out!
Prerequisites
4.3.1
I think so.
Yes
I found this: https://github.com/IronLanguages/ironpython2/issues/789
I looked everywhere, but could not find the module.
Description
[Description of the bug or feature]
My WPF app is for executing Python code that is inputted by the user.
Initially, I was running the code in the same app domain. I realized that I needed to incorporate a "kill switch" of some kind in the event that the code is running for too long and the user wanted to abort execution. After coming to the realization that the "abort" method on threads had been deprecated, I moved the class to run in another app domain. That is when I got an exception, with the message "no module named errno". I have looked and cannot find the module "errno".
Here is the stack trace from the calling method within the main app domain.
Here is the stacktrace from the app domain running the IronPython code:
Steps to Reproduce
Run the code in another app domain.
Expected behavior:
Execution without exceptions.
Actual behavior:
see above
Version Information
If you are using the
ipy
console program, provide output of executingipy -VV
. If it is a local build, provide also the git hash of the commit used to build IronPython. In either case, provide the type of the operating system used.If you are using the IronPython engine embedded in a .NET application, provide the version number of the NuGet package used (or if it is a local build, the git hash of the commit used to build IronPython), and the following info:
sys.version
, from imported modulesys
.Edition Windows 10 Pro Version 22H2 Installed on 7/22/2020 OS build 19045.4046 Experience Windows Feature Experience Pack 1000.19053.1000.0
Microsoft Visual Studio Community 2017 Version 15.9.55 VisualStudio.15.Release/15.9.55+33801.237 Microsoft .NET Framework Version 4.8.09037
Installation was with NuGet Package Manager IronPython 3.4.1 IronPython.StdLib 3.4.1