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

Commitdf98205

Browse files
authored
Merge branch 'master' into PR/Codecs
2 parents4c18a1c +3b938a5 commitdf98205

File tree

2 files changed

+139
-61
lines changed

2 files changed

+139
-61
lines changed

‎src/runtime/importhook.cs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,6 @@ internal static void ReleaseModuleDef()
3535
}
3636
#endif
3737

38-
/// <summary>
39-
/// Get a <i>New reference</i> to the builtins module.
40-
/// </summary>
41-
staticIntPtrGetNewRefToBuiltins()
42-
{
43-
if(Runtime.IsPython3)
44-
{
45-
returnRuntime.PyImport_ImportModule("builtins");
46-
}
47-
else
48-
{
49-
// dict is a borrowed ref, no need to decref
50-
IntPtrdict=Runtime.PyImport_GetModuleDict();
51-
52-
// GetItemString is a borrowed ref; incref to get a new ref
53-
IntPtrbuiltins=Runtime.PyDict_GetItemString(dict,"__builtin__");
54-
Runtime.XIncref(builtins);
55-
returnbuiltins;
56-
}
57-
}
58-
5938
/// <summary>
6039
/// Initialize just the __import__ hook itself.
6140
/// </summary>
@@ -64,7 +43,7 @@ static void InitImport()
6443
// We replace the built-in Python __import__ with our own: first
6544
// look in CLR modules, then if we don't find any call the default
6645
// Python __import__.
67-
IntPtrbuiltins=GetNewRefToBuiltins();
46+
IntPtrbuiltins=Runtime.GetBuiltins();
6847
py_import=Runtime.PyObject_GetAttrString(builtins,"__import__");
6948
PythonException.ThrowIfIsNull(py_import);
7049

