-
Notifications
You must be signed in to change notification settings - Fork 755
Drop LoadLibrary #880
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
Drop LoadLibrary #880
Changes from 1 commit
d1928dc
f000e08
f882400
b7715ee
41ac665
c6dae9e
653a263
b3e889b
04d6dfb
5150e61
65e209e
431d644
2b84394
b7797d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
* Drop C module dependency when getting _PyObject_NextNotImplemented * Exception details for SetNoSiteFlag
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -292,26 +292,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); | ||
|
||
// Since `_PyObject_NextNotImplemented` would set to a heap class | ||
// for tp_iternext which doesn't implement __next__. | ||
// Thus we need a heap class to get it, the ZipImportError is a | ||
// heap class and it's in builtins, so we can use it as a trick. | ||
var zipimport = PyImport_ImportModule("zipimport"); | ||
var ZipImportError = PyObject_GetAttrString(zipimport, "ZipImportError"); | ||
_PyObject_NextNotImplemented = Marshal.ReadIntPtr(ZipImportError, TypeOffset.tp_iternext); | ||
XDecref(ZipImportError); | ||
XDecref(zipimport); | ||
PyModuleType = loader.GetFunction(dllLocal, "PyModule_Type"); | ||
|
||
|
||
// Initialize modules that depend on the runtime class. | ||
AssemblyManager.Initialize(); | ||
PyCLRMetaType = MetaType.Initialize(); | ||
|
@@ -328,6 +320,29 @@ internal static void Initialize(bool initSigs = false) | |
AssemblyManager.UpdatePath(); | ||
} | ||
|
||
private static IntPtr Get_PyObject_NextNotImplemented() | ||
{ | ||
IntPtr globals = PyDict_New(); | ||
if (PyDict_SetItemString(globals, "__builtins__", PyEval_GetBuiltins()) != 0) | ||
{ | ||
XDecref(globals); | ||
throw new PythonException(); | ||
} | ||
const string code = "class A(object): pass"; | ||
IntPtr res = PyRun_String(code, (IntPtr)RunFlagType.File, globals, globals); | ||
if (res == IntPtr.Zero) | ||
{ | ||
XDecref(globals); | ||
throw new PythonException(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might want to construct an instance of |
||
} | ||
XDecref(res); | ||
IntPtr A = PyDict_GetItemString(globals, "A"); | ||
IntPtr iternext = Marshal.ReadIntPtr(A, TypeOffset.tp_iternext); | ||
XDecref(globals); | ||
XDecref(A); | ||
return iternext; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes the data about platforms. | ||
/// | ||
|
@@ -1893,25 +1908,24 @@ internal static IntPtr PyMem_Realloc(IntPtr ptr, long size) | |
|
||
internal static void SetNoSiteFlag() | ||
{ | ||
if (_PythonDll == "__Internal") | ||
{ | ||
throw new NotSupportedException("SetNoSiteFlag didn't support on static compile"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why should this not be supported? The symbol is just in the running DLL, so There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it should be supported. Just a mistake according to my wrong tests. I will recover it back. |
||
} | ||
var loader = LibraryLoader.Get(OperatingSystem); | ||
|
||
IntPtr dllLocal; | ||
if (_PythonDll != "__Internal") | ||
IntPtr dllLocal = loader.Load(_PythonDll); | ||
if (dllLocal == IntPtr.Zero) | ||
{ | ||
dllLocal = loader.Load(_PythonDll); | ||
throw new Exception($"Cannot load {_PythonDll}"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you throw more informative exception? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generally it should throw inside from |
||
} | ||
|
||
try | ||
{ | ||
Py_NoSiteFlag = loader.GetFunction(dllLocal, "Py_NoSiteFlag"); | ||
Marshal.WriteInt32(Py_NoSiteFlag, 1); | ||
} | ||
finally | ||
{ | ||
if (dllLocal != IntPtr.Zero) | ||
{ | ||
loader.Free(dllLocal); | ||
} | ||
loader.Free(dllLocal); | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can
PyEval_GetBuiltins()
return an error?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the docs it doesn't look like it can (https://docs.python.org/3/c-api/reflection.html#c.PyEval_GetBuiltins).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The globals dict is needed, but we don't seem to be using the
__builtins__
in the string. Do we really need this line?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you don't setup a
__builtins__
or setobject
explicit,object
may not be found.