Skip to content

Commit

Permalink
add release method
Browse files Browse the repository at this point in the history
Signed-off-by: Jade Abraham <[email protected]>
  • Loading branch information
jabraham17 committed Nov 20, 2024
1 parent 6e965b0 commit 9400f6b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
18 changes: 17 additions & 1 deletion modules/packages/Python.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
Expand Down
6 changes: 1 addition & 5 deletions test/library/packages/Python/customType.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 9400f6b

Please sign in to comment.