@@ -80,7 +59,7 @@ static void InitImport()
8059
/// </summary>
8160
staticvoidRestoreImport()
8261
{
83-
IntPtrbuiltins=GetNewRefToBuiltins();
62+
IntPtrbuiltins=Runtime.GetBuiltins();
8463

8564
intres=Runtime.PyObject_SetAttrString(builtins,"__import__",py_import);
8665
PythonException.ThrowIfIsNotZero(res);

‎src/runtime/runtime.cs

Lines changed: 137 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ public class Runtime
169169
/// </summary>
170170
internalstaticreadonlyEncodingPyEncoding=_UCS==2?Encoding.Unicode:Encoding.UTF32;
171171

172+
privatestaticPyReferenceCollection_pyRefs=newPyReferenceCollection();
173+
172174
/// <summary>
173175
/// Initialize the runtime...
174176
/// </summary>
@@ -194,16 +196,46 @@ internal static void Initialize(bool initSigs = false)
194196
TypeManager.Reset();
195197

196198
IntPtrop;
197-
IntPtrdict;
198-
if(IsPython3)
199-
{
200-
op=PyImport_ImportModule("builtins");
201-
dict=PyObject_GetAttrString(op,"__dict__");
202-
}
203-
else// Python2
204199
{
205-
dict=PyImport_GetModuleDict();
206-
op=PyDict_GetItemString(dict,"__builtin__");
200+
varbuiltins=GetBuiltins();
201+
SetPyMember(refPyNotImplemented,PyObject_GetAttrString(builtins,"NotImplemented"),
202+
()=>PyNotImplemented=IntPtr.Zero);
203+
204+
SetPyMember(refPyBaseObjectType,PyObject_GetAttrString(builtins,"object"),
205+
()=>PyBaseObjectType=IntPtr.Zero);
206+
207+
SetPyMember(refPyNone,PyObject_GetAttrString(builtins,"None"),
208+
()=>PyNone=IntPtr.Zero);
209+
SetPyMember(refPyTrue,PyObject_GetAttrString(builtins,"True"),
210+
()=>PyTrue=IntPtr.Zero);
211+
SetPyMember(refPyFalse,PyObject_GetAttrString(builtins,"False"),
212+
()=>PyFalse=IntPtr.Zero);
213+
214+
SetPyMember(refPyBoolType,PyObject_Type(PyTrue),
215+
()=>PyBoolType=IntPtr.Zero);
216+
SetPyMember(refPyNoneType,PyObject_Type(PyNone),
217+
()=>PyNoneType=IntPtr.Zero);
218+
SetPyMember(refPyTypeType,PyObject_Type(PyNoneType),
219+
()=>PyTypeType=IntPtr.Zero);
220+
221+
op=PyObject_GetAttrString(builtins,"len");
222+
SetPyMember(refPyMethodType,PyObject_Type(op),
223+
()=>PyMethodType=IntPtr.Zero);
224+
XDecref(op);
225+
226+
// For some arcane reason, builtins.__dict__.__setitem__ is *not*
227+
// a wrapper_descriptor, even though dict.__setitem__ is.
228+
//
229+
// object.__init__ seems safe, though.
230+
op=PyObject_GetAttrString(PyBaseObjectType,"__init__");
231+
SetPyMember(refPyWrapperDescriptorType,PyObject_Type(op),
232+
()=>PyWrapperDescriptorType=IntPtr.Zero);
233+
XDecref(op);
234+
235+
SetPyMember(refPySuper_Type,PyObject_GetAttrString(builtins,"super"),
236+
()=>PySuper_Type=IntPtr.Zero);
237+
238+
XDecref(builtins);
207239
}
208240
PyNotImplemented=PyObject_GetAttrString(op,"NotImplemented");
209241
PyBaseObjectType=PyObject_GetAttrString(op,"object");
@@ -234,60 +266,73 @@ internal static void Initialize(bool initSigs = false)
234266
#endif
235267

236268
op=PyString_FromString("string");
237-
PyStringType=PyObject_Type(op);
269+
SetPyMember(refPyStringType,PyObject_Type(op),
270+
()=>PyStringType=IntPtr.Zero);
238271
XDecref(op);
239272

240273
op=PyUnicode_FromString("unicode");
241-
PyUnicodeType=PyObject_Type(op);
274+
SetPyMember(refPyUnicodeType,PyObject_Type(op),
275+
()=>PyUnicodeType=IntPtr.Zero);
242276
XDecref(op);
243277

244278
#ifPYTHON3
245279
op=PyBytes_FromString("bytes");
246-
PyBytesType=PyObject_Type(op);
280+
SetPyMember(refPyBytesType,PyObject_Type(op),
281+
()=>PyBytesType=IntPtr.Zero);
247282
XDecref(op);
248283
#endif
249284

250285
op=PyTuple_New(0);
251-
PyTupleType=PyObject_Type(op);
286+
SetPyMember(refPyTupleType,PyObject_Type(op),
287+
()=>PyTupleType=IntPtr.Zero);
252288
XDecref(op);
253289

254290
op=PyList_New(0);
255-
PyListType=PyObject_Type(op);
291+
SetPyMember(refPyListType,PyObject_Type(op),
292+
()=>PyListType=IntPtr.Zero);
256293
XDecref(op);
257294

258295
op=PyDict_New();
259-
PyDictType=PyObject_Type(op);
296+
SetPyMember(refPyDictType,PyObject_Type(op),
297+
()=>PyDictType=IntPtr.Zero);
260298
XDecref(op);
261299

262300
op=PyInt_FromInt32(0);
263-
PyIntType=PyObject_Type(op);
301+
SetPyMember(refPyIntType,PyObject_Type(op),
302+
()=>PyIntType=IntPtr.Zero);
264303
XDecref(op);
265304

266305
op=PyLong_FromLong(0);
267-
PyLongType=PyObject_Type(op);
306+
SetPyMember(refPyLongType,PyObject_Type(op),
307+
()=>PyLongType=IntPtr.Zero);
268308
XDecref(op);
269309

270310
op=PyFloat_FromDouble(0);
271-
PyFloatType=PyObject_Type(op);
311+
SetPyMember(refPyFloatType,PyObject_Type(op),
312+
()=>PyFloatType=IntPtr.Zero);
272313
XDecref(op);
273314

274-
#ifPYTHON3
315+
#if!PYTHON2
275316
PyClassType=IntPtr.Zero;
276317
PyInstanceType=IntPtr.Zero;
277-
#elifPYTHON2
278-
IntPtrs=PyString_FromString("_temp");
279-
IntPtrd=PyDict_New();
318+
#else
319+
{
320+
IntPtrs=PyString_FromString("_temp");
321+
IntPtrd=PyDict_New();
280322

281-
IntPtrc=PyClass_New(IntPtr.Zero,d,s);
282-
PyClassType=PyObject_Type(c);
323+
IntPtrc=PyClass_New(IntPtr.Zero,d,s);
324+
SetPyMember(refPyClassType,PyObject_Type(c),
325+
()=>PyClassType=IntPtr.Zero);
283326

284-
IntPtri=PyInstance_New(c,IntPtr.Zero,IntPtr.Zero);
285-
PyInstanceType=PyObject_Type(i);
327+
IntPtri=PyInstance_New(c,IntPtr.Zero,IntPtr.Zero);
328+
SetPyMember(refPyInstanceType,PyObject_Type(i),
329+
()=>PyInstanceType=IntPtr.Zero);
286330

287-
XDecref(s);
288-
XDecref(i);
289-
XDecref(c);
290-
XDecref(d);
331+
XDecref(s);
332+
XDecref(i);
333+
XDecref(c);
334+
XDecref(d);
335+
}
291336
#endif
292337

293338
Error=newIntPtr(-1);
@@ -381,6 +426,9 @@ internal static void Shutdown()
381426
Exceptions.Shutdown();
382427
ImportHook.Shutdown();
383428
Finalizer.Shutdown();
429+
// TOOD: PyCLRMetaType's release operation still in #958
430+
PyCLRMetaType=IntPtr.Zero;
431+
ResetPyMembers();
384432
Py_Finalize();
385433
}
386434

