FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix Re-initialization by filmor · Pull Request #343 · pythonnet/pythonnet · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cs  (3) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
24 changes: 11 additions & 13 deletions src/runtime/importhook.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ internal class ImportHook

#if PYTHON3
static IntPtr py_clr_module;
static IntPtr module_def;
static IntPtr module_def = IntPtr.Zero;

internal static void InitializeModuleDef()
{
if (module_def == IntPtr.Zero)
module_def = ModuleDefOffset.AllocModuleDef("clr");
}
#endif

//===================================================================
Expand Down Expand Up @@ -44,8 +50,8 @@ internal static void Initialize()
root = new CLRModule();

#if PYTHON3
// create a python module with the same methods as the clr module-like object
module_def = ModuleDefOffset.AllocModuleDef("clr");
// create a python module with the same methods as the clr module-like object
InitializeModuleDef();
py_clr_module = Runtime.PyModule_Create2(module_def, 3);

// both dicts are borrowed references
Expand All @@ -70,21 +76,13 @@ internal static void Initialize()

internal static void Shutdown()
{
#if PYTHON3
if (0 != Runtime.Py_IsInitialized()) {
#if PYTHON3
Runtime.XDecref(py_clr_module);
Runtime.XDecref(root.pyHandle);
}
ModuleDefOffset.FreeModuleDef(module_def);
#elif PYTHON2
if (0 != Runtime.Py_IsInitialized())
{
Runtime.XDecref(root.pyHandle);
Runtime.XDecref(root.pyHandle);
}
#endif
if (0 != Runtime.Py_IsInitialized())
{
Runtime.XDecref(root.pyHandle);
Runtime.XDecref(py_import);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/interop.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public static IntPtr AllocModuleDef(string modulename) {
byte[] ascii = Encoding.ASCII.GetBytes(modulename);
int size = name + ascii.Length + 1;
IntPtr ptr = Marshal.AllocHGlobal(size);
for (int i = 0; i <= m_free; i += IntPtr.Size)
for (int i = 0; i < m_free; i += IntPtr.Size)
Marshal.WriteIntPtr(ptr, i, IntPtr.Zero);
Marshal.Copy(ascii, 0, (IntPtr)(ptr + name), ascii.Length);
Marshal.WriteIntPtr(ptr, m_name, (IntPtr)(ptr + name));
Expand Down
59 changes: 40 additions & 19 deletions src/runtime/pythonengine.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -411,33 +411,54 @@ public static PyObject ModuleFromString(string name, string code)
/// executing the code string as a PyObject instance, or null if
/// an exception was raised.
/// </remarks>
public static PyObject RunString(string code)
public static PyObject RunString(
string code, IntPtr? globals = null, IntPtr? locals = null
)
{
IntPtr globals = Runtime.PyEval_GetGlobals();
IntPtr locals = Runtime.PyDict_New();

IntPtr builtins = Runtime.PyEval_GetBuiltins();
Runtime.PyDict_SetItemString(locals, "__builtins__", builtins);
bool borrowedGlobals = true;
if (globals == null)
{
globals = Runtime.PyEval_GetGlobals();
if (globals == IntPtr.Zero)
{
globals = Runtime.PyDict_New();
Runtime.PyDict_SetItemString(
globals.Value, "__builtins__",
Runtime.PyEval_GetBuiltins()
);
borrowedGlobals = false;
}
}

IntPtr flag = (IntPtr)257; /* Py_file_input */
IntPtr result = Runtime.PyRun_String(code, flag, globals, locals);
Runtime.XDecref(locals);
if (result == IntPtr.Zero)
bool borrowedLocals = true;
if (locals == null)
{
return null;
locals = Runtime.PyDict_New();
borrowedLocals = false;
}
return new PyObject(result);
}

public static PyObject RunString(string code, IntPtr globals, IntPtr locals)
{
IntPtr flag = (IntPtr)257; /* Py_file_input */
IntPtr result = Runtime.PyRun_String(code, flag, globals, locals);
if (result == IntPtr.Zero)

try
{
return null;
IntPtr result = Runtime.PyRun_String(
code, flag, globals.Value, locals.Value
);

if (Runtime.PyErr_Occurred() != 0)
{
throw new PythonException();
}

return new PyObject(result);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I don't really see the issue here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

To elaborate, the link that you posted warns against relying on try {} finally {} as the finally may not be executed, but in such a case the problem has either exited already or is in the process of doing so, so we don't have to worry about dereferencing the Python resources anyhow.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

It was more of a caveat, return statements within try statements confuse me at times.

}
finally
{
if (!borrowedLocals)
Runtime.XDecref(locals.Value);
if (!borrowedGlobals)
Runtime.XDecref(globals.Value);
}
return new PyObject(result);
}
}

Expand Down

Back | FazBrowse Home | New Git URL