diff --git a/modules/packages/Python.chpl b/modules/packages/Python.chpl index df8857fb407d..703208d54eb3 100644 --- a/modules/packages/Python.chpl +++ b/modules/packages/Python.chpl @@ -222,7 +222,7 @@ module Python { private import List; private import Map; - private use CPythonInterface; + private use CPythonInterface except PyObject; private use CWChar; private use OS.POSIX only getenv; @@ -1188,6 +1188,22 @@ module Python { return interpreter.fromPython(value, this.obj); } + proc type release(in self: owned PyObject): PyObjectPtr { + var selfUn: unmanaged PyObject = owned.release(self); + selfUn.isOwned = false; + var ptr: PyObjectPtr = selfUn.obj; + delete selfUn; + return ptr; + } + proc type release(in self: owned ClassObject): PyObjectPtr { + var pyObj: unmanaged ClassObject = owned.release(self); + pyObj.obj!.isOwned = false; + var ptr: PyObjectPtr = pyObj.obj!.get(); + delete pyObj; + return ptr; + } + + /* Returns the string representation of the object. This is the same as casting to a string. diff --git a/test/library/packages/Python/customType.chpl b/test/library/packages/Python/customType.chpl index 4cd3561f7e2a..17c3e14e4776 100644 --- a/test/library/packages/Python/customType.chpl +++ b/test/library/packages/Python/customType.chpl @@ -15,11 +15,7 @@ class myRecConverter: TypeConverter { override proc toPython(interpreter: borrowed Interpreter, type T, const value: T): Python.CPythonInterface.PyObjectPtr throws { if T != myRec then halt("Expected myRec"); - var pyObj = owned.release(pyClsType(value.x, value.y)); - pyObj.obj!.isOwned = false; - var ptr = pyObj.obj!.get(); - delete pyObj; - return ptr; + return PyObject.release(pyClsType(value.x, value.y)); } override proc fromPython(interpreter: borrowed Interpreter, type T, obj: Python.CPythonInterface.PyObjectPtr): T throws {