@@ -394,6 +442,19 @@ internal static int AtExit()
394442
return0;
395443
}
396444

445+
privatestaticvoidSetPyMember(refIntPtrobj,IntPtrvalue,ActiononRelease)
446+
{
447+
// XXX: For current usages, value should not be null.
448+
PythonException.ThrowIfIsNull(value);
449+
obj=value;
450+
_pyRefs.Add(value,onRelease);
451+
}
452+
453+
privatestaticvoidResetPyMembers()
454+
{
455+
_pyRefs.Release();
456+
}
457+
397458
internalstaticIntPtrPy_single_input=(IntPtr)256;
398459
internalstaticIntPtrPy_file_input=(IntPtr)257;
399460
internalstaticIntPtrPy_eval_input=(IntPtr)258;
@@ -402,6 +463,7 @@ internal static int AtExit()
402463
internalstaticIntPtrPyModuleType;
403464
internalstaticIntPtrPyClassType;
404465
internalstaticIntPtrPyInstanceType;
466+
internalstaticIntPtrPySuper_Type;
405467
internalstaticIntPtrPyCLRMetaType;
406468
internalstaticIntPtrPyMethodType;
407469
internalstaticIntPtrPyWrapperDescriptorType;
@@ -974,7 +1036,7 @@ internal static int PyObject_Compare(IntPtr value1, IntPtr value2)
9741036

9751037
internalstaticlong PyObject_Size(IntPtr pointer)
9761038
{
977-
return(long)_PyObject_Size(pointer);
1039+
return(long)_PyObject_Size(pointer);
9781040
}
9791041

9801042
[DllImport(_PythonDll, CallingConvention= CallingConvention.Cdecl, EntryPoint= "PyObject_Size")]
@@ -1090,7 +1152,7 @@ internal static bool PyLong_Check(IntPtr ob)
10901152

