@@ -34,7 +34,8 @@ func LoadModule(name string) (ObjectModule, error) {
34
34
35
35
pyMdl , err := C .PyImport_ImportModule (cModule )
36
36
if pyMdl == nil {
37
- ch <- & Result {ObjectModule {}, fmt .Errorf ("cannot load '%v' module: %v" , name , err )}
37
+ ch <- & Result {ObjectModule {}, fmt .Errorf ("cannot load '%v' module: %v" ,
38
+ name , err )}
38
39
return
39
40
}
40
41
@@ -46,54 +47,57 @@ func LoadModule(name string) (ObjectModule, error) {
46
47
}
47
48
48
49
// NewInstance returns `name` constructor.
49
- func (m * ObjectModule ) NewInstance (name string , args ... data.Value ) (ObjectInstance , error ) {
50
- cName := C .CString (name )
51
- defer C .free (unsafe .Pointer (cName ))
52
-
53
- type Result struct {
54
- val ObjectInstance
55
- err error
56
- }
57
- ch := make (chan * Result , 1 )
58
- go func () {
59
- runtime .LockOSThread ()
60
- state := GILState_Ensure ()
61
- defer GILState_Release (state )
62
-
63
- pyInstance := C .PyObject_GetAttrString (m .p , cName )
64
- if pyInstance == nil {
65
- ch <- & Result {ObjectInstance {}, fmt .Errorf ("cannot create '%v' instance" , name )}
66
- return
67
- }
68
- defer C .Py_DecRef (pyInstance )
69
-
70
- pyArg := C .PyTuple_New (C .Py_ssize_t (len (args )))
71
- if pyArg == nil {
72
- ch <- & Result {ObjectInstance {}, getPyErr ()}
73
- return
74
- }
75
- defer C .Py_DecRef (pyArg )
76
-
77
- for i , v := range args {
78
- o , err := newPyObj (v )
79
- if err != nil {
80
- ch <- & Result {ObjectInstance {}, fmt .Errorf ("%v at '%v'" , err .Error (), name )}
81
- return
82
- }
83
- C .PyTuple_SetItem (pyArg , C .Py_ssize_t (i ), o .p )
84
- }
85
-
86
- // get constructor (called `__init__(self)`)
87
- ret := C .PyObject_CallObject (pyInstance , pyArg )
88
- if ret == nil {
89
- ch <- & Result {ObjectInstance {}, fmt .Errorf ("cannot create '%v' instance" , name )}
90
- return
91
- }
92
- ch <- & Result {ObjectInstance {Object {p : ret }}, nil }
93
- }()
94
- res := <- ch
50
+ //
51
+ // ```python
52
+ // class Sample(object):
53
+ // def __init__(self, a, b=5):
54
+ // # initializing
55
+ // ```
56
+ //
57
+ // To get that "Sample" python instance, callers use this function like:
58
+ // NewInstance("Sample", data.Value, data.Int)
59
+ // or
60
+ // NewInstance("Sample", data.Value) // value "b" is optional and will set 5
61
+ func (m * ObjectModule ) NewInstance (name string , args ... data.Value ) (
62
+ ObjectInstance , error ) {
63
+ return newInstance (m , name , nil , args ... )
64
+ }
95
65
96
- return res .val , res .err
66
+ // NewInstanceWithKwd returns 'name' constructor with named arguments.
67
+ //
68
+ // ```python
69
+ // class Sample(object):
70
+ // def __init__(self, a, b=5, **c):
71
+ // # initializing
72
+ // ```
73
+ //
74
+ // To get that "Sample" python instance, callers use a map object as named
75
+ // arguments, like:
76
+ //
77
+ // arg1 := data.Map{
78
+ // "a": data.Value, // ex) data.String("v1")
79
+ // "b": data.Int, // ex) data.Int(5)
80
+ // "hoge1": data.Value, // ex) data.Float(100.0)
81
+ // "hoge2": data.Value, // ex) data.True
82
+ // }
83
+ // `arg1` is same as `Sawmple(a-'v1', b=5, hoge1=100.0, hoge2=True)`.
84
+ //
85
+ // arg2 := data.Map{
86
+ // "a": data.Value, // ex) data.String("v1")
87
+ // "hoge1": data.Value, // ex) data.Float(100.0)
88
+ // "hoge2": data.Value, // ex) data.True
89
+ // }
90
+ // `arg2` is same as `Sample(a='v1', hoge1=100.0, hoge2=True)`, and `self.b`
91
+ // will be set default value (=5).
92
+ //
93
+ // arg3 := data.Map{
94
+ // "a": data.Value, // ex) data.String("v1")
95
+ // }
96
+ // `arg3` is same as `Sample(a='v1')`, `self.b` will be set default value (=5),
97
+ // and `self.c` will be set `{}`
98
+ func (m * ObjectModule ) NewInstanceWithKwd (name string , kwdArgs data.Map ) (
99
+ ObjectInstance , error ) {
100
+ return newInstance (m , name , kwdArgs )
97
101
}
98
102
99
103
// GetClass returns `name` class instance.
@@ -114,7 +118,8 @@ func (m *ObjectModule) GetClass(name string) (ObjectInstance, error) {
114
118
115
119
pyInstance := C .PyObject_GetAttrString (m .p , cName )
116
120
if pyInstance == nil {
117
- ch <- & Result {ObjectInstance {}, fmt .Errorf ("cannot get '%v' instance" , name )}
121
+ ch <- & Result {ObjectInstance {}, fmt .Errorf ("cannot get '%v' instance" ,
122
+ name )}
118
123
return
119
124
}
120
125
ch <- & Result {ObjectInstance {Object {p : pyInstance }}, nil }
0 commit comments