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

Commit18d8da4

Browse files
committed
* Load PyModuleType without LibraryLoader
* Drop C module dependency when getting _PyObject_NextNotImplemented* Exception details for SetNoSiteFlag
1 parent653a263 commit18d8da4

File tree

1 file changed

+46
-32
lines changed

1 file changed

+46
-32
lines changed

‎src/runtime/runtime.cs‎

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -292,26 +292,18 @@ internal static void Initialize(bool initSigs = false)
292292

293293
Error=newIntPtr(-1);
294294

295+
_PyObject_NextNotImplemented=Get_PyObject_NextNotImplemented();
296+
{
297+
IntPtrsys=PyImport_ImportModule("sys");
298+
PyModuleType=PyObject_Type(sys);
299+
XDecref(sys);
300+
}
301+
295302
// Initialize data about the platform we're running on. We need
296303
// this for the type manager and potentially other details. Must
297304
// happen after caching the python types, above.
298305
InitializePlatformData();
299306

300-
IntPtrdllLocal=IntPtr.Zero;
301-
varloader=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-
varzipimport=PyImport_ImportModule("zipimport");
308-
varZipImportError=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-
315307
// Initialize modules that depend on the runtime class.
316308
AssemblyManager.Initialize();
317309
PyCLRMetaType=MetaType.Initialize();
@@ -328,6 +320,29 @@ internal static void Initialize(bool initSigs = false)
328320
AssemblyManager.UpdatePath();
329321
}
330322

323+
privatestaticIntPtrGet_PyObject_NextNotImplemented()
324+
{
325+
IntPtrglobals=PyDict_New();
326+
if(PyDict_SetItemString(globals,"__builtins__",PyEval_GetBuiltins())!=0)
327+
{
328+
XDecref(globals);
329+
thrownewPythonException();
330+
}
331+
conststringcode="class A(object): pass";
332+
IntPtrres=PyRun_String(code,(IntPtr)RunFlagType.File,globals,globals);
333+
if(res==IntPtr.Zero)
334+
{
335+
XDecref(globals);
336+
thrownewPythonException();
337+
}
338+
XDecref(res);
339+
IntPtrA=PyDict_GetItemString(globals,"A");
340+
IntPtriternext=Marshal.ReadIntPtr(A,TypeOffset.tp_iternext);
341+
XDecref(globals);
342+
XDecref(A);
343+
returniternext;
344+
}
345+
331346
/// <summary>
332347
/// Initializes the data about platforms.
333348
///
@@ -936,7 +951,7 @@ internal static int PyObject_Compare(IntPtr value1, IntPtr value2)
936951

937952
internalstaticlong PyObject_Size(IntPtr pointer)
938953
{
939-
return(long)_PyObject_Size(pointer);
954+
return(long)_PyObject_Size(pointer);
940955
}
941956

942957
[DllImport(_PythonDll, CallingConvention= CallingConvention.Cdecl, EntryPoint= "PyObject_Size")]
@@ -1052,7 +1067,7 @@ internal static bool PyLong_Check(IntPtr ob)
10521067

