Skip to content
Closed
Show file tree
Hide file tree
Changes from 8 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
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,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))
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,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

Expand Down
62 changes: 38 additions & 24 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

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();
Expand All @@ -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)
Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Member

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).

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.

The globals dict is needed, but we don't seem to be using the __builtins__ in the string. Do we really need this line?

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.

If you don't setup a __builtins__ or set object explicit, object may not be found.

{
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();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You might want to construct an instance of PythonException before doing XDecref, otherwise last error could potentially be overwritten by XDecref with something new.

}
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.
///
Expand Down Expand Up @@ -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");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 loader.Load(_PythonDll) has to return essentially the result of dlopen(NULL, ...).

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.

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}");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you throw more informative exception? Win32Exception would automatically read last error on Windows, but I don't know what to do for *nix

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.

Generally it should throw inside from Load, the nullptr checking just for in case(if the loader implementation didn't throw an exception). I'm not going to insert any platform specific code here.

}

try
{
Py_NoSiteFlag = loader.GetFunction(dllLocal, "Py_NoSiteFlag");
Marshal.WriteInt32(Py_NoSiteFlag, 1);
}
finally
{
if (dllLocal != IntPtr.Zero)
{
loader.Free(dllLocal);
}
loader.Free(dllLocal);
}
}
}
Expand Down