@@ -292,26 +292,18 @@ internal static void Initialize(bool initSigs = false)
292
292
293
293
Error = new IntPtr ( - 1 ) ;
294
294
295
+ _PyObject_NextNotImplemented = Get_PyObject_NextNotImplemented ( ) ;
296
+ {
297
+ IntPtr sys = PyImport_ImportModule ( "sys" ) ;
298
+ PyModuleType = PyObject_Type ( sys ) ;
299
+ XDecref ( sys ) ;
300
+ }
301
+
295
302
// Initialize data about the platform we're running on. We need
296
303
// this for the type manager and potentially other details. Must
297
304
// happen after caching the python types, above.
298
305
InitializePlatformData ( ) ;
299
306
300
- IntPtr dllLocal = IntPtr . Zero ;
301
- var loader = LibraryLoader . Get ( OperatingSystem ) ;
302
-
303
- // Since `_PyObject_NextNotImplemented` would set to a heap class
304
- // for tp_iternext which doesn't implement __next__.
305
- // Thus we need a heap class to get it, the ZipImportError is a
306
- // heap class and it's in builtins, so we can use it as a trick.
307
- var zipimport = PyImport_ImportModule ( "zipimport" ) ;
308
- var ZipImportError = PyObject_GetAttrString ( zipimport , "ZipImportError" ) ;
309
- _PyObject_NextNotImplemented = Marshal . ReadIntPtr ( ZipImportError , TypeOffset . tp_iternext ) ;
310
- XDecref ( ZipImportError ) ;
311
- XDecref ( zipimport ) ;
312
- PyModuleType = loader . GetFunction ( dllLocal , "PyModule_Type" ) ;
313
-
314
-
315
307
// Initialize modules that depend on the runtime class.
316
308
AssemblyManager . Initialize ( ) ;
317
309
PyCLRMetaType = MetaType . Initialize ( ) ;
@@ -328,6 +320,29 @@ internal static void Initialize(bool initSigs = false)
328
320
AssemblyManager . UpdatePath ( ) ;
329
321
}
330
322
323
+ private static IntPtr Get_PyObject_NextNotImplemented ( )
324
+ {
325
+ IntPtr globals = PyDict_New ( ) ;
326
+ if ( PyDict_SetItemString ( globals , "__builtins__" , PyEval_GetBuiltins ( ) ) != 0 )
327
+ {
328
+ XDecref ( globals ) ;
329
+ throw new PythonException ( ) ;
330
+ }
331
+ const string code = "class A(object): pass" ;
332
+ IntPtr res = PyRun_String ( code , ( IntPtr ) RunFlagType . File , globals , globals ) ;
333
+ if ( res == IntPtr . Zero )
334
+ {
335
+ XDecref ( globals ) ;
336
+ throw new PythonException ( ) ;
337
+ }
338
+ XDecref ( res ) ;
339
+ IntPtr A = PyDict_GetItemString ( globals , "A" ) ;
340
+ IntPtr iternext = Marshal . ReadIntPtr ( A , TypeOffset . tp_iternext ) ;
341
+ XDecref ( globals ) ;
342
+ XDecref ( A ) ;
343
+ return iternext ;
344
+ }
345
+
331
346
/// <summary>
332
347
/// Initializes the data about platforms.
333
348
///
@@ -936,7 +951,7 @@ internal static int PyObject_Compare(IntPtr value1, IntPtr value2)
936
951
937
952
internal static long PyObject_Size( IntPtr pointer)
938
953
{
939
- return ( long ) _PyObject_Size( pointer) ;
954
+ return ( long ) _PyObject_Size( pointer) ;
940
955
}
941
956
942
957
[ DllImport( _PythonDll, CallingConvention = CallingConvention. Cdecl, EntryPoint = "PyObject_Size") ]
@@ -1052,7 +1067,7 @@ internal static bool PyLong_Check(IntPtr ob)
1052
1067
1053
1068
internal static IntPtr PyLong_FromUnsignedLong( object value)
1054
1069
{
1055
- if ( Is32Bit || IsWindows)
1070
+ if ( Is32Bit || IsWindows)
1056
1071
return PyLong_FromUnsignedLong32( Convert. ToUInt32( value) ) ;
1057
1072
else
1058
1073
return PyLong_FromUnsignedLong64( Convert. ToUInt64( value) ) ;
@@ -1242,7 +1257,7 @@ internal static int PySequence_DelSlice(IntPtr pointer, long i1, long i2)
1242
1257
1243
1258
internal static long PySequence_Size( IntPtr pointer)
1244
1259
{
1245
- return ( long ) _PySequence_Size( pointer) ;
1260
+ return ( long ) _PySequence_Size( pointer) ;
1246
1261
}
1247
1262
1248
1263
[ DllImport( _PythonDll, CallingConvention = CallingConvention. Cdecl, EntryPoint = "PySequence_Size") ]
@@ -1267,7 +1282,7 @@ internal static IntPtr PySequence_Repeat(IntPtr pointer, long count)
1267
1282
1268
1283
internal static long PySequence_Count( IntPtr pointer, IntPtr value)
1269
1284
{
1270
- return ( long ) _PySequence_Count( pointer, value) ;
1285
+ return ( long ) _PySequence_Count( pointer, value) ;
1271
1286
}
1272
1287
1273
1288
[ DllImport( _PythonDll, CallingConvention = CallingConvention. Cdecl, EntryPoint = "PySequence_Count") ]
@@ -1310,7 +1325,7 @@ internal static IntPtr PyString_FromString(string value)
1310
1325
1311
1326
internal static long PyBytes_Size( IntPtr op)
1312
1327
{
1313
- return ( long ) _PyBytes_Size( op) ;
1328
+ return ( long ) _PyBytes_Size( op) ;
1314
1329
}
1315
1330
1316
1331
[ DllImport( _PythonDll, CallingConvention = CallingConvention. Cdecl, EntryPoint = "PyBytes_Size") ]
@@ -1537,7 +1552,7 @@ internal static bool PyDict_Check(IntPtr ob)
1537
1552
1538
1553
internal static long PyDict_Size( IntPtr pointer)
1539
1554
{
1540
- return ( long ) _PyDict_Size( pointer) ;
1555
+ return ( long ) _PyDict_Size( pointer) ;
1541
1556
}
1542
1557
1543
1558
[ DllImport( _PythonDll, CallingConvention = CallingConvention. Cdecl, EntryPoint = "PyDict_Size") ]
@@ -1615,7 +1630,7 @@ internal static int PyList_SetSlice(IntPtr pointer, long start, long end, IntPtr
1615
1630
1616
1631
internal static long PyList_Size( IntPtr pointer)
1617
1632
{
1618
- return ( long ) _PyList_Size( pointer) ;
1633
+ return ( long ) _PyList_Size( pointer) ;
1619
1634
}
1620
1635
1621
1636
[ DllImport( _PythonDll, CallingConvention = CallingConvention. Cdecl, EntryPoint = "PyList_Size") ]
@@ -1664,7 +1679,7 @@ internal static IntPtr PyTuple_GetSlice(IntPtr pointer, long start, long end)
1664
1679
1665
1680
internal static long PyTuple_Size( IntPtr pointer)
1666
1681
{
1667
- return ( long ) _PyTuple_Size( pointer) ;
1682
+ return ( long ) _PyTuple_Size( pointer) ;
1668
1683
}
1669
1684
1670
1685
[ DllImport( _PythonDll, CallingConvention = CallingConvention. Cdecl, EntryPoint = "PyTuple_Size") ]
@@ -1893,25 +1908,24 @@ internal static IntPtr PyMem_Realloc(IntPtr ptr, long size)
1893
1908
1894
1909
internal static void SetNoSiteFlag( )
1895
1910
{
1911
+ if ( _PythonDll == "__Internal")
1912
+ {
1913
+ throw new NotSupportedException( "SetNoSiteFlag didn't support on static compile") ;
1914
+ }
1896
1915
var loader = LibraryLoader. Get( OperatingSystem) ;
1897
-
1898
- IntPtr dllLocal;
1899
- if ( _PythonDll != "__Internal")
1916
+ IntPtr dllLocal = loader. Load( _PythonDll) ;
1917
+ if ( dllLocal == IntPtr. Zero)
1900
1918
{
1901
- dllLocal = loader . Load ( _PythonDll) ;
1919
+ throw new Exception ( $"Cannot load { _PythonDll} " ) ;
1902
1920
}
1903
-
1904
1921
try
1905
1922
{
1906
1923
Py_NoSiteFlag = loader. GetFunction( dllLocal, "Py_NoSiteFlag") ;
1907
1924
Marshal. WriteInt32( Py_NoSiteFlag, 1 ) ;
1908
1925
}
1909
1926
finally
1910
1927
{
1911
- if ( dllLocal != IntPtr. Zero)
1912
- {
1913
- loader. Free( dllLocal) ;
1914
- }
1928
+ loader. Free( dllLocal) ;
1915
1929
}
1916
1930
}
1917
1931
}
0 commit comments