Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

RaiseBadPythonDllException instead of confusingTypeLoadException whenPythonDLL was not configured properly#1577

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall 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
9 changes: 5 additions & 4 deletionsREADME.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -45,10 +45,11 @@ module:
Embedding Python in .NET
------------------------

- You must set `Runtime.PythonDLL` property or `PYTHONNET_PYDLL` environment variable
starting with version 3.0, otherwise you will receive `TypeInitializationException`.
Typical values are `python38.dll` (Windows), `libpython3.8.dylib` (Mac),
`libpython3.8.so` (most other *nix).
- You must set ``Runtime.PythonDLL`` property or ``PYTHONNET_PYDLL`` environment variable
starting with version 3.0, otherwise you will receive ``BadPythonDllException``
(internal, derived from ``MissingMethodException``) upon calling ``Initialize``.
Typical values are ``python38.dll`` (Windows), ``libpython3.8.dylib`` (Mac),
``libpython3.8.so`` (most other *nix).
- All calls to python should be inside a
``using (Py.GIL()) {/* Your code here */}`` block.
- Import python modules using ``dynamic mod = Py.Import("mod")``, then
Expand Down
2 changes: 1 addition & 1 deletionsrc/embed_tests/TestPythonEngineProperties.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -81,7 +81,7 @@ public static void GetPythonPathDefault()
public static void GetProgramNameDefault()
{
PythonEngine.Initialize();
string s = PythonEngine.PythonHome;
string s = PythonEngine.ProgramName;

Assert.NotNull(s);
PythonEngine.Shutdown();
Expand Down
4 changes: 2 additions & 2 deletionssrc/runtime/platform/LibraryLoader.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -111,7 +111,7 @@ public IntPtr Load(string dllToLoad)
{
var res = WindowsLoader.LoadLibrary(dllToLoad);
if (res == IntPtr.Zero)
throw new DllNotFoundException($"Could not load {dllToLoad}", new Win32Exception());
throw new DllNotFoundException($"Could not load {dllToLoad}.", new Win32Exception());
return res;
}

Expand All@@ -128,7 +128,7 @@ public IntPtr GetFunction(IntPtr hModule, string procedureName)

var res = WindowsLoader.GetProcAddress(hModule, procedureName);
if (res == IntPtr.Zero)
throw new MissingMethodException($"Failed to load symbol {procedureName}", new Win32Exception());
throw new MissingMethodException($"Failed to load symbol {procedureName}.", new Win32Exception());
return res;
}

Expand Down
18 changes: 12 additions & 6 deletionssrc/runtime/pythonengine.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -86,13 +86,15 @@ public static string ProgramName
{
get
{
IntPtr p = Runtime.Py_GetProgramName();
IntPtr p = Runtime.TryUsingDll(() => Runtime.Py_GetProgramName());
return UcsMarshaler.PtrToPy3UnicodePy2String(p) ?? "";
}
set
{
Marshal.FreeHGlobal(_programName);
_programName = UcsMarshaler.Py3UnicodePy2StringtoPtr(value);
_programName = Runtime.TryUsingDll(
() => UcsMarshaler.Py3UnicodePy2StringtoPtr(value)
);
Runtime.Py_SetProgramName(_programName);
}
}
Expand All@@ -101,14 +103,16 @@ public static string PythonHome
{
get
{
IntPtr p = Runtime.Py_GetPythonHome();
IntPtr p = Runtime.TryUsingDll(() => Runtime.Py_GetPythonHome());
return UcsMarshaler.PtrToPy3UnicodePy2String(p) ?? "";
}
set
{
// this value is null in the beginning
Marshal.FreeHGlobal(_pythonHome);
_pythonHome = UcsMarshaler.Py3UnicodePy2StringtoPtr(value);
_pythonHome = Runtime.TryUsingDll(
() => UcsMarshaler.Py3UnicodePy2StringtoPtr(value)
);
Runtime.Py_SetPythonHome(_pythonHome);
}
}
Expand All@@ -117,13 +121,15 @@ public static string PythonPath
{
get
{
IntPtr p = Runtime.Py_GetPath();
IntPtr p = Runtime.TryUsingDll(() => Runtime.Py_GetPath());
return UcsMarshaler.PtrToPy3UnicodePy2String(p) ?? "";
}
set
{
Marshal.FreeHGlobal(_pythonPath);
_pythonPath = UcsMarshaler.Py3UnicodePy2StringtoPtr(value);
_pythonPath = Runtime.TryUsingDll(
() => UcsMarshaler.Py3UnicodePy2StringtoPtr(value)
);
Runtime.Py_SetPath(_pythonPath);
}
}
Expand Down
48 changes: 40 additions & 8 deletionssrc/runtime/runtime.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -93,7 +93,6 @@ internal static Version PyVersion
}
}


/// <summary>
/// Initialize the runtime...
/// </summary>
Expand All@@ -113,7 +112,10 @@ internal static void Initialize(bool initSigs = false, ShutdownMode mode = Shutd
}
ShutdownMode = mode;

if (Py_IsInitialized() == 0)
bool interpreterAlreadyInitialized = TryUsingDll(
() => Py_IsInitialized() != 0
);
if (!interpreterAlreadyInitialized)
{
Py_InitializeEx(initSigs ? 1 : 0);
if (PyEval_ThreadsInitialized() == 0)
Expand DownExpand Up@@ -787,6 +789,34 @@ internal static unsafe long Refcount(IntPtr op)
return *p;
}

/// <summary>
/// Call specified function, and handle PythonDLL-related failures.
/// </summary>
internal static T TryUsingDll<T>(Func<T> op)
{
try
{
return op();
}
catch (TypeInitializationException loadFailure)
{
var delegatesLoadFailure = loadFailure;
// failure to load Delegates type might have been the cause
// of failure to load some higher-level type
while (delegatesLoadFailure.InnerException is TypeInitializationException nested)
{
delegatesLoadFailure = nested;
}

if (delegatesLoadFailure.InnerException is BadPythonDllException badDll)
{
throw badDll;
}

throw;
}
}

/// <summary>
/// Export of Macro Py_XIncRef. Use XIncref instead.
/// Limit this function usage for Testing and Py_Debug builds
Expand DownExpand Up@@ -2270,10 +2300,6 @@ internal static void SetNoSiteFlag()
if (_PythonDll != "__Internal")
{
dllLocal = loader.Load(_PythonDll);
if (dllLocal == IntPtr.Zero)
{
throw new Exception($"Cannot load {_PythonDll}");
}
}
try
{
Expand DownExpand Up@@ -2617,8 +2643,8 @@ static Delegates()
}
catch (MissingMethodException e) when (libraryHandle == IntPtr.Zero)
{
throw newMissingMethodException(
"Did you forgettoset Runtime.PythonDLL?" +
throw newBadPythonDllException(
"Runtime.PythonDLL was not set or does not pointtoa supported Python runtime DLL." +
" See https://github.com/pythonnet/pythonnet#embedding-python-in-net",
e);
}
Expand DownExpand Up@@ -2889,6 +2915,12 @@ static Delegates()
}
}

internal class BadPythonDllException : MissingMethodException
{
public BadPythonDllException(string message, Exception innerException)
: base(message, innerException) { }
}


public enum ShutdownMode
{
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp