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

Commit5f56ebc

Browse files
amos402filmor
authored andcommitted
Fix refcnt errors (split from#958) (#1001)
* Add exception helper* Fixed refcnt error in ExtensionType.FinalizeObject* Fixed typename leaking* Fix refcnt error by using `using`
1 parentd1044c3 commit5f56ebc

File tree

6 files changed

+27
-8
lines changed

6 files changed

+27
-8
lines changed

‎.editorconfig‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ dotnet_sort_system_directives_first = true
2525
dotnet_separate_import_directive_groups =true
2626

2727
[*.cs]
28-
csharp_new_line_before_open_brace =true
28+
csharp_new_line_before_open_brace =all
2929
csharp_new_line_before_else =true
3030
csharp_new_line_before_catch =true
3131
csharp_new_line_before_finally =true

‎src/runtime/extensiontype.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public ExtensionType()
3838

3939
Runtime.PyObject_GC_UnTrack(py);
4040

41+
// Steals a ref to tpHandle.
4142
tpHandle=tp;
4243
pyHandle=py;
4344
gcHandle=gc;
@@ -50,7 +51,7 @@ public ExtensionType()
5051
publicstaticvoidFinalizeObject(ManagedTypeself)
5152
{
5253
Runtime.PyObject_GC_Del(self.pyHandle);
53-
Runtime.XDecref(self.tpHandle);
54+
// Not necessary for decref of `tpHandle`.
5455
self.gcHandle.Free();
5556
}
5657

‎src/runtime/metatype.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ private static IntPtr DoInstanceCheck(IntPtr tp, IntPtr args, bool checkType)
266266
returnRuntime.PyFalse;
267267
}
268268

269+
Runtime.XIncref(args);
269270
using(varargsObj=newPyList(args))
270271
{
271272
if(argsObj.Length()!=1)

‎src/runtime/pythonexception.cs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
usingSystem;
2+
usingSystem.Runtime.CompilerServices;
23

34
namespacePython.Runtime
45
{
@@ -190,5 +191,21 @@ public static bool Matches(IntPtr ob)
190191
{
191192
returnRuntime.PyErr_ExceptionMatches(ob)!=0;
192193
}
194+
195+
publicstaticvoidThrowIfIsNull(IntPtrob)
196+
{
197+
if(ob==IntPtr.Zero)
198+
{
199+
thrownewPythonException();
200+
}
201+
}
202+
203+
publicstaticvoidThrowIfIsNotZero(intvalue)
204+
{
205+
if(value!=0)
206+
{
207+
thrownewPythonException();
208+
}
209+
}
193210
}
194211
}

‎src/runtime/runtime.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,10 @@ internal static IntPtr PyUnicode_FromStringAndSize(IntPtr value, long size)
13681368

13691369
[DllImport(_PythonDll, CallingConvention= CallingConvention.Cdecl)]
13701370
privatestaticextern IntPtr PyUnicode_FromStringAndSize(IntPtr value, IntPtr size);
1371+
1372+
[DllImport(_PythonDll, CallingConvention= CallingConvention.Cdecl)]
1373+
internalstaticextern IntPtr PyUnicode_AsUTF8(IntPtr unicode);
1374+
13711375
#elif PYTHON2
13721376
internalstatic IntPtr PyString_FromStringAndSize(string value,long size)
13731377
{

‎src/runtime/typemanager.cs‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,12 +409,8 @@ internal static IntPtr AllocateTypeObject(string name)
409409
// the Python version of the type name - otherwise we'd have to
410410
// allocate the tp_name and would have no way to free it.
411411
#ifPYTHON3
412-
// For python3 we leak two objects. One for the ASCII representation
413-
// required for tp_name, and another for the Unicode representation
414-
// for ht_name.
415-
IntPtrtemp=Runtime.PyBytes_FromString(name);
416-
IntPtrraw=Runtime.PyBytes_AS_STRING(temp);
417-
temp=Runtime.PyUnicode_FromString(name);
412+
IntPtrtemp=Runtime.PyUnicode_FromString(name);
413+
IntPtrraw=Runtime.PyUnicode_AsUTF8(temp);
418414
#elifPYTHON2
419415
IntPtrtemp=Runtime.PyString_FromString(name);
420416
IntPtrraw=Runtime.PyString_AsString(temp);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp