8000 Drop LoadLibrary by amos402 · Pull Request #880 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

Skip to content

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

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
36 changes: 19 additions & 17 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}

/// <summary>
/// Initializes the data about platforms.
///
Expand Down Expand Up @@ -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");
Expand Down
34 changes: 34 additions & 0 deletions
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
4C95 }
Runtime.XDecref(res);
IntPtr A = Runtime.PyDict_GetItemString(globals, "A");
Debug.Assert(A != IntPtr.Zero);
Runtime.XIncref(A);
Runtime.XDecref(globals);
return A;
}
}
}
0