10531068
internalstatic IntPtr PyLong_FromUnsignedLong(object value)
10541069
{
1055-
if(Is32Bit|| IsWindows)
1070+
if(Is32Bit|| IsWindows)
10561071
return PyLong_FromUnsignedLong32(Convert.ToUInt32(value));
10571072
else
10581073
return PyLong_FromUnsignedLong64(Convert.ToUInt64(value));
@@ -1242,7 +1257,7 @@ internal static int PySequence_DelSlice(IntPtr pointer, long i1, long i2)
12421257

12431258
internalstaticlong PySequence_Size(IntPtr pointer)
12441259
{
1245-
return(long)_PySequence_Size(pointer);
1260+
return(long)_PySequence_Size(pointer);
12461261
}
12471262

12481263
[DllImport(_PythonDll, CallingConvention= CallingConvention.Cdecl, EntryPoint= "PySequence_Size")]
@@ -1267,7 +1282,7 @@ internal static IntPtr PySequence_Repeat(IntPtr pointer, long count)
12671282

12681283
internalstaticlong PySequence_Count(IntPtr pointer, IntPtr value)
12691284
{
1270-
return(long)_PySequence_Count(pointer, value);
1285+
return(long)_PySequence_Count(pointer, value);
12711286
}
12721287

12731288
[DllImport(_PythonDll, CallingConvention= CallingConvention.Cdecl, EntryPoint= "PySequence_Count")]
@@ -1310,7 +1325,7 @@ internal static IntPtr PyString_FromString(string value)
13101325

13111326
internalstaticlong PyBytes_Size(IntPtr op)
13121327
{
1313-
return(long)_PyBytes_Size(op);
1328+
return(long)_PyBytes_Size(op);
13141329
}
13151330

13161331
[DllImport(_PythonDll, CallingConvention= CallingConvention.Cdecl, EntryPoint= "PyBytes_Size")]
@@ -1537,7 +1552,7 @@ internal static bool PyDict_Check(IntPtr ob)
15371552

15381553
internalstaticlong PyDict_Size(IntPtr pointer)
15391554
{
1540-
return(long)_PyDict_Size(pointer);
1555+
return(long)_PyDict_Size(pointer);
15411556
}
15421557

15431558
[DllImport(_PythonDll, CallingConvention= CallingConvention.Cdecl, EntryPoint= "PyDict_Size")]
@@ -1615,7 +1630,7 @@ internal static int PyList_SetSlice(IntPtr pointer, long start, long end, IntPtr
16151630

16161631
internalstaticlong PyList_Size(IntPtr pointer)
16171632
{
1618-
return(long)_PyList_Size(pointer);
1633+
return(long)_PyList_Size(pointer);
16191634
}
16201635

16211636
[DllImport(_PythonDll, CallingConvention= CallingConvention.Cdecl, EntryPoint= "PyList_Size")]
@@ -1664,7 +1679,7 @@ internal static IntPtr PyTuple_GetSlice(IntPtr pointer, long start, long end)
16641679

16651680
internalstaticlong PyTuple_Size(IntPtr pointer)
16661681
{
1667-
return(long)_PyTuple_Size(pointer);
1682+
return(long)_PyTuple_Size(pointer);
16681683
}
16691684

16701685
[DllImport(_PythonDll, CallingConvention= CallingConvention.Cdecl, EntryPoint= "PyTuple_Size")]
@@ -1893,25 +1908,24 @@ internal static IntPtr PyMem_Realloc(IntPtr ptr, long size)
18931908

18941909
internalstaticvoid SetNoSiteFlag()
18951910
{
1911+
if(_PythonDll== "__Internal")
1912+
{
1913+
thrownew NotSupportedException("SetNoSiteFlag didn't support onstatic compile");
1914+
}
18961915
var loader= LibraryLoader.Get(OperatingSystem);
1897-
1898-
IntPtr dllLocal;
1899-
if(_PythonDll!= "__Internal")
1916+
IntPtr dllLocal= loader.Load(_PythonDll);
1917+
if(dllLocal== IntPtr.Zero)
19001918
{
1901-
dllLocal= loader.Load(_PythonDll);
1919+
thrownew Exception($"Cannot load{_PythonDll}");
19021920
}
1903-
19041921
try
19051922
{
19061923
Py_NoSiteFlag= loader.GetFunction(dllLocal, "Py_NoSiteFlag");
19071924
Marshal.WriteInt32(Py_NoSiteFlag,1);
19081925
}
19091926
finally
19101927
{
1911-
if(dllLocal!= IntPtr.Zero)
1912-
{
1913-
loader.Free(dllLocal);
1914-
}
1928+
loader.Free(dllLocal);
19151929
}
19161930
}
19171931
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp