-
Notifications
You must be signed in to change notification settings - Fork 424
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Python module API to prefer methods over constructors (#26695)
Refactors the Python module API to prefer methods over constructors. The old constructors are still available and still useful in some places, but for the most part users should prefer the new API. Old style ```chapel var interp = new Interpreter(); var mod = new Module(interp, 'mod'); var func = new Function(mod, 'myfunc'); ``` New style ```chapel var interp = new Interpreter(); var mod = interp.importModule('mod'); var func = mod.get('myfunc'); ``` This PR also makes a few other changes - renamed the internal `.get` to `.getPyObject` - renamed `getAttr`/`setAttr` to `get`/`set` - renamed `getIdx`/`setIdx` to `get`/`set` - added support for passing kwargs to `.call` - made a variety of doc cleanups/reordered code for better clarity - [x] `st test/library/packages/Python` - [x] `st test/library/packages/Python` with GASNet [Reviewed by @lydia-duncan]
- Loading branch information
Showing
28 changed files
with
449 additions
and
208 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
test/library/packages/Python/correctness/arrays/myArrayReturnArray.good
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
$CHPL_HOME/modules/packages/Python.chpl:nnnn: In method 'callInternal': | ||
$CHPL_HOME/modules/packages/Python.chpl:nnnn: error: Cannot create an Array from an existing PyObject | ||
$CHPL_HOME/modules/packages/Python.chpl:nnnn: called as Value.callInternal(type retType = owned Array(nothing), pyArg: c_ptr(PyObject), kwargs: nothing) from method 'this' | ||
$CHPL_HOME/modules/packages/Python.chpl:nnnn: called as Value.callInternal(type retType = owned Array(nothing), pyFunc: c_ptr(PyObject), pyArg: c_ptr(PyObject), kwargs: nothing) from method 'this' | ||
myArrayReturnArray.chpl:16: called as Value.this(type retType = owned Array(nothing), args(0): owned Array(int(64))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
use Python; | ||
|
||
var code = """ | ||
class C: | ||
def __init__(self): | ||
self.x = 17 | ||
def foo(self): | ||
return self.x | ||
def bar(): | ||
return 18 | ||
"""; | ||
|
||
var interp = new Interpreter(); | ||
var mod = interp.importModule('__empty__', code); | ||
|
||
{ | ||
var res1 = mod.call('bar'); | ||
|
||
var bar = mod.get('bar'); | ||
var res2 = bar(); | ||
|
||
var res3 = mod.get('bar')(); | ||
|
||
// these are all the same | ||
writeln("mod.call('bar'): ", res1); | ||
writeln("bar(): ", res2); | ||
writeln("mod.get('bar')(): ", res3); | ||
} | ||
|
||
{ | ||
var C = mod.get('C'); | ||
var c = C(); | ||
var res1 = c.call('foo'); | ||
|
||
var foo = c.get('foo'); | ||
var res2 = foo(); | ||
|
||
var res3 = mod.get('C')().call('foo'); | ||
|
||
// these are all the same | ||
writeln("c.call('foo'): ", res1); | ||
writeln("foo(): ", res2); | ||
writeln("mod.get('C')().call('foo'): ", res3); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
mod.call('bar'): 18 | ||
bar(): 18 | ||
mod.get('bar')(): 18 | ||
c.call('foo'): 17 | ||
foo(): 17 | ||
mod.get('C')().call('foo'): 17 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.