Skip to content

Raise BadPythonDllException instead of confusing TypeLoadException when PythonDLL 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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 29, 2021
Merged
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
9 changes: 5 additions & 4 deletions README.rst
Original file line number Diff line number Diff 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 deletion src/embed_tests/TestPythonEngineProperties.cs
Original file line number Diff line number Diff 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 deletions src/runtime/platform/LibraryLoader.cs
Original file line number Diff line number Diff 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 deletions src/runtime/pythonengine.cs
Original file line number Diff line number Diff 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 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff 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 Down Expand 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 Down Expand 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 Down Expand Up @@ -2617,8 +2643,8 @@ static Delegates()
}
catch (MissingMethodException e) when (libraryHandle == IntPtr.Zero)
{
throw new MissingMethodException(
"Did you forget to set Runtime.PythonDLL?" +
throw new BadPythonDllException(
"Runtime.PythonDLL was not set or does not point to a supported Python runtime DLL." +
" See https://github.com/pythonnet/pythonnet#embedding-python-in-net",
e);
}
Expand Down Expand 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