66
77namespace Python . Runtime
88{
9+ using System . IO ;
10+
911 [ SuppressUnmanagedCodeSecurity ]
1012 internal static class NativeMethods
1113 {
@@ -869,18 +871,6 @@ public static int Py_Main(
869871
870872 internal static IntPtr PyCFunction_Call ( IntPtr func , IntPtr args , IntPtr kw ) => Delegates . PyCFunction_Call ( func , args , kw ) ;
871873
872-
873- internal static IntPtr PyClass_New ( IntPtr bases , IntPtr dict , IntPtr name ) => Delegates . PyClass_New ( bases , dict , name ) ;
874-
875-
876- internal static IntPtr PyInstance_New ( IntPtr cls , IntPtr args , IntPtr kw ) => Delegates . PyInstance_New ( cls , args , kw ) ;
877-
878-
879- internal static IntPtr PyInstance_NewRaw ( IntPtr cls , IntPtr dict ) => Delegates . PyInstance_NewRaw ( cls , dict ) ;
880-
881-
882- internal static IntPtr PyMethod_New ( IntPtr func , IntPtr self , IntPtr cls ) => Delegates . PyMethod_New ( func , self , cls ) ;
883-
884874
885875 //====================================================================
886876 // Python abstract object API
@@ -1107,9 +1097,6 @@ internal static IntPtr PyInt_FromInt64(long value)
11071097
11081098
11091099 internal static IntPtr PyInt_FromString ( string value , IntPtr end , int radix ) => Delegates . PyInt_FromString ( value , end , radix ) ;
1110-
1111-
1112- internal static int PyInt_GetMax ( ) => Delegates . PyInt_GetMax ( ) ;
11131100#elif PYTHON2
11141101 [ DllImport ( _PythonDll , CallingConvention = CallingConvention . Cdecl ) ]
11151102 private static extern IntPtr PyInt_FromLong ( IntPtr value ) ;
@@ -1864,9 +1851,6 @@ internal static IntPtr PyType_GenericAlloc(IntPtr type, long n)
18641851 internal static IntPtr _PyObject_GetDictPtr ( IntPtr obj ) => Delegates . _PyObject_GetDictPtr ( obj ) ;
18651852
18661853
1867- internal static IntPtr PyObject_GC_New ( IntPtr tp ) => Delegates . PyObject_GC_New ( tp ) ;
1868-
1869-
18701854 internal static void PyObject_GC_Del ( IntPtr tp ) => Delegates . PyObject_GC_Del ( tp ) ;
18711855
18721856
@@ -2002,10 +1986,6 @@ static Delegates()
20021986 PyImport_ExecCodeModule = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyImport_ExecCodeModuleDelegate > ( GetFunctionByName ( nameof ( PyImport_ExecCodeModule ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
20031987 PyCFunction_NewEx = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyCFunction_NewExDelegate > ( GetFunctionByName ( nameof ( PyCFunction_NewEx ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
20041988 PyCFunction_Call = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyCFunction_CallDelegate > ( GetFunctionByName ( nameof ( PyCFunction_Call ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
2005- PyClass_New = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyClass_NewDelegate > ( GetFunctionByName ( nameof ( PyClass_New ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
2006- PyInstance_New = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyInstance_NewDelegate > ( GetFunctionByName ( nameof ( PyInstance_New ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
2007- PyInstance_NewRaw = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyInstance_NewRawDelegate > ( GetFunctionByName ( nameof ( PyInstance_NewRaw ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
2008- PyMethod_New = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyMethod_NewDelegate > ( GetFunctionByName ( nameof ( PyMethod_New ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
20091989 PyObject_HasAttrString = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyObject_HasAttrStringDelegate > ( GetFunctionByName ( nameof ( PyObject_HasAttrString ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
20101990 PyObject_GetAttrString = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyObject_GetAttrStringDelegate > ( GetFunctionByName ( nameof ( PyObject_GetAttrString ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
20111991 PyObject_SetAttrString = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyObject_SetAttrStringDelegate > ( GetFunctionByName ( nameof ( PyObject_SetAttrString ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
@@ -2037,7 +2017,6 @@ static Delegates()
20372017 PyInt_FromLong = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyInt_FromLongDelegate > ( GetFunctionByName ( "PyLong_FromLong" , GetUnmanagedDll ( _PythonDll ) ) ) ;
20382018 PyInt_AsLong = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyInt_AsLongDelegate > ( GetFunctionByName ( "PyLong_AsLong" , GetUnmanagedDll ( _PythonDll ) ) ) ;
20392019 PyInt_FromString = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyInt_FromStringDelegate > ( GetFunctionByName ( "PyLong_FromString" , GetUnmanagedDll ( _PythonDll ) ) ) ;
2040- PyInt_GetMax = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyInt_GetMaxDelegate > ( GetFunctionByName ( "PyLong_GetMax" , GetUnmanagedDll ( _PythonDll ) ) ) ;
20412020 PyLong_FromLong = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyLong_FromLongDelegate > ( GetFunctionByName ( nameof ( PyLong_FromLong ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
20422021 PyLong_FromUnsignedLong = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyLong_FromUnsignedLongDelegate > ( GetFunctionByName ( nameof ( PyLong_FromUnsignedLong ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
20432022 PyLong_FromDouble = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyLong_FromDoubleDelegate > ( GetFunctionByName ( nameof ( PyLong_FromDouble ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
@@ -2054,7 +2033,7 @@ static Delegates()
20542033 PyNumber_Add = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyNumber_AddDelegate > ( GetFunctionByName ( nameof ( PyNumber_Add ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
20552034 PyNumber_Subtract = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyNumber_SubtractDelegate > ( GetFunctionByName ( nameof ( PyNumber_Subtract ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
20562035 PyNumber_Multiply = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyNumber_MultiplyDelegate > ( GetFunctionByName ( nameof ( PyNumber_Multiply ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
2057- PyNumber_Divide = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyNumber_DivideDelegate > ( GetFunctionByName ( nameof ( PyNumber_Divide ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
2036+ PyNumber_Divide = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyNumber_DivideDelegate > ( GetFunctionByName ( "PyNumber_TrueDivide" , GetUnmanagedDll ( _PythonDll ) ) ) ;
20582037 PyNumber_And = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyNumber_AndDelegate > ( GetFunctionByName ( nameof ( PyNumber_And ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
20592038 PyNumber_Xor = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyNumber_XorDelegate > ( GetFunctionByName ( nameof ( PyNumber_Xor ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
20602039 PyNumber_Or = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyNumber_OrDelegate > ( GetFunctionByName ( nameof ( PyNumber_Or ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
@@ -2065,7 +2044,7 @@ static Delegates()
20652044 PyNumber_InPlaceAdd = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyNumber_InPlaceAddDelegate > ( GetFunctionByName ( nameof ( PyNumber_InPlaceAdd ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
20662045 PyNumber_InPlaceSubtract = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyNumber_InPlaceSubtractDelegate > ( GetFunctionByName ( nameof ( PyNumber_InPlaceSubtract ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
20672046 PyNumber_InPlaceMultiply = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyNumber_InPlaceMultiplyDelegate > ( GetFunctionByName ( nameof ( PyNumber_InPlaceMultiply ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
2068- PyNumber_InPlaceDivide = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyNumber_InPlaceDivideDelegate > ( GetFunctionByName ( nameof ( PyNumber_InPlaceDivide ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
2047+ PyNumber_InPlaceDivide = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyNumber_InPlaceDivideDelegate > ( GetFunctionByName ( "PyNumber_InPlaceTrueDivide" , GetUnmanagedDll ( _PythonDll ) ) ) ;
20692048 PyNumber_InPlaceAnd = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyNumber_InPlaceAndDelegate > ( GetFunctionByName ( nameof ( PyNumber_InPlaceAnd ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
20702049 PyNumber_InPlaceXor = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyNumber_InPlaceXorDelegate > ( GetFunctionByName ( nameof ( PyNumber_InPlaceXor ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
20712050 PyNumber_InPlaceOr = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyNumber_InPlaceOrDelegate > ( GetFunctionByName ( nameof ( PyNumber_InPlaceOr ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
@@ -2156,7 +2135,6 @@ static Delegates()
21562135 PyObject_GenericGetAttr = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyObject_GenericGetAttrDelegate > ( GetFunctionByName ( nameof ( PyObject_GenericGetAttr ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
21572136 PyObject_GenericSetAttr = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyObject_GenericSetAttrDelegate > ( GetFunctionByName ( nameof ( PyObject_GenericSetAttr ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
21582137 _PyObject_GetDictPtr = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < _PyObject_GetDictPtrDelegate > ( GetFunctionByName ( nameof ( _PyObject_GetDictPtr ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
2159- PyObject_GC_New = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyObject_GC_NewDelegate > ( GetFunctionByName ( nameof ( PyObject_GC_New ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
21602138 PyObject_GC_Del = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyObject_GC_DelDelegate > ( GetFunctionByName ( nameof ( PyObject_GC_Del ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
21612139 PyObject_GC_Track = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyObject_GC_TrackDelegate > ( GetFunctionByName ( nameof ( PyObject_GC_Track ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
21622140 PyObject_GC_UnTrack = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyObject_GC_UnTrackDelegate > ( GetFunctionByName ( nameof ( PyObject_GC_UnTrack ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
@@ -2179,14 +2157,25 @@ static Delegates()
21792157 PyMethod_Function = global ::System . Runtime . InteropServices . Marshal . GetDelegateForFunctionPointer < PyMethod_FunctionDelegate > ( GetFunctionByName ( nameof ( PyMethod_Function ) , GetUnmanagedDll ( _PythonDll ) ) ) ;
21802158 }
21812159
2182- static global ::System . IntPtr GetUnmanagedDll ( string libraryName )
2183- {
2184- throw new NotImplementedException ( ) ;
2160+ static global ::System . IntPtr GetUnmanagedDll ( string libraryName ) {
2161+ if ( string . IsNullOrEmpty ( Path . GetExtension ( libraryName ) ) ) {
2162+ libraryName =
2163+ RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ? libraryName + ".dll"
2164+ : RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) ? $ "lib{ libraryName } .so"
2165+ : RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) ? $ "lib{ libraryName } .dylib"
2166+ : throw new PlatformNotSupportedException ( ) ;
2167+ }
2168+ IntPtr handle = NativeMethods . LoadLibrary ( libraryName ) ;
2169+ if ( handle == IntPtr . Zero )
2170+ throw new FileLoadException ( ) ;
2171+ return handle ;
21852172 }
21862173
2187- static global ::System . IntPtr GetFunctionByName ( string functionName , global ::System . IntPtr libraryHandle )
2188- {
2189- throw new NotImplementedException ( ) ;
2174+ static global ::System . IntPtr GetFunctionByName ( string functionName , global ::System . IntPtr libraryHandle ) {
2175+ IntPtr functionPointer = NativeMethods . GetProcAddress ( libraryHandle , functionName ) ;
2176+ if ( functionPointer == IntPtr . Zero )
2177+ throw new EntryPointNotFoundException ( $ "Function { functionName } was not found") ;
2178+ return functionPointer ;
21902179 }
21912180
21922181 internal static Py_IncRefDelegate Py_IncRef { get ; }
@@ -2427,26 +2416,6 @@ public delegate int Py_MainDelegate(
24272416 [ global ::System . Runtime . InteropServices . UnmanagedFunctionPointerAttribute ( CallingConvention . Cdecl ) ]
24282417 public delegate IntPtr PyCFunction_CallDelegate ( IntPtr func , IntPtr args , IntPtr kw ) ;
24292418
2430- internal static PyClass_NewDelegate PyClass_New { get ; }
2431-
2432- [ global ::System . Runtime . InteropServices . UnmanagedFunctionPointerAttribute ( CallingConvention . Cdecl ) ]
2433- public delegate IntPtr PyClass_NewDelegate ( IntPtr bases , IntPtr dict , IntPtr name ) ;
2434-
2435- internal static PyInstance_NewDelegate PyInstance_New { get ; }
2436-
2437- [ global ::System . Runtime . InteropServices . UnmanagedFunctionPointerAttribute ( CallingConvention . Cdecl ) ]
2438- public delegate IntPtr PyInstance_NewDelegate ( IntPtr cls , IntPtr args , IntPtr kw ) ;
2439-
2440- internal static PyInstance_NewRawDelegate PyInstance_NewRaw { get ; }
2441-
2442- [ global ::System . Runtime . InteropServices . UnmanagedFunctionPointerAttribute ( CallingConvention . Cdecl ) ]
2443- public delegate IntPtr PyInstance_NewRawDelegate ( IntPtr cls , IntPtr dict ) ;
2444-
2445- internal static PyMethod_NewDelegate PyMethod_New { get ; }
2446-
2447- [ global ::System . Runtime . InteropServices . UnmanagedFunctionPointerAttribute ( CallingConvention . Cdecl ) ]
2448- public delegate IntPtr PyMethod_NewDelegate ( IntPtr func , IntPtr self , IntPtr cls ) ;
2449-
24502419 internal static PyObject_HasAttrStringDelegate PyObject_HasAttrString { get ; }
24512420
24522421 [ global ::System . Runtime . InteropServices . UnmanagedFunctionPointerAttribute ( CallingConvention . Cdecl ) ]
@@ -2602,11 +2571,6 @@ public delegate int Py_MainDelegate(
26022571 [ global ::System . Runtime . InteropServices . UnmanagedFunctionPointerAttribute ( CallingConvention . Cdecl ) ]
26032572 public delegate IntPtr PyInt_FromStringDelegate ( string value , IntPtr end , int radix ) ;
26042573
2605- internal static PyInt_GetMaxDelegate PyInt_GetMax { get ; }
2606-
2607- [ global ::System . Runtime . InteropServices . UnmanagedFunctionPointerAttribute ( CallingConvention . Cdecl ) ]
2608- public delegate int PyInt_GetMaxDelegate ( ) ;
2609-
26102574 internal static PyLong_FromLongDelegate PyLong_FromLong { get ; }
26112575
26122576 [ global ::System . Runtime . InteropServices . UnmanagedFunctionPointerAttribute ( CallingConvention . Cdecl ) ]
@@ -3208,11 +3172,6 @@ int updatepath
32083172 [ global ::System . Runtime . InteropServices . UnmanagedFunctionPointerAttribute ( CallingConvention . Cdecl ) ]
32093173 public delegate IntPtr _PyObject_GetDictPtrDelegate ( IntPtr obj ) ;
32103174
3211- internal static PyObject_GC_NewDelegate PyObject_GC_New { get ; }
3212-
3213- [ global ::System . Runtime . InteropServices . UnmanagedFunctionPointerAttribute ( CallingConvention . Cdecl ) ]
3214- public delegate IntPtr PyObject_GC_NewDelegate ( IntPtr tp ) ;
3215-
32163175 internal static PyObject_GC_DelDelegate PyObject_GC_Del { get ; }
32173176
32183177 [ global ::System . Runtime . InteropServices . UnmanagedFunctionPointerAttribute ( CallingConvention . Cdecl ) ]
0 commit comments