Skip to content

Fix refcnt errors (split from #958) #1001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dotnet_sort_system_directives_first = true
dotnet_separate_import_directive_groups = true

[*.cs]
csharp_new_line_before_open_brace = true
csharp_new_line_before_open_brace = all
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/extensiontype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public ExtensionType()

Runtime.PyObject_GC_UnTrack(py);

// Steals a ref to tpHandle.
tpHandle = tp;
pyHandle = py;
gcHandle = gc;
Expand All @@ -50,7 +51,7 @@ public ExtensionType()
public static void FinalizeObject(ManagedType self)
{
Runtime.PyObject_GC_Del(self.pyHandle);
Runtime.XDecref(self.tpHandle);
// Not necessary for decref of `tpHandle`.
self.gcHandle.Free();
}

Expand Down
1 change: 1 addition & 0 deletions src/runtime/metatype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ private static IntPtr DoInstanceCheck(IntPtr tp, IntPtr args, bool checkType)
return Runtime.PyFalse;
}

Runtime.XIncref(args);
using (var argsObj = new PyList(args))
{
if (argsObj.Length() != 1)
Expand Down
17 changes: 17 additions & 0 deletions src/runtime/pythonexception.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;

namespace Python.Runtime
{
Expand Down Expand Up @@ -190,5 +191,21 @@ public static bool Matches(IntPtr ob)
{
return Runtime.PyErr_ExceptionMatches(ob) != 0;
}

public static void ThrowIfIsNull(IntPtr ob)
{
if (ob == IntPtr.Zero)
{
throw new PythonException();
}
}

public static void ThrowIfIsNotZero(int value)
{
if (value != 0)
{
throw new PythonException();
}
}
}
}
4 changes: 4 additions & 0 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,10 @@ internal static IntPtr PyUnicode_FromStringAndSize(IntPtr value, long size)

[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr PyUnicode_FromStringAndSize(IntPtr value, IntPtr size);

[DllImport(_PythonDll, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr PyUnicode_AsUTF8(IntPtr unicode);

#elif PYTHON2
internal static IntPtr PyString_FromStringAndSize(string value, long size)
{
Expand Down
8 changes: 2 additions & 6 deletions src/runtime/typemanager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,12 +415,8 @@ internal static IntPtr AllocateTypeObject(string name)
// the Python version of the type name - otherwise we'd have to
// allocate the tp_name and would have no way to free it.
#if PYTHON3
// For python3 we leak two objects. One for the ASCII representation
// required for tp_name, and another for the Unicode representation
// for ht_name.
IntPtr temp = Runtime.PyBytes_FromString(name);
IntPtr raw = Runtime.PyBytes_AS_STRING(temp);
temp = Runtime.PyUnicode_FromString(name);
IntPtr temp = Runtime.PyUnicode_FromString(name);
IntPtr raw = Runtime.PyUnicode_AsUTF8(temp);
#elif PYTHON2
IntPtr temp = Runtime.PyString_FromString(name);
IntPtr raw = Runtime.PyString_AsString(temp);
Expand Down