diff --git a/AUTHORS.md b/AUTHORS.md index 26285bf6a..293f0f666 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -60,6 +60,7 @@ - William Sardar ([@williamsardar])(https://github.com/williamsardar) - Xavier Dupré ([@sdpython](https://github.com/sdpython)) - Zane Purvis ([@zanedp](https://github.com/zanedp)) +- ([@amos402]https://github.com/amos402) - ([@bltribble](https://github.com/bltribble)) - ([@civilx64](https://github.com/civilx64)) - ([@GSPP](https://github.com/GSPP)) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fd2b1dcf..7b3bba9f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,6 +58,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][]. - PythonEngine.Intialize will now call `Py_InitializeEx` with a default value of 0, so signals will not be configured by default on embedding. This is different from the previous behaviour, where `Py_Initialize` was called instead, which sets initSigs to 1. ([#449][i449]) - Refactored MethodBinder.Bind in preparation to make it extensible (#829) - Look for installed Windows 10 sdk's during installation instead of relying on specific versions. +- Remove `LoadLibrary` call. ([#880][p880]) ### Fixed diff --git a/src/runtime/runtime.cs b/src/runtime/runtime.cs index 7748bafa9..7da70ddcd 100644 --- a/src/runtime/runtime.cs +++ b/src/runtime/runtime.cs @@ -310,26 +310,18 @@ internal static void Initialize(bool initSigs = false) Error = new IntPtr(-1); + _PyObject_NextNotImplemented = Get_PyObject_NextNotImplemented(); + { + IntPtr sys = PyImport_ImportModule("sys"); + PyModuleType = PyObject_Type(sys); + XDecref(sys); + } + // Initialize data about the platform we're running on. We need // this for the type manager and potentially other details. Must // happen after caching the python types, above. InitializePlatformData(); - IntPtr dllLocal = IntPtr.Zero; - var loader = LibraryLoader.Get(OperatingSystem); - - if (_PythonDll != "__Internal") - { - dllLocal = loader.Load(_PythonDll); - } - _PyObject_NextNotImplemented = loader.GetFunction(dllLocal, "_PyObject_NextNotImplemented"); - PyModuleType = loader.GetFunction(dllLocal, "PyModule_Type"); - - if (dllLocal != IntPtr.Zero) - { - loader.Free(dllLocal); - } - // Initialize modules that depend on the runtime class. AssemblyManager.Initialize(); PyCLRMetaType = MetaType.Initialize(); @@ -346,6 +338,14 @@ internal static void Initialize(bool initSigs = false) AssemblyManager.UpdatePath(); } + private static IntPtr Get_PyObject_NextNotImplemented() + { + IntPtr pyType = SlotHelper.CreateObjectType(); + IntPtr iternext = Marshal.ReadIntPtr(pyType, TypeOffset.tp_iternext); + Runtime.XDecref(pyType); + return iternext; + } + /// /// Initializes the data about platforms. /// @@ -1964,13 +1964,15 @@ internal static IntPtr PyMem_Realloc(IntPtr ptr, long size) internal static void SetNoSiteFlag() { var loader = LibraryLoader.Get(OperatingSystem); - IntPtr dllLocal; if (_PythonDll != "__Internal") { dllLocal = loader.Load(_PythonDll); + if (dllLocal == IntPtr.Zero) + { + throw new Exception($"Cannot load {_PythonDll}"); + } } - try { Py_NoSiteFlag = loader.GetFunction(dllLocal, "Py_NoSiteFlag"); diff --git a/src/runtime/typemanager.cs b/src/runtime/typemanager.cs index bb920b74f..c541f9dc2 100644 --- a/src/runtime/typemanager.cs +++ b/src/runtime/typemanager.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; @@ -820,4 +821,37 @@ internal static void CopySlot(IntPtr from, IntPtr to, int offset) Marshal.WriteIntPtr(to, offset, fp); } } + + + static class SlotHelper + { + public static IntPtr CreateObjectType() + { + IntPtr globals = Runtime.PyDict_New(); + if (Runtime.PyDict_SetItemString(globals, "__builtins__", Runtime.PyEval_GetBuiltins()) != 0) + { + Runtime.XDecref(globals); + throw new PythonException(); + } + const string code = "class A(object): pass"; + IntPtr res = Runtime.PyRun_String(code, (IntPtr)RunFlagType.File, globals, globals); + if (res == IntPtr.Zero) + { + try + { + throw new PythonException(); + } + finally + { + Runtime.XDecref(globals); + } + } + Runtime.XDecref(res); + IntPtr A = Runtime.PyDict_GetItemString(globals, "A"); + Debug.Assert(A != IntPtr.Zero); + Runtime.XIncref(A); + Runtime.XDecref(globals); + return A; + } + } }