10911153
internalstatic IntPtr PyLong_FromUnsignedLong(object value)
10921154
{
1093-
if(Is32Bit|| IsWindows)
1155+
if(Is32Bit|| IsWindows)
10941156
return PyLong_FromUnsignedLong32(Convert.ToUInt32(value));
10951157
else
10961158
return PyLong_FromUnsignedLong64(Convert.ToUInt64(value));
@@ -1280,7 +1342,7 @@ internal static int PySequence_DelSlice(IntPtr pointer, long i1, long i2)
12801342

12811343
internalstaticlong PySequence_Size(IntPtr pointer)
12821344
{
1283-
return(long)_PySequence_Size(pointer);
1345+
return(long)_PySequence_Size(pointer);
12841346
}
12851347

12861348
[DllImport(_PythonDll, CallingConvention= CallingConvention.Cdecl, EntryPoint= "PySequence_Size")]
@@ -1305,7 +1367,7 @@ internal static IntPtr PySequence_Repeat(IntPtr pointer, long count)
13051367

13061368
internalstaticlong PySequence_Count(IntPtr pointer, IntPtr value)
13071369
{
1308-
return(long)_PySequence_Count(pointer, value);
1370+
return(long)_PySequence_Count(pointer, value);
13091371
}
13101372

13111373
[DllImport(_PythonDll, CallingConvention= CallingConvention.Cdecl, EntryPoint= "PySequence_Count")]
@@ -1348,7 +1410,7 @@ internal static IntPtr PyString_FromString(string value)
13481410

13491411
internalstaticlong PyBytes_Size(IntPtr op)
13501412
{
1351-
return(long)_PyBytes_Size(op);
1413+
return(long)_PyBytes_Size(op);
13521414
}
13531415

13541416
[DllImport(_PythonDll, CallingConvention= CallingConvention.Cdecl, EntryPoint= "PyBytes_Size")]
@@ -1579,7 +1641,7 @@ internal static bool PyDict_Check(IntPtr ob)
15791641

15801642
internalstaticlong PyDict_Size(IntPtr pointer)
15811643
{
1582-
return(long)_PyDict_Size(pointer);
1644+
return(long)_PyDict_Size(pointer);
15831645
}
15841646

15851647
[DllImport(_PythonDll, CallingConvention= CallingConvention.Cdecl, EntryPoint= "PyDict_Size")]
@@ -1657,7 +1719,7 @@ internal static int PyList_SetSlice(IntPtr pointer, long start, long end, IntPtr
16571719

16581720
internalstaticlong PyList_Size(IntPtr pointer)
16591721
{
1660-
return(long)_PyList_Size(pointer);
1722+
return(long)_PyList_Size(pointer);
16611723
}
16621724

16631725
[DllImport(_PythonDll, CallingConvention= CallingConvention.Cdecl, EntryPoint= "PyList_Size")]
@@ -1706,7 +1768,7 @@ internal static IntPtr PyTuple_GetSlice(IntPtr pointer, long start, long end)
17061768

17071769
internalstaticlong PyTuple_Size(IntPtr pointer)
17081770
{
1709-
return(long)_PyTuple_Size(pointer);
1771+
return(long)_PyTuple_Size(pointer);
17101772
}
17111773

17121774
[DllImport(_PythonDll, CallingConvention= CallingConvention.Cdecl, EntryPoint= "PyTuple_Size")]
@@ -1757,6 +1819,9 @@ internal static bool PyIter_Check(IntPtr pointer)
17571819
[DllImport(_PythonDll, CallingConvention= CallingConvention.Cdecl)]
17581820
internalstaticextern IntPtr PyImport_Import(IntPtr name);
17591821

1822+
/// <summary>
1823+
/// Return value: New reference.
1824+
/// </summary>
17601825
[DllImport(_PythonDll, CallingConvention= CallingConvention.Cdecl)]
17611826
internalstaticextern IntPtr PyImport_ImportModule(string name);
17621827

@@ -1956,5 +2021,39 @@ internal static void SetNoSiteFlag()
19562021
}
19572022
}
19582023
}
2024+
2025+
/// <summary>
2026+
/// Return value: New reference.
2027+
/// </summary>
2028+
internalstatic IntPtr GetBuiltins()
2029+
{
2030+
return IsPython3? PyImport_ImportModule("builtins")
2031+
: PyImport_ImportModule("__builtin__");
2032+
}
2033+
}
2034+
2035+
2036+
class PyReferenceCollection
2037+
{
2038+
private List<KeyValuePair<IntPtr, Action>> _actions=new List<KeyValuePair<IntPtr, Action>>();
2039+
2040+
/// <summary>
2041+
/// Record obj's address to release the obj in the future,
2042+
/// obj must alive before calling Release.
2043+
/// </summary>
2044+
publicvoid Add(IntPtr ob, Action onRelease)
2045+
{
2046+
_actions.Add(new KeyValuePair<IntPtr, Action>(ob, onRelease));
2047+
}
2048+
2049+
publicvoid Release()
2050+
{
2051+
foreach(var itemin _actions)
2052+
{
2053+
Runtime.XDecref(item.Key);
2054+
item.Value?.Invoke();
2055+
}
2056+
_actions.Clear();
2057+
}
19592058
}
19602059
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp