Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Split from PR 958: restoring the __import__ after shutdown. #993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 21, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 54 additions & 13 deletions src/runtime/importhook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,64 @@ internal static void InitializeModuleDef()
#endif

/// <summary>
/// Initialization performed on startup of the Python runtime.
/// Get a <i>New reference</i> to the builtins module.
/// </summary>
internal static void Initialize()
static IntPtr GetNewRefToBuiltins()
{
// Initialize the Python <--> CLR module hook. We replace the
// built-in Python __import__ with our own. This isn't ideal,
// but it provides the most "Pythonic" way of dealing with CLR
// modules (Python doesn't provide a way to emulate packages).
IntPtr dict = Runtime.PyImport_GetModuleDict();
if (Runtime.IsPython3)
{
return Runtime.PyImport_ImportModule("builtins");
}
else
{
// dict is a borrowed ref, no need to decref
IntPtr dict = Runtime.PyImport_GetModuleDict();

IntPtr mod = Runtime.IsPython3
? Runtime.PyImport_ImportModule("builtins")
: Runtime.PyDict_GetItemString(dict, "__builtin__");
// GetItemString is a borrowed ref; incref to get a new ref
IntPtr builtins = Runtime.PyDict_GetItemString(dict, "__builtin__");
Runtime.XIncref(builtins);
return builtins;
}
}

py_import = Runtime.PyObject_GetAttrString(mod, "__import__");
/// <summary>
/// Initialize just the __import__ hook itself.
/// </summary>
static void InitImport()
{
// We replace the built-in Python __import__ with our own: first
// look in CLR modules, then if we don't find any call the default
// Python __import__.
IntPtr builtins = GetNewRefToBuiltins();
py_import = Runtime.PyObject_GetAttrString(builtins, "__import__");
hook = new MethodWrapper(typeof(ImportHook), "__import__", "TernaryFunc");
Runtime.PyObject_SetAttrString(mod, "__import__", hook.ptr);
Runtime.PyObject_SetAttrString(builtins, "__import__", hook.ptr);
Runtime.XDecref(hook.ptr);
Runtime.XDecref(builtins);
}

/// <summary>
/// Restore the __import__ hook.
/// </summary>
static void RestoreImport()
{
IntPtr builtins = GetNewRefToBuiltins();

Runtime.PyObject_SetAttrString(builtins, "__import__", py_import);
Runtime.XDecref(py_import);
py_import = IntPtr.Zero;

Runtime.XDecref(builtins);
}

/// <summary>
/// Initialization performed on startup of the Python runtime.
/// </summary>
internal static void Initialize()
{
InitImport();

// Initialize the clr module and tell Python about it.
root = new CLRModule();

#if PYTHON3
Expand All @@ -62,6 +101,7 @@ internal static void Initialize()
Runtime.XIncref(root.pyHandle); // we are using the module two times
py_clr_module = root.pyHandle; // Alias handle for PY2/PY3
#endif
IntPtr dict = Runtime.PyImport_GetModuleDict();
Runtime.PyDict_SetItemString(dict, "CLR", py_clr_module);
Runtime.PyDict_SetItemString(dict, "clr", py_clr_module);
}
Expand All @@ -74,9 +114,10 @@ internal static void Shutdown()
{
if (Runtime.Py_IsInitialized() != 0)
{
RestoreImport();

Runtime.XDecref(py_clr_module);
Runtime.XDecref(root.pyHandle);
Runtime.XDecref(py_import);
}
}

Expand Down