diff --git a/capi.go b/.legacy/capi.go similarity index 100% rename from capi.go rename to .legacy/capi.go diff --git a/dict.go b/.legacy/dict.go similarity index 100% rename from dict.go rename to .legacy/dict.go diff --git a/exceptions.go b/.legacy/exceptions.go similarity index 100% rename from exceptions.go rename to .legacy/exceptions.go diff --git a/exceptions_posix.go b/.legacy/exceptions_posix.go similarity index 100% rename from exceptions_posix.go rename to .legacy/exceptions_posix.go diff --git a/go-python.c b/.legacy/go-python.c similarity index 98% rename from go-python.c rename to .legacy/go-python.c index 301600f..fc31f53 100644 --- a/go-python.c +++ b/.legacy/go-python.c @@ -450,10 +450,3 @@ _gopy_PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals, P //void _gopy_PySys_WriteStdout(const char *data) { // PySys_WriteStdout("%s", data) //} - -/* --- veryhigh --- */ - -int -_gopy_PyRun_SimpleString(const char *command) { - return PyRun_SimpleString(command); -} diff --git a/go-python.go b/.legacy/go-python.go similarity index 100% rename from go-python.go rename to .legacy/go-python.go diff --git a/go-python.h b/.legacy/go-python.h similarity index 100% rename from go-python.h rename to .legacy/go-python.h diff --git a/heap.go b/.legacy/heap.go similarity index 100% rename from heap.go rename to .legacy/heap.go diff --git a/none.go b/.legacy/none.go similarity index 100% rename from none.go rename to .legacy/none.go diff --git a/numeric.go b/.legacy/numeric.go similarity index 100% rename from numeric.go rename to .legacy/numeric.go diff --git a/.legacy/object.go b/.legacy/object.go new file mode 100644 index 0000000..274fcfd --- /dev/null +++ b/.legacy/object.go @@ -0,0 +1,471 @@ +package python + +//#include "go-python.h" +import "C" + +import ( + "fmt" + "os" + "strings" + "unsafe" +) + +// PyObject layer +type PyObject struct { + ptr *C.PyObject +} + +func (self *PyObject) topy() *C.PyObject { + return self.ptr +} + +func topy(self *PyObject) *C.PyObject { + if self == nil { + return nil + } + return self.ptr +} + +func togo(obj *C.PyObject) *PyObject { + if obj == nil { + return nil + } + return &PyObject{ptr: obj} +} + +// PyObject_FromVoidPtr converts a PyObject from an unsafe.Pointer +func PyObject_FromVoidPtr(ptr unsafe.Pointer) *PyObject { + return togo((*C.PyObject)(ptr)) +} + +func int2bool(i C.int) bool { + switch i { + case -1: + return false + case 0: + return false + case 1: + return true + default: + return true + } + return false +} + +func long2bool(i C.long) bool { + switch i { + case -1: + return false + case 0: + return false + case 1: + return true + default: + return true + } + return false +} + +func bool2int(i bool) C.int { + if i { + return C.int(1) + } + return C.int(0) +} + +type gopy_err struct { + err string +} + +func (self *gopy_err) Error() string { + return self.err +} + +func int2err(i C.int) error { + if i == 0 { + return nil + } + //FIXME: also handle python exceptions ? + return &gopy_err{fmt.Sprintf("error in C-Python (rc=%d)", int(i))} +} + +func file2go(f *C.FILE) *os.File { + return nil +} + +// C.PyObject* PyObject_GetCPointer(PyObject *o) +// Returns the internal C pointer to CPython object. +func (self *PyObject) GetCPointer() *C.PyObject { + return self.ptr +} + +// void Py_IncRef(PyObject *o) +// Increment the reference count for object o. The object may be +// NULL, in which case the function has no effect. +func (self *PyObject) IncRef() { + C.Py_IncRef(self.ptr) +} + +// void Py_DecRef(PyObject *o) +// Decrement the reference count for object o. If the object is +// NULL, nothing happens. If the reference count reaches zero, the +// object’s type’s deallocation function (which must not be NULL) is +// invoked. +// WARNING: The deallocation function can cause arbitrary Python +// code to be invoked. See the warnings and instructions in the +// Python docs, and consider using Clear instead. +func (self *PyObject) DecRef() { + C.Py_DecRef(self.ptr) +} + +// void Py_CLEAR(PyObject *o) +// Clear sets the PyObject's internal pointer to nil +// before calling Py_DecRef. This avoids the potential issues with +// Python code called by the deallocator referencing invalid, +// partially-deallocated data. +func (self *PyObject) Clear() { + tmp := self.ptr + self.ptr = nil + C.Py_DecRef(tmp) +} + +// int PyObject_HasAttr(PyObject *o, PyObject *attr_name) +// Returns 1 if o has the attribute attr_name, and 0 otherwise. This is equivalent to the Python expression hasattr(o, attr_name). This function always succeeds. +func (self *PyObject) HasAttr(attr_name *PyObject) int { + return int(C.PyObject_HasAttr(self.ptr, attr_name.ptr)) +} + +// int PyObject_HasAttrString(PyObject *o, const char *attr_name) +// Returns 1 if o has the attribute attr_name, and 0 otherwise. This is equivalent to the Python expression hasattr(o, attr_name). This function always succeeds. +func (self *PyObject) HasAttrString(attr_name string) int { + c_attr_name := C.CString(attr_name) + defer C.free(unsafe.Pointer(c_attr_name)) + + return int(C.PyObject_HasAttrString(self.ptr, c_attr_name)) +} + +// PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name) +// Return value: New reference. +// Retrieve an attribute named attr_name from object o. Returns the attribute value on success, or NULL on failure. This is the equivalent of the Python expression o.attr_name. +func (self *PyObject) GetAttr(attr_name *PyObject) *PyObject { + return togo(C.PyObject_GetAttr(self.ptr, attr_name.ptr)) +} + +// PyObject* PyObject_Dir() +// Return value: New reference. +// This is equivalent to the Python expression dir(o), returning a (possibly empty) list of strings appropriate for the object argument, or NULL if there was an error. If the argument is NULL, this is like the Python dir(), returning the names of the current locals; in this case, if no execution frame is active then NULL is returned but PyErr_Occurred() will return false. +func (self *PyObject) PyObject_Dir() *PyObject { + return togo(C.PyObject_Dir(self.ptr)) + +} + +// PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name) +// Return value: New reference. +// Retrieve an attribute named attr_name from object o. Returns the attribute value on success, or NULL on failure. This is the equivalent of the Python expression o.attr_name. +func (self *PyObject) GetAttrString(attr_name string) *PyObject { + c_attr_name := C.CString(attr_name) + defer C.free(unsafe.Pointer(c_attr_name)) + return togo(C.PyObject_GetAttrString(self.ptr, c_attr_name)) +} + +// PyObject* PyObject_GenericGetAttr(PyObject *o, PyObject *name) +// Generic attribute getter function that is meant to be put into a type object’s tp_getattro slot. It looks for a descriptor in the dictionary of classes in the object’s MRO as well as an attribute in the object’s __dict__ (if present). As outlined in Implementing Descriptors, data descriptors take preference over instance attributes, while non-data descriptors don’t. Otherwise, an AttributeError is raised. +func (self *PyObject) GenericGetAttr(name *PyObject) *PyObject { + return togo(C.PyObject_GenericGetAttr(self.ptr, name.ptr)) +} + +// int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v) +// Set the value of the attribute named attr_name, for object o, to the value v. Returns -1 on failure. This is the equivalent of the Python statement o.attr_name = v. +func (self *PyObject) SetAttr(attr_name, v *PyObject) int { + return int(C.PyObject_SetAttr(self.ptr, attr_name.ptr, v.ptr)) +} + +// int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v) +// Set the value of the attribute named attr_name, for object o, to the value v. Returns -1 on failure. This is the equivalent of the Python statement o.attr_name = v. +func (self *PyObject) SetAttrString(attr_name string, v *PyObject) int { + c_attr_name := C.CString(attr_name) + defer C.free(unsafe.Pointer(c_attr_name)) + return int(C.PyObject_SetAttrString(self.ptr, c_attr_name, v.ptr)) +} + +// int PyObject_GenericSetAttr(PyObject *o, PyObject *name, PyObject *value) +// Generic attribute setter function that is meant to be put into a type object’s tp_setattro slot. It looks for a data descriptor in the dictionary of classes in the object’s MRO, and if found it takes preference over setting the attribute in the instance dictionary. Otherwise, the attribute is set in the object’s __dict__ (if present). Otherwise, an AttributeError is raised and -1 is returned. +func (self *PyObject) GenericSetAttr(name, value *PyObject) int { + return int(C.PyObject_GenericSetAttr(self.ptr, name.ptr, value.ptr)) +} + +// int PyObject_DelAttr(PyObject *o, PyObject *attr_name) +// Delete attribute named attr_name, for object o. Returns -1 on failure. This is the equivalent of the Python statement del o.attr_name. +func (self *PyObject) DelAttr(attr_name *PyObject) int { + return int(C._gopy_PyObject_DelAttr(self.ptr, attr_name.ptr)) +} + +// int PyObject_DelAttrString(PyObject *o, const char *attr_name) +// Delete attribute named attr_name, for object o. Returns -1 on failure. This is the equivalent of the Python statement del o.attr_name. +func (self *PyObject) DelAttrString(attr_name string) int { + c_attr_name := C.CString(attr_name) + defer C.free(unsafe.Pointer(c_attr_name)) + return int(C._gopy_PyObject_DelAttrString(self.ptr, c_attr_name)) +} + +type Py_OPID C.int + +const ( + Py_LT Py_OPID = C.Py_LT + Py_LE Py_OPID = C.Py_LE + Py_EQ Py_OPID = C.Py_EQ + Py_NE Py_OPID = C.Py_NE + Py_GT Py_OPID = C.Py_GT + Py_GE Py_OPID = C.Py_GE +) + +// PyObject* PyObject_RichCompare(PyObject *o1, PyObject *o2, int opid) +// Return value: New reference. +// Compare the values of o1 and o2 using the operation specified by opid, which must be one of Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, or Py_GE, corresponding to <, <=, ==, !=, >, or >= respectively. This is the equivalent of the Python expression o1 op o2, where op is the operator corresponding to opid. Returns the value of the comparison on success, or NULL on failure. +func (self *PyObject) RichCompare(o2 *PyObject, opid Py_OPID) *PyObject { + return togo(C.PyObject_RichCompare(self.ptr, o2.ptr, C.int(opid))) +} + +// int PyObject_RichCompareBool(PyObject *o1, PyObject *o2, int opid) +// Compare the values of o1 and o2 using the operation specified by opid, which must be one of Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, or Py_GE, corresponding to <, <=, ==, !=, >, or >= respectively. Returns -1 on error, 0 if the result is false, 1 otherwise. This is the equivalent of the Python expression o1 op o2, where op is the operator corresponding to opid. +func (self *PyObject) RichCompareBool(o2 *PyObject, opid Py_OPID) int { + return int(C.PyObject_RichCompareBool(self.ptr, o2.ptr, C.int(opid))) +} + +// int PyObject_Cmp(PyObject *o1, PyObject *o2, int *result) +// Compare the values of o1 and o2 using a routine provided by o1, if one exists, otherwise with a routine provided by o2. The result of the comparison is returned in result. Returns -1 on failure. This is the equivalent of the Python statement result = cmp(o1, o2). +func (self *PyObject) Cmp(o2 *PyObject) (err, result int) { + var c_result C.int = -1 + var c_err C.int = -1 + + c_err = C.PyObject_Cmp(self.ptr, o2.ptr, &c_result) + return int(c_err), int(c_result) +} + +// int PyObject_Compare(PyObject *o1, PyObject *o2) +// Compare the values of o1 and o2 using a routine provided by o1, if one exists, otherwise with a routine provided by o2. Returns the result of the comparison on success. On error, the value returned is undefined; use PyErr_Occurred() to detect an error. This is equivalent to the Python expression cmp(o1, o2). +func (self *PyObject) Compare(o2 *PyObject) int { + return int(C.PyObject_Compare(self.ptr, o2.ptr)) +} + +// PyObject* PyObject_Repr(PyObject *o) +// Return value: New reference. +// Compute a string representation of object o. Returns the string representation on success, NULL on failure. This is the equivalent of the Python expression repr(o). Called by the repr() built-in function and by reverse quotes. +func (self *PyObject) Repr() *PyObject { + return togo(C.PyObject_Repr(self.ptr)) +} + +// PyObject* PyObject_Str(PyObject *o) +// Return value: New reference. +// Compute a string representation of object o. Returns the string representation on success, NULL on failure. This is the equivalent of the Python expression str(o). Called by the str() built-in function and by the print statement. +func (self *PyObject) Str() *PyObject { + return togo(C.PyObject_Str(self.ptr)) +} + +// PyObject* PyObject_Bytes(PyObject *o) +// Compute a bytes representation of object o. In 2.x, this is just a alias for PyObject_Str(). +func (self *PyObject) Bytes() *PyObject { + return togo(C.PyObject_Bytes(self.ptr)) +} + +// PyObject* PyObject_Unicode(PyObject *o) +// Return value: New reference. +// Compute a Unicode string representation of object o. Returns the Unicode string representation on success, NULL on failure. This is the equivalent of the Python expression unicode(o). Called by the unicode() built-in function. +func (self *PyObject) Unicode() *PyObject { + return togo(C.PyObject_Unicode(self.ptr)) +} + +// int PyObject_IsInstance(PyObject *inst, PyObject *cls) +// Returns 1 if inst is an instance of the class cls or a subclass of cls, or 0 if not. On error, returns -1 and sets an exception. If cls is a type object rather than a class object, PyObject_IsInstance() returns 1 if inst is of type cls. If cls is a tuple, the check will be done against every entry in cls. The result will be 1 when at least one of the checks returns 1, otherwise it will be 0. If inst is not a class instance and cls is neither a type object, nor a class object, nor a tuple, inst must have a __class__ attribute — the class relationship of the value of that attribute with cls will be used to determine the result of this function. +// +// New in version 2.1. +// +// Changed in version 2.2: Support for a tuple as the second argument added. +// +// Subclass determination is done in a fairly straightforward way, but includes a wrinkle that implementors of extensions to the class system may want to be aware of. If A and B are class objects, B is a subclass of A if it inherits from A either directly or indirectly. If either is not a class object, a more general mechanism is used to determine the class relationship of the two objects. When testing if B is a subclass of A, if A is B, PyObject_IsSubclass() returns true. If A and B are different objects, B‘s __bases__ attribute is searched in a depth-first fashion for A — the presence of the __bases__ attribute is considered sufficient for this determination. +func (self *PyObject) IsInstance(cls *PyObject) int { + return int(C.PyObject_IsInstance(self.ptr, cls.ptr)) +} + +// int PyObject_IsSubclass(PyObject *derived, PyObject *cls) +// Returns 1 if the class derived is identical to or derived from the class cls, otherwise returns 0. In case of an error, returns -1. If cls is a tuple, the check will be done against every entry in cls. The result will be 1 when at least one of the checks returns 1, otherwise it will be 0. If either derived or cls is not an actual class object (or tuple), this function uses the generic algorithm described above. +// +// New in version 2.1. +// +// Changed in version 2.3: Older versions of Python did not support a tuple as the second argument. +func (self *PyObject) IsSubclass(cls *PyObject) int { + return int(C.PyObject_IsSubclass(self.ptr, cls.ptr)) +} + +// int PyCallable_Check(PyObject *o) +// Determine if the object o is callable. Return 1 if the object is callable and 0 otherwise. This function always succeeds. +// PyObject* PyObject_Call(PyObject *callable_object, PyObject *args, PyObject *kw) +// Return value: New reference. +// Call a callable Python object callable_object, with arguments given by the tuple args, and named arguments given by the dictionary kw. If no named arguments are needed, kw may be NULL. args must not be NULL, use an empty tuple if no arguments are needed. Returns the result of the call on success, or NULL on failure. This is the equivalent of the Python expression apply(callable_object, args, kw) or callable_object(*args, **kw). +// +// New in version 2.2. +func (self *PyObject) Check_Callable() bool { + return int2bool(C.PyCallable_Check(self.ptr)) +} + +// PyObject* PyObject_Call(PyObject *callable_object, PyObject *args, PyObject *kw) +// Return value: New reference. +// Call a callable Python object callable_object, with arguments given by the tuple args, and named arguments given by the dictionary kw. If no named arguments are needed, kw may be NULL. args must not be NULL, use an empty tuple if no arguments are needed. Returns the result of the call on success, or NULL on failure. This is the equivalent of the Python expression apply(callable_object, args, kw) or callable_object(*args, **kw). +func (self *PyObject) Call(args, kw *PyObject) *PyObject { + return togo(C.PyObject_Call(self.ptr, args.ptr, kw.ptr)) +} + +// PyObject* PyObject_CallObject(PyObject *callable_object, PyObject *args) +// Return value: New reference. +// Call a callable Python object callable_object, with arguments given by the tuple args. If no arguments are needed, then args may be NULL. Returns the result of the call on success, or NULL on failure. This is the equivalent of the Python expression apply(callable_object, args) or callable_object(*args). +func (self *PyObject) CallObject(args *PyObject) *PyObject { + return togo(C.PyObject_CallObject(self.ptr, args.ptr)) +} + +// PyObject* PyObject_CallFunction(PyObject *callable, char *format, ...) +// Return value: New reference. +// Call a callable Python object callable, with a variable number of C arguments. The C arguments are described using a Py_BuildValue() style format string. The format may be NULL, indicating that no arguments are provided. Returns the result of the call on success, or NULL on failure. This is the equivalent of the Python expression apply(callable, args) or callable(*args). Note that if you only pass PyObject * args, PyObject_CallFunctionObjArgs() is a faster alternative. +func (self *PyObject) CallFunction(args ...interface{}) *PyObject { + if len(args) > int(C._gopy_max_varargs) { + panic(fmt.Errorf( + "gopy: maximum number of varargs (%d) exceeded (%d)", + int(C._gopy_max_varargs), + len(args), + )) + } + + types := make([]string, 0, len(args)) + cargs := make([]unsafe.Pointer, 0, len(args)) + + for _, arg := range args { + ptr, typ := pyfmt(arg) + types = append(types, typ) + cargs = append(cargs, ptr) + if typ == "s" { + defer func(ptr unsafe.Pointer) { + C.free(ptr) + }(ptr) + } + } + + if len(args) <= 0 { + o := C._gopy_PyObject_CallFunction(self.ptr, 0, nil, nil) + return togo(o) + } + + pyfmt := C.CString(strings.Join(types, "")) + defer C.free(unsafe.Pointer(pyfmt)) + o := C._gopy_PyObject_CallFunction( + self.ptr, + C.int(len(args)), + pyfmt, + unsafe.Pointer(&cargs[0]), + ) + + return togo(o) + +} + +// PyObject* PyObject_CallMethod(PyObject *o, char *method, char *format, ...) +// Return value: New reference. +// Call the method named method of object o with a variable number of C arguments. The C arguments are described by a Py_BuildValue() format string that should produce a tuple. The format may be NULL, indicating that no arguments are provided. Returns the result of the call on success, or NULL on failure. This is the equivalent of the Python expression o.method(args). Note that if you only pass PyObject * args, PyObject_CallMethodObjArgs() is a faster alternative. +func (self *PyObject) CallMethod(method string, args ...interface{}) *PyObject { + if len(args) > int(C._gopy_max_varargs) { + panic(fmt.Errorf( + "gopy: maximum number of varargs (%d) exceeded (%d)", + int(C._gopy_max_varargs), + len(args), + )) + } + + cmethod := C.CString(method) + defer C.free(unsafe.Pointer(cmethod)) + + types := make([]string, 0, len(args)) + cargs := make([]unsafe.Pointer, 0, len(args)) + + for _, arg := range args { + ptr, typ := pyfmt(arg) + types = append(types, typ) + cargs = append(cargs, ptr) + if typ == "s" { + defer func(ptr unsafe.Pointer) { + C.free(ptr) + }(ptr) + } + } + + if len(args) <= 0 { + o := C._gopy_PyObject_CallMethod(self.ptr, cmethod, 0, nil, nil) + return togo(o) + } + + pyfmt := C.CString(strings.Join(types, "")) + defer C.free(unsafe.Pointer(pyfmt)) + o := C._gopy_PyObject_CallMethod( + self.ptr, + cmethod, + C.int(len(args)), + pyfmt, + unsafe.Pointer(&cargs[0]), + ) + + return togo(o) +} + +/* +PyObject* PyObject_CallFunctionObjArgs(PyObject *callable, ..., NULL) +Return value: New reference. +Call a callable Python object callable, with a variable number of PyObject* arguments. The arguments are provided as a variable number of parameters followed by NULL. Returns the result of the call on success, or NULL on failure. + +New in version 2.2. +*/ +func (self *PyObject) CallFunctionObjArgs(format string, args ...interface{}) *PyObject { + return self.CallFunction(args...) +} + +/* +PyObject* PyObject_CallMethodObjArgs(PyObject *o, PyObject *name, ..., NULL) +Return value: New reference. +Calls a method of the object o, where the name of the method is given as a Python string object in name. It is called with a variable number of PyObject* arguments. The arguments are provided as a variable number of parameters followed by NULL. Returns the result of the call on success, or NULL on failure. + +New in version 2.2. +*/ +func (self *PyObject) CallMethodObjArgs(method string, args ...interface{}) *PyObject { + return self.CallMethod(method, args...) +} + +// long PyObject_Hash(PyObject *o) +// Compute and return the hash value of an object o. On failure, return -1. This is the equivalent of the Python expression hash(o). +func (self *PyObject) Hash() int64 { + return int64(C.PyObject_Hash(topy(self))) +} + +// long PyObject_HashNotImplemented(PyObject *o) +// Set a TypeError indicating that type(o) is not hashable and return -1. This function receives special treatment when stored in a tp_hash slot, allowing a type to explicitly indicate to the interpreter that it is not hashable. +// +// New in version 2.6. +func (self *PyObject) HashNotImplemented() bool { + return long2bool(C.PyObject_HashNotImplemented(topy(self))) +} + +// int PyObject_IsTrue(PyObject *o) +// Returns 1 if the object o is considered to be true, and 0 otherwise. This is equivalent to the Python expression not not o. On failure, return -1. +func (self *PyObject) IsTrue() bool { + return int2bool(C.PyObject_IsTrue(topy(self))) +} + +// int PyObject_Not(PyObject *o) +// Returns 0 if the object o is considered to be true, and 1 otherwise. This is equivalent to the Python expression not o. On failure, return -1. +func (self *PyObject) Not() bool { + return int2bool(C.PyObject_Not(topy(self))) +} + +// PyObject* PyObject_Type(PyObject *o) +// Return value: New reference. +// When o is non-NULL, returns a type object corresponding to the object type of object o. On failure, raises SystemError and returns NULL. This is equivalent to the Python expression type(o). This function increments the reference count of the return value. There’s really no reason to use this function instead of the common expression o->ob_type, which returns a pointer of type PyTypeObject*, except when the incremented reference count is needed. +func (self *PyObject) Type() *PyObject { + return togo(C.PyObject_Type(topy(self))) +} + +// EOF diff --git a/object_posix.go b/.legacy/object_posix.go similarity index 100% rename from object_posix.go rename to .legacy/object_posix.go diff --git a/object_windows.go b/.legacy/object_windows.go similarity index 100% rename from object_windows.go rename to .legacy/object_windows.go diff --git a/otherobjects.go b/.legacy/otherobjects.go similarity index 99% rename from otherobjects.go rename to .legacy/otherobjects.go index 08ae445..7e59294 100644 --- a/otherobjects.go +++ b/.legacy/otherobjects.go @@ -531,7 +531,7 @@ func PySeqIter_Check(op *PyObject) bool { // // Return the next value from the iteration o. The object must be an iterator (it is up to the caller to check this). If there are no remaining values, returns NULL with no exception set. If an error occurs while retrieving the item, returns NULL and passes along the exception. func PyIter_Next(op *PyObject) *PyObject { - return togo(C.PyIter_Next(topy(op))) + return togo(C.PyIter_Next(topy(op))) } // PyObject* PySeqIter_New(PyObject *seq) diff --git a/.legacy/python.go b/.legacy/python.go new file mode 100644 index 0000000..1776e02 --- /dev/null +++ b/.legacy/python.go @@ -0,0 +1,104 @@ +// simplistic wrapper around the python C-API +package python + +//#include "go-python.h" +import "C" + +import ( + "fmt" +) + +// PyGILState is the Go alias for the PyGILState_STATE enum +type PyGILState C.PyGILState_STATE + +// PyThreadState layer +type PyThreadState struct { + ptr *C.PyThreadState +} + +// Initialize initializes the python interpreter and its GIL +func Initialize() error { + // make sure the python interpreter has been initialized + if C.Py_IsInitialized() == 0 { + C.Py_Initialize() + } + if C.Py_IsInitialized() == 0 { + return fmt.Errorf("python: could not initialize the python interpreter") + } + + // make sure the GIL is correctly initialized + if C.PyEval_ThreadsInitialized() == 0 { + C.PyEval_InitThreads() + } + if C.PyEval_ThreadsInitialized() == 0 { + return fmt.Errorf("python: could not initialize the GIL") + } + + return nil +} + +// Finalize shutdowns the python interpreter +func Finalize() error { + C.Py_Finalize() + return nil +} + +// PyThreadState* PyEval_SaveThread() +// Release the global interpreter lock (if it has been created and thread +// support is enabled) and reset the thread state to NULL, returning the +// previous thread state (which is not NULL). If the lock has been created, +// the current thread must have acquired it. (This function is available even +// when thread support is disabled at compile time.) +func PyEval_SaveThread() *PyThreadState { + state := C.PyEval_SaveThread() + return &PyThreadState{ptr: state} +} + +// void PyEval_RestoreThread(PyThreadState *tstate) +// Acquire the global interpreter lock (if it has been created and thread +// support is enabled) and set the thread state to tstate, which must not be +// NULL. If the lock has been created, the current thread must not have +// acquired it, otherwise deadlock ensues. (This function is available even +// when thread support is disabled at compile time.) +func PyEval_RestoreThread(state *PyThreadState) { + C.PyEval_RestoreThread(state.ptr) +} + +// Ensure that the current thread is ready to call the Python C API regardless +// of the current state of Python, or of the global interpreter lock. This may +// be called as many times as desired by a thread as long as each call is +// matched with a call to PyGILState_Release(). In general, other thread-related +// APIs may be used between PyGILState_Ensure() and PyGILState_Release() calls +// as long as the thread state is restored to its previous state before the +// Release(). For example, normal usage of the Py_BEGIN_ALLOW_THREADS and +// Py_END_ALLOW_THREADS macros is acceptable. +// +// The return value is an opaque “handle” to the thread state when +// PyGILState_Ensure() was called, and must be passed to PyGILState_Release() +// to ensure Python is left in the same state. Even though recursive calls are +// allowed, these handles cannot be shared - each unique call to +// PyGILState_Ensure() must save the handle for its call to PyGILState_Release(). +// +// When the function returns, the current thread will hold the GIL and be able +// to call arbitrary Python code. Failure is a fatal error. +// +// New in version 2.3. +func PyGILState_Ensure() PyGILState { + return PyGILState(C.PyGILState_Ensure()) +} + +// void PyGILState_Release(PyGILState_STATE) +// Release any resources previously acquired. After this call, Python’s state +// will be the same as it was prior to the corresponding PyGILState_Ensure() +// call (but generally this state will be unknown to the caller, hence the use +// of the GILState API). +// +// Every call to PyGILState_Ensure() must be matched by a call to +// PyGILState_Release() on the same thread. +// +// New in version 2.3. +func PyGILState_Release(state PyGILState) { + C.PyGILState_Release(C.PyGILState_STATE(state)) +} + +// EOF diff --git a/python_test.go b/.legacy/python_test.go similarity index 100% rename from python_test.go rename to .legacy/python_test.go diff --git a/sequence.go b/.legacy/sequence.go similarity index 100% rename from sequence.go rename to .legacy/sequence.go diff --git a/tests/cpickle/main.go b/.legacy/tests/cpickle/main.go similarity index 100% rename from tests/cpickle/main.go rename to .legacy/tests/cpickle/main.go diff --git a/tests/errfetch/main.go b/.legacy/tests/errfetch/main.go similarity index 100% rename from tests/errfetch/main.go rename to .legacy/tests/errfetch/main.go diff --git a/tests/issue61/main.go b/.legacy/tests/issue61/main.go similarity index 100% rename from tests/issue61/main.go rename to .legacy/tests/issue61/main.go diff --git a/tests/kw-args/kwargs.py b/.legacy/tests/kw-args/kwargs.py similarity index 100% rename from tests/kw-args/kwargs.py rename to .legacy/tests/kw-args/kwargs.py diff --git a/tests/kw-args/main.go b/.legacy/tests/kw-args/main.go similarity index 100% rename from tests/kw-args/main.go rename to .legacy/tests/kw-args/main.go diff --git a/tests/modify-values/main.go b/.legacy/tests/modify-values/main.go similarity index 100% rename from tests/modify-values/main.go rename to .legacy/tests/modify-values/main.go diff --git a/tests/modify-values/values.py b/.legacy/tests/modify-values/values.py similarity index 100% rename from tests/modify-values/values.py rename to .legacy/tests/modify-values/values.py diff --git a/type.go b/.legacy/type.go similarity index 100% rename from type.go rename to .legacy/type.go diff --git a/utilities.go b/.legacy/utilities.go similarity index 90% rename from utilities.go rename to .legacy/utilities.go index ec5d543..9b1d674 100644 --- a/utilities.go +++ b/.legacy/utilities.go @@ -5,7 +5,6 @@ import "C" import ( "errors" - "fmt" "unsafe" ) @@ -47,33 +46,6 @@ func PyOS_setsig(i int, h C.PyOS_sighandler_t) C.PyOS_sighandler_t { ///// system functions ///// -// PyObject *PySys_GetObject(char *name) -// Return value: Borrowed reference. -// Return the object name from the sys module or NULL if it does not exist, without setting an exception. -func PySys_GetObject(name string) *PyObject { - c_name := C.CString(name) - defer C.free(unsafe.Pointer(c_name)) - - return togo(C.PySys_GetObject(c_name)) -} - -// FILE *PySys_GetFile(char *name, FILE *def) -// Return the FILE* associated with the object name in the sys module, or def if name is not in the module or is not associated with a FILE*. -func PySys_GetFile(name string, def *C.FILE) *C.FILE { - c_name := C.CString(name) - defer C.free(unsafe.Pointer(c_name)) - //FIXME use go os.File ? - return C.PySys_GetFile(c_name, def) -} - -// int PySys_SetObject(char *name, PyObject *v) -// Set name in the sys module to v unless v is NULL, in which case name is deleted from the sys module. Returns 0 on success, -1 on error. -func PySys_SetObject(name string, v *PyObject) error { - c_name := C.CString(name) - defer C.free(unsafe.Pointer(c_name)) - return int2err(C.PySys_SetObject(c_name, topy(v))) -} - // void PySys_ResetWarnOptions() // Reset sys.warnoptions to an empty list. func PySys_ResetWarnOptions() { @@ -96,32 +68,6 @@ func PySys_SetPath(path string) { C.PySys_SetPath(c_path) } -// void PySys_WriteStdout(const char *format, ...) -// Write the output string described by format to sys.stdout. No exceptions are raised, even if truncation occurs (see below). -// -// format should limit the total size of the formatted output string to 1000 bytes or less – after 1000 bytes, the output string is truncated. In particular, this means that no unrestricted “%s” formats should occur; these should be limited using “%.s” where is a decimal number calculated so that plus the maximum size of other formatted text does not exceed 1000 bytes. Also watch out for “%f”, which can print hundreds of digits for very large numbers. -// -// If a problem occurs, or sys.stdout is unset, the formatted message is written to the real (C level) stdout. -func PySys_WriteStdout(format string, args ...interface{}) { - //FIXME go-sprintf format and python-format may differ... - s := fmt.Sprintf(format, args...) - c_s := C.CString(s) - defer C.free(unsafe.Pointer(c_s)) - - //c_format := C.CString("%s") - //defer C.free(unsafe.Pointer(c_format)) - //C._gopy_PySys_WriteStdout(c_s) - - panic("not implemented") -} - -// void PySys_WriteStderr(const char *format, ...) -// As above, but write to sys.stderr or stderr instead. -func PySys_WriteStderr(format string, args ...interface{}) { - //FIXME - panic("not implemented") -} - /////// Process Control ///////// // void Py_FatalError(const char *message) diff --git a/Makefile b/Makefile index cb18d84..1a66176 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. -.PHONY: all install test +.PHONY: all install test build # default to gc, but allow caller to override on command line GO_COMPILER:=$(GC) @@ -26,3 +26,12 @@ install: test: install $(test_cwd) + +build-py2: install + go build -buildmode=plugin ./python2/plugin/python2.go + +build-py3: install + go build -buildmode=plugin ./python3/plugin/python3.go + +build: build-py2 build-py3 + go build ./cmd/go-python diff --git a/cgoflags.go b/cgoflags.go deleted file mode 100644 index 5f1f0fa..0000000 --- a/cgoflags.go +++ /dev/null @@ -1,7 +0,0 @@ -package python - -// #cgo pkg-config: python-2.7 -// #include "go-python.h" -import "C" - -// EOF diff --git a/cmd/go-python/go-python.go b/cmd/go-python/go-python.go new file mode 100644 index 0000000..5655361 --- /dev/null +++ b/cmd/go-python/go-python.go @@ -0,0 +1,47 @@ +// a go wrapper around py-main +package main + +import ( + "fmt" + "os" + "plugin" + + "github.com/sbinet/go-python" +) + +func loadRuntime(vers string) python.Runtime { + pl, err := plugin.Open(fmt.Sprintf("python%s.so", vers)) + if err != nil { + panic(err) + } + s, err := pl.Lookup("Runtime") + if err != nil { + panic(err) + } + r, ok := s.(*python.Runtime) + if !ok { + panic(fmt.Errorf("unexpected type: %T", s)) + } + return *r +} + +func main() { + vers := "2" + if os.Getenv("GO_PYTHON") == "3" { + vers = "3" + } + r := loadRuntime(vers) + py := python.NewInterpreter(r) + err := py.Initialize(true) + if err != nil { + panic(err) + } + defer py.Close() + + err = py.Main(os.Args) + if e, ok := err.(python.RunError); ok { + os.Exit(e.Code) + } else if err != nil { + panic(err) + } +} diff --git a/cmd/go-python/main.go b/cmd/go-python/main.go deleted file mode 100644 index 6c4ee23..0000000 --- a/cmd/go-python/main.go +++ /dev/null @@ -1,19 +0,0 @@ -// a go wrapper around py-main -package main - -import ( - "github.com/sbinet/go-python" - "os" -) - -func init() { - err := python.Initialize() - if err != nil { - panic(err.Error()) - } -} - -func main() { - rc := python.Py_Main(os.Args) - os.Exit(rc) -} diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..760af96 --- /dev/null +++ b/errors.go @@ -0,0 +1,27 @@ +package python + +import "fmt" + +type RunError struct { + Code int + File string +} + +func (e RunError) Error() string { + return fmt.Sprintf("python: error %d executing script %s", e.Code, e.File) +} + +func errCode(ret int) error { + if ret == 0 { + return nil + } + return Error{Code: ret} +} + +type Error struct { + Code int +} + +func (e Error) Error() string { + return fmt.Sprintf("python: C-Python error code %d", e.Code) +} diff --git a/eval.go b/eval.go new file mode 100644 index 0000000..6575d24 --- /dev/null +++ b/eval.go @@ -0,0 +1,21 @@ +package python + +import "github.com/sbinet/go-python/runtime" + +type TraceFunc func(frame *Frame, what runtime.TraceType, arg runtime.Object) + +func (py *Interpreter) Trace(fnc TraceFunc) { + var obj runtime.Object // TODO: unique value + py.r.EvalSetTrace(func(_ runtime.Object, frame runtime.Frame, what runtime.TraceType, arg runtime.Object) int { + fnc(&Frame{ptr: frame}, what, arg) + return 0 + }, obj) +} + +func (py *Interpreter) GetFrame() *Frame { + f := py.r.EvalGetFrame() + if f == nil { + return nil + } + return &Frame{ptr: f} +} diff --git a/exec.go b/exec.go new file mode 100644 index 0000000..aeeb535 --- /dev/null +++ b/exec.go @@ -0,0 +1,121 @@ +package python + +import ( + "bytes" + "io" + "io/ioutil" + "os" + "sync" +) + +func (py *Interpreter) Command(pyfile string, args ...string) *Cmd { + return &Cmd{py: py, file: pyfile, Args: args} +} + +type Cmd struct { + //Path string // TODO: + + Args []string + //Env []string // TODO + //Dir string + + Stdin io.Reader + Stdout io.Writer + Stderr io.Writer + + py *Interpreter + file string + fds []io.Closer + wg sync.WaitGroup +} + +func (cmd *Cmd) close() error { + for _, f := range cmd.fds { + f.Close() + } + cmd.fds = nil + return nil +} + +func (cmd *Cmd) inStream(r io.Reader) (*os.File, error) { + pr, pw, err := os.Pipe() + if err != nil { + return nil, err + } + cmd.fds = append(cmd.fds, pw, pr) + cmd.wg.Add(1) + go func(r io.Reader) { + defer cmd.wg.Done() + defer pw.Close() + _, _ = io.Copy(pw, r) + }(cmd.Stdin) + return pr, nil +} + +func (cmd *Cmd) outStream(w io.Writer) (*os.File, error) { + pr, pw, err := os.Pipe() + if err != nil { + return nil, err + } + cmd.fds = append(cmd.fds, pw, pr) + cmd.wg.Add(1) + go func() { + defer cmd.wg.Done() + _, _ = io.Copy(w, pr) + }() + return pw, nil +} +func (cmd *Cmd) Run() error { + defer cmd.close() + if cmd.Stdin == nil { + cmd.Stdin = bytes.NewReader(nil) + } + if cmd.Stdout == nil { + // TODO: it's better to open /dev/null + cmd.Stdout = ioutil.Discard + } + if cmd.Stderr == nil { + cmd.Stderr = ioutil.Discard + } + var ( + in, out, er *os.File + err error + ) + if f, ok := cmd.Stdin.(*os.File); ok { + in = f + } else { + in, err = cmd.inStream(cmd.Stdin) + if err != nil { + return err + } + } + if f, ok := cmd.Stdout.(*os.File); ok { + out = f + } else { + out, err = cmd.outStream(cmd.Stdout) + if err != nil { + return err + } + } + if f, ok := cmd.Stderr.(*os.File); ok { + er = f + } else { + er, err = cmd.outStream(cmd.Stderr) + if err != nil { + return err + } + } + if err := cmd.py.SetStdinFile(in); err != nil { + return err + } + if err := cmd.py.SetStdoutFile(out); err != nil { + return err + } + if err := cmd.py.SetStderrFile(er); err != nil { + return err + } + err = cmd.py.RunMain(cmd.file, cmd.Args...) + cmd.close() + cmd.wg.Wait() + return err +} diff --git a/file.go b/file.go index 983ebe8..e35494a 100644 --- a/file.go +++ b/file.go @@ -1,47 +1,36 @@ package python -/* -#include -#include "go-python.h" +import "os" -PyObject* -_gopy_PyFile_FromFile(int fd, char *name, char *mode) { - FILE *f = fdopen(fd, mode); - PyObject *py = PyFile_FromFile(f, name, mode, NULL); - PyFile_SetBufSize(py, 0); - return py; +func (py *Interpreter) fromFile(f *os.File, mode string) *Object { + p := py.r.FromFile(f, mode) + return newObject(p) } -*/ -import "C" - -import ( - "os" - "unsafe" -) - -// FromFile converts a Go file to Python file object. -// Calling close from Python will not close a file descriptor. -func FromFile(f *os.File, mode string) *PyObject { - cname := C.CString(f.Name()) - cmode := C.CString(mode) - p := C._gopy_PyFile_FromFile(C.int(f.Fd()), cname, cmode) - C.free(unsafe.Pointer(cname)) - C.free(unsafe.Pointer(cmode)) - return togo(p) +// FromFile converts Go file into python file object. +func (py *Interpreter) FromFile(f *os.File, mode string) *Object { + obj := py.fromFile(f, mode) + obj.setFinalizer() + return obj } -// SetStdin sets a sys.stdin to a specified file descriptor. -func SetStdin(f *os.File) error { - return PySys_SetObject("stdin", FromFile(f, "r")) +// SetStdinFile sets a sys.stdin to a specified file descriptor. +func (py *Interpreter) SetStdinFile(f *os.File) error { + pf := py.fromFile(f, "r") + defer pf.decRef() + return py.SetStdinObject(pf) } -// SetStdout sets a sys.stdout to a specified file descriptor. -func SetStdout(f *os.File) error { - return PySys_SetObject("stdout", FromFile(f, "w")) +// SetStdoutFile sets a sys.stdout to a specified file descriptor. +func (py *Interpreter) SetStdoutFile(f *os.File) error { + pf := py.fromFile(f, "w") + defer pf.decRef() + return py.SetStdoutObject(pf) } -// SetStderr sets a sys.stderr to a specified file descriptor. -func SetStderr(f *os.File) error { - return PySys_SetObject("stderr", FromFile(f, "w")) +// SetStderrFile sets a sys.stderr to a specified file descriptor. +func (py *Interpreter) SetStderrFile(f *os.File) error { + pf := py.fromFile(f, "w") + defer pf.decRef() + return py.SetStderrObject(pf) } diff --git a/frame.go b/frame.go new file mode 100644 index 0000000..a643cc8 --- /dev/null +++ b/frame.go @@ -0,0 +1,30 @@ +package python + +import ( + "fmt" + "github.com/sbinet/go-python/runtime" +) + +type Frame struct { + ptr runtime.Frame +} + +func (f *Frame) GetFilePos() *FileLine { + if f == nil || f.ptr == nil { + return nil + } + l := &FileLine{Line: f.ptr.GetLineNumber()} + if name := f.ptr.GetFilename(); name.Valid() { + l.Filename = name.AsString() + } + return l +} + +type FileLine struct { + Filename string + Line int +} + +func (l FileLine) String() string { + return fmt.Sprintf("%s:%d", l.Filename, l.Line) +} diff --git a/object.go b/object.go index 274fcfd..3626ca1 100644 --- a/object.go +++ b/object.go @@ -1,471 +1,46 @@ package python -//#include "go-python.h" -import "C" - import ( "fmt" - "os" - "strings" - "unsafe" + "github.com/sbinet/go-python/runtime" ) -// PyObject layer -type PyObject struct { - ptr *C.PyObject -} - -func (self *PyObject) topy() *C.PyObject { - return self.ptr +type Object struct { + ptr runtime.Object } -func topy(self *PyObject) *C.PyObject { - if self == nil { - return nil +func (obj *Object) String() string { + if obj == nil || obj.ptr == nil { + return "" + } else if !obj.ptr.Valid() { + return "" + } else if obj.ptr.IsNone() { + return "" } - return self.ptr -} - -func togo(obj *C.PyObject) *PyObject { - if obj == nil { - return nil + if obj.ptr.StringCheck() { + return obj.ptr.AsString() } - return &PyObject{ptr: obj} -} - -// PyObject_FromVoidPtr converts a PyObject from an unsafe.Pointer -func PyObject_FromVoidPtr(ptr unsafe.Pointer) *PyObject { - return togo((*C.PyObject)(ptr)) -} - -func int2bool(i C.int) bool { - switch i { - case -1: - return false - case 0: - return false - case 1: - return true - default: - return true - } - return false -} - -func long2bool(i C.long) bool { - switch i { - case -1: - return false - case 0: - return false - case 1: - return true - default: - return true - } - return false -} - -func bool2int(i bool) C.int { - if i { - return C.int(1) - } - return C.int(0) -} - -type gopy_err struct { - err string -} - -func (self *gopy_err) Error() string { - return self.err -} - -func int2err(i C.int) error { - if i == 0 { - return nil - } - //FIXME: also handle python exceptions ? - return &gopy_err{fmt.Sprintf("error in C-Python (rc=%d)", int(i))} -} - -func file2go(f *C.FILE) *os.File { - return nil -} - -// C.PyObject* PyObject_GetCPointer(PyObject *o) -// Returns the internal C pointer to CPython object. -func (self *PyObject) GetCPointer() *C.PyObject { - return self.ptr -} - -// void Py_IncRef(PyObject *o) -// Increment the reference count for object o. The object may be -// NULL, in which case the function has no effect. -func (self *PyObject) IncRef() { - C.Py_IncRef(self.ptr) -} - -// void Py_DecRef(PyObject *o) -// Decrement the reference count for object o. If the object is -// NULL, nothing happens. If the reference count reaches zero, the -// object’s type’s deallocation function (which must not be NULL) is -// invoked. -// WARNING: The deallocation function can cause arbitrary Python -// code to be invoked. See the warnings and instructions in the -// Python docs, and consider using Clear instead. -func (self *PyObject) DecRef() { - C.Py_DecRef(self.ptr) -} - -// void Py_CLEAR(PyObject *o) -// Clear sets the PyObject's internal pointer to nil -// before calling Py_DecRef. This avoids the potential issues with -// Python code called by the deallocator referencing invalid, -// partially-deallocated data. -func (self *PyObject) Clear() { - tmp := self.ptr - self.ptr = nil - C.Py_DecRef(tmp) -} - -// int PyObject_HasAttr(PyObject *o, PyObject *attr_name) -// Returns 1 if o has the attribute attr_name, and 0 otherwise. This is equivalent to the Python expression hasattr(o, attr_name). This function always succeeds. -func (self *PyObject) HasAttr(attr_name *PyObject) int { - return int(C.PyObject_HasAttr(self.ptr, attr_name.ptr)) -} - -// int PyObject_HasAttrString(PyObject *o, const char *attr_name) -// Returns 1 if o has the attribute attr_name, and 0 otherwise. This is equivalent to the Python expression hasattr(o, attr_name). This function always succeeds. -func (self *PyObject) HasAttrString(attr_name string) int { - c_attr_name := C.CString(attr_name) - defer C.free(unsafe.Pointer(c_attr_name)) - - return int(C.PyObject_HasAttrString(self.ptr, c_attr_name)) -} - -// PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name) -// Return value: New reference. -// Retrieve an attribute named attr_name from object o. Returns the attribute value on success, or NULL on failure. This is the equivalent of the Python expression o.attr_name. -func (self *PyObject) GetAttr(attr_name *PyObject) *PyObject { - return togo(C.PyObject_GetAttr(self.ptr, attr_name.ptr)) -} - -// PyObject* PyObject_Dir() -// Return value: New reference. -// This is equivalent to the Python expression dir(o), returning a (possibly empty) list of strings appropriate for the object argument, or NULL if there was an error. If the argument is NULL, this is like the Python dir(), returning the names of the current locals; in this case, if no execution frame is active then NULL is returned but PyErr_Occurred() will return false. -func (self *PyObject) PyObject_Dir() *PyObject { - return togo(C.PyObject_Dir(self.ptr)) - -} - -// PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name) -// Return value: New reference. -// Retrieve an attribute named attr_name from object o. Returns the attribute value on success, or NULL on failure. This is the equivalent of the Python expression o.attr_name. -func (self *PyObject) GetAttrString(attr_name string) *PyObject { - c_attr_name := C.CString(attr_name) - defer C.free(unsafe.Pointer(c_attr_name)) - return togo(C.PyObject_GetAttrString(self.ptr, c_attr_name)) -} - -// PyObject* PyObject_GenericGetAttr(PyObject *o, PyObject *name) -// Generic attribute getter function that is meant to be put into a type object’s tp_getattro slot. It looks for a descriptor in the dictionary of classes in the object’s MRO as well as an attribute in the object’s __dict__ (if present). As outlined in Implementing Descriptors, data descriptors take preference over instance attributes, while non-data descriptors don’t. Otherwise, an AttributeError is raised. -func (self *PyObject) GenericGetAttr(name *PyObject) *PyObject { - return togo(C.PyObject_GenericGetAttr(self.ptr, name.ptr)) -} - -// int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v) -// Set the value of the attribute named attr_name, for object o, to the value v. Returns -1 on failure. This is the equivalent of the Python statement o.attr_name = v. -func (self *PyObject) SetAttr(attr_name, v *PyObject) int { - return int(C.PyObject_SetAttr(self.ptr, attr_name.ptr, v.ptr)) -} - -// int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v) -// Set the value of the attribute named attr_name, for object o, to the value v. Returns -1 on failure. This is the equivalent of the Python statement o.attr_name = v. -func (self *PyObject) SetAttrString(attr_name string, v *PyObject) int { - c_attr_name := C.CString(attr_name) - defer C.free(unsafe.Pointer(c_attr_name)) - return int(C.PyObject_SetAttrString(self.ptr, c_attr_name, v.ptr)) -} - -// int PyObject_GenericSetAttr(PyObject *o, PyObject *name, PyObject *value) -// Generic attribute setter function that is meant to be put into a type object’s tp_setattro slot. It looks for a data descriptor in the dictionary of classes in the object’s MRO, and if found it takes preference over setting the attribute in the instance dictionary. Otherwise, the attribute is set in the object’s __dict__ (if present). Otherwise, an AttributeError is raised and -1 is returned. -func (self *PyObject) GenericSetAttr(name, value *PyObject) int { - return int(C.PyObject_GenericSetAttr(self.ptr, name.ptr, value.ptr)) -} - -// int PyObject_DelAttr(PyObject *o, PyObject *attr_name) -// Delete attribute named attr_name, for object o. Returns -1 on failure. This is the equivalent of the Python statement del o.attr_name. -func (self *PyObject) DelAttr(attr_name *PyObject) int { - return int(C._gopy_PyObject_DelAttr(self.ptr, attr_name.ptr)) -} - -// int PyObject_DelAttrString(PyObject *o, const char *attr_name) -// Delete attribute named attr_name, for object o. Returns -1 on failure. This is the equivalent of the Python statement del o.attr_name. -func (self *PyObject) DelAttrString(attr_name string) int { - c_attr_name := C.CString(attr_name) - defer C.free(unsafe.Pointer(c_attr_name)) - return int(C._gopy_PyObject_DelAttrString(self.ptr, c_attr_name)) -} - -type Py_OPID C.int - -const ( - Py_LT Py_OPID = C.Py_LT - Py_LE Py_OPID = C.Py_LE - Py_EQ Py_OPID = C.Py_EQ - Py_NE Py_OPID = C.Py_NE - Py_GT Py_OPID = C.Py_GT - Py_GE Py_OPID = C.Py_GE -) - -// PyObject* PyObject_RichCompare(PyObject *o1, PyObject *o2, int opid) -// Return value: New reference. -// Compare the values of o1 and o2 using the operation specified by opid, which must be one of Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, or Py_GE, corresponding to <, <=, ==, !=, >, or >= respectively. This is the equivalent of the Python expression o1 op o2, where op is the operator corresponding to opid. Returns the value of the comparison on success, or NULL on failure. -func (self *PyObject) RichCompare(o2 *PyObject, opid Py_OPID) *PyObject { - return togo(C.PyObject_RichCompare(self.ptr, o2.ptr, C.int(opid))) -} - -// int PyObject_RichCompareBool(PyObject *o1, PyObject *o2, int opid) -// Compare the values of o1 and o2 using the operation specified by opid, which must be one of Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, or Py_GE, corresponding to <, <=, ==, !=, >, or >= respectively. Returns -1 on error, 0 if the result is false, 1 otherwise. This is the equivalent of the Python expression o1 op o2, where op is the operator corresponding to opid. -func (self *PyObject) RichCompareBool(o2 *PyObject, opid Py_OPID) int { - return int(C.PyObject_RichCompareBool(self.ptr, o2.ptr, C.int(opid))) -} - -// int PyObject_Cmp(PyObject *o1, PyObject *o2, int *result) -// Compare the values of o1 and o2 using a routine provided by o1, if one exists, otherwise with a routine provided by o2. The result of the comparison is returned in result. Returns -1 on failure. This is the equivalent of the Python statement result = cmp(o1, o2). -func (self *PyObject) Cmp(o2 *PyObject) (err, result int) { - var c_result C.int = -1 - var c_err C.int = -1 - - c_err = C.PyObject_Cmp(self.ptr, o2.ptr, &c_result) - return int(c_err), int(c_result) -} - -// int PyObject_Compare(PyObject *o1, PyObject *o2) -// Compare the values of o1 and o2 using a routine provided by o1, if one exists, otherwise with a routine provided by o2. Returns the result of the comparison on success. On error, the value returned is undefined; use PyErr_Occurred() to detect an error. This is equivalent to the Python expression cmp(o1, o2). -func (self *PyObject) Compare(o2 *PyObject) int { - return int(C.PyObject_Compare(self.ptr, o2.ptr)) -} - -// PyObject* PyObject_Repr(PyObject *o) -// Return value: New reference. -// Compute a string representation of object o. Returns the string representation on success, NULL on failure. This is the equivalent of the Python expression repr(o). Called by the repr() built-in function and by reverse quotes. -func (self *PyObject) Repr() *PyObject { - return togo(C.PyObject_Repr(self.ptr)) -} - -// PyObject* PyObject_Str(PyObject *o) -// Return value: New reference. -// Compute a string representation of object o. Returns the string representation on success, NULL on failure. This is the equivalent of the Python expression str(o). Called by the str() built-in function and by the print statement. -func (self *PyObject) Str() *PyObject { - return togo(C.PyObject_Str(self.ptr)) -} - -// PyObject* PyObject_Bytes(PyObject *o) -// Compute a bytes representation of object o. In 2.x, this is just a alias for PyObject_Str(). -func (self *PyObject) Bytes() *PyObject { - return togo(C.PyObject_Bytes(self.ptr)) -} - -// PyObject* PyObject_Unicode(PyObject *o) -// Return value: New reference. -// Compute a Unicode string representation of object o. Returns the Unicode string representation on success, NULL on failure. This is the equivalent of the Python expression unicode(o). Called by the unicode() built-in function. -func (self *PyObject) Unicode() *PyObject { - return togo(C.PyObject_Unicode(self.ptr)) -} - -// int PyObject_IsInstance(PyObject *inst, PyObject *cls) -// Returns 1 if inst is an instance of the class cls or a subclass of cls, or 0 if not. On error, returns -1 and sets an exception. If cls is a type object rather than a class object, PyObject_IsInstance() returns 1 if inst is of type cls. If cls is a tuple, the check will be done against every entry in cls. The result will be 1 when at least one of the checks returns 1, otherwise it will be 0. If inst is not a class instance and cls is neither a type object, nor a class object, nor a tuple, inst must have a __class__ attribute — the class relationship of the value of that attribute with cls will be used to determine the result of this function. -// -// New in version 2.1. -// -// Changed in version 2.2: Support for a tuple as the second argument added. -// -// Subclass determination is done in a fairly straightforward way, but includes a wrinkle that implementors of extensions to the class system may want to be aware of. If A and B are class objects, B is a subclass of A if it inherits from A either directly or indirectly. If either is not a class object, a more general mechanism is used to determine the class relationship of the two objects. When testing if B is a subclass of A, if A is B, PyObject_IsSubclass() returns true. If A and B are different objects, B‘s __bases__ attribute is searched in a depth-first fashion for A — the presence of the __bases__ attribute is considered sufficient for this determination. -func (self *PyObject) IsInstance(cls *PyObject) int { - return int(C.PyObject_IsInstance(self.ptr, cls.ptr)) -} - -// int PyObject_IsSubclass(PyObject *derived, PyObject *cls) -// Returns 1 if the class derived is identical to or derived from the class cls, otherwise returns 0. In case of an error, returns -1. If cls is a tuple, the check will be done against every entry in cls. The result will be 1 when at least one of the checks returns 1, otherwise it will be 0. If either derived or cls is not an actual class object (or tuple), this function uses the generic algorithm described above. -// -// New in version 2.1. -// -// Changed in version 2.3: Older versions of Python did not support a tuple as the second argument. -func (self *PyObject) IsSubclass(cls *PyObject) int { - return int(C.PyObject_IsSubclass(self.ptr, cls.ptr)) -} - -// int PyCallable_Check(PyObject *o) -// Determine if the object o is callable. Return 1 if the object is callable and 0 otherwise. This function always succeeds. -// PyObject* PyObject_Call(PyObject *callable_object, PyObject *args, PyObject *kw) -// Return value: New reference. -// Call a callable Python object callable_object, with arguments given by the tuple args, and named arguments given by the dictionary kw. If no named arguments are needed, kw may be NULL. args must not be NULL, use an empty tuple if no arguments are needed. Returns the result of the call on success, or NULL on failure. This is the equivalent of the Python expression apply(callable_object, args, kw) or callable_object(*args, **kw). -// -// New in version 2.2. -func (self *PyObject) Check_Callable() bool { - return int2bool(C.PyCallable_Check(self.ptr)) -} - -// PyObject* PyObject_Call(PyObject *callable_object, PyObject *args, PyObject *kw) -// Return value: New reference. -// Call a callable Python object callable_object, with arguments given by the tuple args, and named arguments given by the dictionary kw. If no named arguments are needed, kw may be NULL. args must not be NULL, use an empty tuple if no arguments are needed. Returns the result of the call on success, or NULL on failure. This is the equivalent of the Python expression apply(callable_object, args, kw) or callable_object(*args, **kw). -func (self *PyObject) Call(args, kw *PyObject) *PyObject { - return togo(C.PyObject_Call(self.ptr, args.ptr, kw.ptr)) -} - -// PyObject* PyObject_CallObject(PyObject *callable_object, PyObject *args) -// Return value: New reference. -// Call a callable Python object callable_object, with arguments given by the tuple args. If no arguments are needed, then args may be NULL. Returns the result of the call on success, or NULL on failure. This is the equivalent of the Python expression apply(callable_object, args) or callable_object(*args). -func (self *PyObject) CallObject(args *PyObject) *PyObject { - return togo(C.PyObject_CallObject(self.ptr, args.ptr)) + return fmt.Sprint(obj.ptr) } -// PyObject* PyObject_CallFunction(PyObject *callable, char *format, ...) -// Return value: New reference. -// Call a callable Python object callable, with a variable number of C arguments. The C arguments are described using a Py_BuildValue() style format string. The format may be NULL, indicating that no arguments are provided. Returns the result of the call on success, or NULL on failure. This is the equivalent of the Python expression apply(callable, args) or callable(*args). Note that if you only pass PyObject * args, PyObject_CallFunctionObjArgs() is a faster alternative. -func (self *PyObject) CallFunction(args ...interface{}) *PyObject { - if len(args) > int(C._gopy_max_varargs) { - panic(fmt.Errorf( - "gopy: maximum number of varargs (%d) exceeded (%d)", - int(C._gopy_max_varargs), - len(args), - )) +func (obj *Object) AsString() (string, bool) { + if obj.ptr.StringCheck() { + return obj.ptr.AsString(), true } - - types := make([]string, 0, len(args)) - cargs := make([]unsafe.Pointer, 0, len(args)) - - for _, arg := range args { - ptr, typ := pyfmt(arg) - types = append(types, typ) - cargs = append(cargs, ptr) - if typ == "s" { - defer func(ptr unsafe.Pointer) { - C.free(ptr) - }(ptr) - } - } - - if len(args) <= 0 { - o := C._gopy_PyObject_CallFunction(self.ptr, 0, nil, nil) - return togo(o) - } - - pyfmt := C.CString(strings.Join(types, "")) - defer C.free(unsafe.Pointer(pyfmt)) - o := C._gopy_PyObject_CallFunction( - self.ptr, - C.int(len(args)), - pyfmt, - unsafe.Pointer(&cargs[0]), - ) - - return togo(o) - + return "", false } -// PyObject* PyObject_CallMethod(PyObject *o, char *method, char *format, ...) -// Return value: New reference. -// Call the method named method of object o with a variable number of C arguments. The C arguments are described by a Py_BuildValue() format string that should produce a tuple. The format may be NULL, indicating that no arguments are provided. Returns the result of the call on success, or NULL on failure. This is the equivalent of the Python expression o.method(args). Note that if you only pass PyObject * args, PyObject_CallMethodObjArgs() is a faster alternative. -func (self *PyObject) CallMethod(method string, args ...interface{}) *PyObject { - if len(args) > int(C._gopy_max_varargs) { - panic(fmt.Errorf( - "gopy: maximum number of varargs (%d) exceeded (%d)", - int(C._gopy_max_varargs), - len(args), - )) +func (obj *Object) decRef() { + if obj == nil || obj.ptr == nil { + return } - - cmethod := C.CString(method) - defer C.free(unsafe.Pointer(cmethod)) - - types := make([]string, 0, len(args)) - cargs := make([]unsafe.Pointer, 0, len(args)) - - for _, arg := range args { - ptr, typ := pyfmt(arg) - types = append(types, typ) - cargs = append(cargs, ptr) - if typ == "s" { - defer func(ptr unsafe.Pointer) { - C.free(ptr) - }(ptr) - } - } - - if len(args) <= 0 { - o := C._gopy_PyObject_CallMethod(self.ptr, cmethod, 0, nil, nil) - return togo(o) - } - - pyfmt := C.CString(strings.Join(types, "")) - defer C.free(unsafe.Pointer(pyfmt)) - o := C._gopy_PyObject_CallMethod( - self.ptr, - cmethod, - C.int(len(args)), - pyfmt, - unsafe.Pointer(&cargs[0]), - ) - - return togo(o) -} - -/* -PyObject* PyObject_CallFunctionObjArgs(PyObject *callable, ..., NULL) -Return value: New reference. -Call a callable Python object callable, with a variable number of PyObject* arguments. The arguments are provided as a variable number of parameters followed by NULL. Returns the result of the call on success, or NULL on failure. - -New in version 2.2. -*/ -func (self *PyObject) CallFunctionObjArgs(format string, args ...interface{}) *PyObject { - return self.CallFunction(args...) -} - -/* -PyObject* PyObject_CallMethodObjArgs(PyObject *o, PyObject *name, ..., NULL) -Return value: New reference. -Calls a method of the object o, where the name of the method is given as a Python string object in name. It is called with a variable number of PyObject* arguments. The arguments are provided as a variable number of parameters followed by NULL. Returns the result of the call on success, or NULL on failure. - -New in version 2.2. -*/ -func (self *PyObject) CallMethodObjArgs(method string, args ...interface{}) *PyObject { - return self.CallMethod(method, args...) + obj.ptr.DecRef() } -// long PyObject_Hash(PyObject *o) -// Compute and return the hash value of an object o. On failure, return -1. This is the equivalent of the Python expression hash(o). -func (self *PyObject) Hash() int64 { - return int64(C.PyObject_Hash(topy(self))) +func (obj *Object) setFinalizer() { + // FIXME: set Go finalizer? } -// long PyObject_HashNotImplemented(PyObject *o) -// Set a TypeError indicating that type(o) is not hashable and return -1. This function receives special treatment when stored in a tp_hash slot, allowing a type to explicitly indicate to the interpreter that it is not hashable. -// -// New in version 2.6. -func (self *PyObject) HashNotImplemented() bool { - return long2bool(C.PyObject_HashNotImplemented(topy(self))) +func newObject(ptr runtime.Object) *Object { + return &Object{ptr: ptr} } - -// int PyObject_IsTrue(PyObject *o) -// Returns 1 if the object o is considered to be true, and 0 otherwise. This is equivalent to the Python expression not not o. On failure, return -1. -func (self *PyObject) IsTrue() bool { - return int2bool(C.PyObject_IsTrue(topy(self))) -} - -// int PyObject_Not(PyObject *o) -// Returns 0 if the object o is considered to be true, and 1 otherwise. This is equivalent to the Python expression not o. On failure, return -1. -func (self *PyObject) Not() bool { - return int2bool(C.PyObject_Not(topy(self))) -} - -// PyObject* PyObject_Type(PyObject *o) -// Return value: New reference. -// When o is non-NULL, returns a type object corresponding to the object type of object o. On failure, raises SystemError and returns NULL. This is equivalent to the Python expression type(o). This function increments the reference count of the return value. There’s really no reason to use this function instead of the common expression o->ob_type, which returns a pointer of type PyTypeObject*, except when the incremented reference count is needed. -func (self *PyObject) Type() *PyObject { - return togo(C.PyObject_Type(topy(self))) -} - -// EOF diff --git a/pytest/files/args.py b/pytest/files/args.py new file mode 100644 index 0000000..ce6c357 --- /dev/null +++ b/pytest/files/args.py @@ -0,0 +1,3 @@ +import sys + +print(sys.argv) \ No newline at end of file diff --git a/pytest/files/hi.py b/pytest/files/hi.py new file mode 100644 index 0000000..54cacc6 --- /dev/null +++ b/pytest/files/hi.py @@ -0,0 +1 @@ +print('hi') \ No newline at end of file diff --git a/pytest/files/hi8.py b/pytest/files/hi8.py new file mode 100644 index 0000000..8b6a5c7 --- /dev/null +++ b/pytest/files/hi8.py @@ -0,0 +1,2 @@ +# coding=utf-8 +print('hi, 世界') \ No newline at end of file diff --git a/pytest/files/raise.py b/pytest/files/raise.py new file mode 100644 index 0000000..612e7ea --- /dev/null +++ b/pytest/files/raise.py @@ -0,0 +1 @@ +raise Exception('This is exceptional') \ No newline at end of file diff --git a/pytest/pytest.go b/pytest/pytest.go new file mode 100644 index 0000000..3895bfb --- /dev/null +++ b/pytest/pytest.go @@ -0,0 +1,122 @@ +package pytest + +import ( + "bytes" + "log" + "path/filepath" + "sync" + "testing" + "time" + + "github.com/sbinet/go-python" + "github.com/sbinet/go-python/runtime" + "github.com/stretchr/testify/require" +) + +const runTrace = false + +func TestRuntime(t *testing.T, r python.Runtime) { + py := python.NewInterpreter(r) + for _, c := range testTable { + c := c + t.Run(c.name, func(t *testing.T) { + err := py.Initialize(false) + require.NoError(t, err) + defer py.Close() + + if runTrace { + py.Trace(func(frame *python.Frame, what runtime.TraceType, arg runtime.Object) { + log.Println(frame.GetFilePos(), what, arg) + }) + } + + c.test(t, py) + }) + } +} + +const testDir = "../pytest/files/" + +func pyFile(name string) string { + return filepath.Join(testDir, name) +} + +var testTable = []struct { + name string + test func(t *testing.T, py *python.Interpreter) +}{ + {name: "to string", test: testToString}, + {name: "run string", test: testRunString}, + {name: "run file", test: testRunFile}, + {name: "run file 2", test: testRunFile2}, + {name: "main", test: testMain}, + {name: "exec out", test: testExec}, + {name: "exec error", test: testExecErr}, +} + +func testToString(t *testing.T, py *python.Interpreter) { + s := "Hello, 世界" + got, ok := py.FromString(s).AsString() + require.True(t, ok) + require.Equal(t, s, got) +} + +func testRunString(t *testing.T, py *python.Interpreter) { + err := py.RunString("print('hi')") + require.NoError(t, err) +} + +func testRunFile(t *testing.T, py *python.Interpreter) { + err := py.RunFile(pyFile("hi.py")) + require.NoError(t, err) +} + +func testRunFile2(t *testing.T, py *python.Interpreter) { + err := py.RunFile(pyFile("hi8.py")) + require.NoError(t, err) +} + +func testMain(t *testing.T, py *python.Interpreter) { + err := py.RunMain(pyFile("args.py"), "5") + require.NoError(t, err) +} + +func runCmd(t *testing.T, cmd *python.Cmd, py *python.Interpreter) error { + var wg sync.WaitGroup + wg.Add(1) + done := make(chan struct{}) + go func() { + defer wg.Done() + select { + case <-done: + case <-time.After(time.Second): + if f := py.GetFrame(); f != nil { + log.Println(f.GetFilePos()) + } + py.Close() + require.Fail(t, "timeout") + } + }() + err := cmd.Run() // keep on main thread + close(done) + wg.Wait() + return err +} + +func testExec(t *testing.T, py *python.Interpreter) { + name := pyFile("args.py") + cmd := py.Command(name, "5") + buf := bytes.NewBuffer(nil) + cmd.Stdout = buf + + err := runCmd(t, cmd, py) + require.NoError(t, err) + require.Equal(t, buf.String(), "['"+name+"', '5']\n") +} + +func testExecErr(t *testing.T, py *python.Interpreter) { + cmd := py.Command(pyFile("raise.py")) + + err := runCmd(t, cmd, py) + require.NotNil(t, err) +} diff --git a/python.go b/python.go index 1776e02..657809c 100644 --- a/python.go +++ b/python.go @@ -1,104 +1,44 @@ -// simplistic wrapper around the python C-API package python -//#include "go-python.h" -import "C" - import ( "fmt" + "github.com/sbinet/go-python/runtime" + "sync" ) -// PyGILState is the Go alias for the PyGILState_STATE enum -type PyGILState C.PyGILState_STATE +type Runtime = runtime.Runtime + +func NewInterpreter(r Runtime) *Interpreter { + return &Interpreter{r: r} +} -// PyThreadState layer -type PyThreadState struct { - ptr *C.PyThreadState +type Interpreter struct { + mu sync.Mutex + r Runtime } -// Initialize initializes the python interpreter and its GIL -func Initialize() error { +// Initialize initializes the python interpreter and its GIL. +func (py *Interpreter) Initialize(signals bool) error { // make sure the python interpreter has been initialized - if C.Py_IsInitialized() == 0 { - C.Py_Initialize() + if !py.r.IsInitialized() { + py.r.Initialize(signals) } - if C.Py_IsInitialized() == 0 { + if !py.r.IsInitialized() { return fmt.Errorf("python: could not initialize the python interpreter") } // make sure the GIL is correctly initialized - if C.PyEval_ThreadsInitialized() == 0 { - C.PyEval_InitThreads() + if !py.r.EvalThreadsInitialized() { + py.r.EvalInitThreads() } - if C.PyEval_ThreadsInitialized() == 0 { + if !py.r.EvalThreadsInitialized() { return fmt.Errorf("python: could not initialize the GIL") } - return nil } -// Finalize shutdowns the python interpreter -func Finalize() error { - C.Py_Finalize() +// Close shutdowns the python interpreter by calling Finalize. +func (py *Interpreter) Close() error { + py.r.Finalize() return nil } - -// PyThreadState* PyEval_SaveThread() -// Release the global interpreter lock (if it has been created and thread -// support is enabled) and reset the thread state to NULL, returning the -// previous thread state (which is not NULL). If the lock has been created, -// the current thread must have acquired it. (This function is available even -// when thread support is disabled at compile time.) -func PyEval_SaveThread() *PyThreadState { - state := C.PyEval_SaveThread() - return &PyThreadState{ptr: state} -} - -// void PyEval_RestoreThread(PyThreadState *tstate) -// Acquire the global interpreter lock (if it has been created and thread -// support is enabled) and set the thread state to tstate, which must not be -// NULL. If the lock has been created, the current thread must not have -// acquired it, otherwise deadlock ensues. (This function is available even -// when thread support is disabled at compile time.) -func PyEval_RestoreThread(state *PyThreadState) { - C.PyEval_RestoreThread(state.ptr) -} - -// Ensure that the current thread is ready to call the Python C API regardless -// of the current state of Python, or of the global interpreter lock. This may -// be called as many times as desired by a thread as long as each call is -// matched with a call to PyGILState_Release(). In general, other thread-related -// APIs may be used between PyGILState_Ensure() and PyGILState_Release() calls -// as long as the thread state is restored to its previous state before the -// Release(). For example, normal usage of the Py_BEGIN_ALLOW_THREADS and -// Py_END_ALLOW_THREADS macros is acceptable. -// -// The return value is an opaque “handle” to the thread state when -// PyGILState_Ensure() was called, and must be passed to PyGILState_Release() -// to ensure Python is left in the same state. Even though recursive calls are -// allowed, these handles cannot be shared - each unique call to -// PyGILState_Ensure() must save the handle for its call to PyGILState_Release(). -// -// When the function returns, the current thread will hold the GIL and be able -// to call arbitrary Python code. Failure is a fatal error. -// -// New in version 2.3. -func PyGILState_Ensure() PyGILState { - return PyGILState(C.PyGILState_Ensure()) -} - -// void PyGILState_Release(PyGILState_STATE) -// Release any resources previously acquired. After this call, Python’s state -// will be the same as it was prior to the corresponding PyGILState_Ensure() -// call (but generally this state will be unknown to the caller, hence the use -// of the GILState API). -// -// Every call to PyGILState_Ensure() must be matched by a call to -// PyGILState_Release() on the same thread. -// -// New in version 2.3. -func PyGILState_Release(state PyGILState) { - C.PyGILState_Release(C.PyGILState_STATE(state)) -} - -// EOF diff --git a/python2/ceval.go b/python2/ceval.go new file mode 100644 index 0000000..eff188f --- /dev/null +++ b/python2/ceval.go @@ -0,0 +1,12 @@ +package python2 + +/* +#include "go-python.h" + +// The gateway function +int go_trace_cgo(PyObject *obj, PyFrameObject *frame, int what, PyObject *arg) { + int go_trace(PyObject *obj, PyFrameObject *frame, int what, PyObject *arg); + return go_trace(obj, frame, what, arg); +} +*/ +import "C" diff --git a/python2/eval.go b/python2/eval.go new file mode 100644 index 0000000..3fbc556 --- /dev/null +++ b/python2/eval.go @@ -0,0 +1,95 @@ +package python2 + +/* +#include "go-python.h" + +// Forward declaration. +int go_trace_cgo(PyObject *obj, PyFrameObject *frame, int what, PyObject *arg); +*/ +import "C" + +import ( + "github.com/sbinet/go-python/runtime" + "unsafe" +) + +type Frame struct { + ptr *C.PyFrameObject +} + +func (f *Frame) toPy() *C.PyFrameObject { + if f == nil { + return nil + } + return f.ptr +} +func (f *Frame) GetLineNumber() int { + return int(C.PyFrame_GetLineNumber(f.ptr)) +} +func (f *Frame) GetFilename() runtime.Object { + if f.ptr.f_code == nil { + return nil + } + c := f.ptr.f_code + return toGo(c.co_filename) +} +func (f *Frame) Builtins() runtime.DictObject { + if f.ptr == nil { + return nil + } + return toGo(f.ptr.f_builtins) +} +func (f *Frame) Globals() runtime.DictObject { + if f.ptr == nil { + return nil + } + return toGo(f.ptr.f_globals) +} +func (f *Frame) Locals() runtime.Object { + if f.ptr == nil { + return nil + } + return toGo(f.ptr.f_locals) +} + +func (py2Runtime) EvalThreadsInitialized() bool { + return C.PyEval_ThreadsInitialized() != 0 +} + +func (py2Runtime) EvalInitThreads() { + C.PyEval_InitThreads() +} + +func (py2Runtime) EvalGetFrame() runtime.Frame { + f := C.PyEval_GetFrame() + if f == nil { + return nil + } + return &Frame{ptr: f} +} + +var ( + traces = make(map[*C.PyObject]runtime.TraceFunc) + traceTypes = map[int]runtime.TraceType{ + int(C.PyTrace_CALL): runtime.TraceCall, + int(C.PyTrace_EXCEPTION): runtime.TraceException, + int(C.PyTrace_LINE): runtime.TraceLine, + int(C.PyTrace_RETURN): runtime.TraceReturn, + int(C.PyTrace_C_CALL): runtime.TraceCCall, + int(C.PyTrace_C_EXCEPTION): runtime.TraceCException, + int(C.PyTrace_C_RETURN): runtime.TraceCReturn, + } +) + +//export go_trace +func go_trace(obj *C.PyObject, frame *C.PyFrameObject, what int, arg *C.PyObject) int { + fnc := traces[obj] + tt := traceTypes[what] + return fnc(toGo(obj), &Frame{ptr: frame}, tt, toGo(arg)) +} + +func (py2Runtime) EvalSetTrace(fnc runtime.TraceFunc, obj runtime.Object) { + o := fromPtr(obj).toPy() + traces[o] = fnc + C.PyEval_SetTrace((C.Py_tracefunc)(unsafe.Pointer(C.go_trace_cgo)), o) +} diff --git a/python2/file.go b/python2/file.go new file mode 100644 index 0000000..baa5fe3 --- /dev/null +++ b/python2/file.go @@ -0,0 +1,40 @@ +package python2 + +/* +#include +#include "go-python.h" + +PyObject* _gopy2_PyFile_FromFile(int fd, char *name, char *mode) { + FILE *f = fdopen(fd, mode); + PyObject *py = PyFile_FromFile(f, name, mode, NULL); + PyFile_SetBufSize(py, 0); + return py; +} + +*/ +import "C" + +import ( + "github.com/sbinet/go-python/runtime" + "os" + "unsafe" +) + +// FromFile converts a Go file to Python file object. +// Calling close from Python will not close a file descriptor. +func (py2Runtime) fromFile(f *os.File, mode string) *Object { + cname := C.CString(f.Name()) + cmode := C.CString(mode) + defer func() { + C.free(unsafe.Pointer(cname)) + C.free(unsafe.Pointer(cmode)) + }() + + p := C._gopy2_PyFile_FromFile(C.int(f.Fd()), cname, cmode) + return toGo(p) +} + +func (py py2Runtime) FromFile(f *os.File, mode string) runtime.Object { + p := py.fromFile(f, mode) + return toPtr(p) +} diff --git a/python2/go-python.h b/python2/go-python.h new file mode 100644 index 0000000..689571e --- /dev/null +++ b/python2/go-python.h @@ -0,0 +1,13 @@ +#ifndef GOPYTHON_GOPYTHON_H +#define GOPYTHON_GOPYTHON_H 1 + +#include "Python.h" + +#include "frameobject.h" +#include "marshal.h" + +// stdlib +#include +#include + +#endif // !GOPYTHON_GOPYTHON_H diff --git a/python2/object.go b/python2/object.go new file mode 100644 index 0000000..50c0b69 --- /dev/null +++ b/python2/object.go @@ -0,0 +1,67 @@ +package python2 + +/* +#include "go-python.h" + +void _gopy2_decref(PyObject *ptr) { Py_DECREF(ptr); } +int _gopy2_PyString_Check(PyObject *ptr) { PyString_Check(ptr); } +int _gopy2_PyString_CheckExact(PyObject *ptr) { PyString_CheckExact(ptr); } +*/ +import "C" + +import ( + "github.com/sbinet/go-python/runtime" +) + +type Object C.PyObject + +func (o *Object) Valid() bool { + return o != nil +} +func (o *Object) IsNone() bool { + return o.toPy() == C.Py_None +} +func (o *Object) DecRef() { + if !o.Valid() { + return + } + C._gopy2_decref(o.toPy()) +} + +func (o *Object) toPy() *C.PyObject { + return (*C.PyObject)(o) +} + +func toGo(obj *C.PyObject) *Object { + return (*Object)(obj) +} + +func fromPtr(obj runtime.Object) *Object { + if obj == nil { + return nil + } + return obj.(*Object) +} + +func toPtr(obj *Object) runtime.Object { + if obj == nil { + return nil + } + return obj +} + +func (obj *Object) HasAttr(name runtime.Object) bool { + return C.PyObject_HasAttr(obj.toPy(), fromPtr(name).toPy()) != 0 +} + +func (obj *Object) AsString() string { + return C.GoString(C.PyString_AsString(obj.toPy())) +} + +func (obj *Object) StringCheck() bool { + return C._gopy2_PyString_Check(obj.toPy()) != 0 +} + +func (obj *Object) StringCheckExact() bool { + return C._gopy2_PyString_CheckExact(obj.toPy()) != 0 +} diff --git a/python2/plugin/python2.go b/python2/plugin/python2.go new file mode 100644 index 0000000..9b40a12 --- /dev/null +++ b/python2/plugin/python2.go @@ -0,0 +1,5 @@ +package main + +import "github.com/sbinet/go-python/python2" + +var Runtime = python2.Runtime diff --git a/python2/python.go b/python2/python.go new file mode 100644 index 0000000..e3532a8 --- /dev/null +++ b/python2/python.go @@ -0,0 +1,79 @@ +package python2 + +//#cgo pkg-config: python-2.7 +//#include "go-python.h" +import "C" + +import ( + python "github.com/sbinet/go-python/runtime" + "os" + "unsafe" +) + +var Runtime python.Runtime = py2Runtime{} + +type py2Runtime struct{} + +func (py2Runtime) GetVersion() string { + return C.GoString(C.Py_GetVersion()) +} + +func (py2Runtime) GetCompiler() string { + return C.GoString(C.Py_GetCompiler()) +} + +func (py2Runtime) Initialize(signals bool) { + sig := 0 + if signals { + sig = 1 + } + C.Py_InitializeEx(C.int(sig)) +} + +func (py2Runtime) IsInitialized() bool { + return C.Py_IsInitialized() != 0 +} + +func (py2Runtime) Finalize() { + C.Py_Finalize() +} + +// Main runs the main program for the standard interpreter. This is made available for programs which embed Python. +// The args parameters should be prepared exactly as those which are passed to a C program’s main() function. +// It is important to note that the argument list may be modified (but the contents of the strings pointed to +// by the argument list are not). The return value will be the integer passed to the sys.exit() function, +// 1 if the interpreter exits due to an exception, or 2 if the parameter list does not represent a valid Python command line. +// +// Note that if an otherwise unhandled SystemError is raised, this function will not return 1, but exit the process, +// as long as Py_InspectFlag is not set. +func (py2Runtime) Main(args []string) int { + argc := C.int(len(args)) + // no need to free. Py_Main takes ownership. + argv := make([]*C.char, argc) + for i, arg := range args { + argv[i] = C.CString(arg) + } + return int(C.Py_Main(argc, &argv[0])) +} + +// RunString executes the Python source code from command in the __main__ module according to the flags argument. +// If __main__ does not already exist, it is created. Returns 0 on success or -1 if an exception was raised. +// If there was an error, there is no way to get the exception information. +func (py2Runtime) RunString(command string) int { + cmd := C.CString(command) + defer C.free(unsafe.Pointer(cmd)) + + return int(C.PyRun_SimpleStringFlags(cmd, nil)) +} + +func (py2Runtime) RunFile(f *os.File) int { + cname := C.CString(f.Name()) + defer C.free(unsafe.Pointer(cname)) + + cmode := C.CString("r") + defer C.free(unsafe.Pointer(cmode)) + + cf := C.fdopen(C.int(f.Fd()), cmode) + + return int(C.PyRun_SimpleFileExFlags(cf, cname, 0, nil)) +} diff --git a/python2/python_test.go b/python2/python_test.go new file mode 100644 index 0000000..60c2be9 --- /dev/null +++ b/python2/python_test.go @@ -0,0 +1,10 @@ +package python2 + +import ( + "github.com/sbinet/go-python/pytest" + "testing" +) + +func TestPython2(t *testing.T) { + pytest.TestRuntime(t, Runtime) +} diff --git a/python2/sys.go b/python2/sys.go new file mode 100644 index 0000000..ffff9ae --- /dev/null +++ b/python2/sys.go @@ -0,0 +1,33 @@ +package python2 + +//#include "go-python.h" +import "C" + +import ( + "github.com/sbinet/go-python/runtime" + "unsafe" +) + +func (py2Runtime) sysSetObject(name string, v *Object) int { + cname := C.CString(name) + defer C.free(unsafe.Pointer(cname)) + + return int(C.PySys_SetObject(cname, v.toPy())) +} + +func (py py2Runtime) SysSetObject(name string, v runtime.Object) int { + p := fromPtr(v) + return py.sysSetObject(name, p) +} + +func (py2Runtime) sysGetObject(name string) *Object { + cname := C.CString(name) + defer C.free(unsafe.Pointer(cname)) + + return toGo(C.PySys_GetObject(cname)) +} + +func (py py2Runtime) SysGetObject(name string) runtime.Object { + p := py.sysGetObject(name) + return toPtr(p) +} diff --git a/python2/types.go b/python2/types.go new file mode 100644 index 0000000..7a3d8e6 --- /dev/null +++ b/python2/types.go @@ -0,0 +1,38 @@ +package python2 + +/* +#include "go-python.h" +*/ +import "C" + +import ( + "github.com/sbinet/go-python/runtime" + "unsafe" +) + +func (py2Runtime) None() runtime.Object { + return toGo(C.Py_None) +} +func (py2Runtime) False() runtime.Object { + return toGo(C.Py_False) +} +func (py2Runtime) True() runtime.Object { + return toGo(C.Py_True) +} +func (py2Runtime) FromString(v string) runtime.Object { + cv := C.CString(v) + defer C.free(unsafe.Pointer(cv)) + return toGo(C.PyString_FromString(cv)) +} +func (py2Runtime) FromInt64(v int64) runtime.Object { + return toGo(C.PyLong_FromLong(C.long(v))) +} +func (py2Runtime) FromFloat64(v float64) runtime.Object { + return toGo(C.PyFloat_FromDouble(C.double(v))) +} +func (py py2Runtime) FromBool(v bool) runtime.Object { + if v { + return py.True() + } + return py.False() +} diff --git a/python3/ceval.go b/python3/ceval.go new file mode 100644 index 0000000..58b9e28 --- /dev/null +++ b/python3/ceval.go @@ -0,0 +1,12 @@ +package python3 + +/* +#include "go-python.h" + +// The gateway function +int go_trace_cgo(PyObject *obj, PyFrameObject *frame, int what, PyObject *arg) { + int go_trace(PyObject *obj, PyFrameObject *frame, int what, PyObject *arg); + return go_trace(obj, frame, what, arg); +} +*/ +import "C" diff --git a/python3/eval.go b/python3/eval.go new file mode 100644 index 0000000..5640fa2 --- /dev/null +++ b/python3/eval.go @@ -0,0 +1,95 @@ +package python3 + +/* +#include "go-python.h" + +// Forward declaration. +int go_trace_cgo(PyObject *obj, PyFrameObject *frame, int what, PyObject *arg); +*/ +import "C" + +import ( + "github.com/sbinet/go-python/runtime" + "unsafe" +) + +type Frame struct { + ptr *C.PyFrameObject +} + +func (f *Frame) toPy() *C.PyFrameObject { + if f == nil { + return nil + } + return f.ptr +} +func (f *Frame) GetLineNumber() int { + return int(C.PyFrame_GetLineNumber(f.ptr)) +} +func (f *Frame) GetFilename() runtime.Object { + if f.ptr.f_code == nil { + return nil + } + c := f.ptr.f_code + return toGo(c.co_filename) +} +func (f *Frame) Builtins() runtime.DictObject { + if f.ptr == nil { + return nil + } + return toGo(f.ptr.f_builtins) +} +func (f *Frame) Globals() runtime.DictObject { + if f.ptr == nil { + return nil + } + return toGo(f.ptr.f_globals) +} +func (f *Frame) Locals() runtime.Object { + if f.ptr == nil { + return nil + } + return toGo(f.ptr.f_locals) +} + +func (py3Runtime) EvalThreadsInitialized() bool { + return C.PyEval_ThreadsInitialized() != 0 +} + +func (py3Runtime) EvalInitThreads() { + C.PyEval_InitThreads() +} + +func (py3Runtime) EvalGetFrame() runtime.Frame { + f := C.PyEval_GetFrame() + if f == nil { + return nil + } + return &Frame{ptr: f} +} + +var ( + traces = make(map[*C.PyObject]runtime.TraceFunc) + traceTypes = map[int]runtime.TraceType{ + int(C.PyTrace_CALL): runtime.TraceCall, + int(C.PyTrace_EXCEPTION): runtime.TraceException, + int(C.PyTrace_LINE): runtime.TraceLine, + int(C.PyTrace_RETURN): runtime.TraceReturn, + int(C.PyTrace_C_CALL): runtime.TraceCCall, + int(C.PyTrace_C_EXCEPTION): runtime.TraceCException, + int(C.PyTrace_C_RETURN): runtime.TraceCReturn, + } +) + +//export go_trace +func go_trace(obj *C.PyObject, frame *C.PyFrameObject, what int, arg *C.PyObject) int { + fnc := traces[obj] + tt := traceTypes[what] + return fnc(toGo(obj), &Frame{ptr: frame}, tt, toGo(arg)) +} + +func (py3Runtime) EvalSetTrace(fnc runtime.TraceFunc, obj runtime.Object) { + o := fromPtr(obj).toPy() + traces[o] = fnc + C.PyEval_SetTrace((C.Py_tracefunc)(unsafe.Pointer(C.go_trace_cgo)), o) +} diff --git a/python3/file.go b/python3/file.go new file mode 100644 index 0000000..36987ad --- /dev/null +++ b/python3/file.go @@ -0,0 +1,29 @@ +package python3 + +//#include "go-python.h" +import "C" + +import ( + "github.com/sbinet/go-python/runtime" + "os" + "unsafe" +) + +// FromFile converts a Go file to Python file object. +// Calling close from Python will not close a file descriptor. +func (py3Runtime) fromFile(f *os.File, mode string) *Object { + cname := C.CString(f.Name()) + cmode := C.CString(mode) + defer func() { + C.free(unsafe.Pointer(cname)) + C.free(unsafe.Pointer(cmode)) + }() + + p := C.PyFile_FromFd(C.int(f.Fd()), cname, cmode, 0, nil, nil, nil, 0) + return toGo(p) +} + +func (py py3Runtime) FromFile(f *os.File, mode string) runtime.Object { + p := py.fromFile(f, mode) + return toPtr(p) +} diff --git a/python3/go-python.h b/python3/go-python.h new file mode 100644 index 0000000..689571e --- /dev/null +++ b/python3/go-python.h @@ -0,0 +1,13 @@ +#ifndef GOPYTHON_GOPYTHON_H +#define GOPYTHON_GOPYTHON_H 1 + +#include "Python.h" + +#include "frameobject.h" +#include "marshal.h" + +// stdlib +#include +#include + +#endif // !GOPYTHON_GOPYTHON_H diff --git a/python3/object.go b/python3/object.go new file mode 100644 index 0000000..ab92694 --- /dev/null +++ b/python3/object.go @@ -0,0 +1,75 @@ +package python3 + +/* +#include "go-python.h" + +void _gopy3_decref(PyObject *ptr) { Py_DECREF(ptr); } +int _gopy3_PyUnicode_Check(PyObject *ptr) { PyUnicode_Check(ptr); } +int _gopy3_PyUnicode_CheckExact(PyObject *ptr) { PyUnicode_CheckExact(ptr); } +*/ +import "C" + +import ( + "github.com/sbinet/go-python/runtime" + "unsafe" +) + +type Object C.PyObject + +func (o *Object) Valid() bool { + return o != nil +} +func (o *Object) IsNone() bool { + return o.toPy() == C.Py_None +} +func (o *Object) DecRef() { + if !o.Valid() { + return + } + C._gopy3_decref(o.toPy()) +} + +func (o *Object) toPy() *C.PyObject { + return (*C.PyObject)(o) +} + +func toGo(obj *C.PyObject) *Object { + return (*Object)(obj) +} + +func fromPtr(obj runtime.Object) *Object { + if obj == nil { + return nil + } + return obj.(*Object) +} + +func toPtr(obj *Object) runtime.Object { + if obj == nil { + return nil + } + return obj +} + +func (obj *Object) HasAttr(name runtime.Object) bool { + return C.PyObject_HasAttr(obj.toPy(), fromPtr(name).toPy()) != 0 +} + +func (obj *Object) AsString() string { + var size C.Py_ssize_t + data := C.PyUnicode_AsWideCharString(obj.toPy(), &size) + defer C.PyMem_Free(unsafe.Pointer(data)) + s, err := wcharTNToString(data, C.size_t(size)) + if err != nil { + panic(err) + } + return s +} + +func (obj *Object) StringCheck() bool { + return C._gopy3_PyUnicode_Check(obj.toPy()) != 0 +} + +func (obj *Object) StringCheckExact() bool { + return C._gopy3_PyUnicode_CheckExact(obj.toPy()) != 0 +} diff --git a/python3/plugin/python3.go b/python3/plugin/python3.go new file mode 100644 index 0000000..3527e4e --- /dev/null +++ b/python3/plugin/python3.go @@ -0,0 +1,7 @@ +package main + +import ( + "github.com/sbinet/go-python/python3" +) + +var Runtime = python3.Runtime diff --git a/python3/python.go b/python3/python.go new file mode 100644 index 0000000..f40621f --- /dev/null +++ b/python3/python.go @@ -0,0 +1,79 @@ +package python3 + +//#cgo pkg-config: python-3.6 +//#include "go-python.h" +import "C" + +import ( + python "github.com/sbinet/go-python/runtime" + "os" + "unsafe" +) + +var Runtime python.Runtime = py3Runtime{} + +type py3Runtime struct{} + +func (py3Runtime) GetVersion() string { + return C.GoString(C.Py_GetVersion()) +} + +func (py3Runtime) GetCompiler() string { + return C.GoString(C.Py_GetCompiler()) +} + +func (py3Runtime) Initialize(signals bool) { + sig := 0 + if signals { + sig = 1 + } + C.Py_InitializeEx(C.int(sig)) +} + +func (py3Runtime) IsInitialized() bool { + return C.Py_IsInitialized() != 0 +} + +func (py3Runtime) Finalize() { + C.Py_Finalize() +} + +// Main runs the main program for the standard interpreter. This is made available for programs which embed Python. +// The args parameters should be prepared exactly as those which are passed to a C program’s main() function. +// It is important to note that the argument list may be modified (but the contents of the strings pointed to +// by the argument list are not). The return value will be the integer passed to the sys.exit() function, +// 1 if the interpreter exits due to an exception, or 2 if the parameter list does not represent a valid Python command line. +// +// Note that if an otherwise unhandled SystemError is raised, this function will not return 1, but exit the process, +// as long as Py_InspectFlag is not set. +func (py3Runtime) Main(args []string) int { + argc := C.int(len(args)) + // no need to free. Py_Main takes ownership. + argv := make([]*C.wchar_t, argc) + for i, arg := range args { + argv[i], _ = stringToWcharT(arg) + } + return int(C.Py_Main(argc, &argv[0])) +} + +// RunString executes the Python source code from command in the __main__ module according to the flags argument. +// If __main__ does not already exist, it is created. Returns 0 on success or -1 if an exception was raised. +// If there was an error, there is no way to get the exception information. +func (py3Runtime) RunString(command string) int { + cmd := C.CString(command) + defer C.free(unsafe.Pointer(cmd)) + + return int(C.PyRun_SimpleStringFlags(cmd, nil)) +} + +func (py3Runtime) RunFile(f *os.File) int { + cname := C.CString(f.Name()) + defer C.free(unsafe.Pointer(cname)) + + cmode := C.CString("r") + defer C.free(unsafe.Pointer(cmode)) + + cf := C.fdopen(C.int(f.Fd()), cmode) + + return int(C.PyRun_SimpleFileExFlags(cf, cname, 0, nil)) +} diff --git a/python3/python_test.go b/python3/python_test.go new file mode 100644 index 0000000..138a0f5 --- /dev/null +++ b/python3/python_test.go @@ -0,0 +1,10 @@ +package python3 + +import ( + "github.com/sbinet/go-python/pytest" + "testing" +) + +func TestPython3(t *testing.T) { + pytest.TestRuntime(t, Runtime) +} diff --git a/python3/sys.go b/python3/sys.go new file mode 100644 index 0000000..597bfb5 --- /dev/null +++ b/python3/sys.go @@ -0,0 +1,33 @@ +package python3 + +//#include "go-python.h" +import "C" + +import ( + "github.com/sbinet/go-python/runtime" + "unsafe" +) + +func (py3Runtime) sysSetObject(name string, v *Object) int { + cname := C.CString(name) + defer C.free(unsafe.Pointer(cname)) + + return int(C.PySys_SetObject(cname, v.toPy())) +} + +func (py py3Runtime) SysSetObject(name string, v runtime.Object) int { + p := fromPtr(v) + return py.sysSetObject(name, p) +} + +func (py3Runtime) sysGetObject(name string) *Object { + cname := C.CString(name) + defer C.free(unsafe.Pointer(cname)) + + return toGo(C.PySys_GetObject(cname)) +} + +func (py py3Runtime) SysGetObject(name string) runtime.Object { + p := py.sysGetObject(name) + return toPtr(p) +} diff --git a/python3/types.go b/python3/types.go new file mode 100644 index 0000000..c36b565 --- /dev/null +++ b/python3/types.go @@ -0,0 +1,37 @@ +package python3 + +/* +#include "go-python.h" +*/ +import "C" + +import ( + "github.com/sbinet/go-python/runtime" +) + +func (py3Runtime) None() runtime.Object { + return toGo(C.Py_None) +} +func (py3Runtime) False() runtime.Object { + return toGo(C.Py_False) +} +func (py3Runtime) True() runtime.Object { + return toGo(C.Py_True) +} +func (py3Runtime) FromString(v string) runtime.Object { + data, size := stringToWcharT(v) + size-- // \0 + return toGo(C.PyUnicode_FromWideChar(data, C.Py_ssize_t(size))) +} +func (py3Runtime) FromInt64(v int64) runtime.Object { + return toGo(C.PyLong_FromLong(C.long(v))) +} +func (py3Runtime) FromFloat64(v float64) runtime.Object { + return toGo(C.PyFloat_FromDouble(C.double(v))) +} +func (py py3Runtime) FromBool(v bool) runtime.Object { + if v { + return py.True() + } + return py.False() +} diff --git a/python3/wchar.go b/python3/wchar.go new file mode 100644 index 0000000..34a178c --- /dev/null +++ b/python3/wchar.go @@ -0,0 +1,250 @@ +/* +gowchar library. +https://github.com/orofarne/gowchar/ + +Copyright (c) 2013, Maxim Dementyev. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of the author. nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +package python3 + +/* +#include + +const size_t SIZEOF_WCHAR_T = sizeof(wchar_t); + +void gowchar_set (wchar_t *arr, int pos, wchar_t val) { + arr[pos] = val; +} + +wchar_t gowchar_get (wchar_t *arr, int pos) { + return arr[pos]; +} +*/ +import "C" + +import ( + "fmt" + "unicode/utf16" + "unicode/utf8" +) + +var sizeof_WCHAR_T C.size_t = C.size_t(C.SIZEOF_WCHAR_T) + +func stringToWcharT(s string) (*C.wchar_t, C.size_t) { + switch sizeof_WCHAR_T { + case 2: + return stringToWchar2(s) // Windows + case 4: + return stringToWchar4(s) // Unix + default: + panic(fmt.Sprintf("Invalid sizeof(wchar_t) = %v", sizeof_WCHAR_T)) + } + panic("?!!") +} + +func wcharTToString(s *C.wchar_t) (string, error) { + switch sizeof_WCHAR_T { + case 2: + return wchar2ToString(s) // Windows + case 4: + return wchar4ToString(s) // Unix + default: + panic(fmt.Sprintf("Invalid sizeof(wchar_t) = %v", sizeof_WCHAR_T)) + } + panic("?!!") +} + +func wcharTNToString(s *C.wchar_t, size C.size_t) (string, error) { + switch sizeof_WCHAR_T { + case 2: + return wchar2NToString(s, size) // Windows + case 4: + return wchar4NToString(s, size) // Unix + default: + panic(fmt.Sprintf("Invalid sizeof(wchar_t) = %v", sizeof_WCHAR_T)) + } + panic("?!!") +} + +// Windows +func stringToWchar2(s string) (*C.wchar_t, C.size_t) { + var slen int + s1 := s + for len(s1) > 0 { + r, size := utf8.DecodeRuneInString(s1) + if er, _ := utf16.EncodeRune(r); er == '\uFFFD' { + slen += 1 + } else { + slen += 2 + } + s1 = s1[size:] + } + slen++ // \0 + res := C.malloc(C.size_t(slen) * sizeof_WCHAR_T) + var i int + for len(s) > 0 { + r, size := utf8.DecodeRuneInString(s) + if r1, r2 := utf16.EncodeRune(r); r1 != '\uFFFD' { + C.gowchar_set((*C.wchar_t)(res), C.int(i), C.wchar_t(r1)) + i++ + C.gowchar_set((*C.wchar_t)(res), C.int(i), C.wchar_t(r2)) + i++ + } else { + C.gowchar_set((*C.wchar_t)(res), C.int(i), C.wchar_t(r)) + i++ + } + s = s[size:] + } + C.gowchar_set((*C.wchar_t)(res), C.int(slen-1), C.wchar_t(0)) // \0 + return (*C.wchar_t)(res), C.size_t(slen) +} + +// Unix +func stringToWchar4(s string) (*C.wchar_t, C.size_t) { + slen := utf8.RuneCountInString(s) + slen++ // \0 + res := C.malloc(C.size_t(slen) * sizeof_WCHAR_T) + var i int + for len(s) > 0 { + r, size := utf8.DecodeRuneInString(s) + C.gowchar_set((*C.wchar_t)(res), C.int(i), C.wchar_t(r)) + s = s[size:] + i++ + } + C.gowchar_set((*C.wchar_t)(res), C.int(slen-1), C.wchar_t(0)) // \0 + return (*C.wchar_t)(res), C.size_t(slen) +} + +// Windows +func wchar2ToString(s *C.wchar_t) (string, error) { + var i int + var res string + for { + ch := C.gowchar_get(s, C.int(i)) + if ch == 0 { + break + } + r := rune(ch) + i++ + if !utf16.IsSurrogate(r) { + if !utf8.ValidRune(r) { + err := fmt.Errorf("Invalid rune at position %v", i) + return "", err + } + res += string(r) + } else { + ch2 := C.gowchar_get(s, C.int(i)) + r2 := rune(ch2) + r12 := utf16.DecodeRune(r, r2) + if r12 == '\uFFFD' { + err := fmt.Errorf("Invalid surrogate pair at position %v", i-1) + return "", err + } + res += string(r12) + i++ + } + } + return res, nil +} + +// Unix +func wchar4ToString(s *C.wchar_t) (string, error) { + var i int + var res string + for { + ch := C.gowchar_get(s, C.int(i)) + if ch == 0 { + break + } + r := rune(ch) + if !utf8.ValidRune(r) { + err := fmt.Errorf("Invalid rune at position %v", i) + return "", err + } + res += string(r) + i++ + } + return res, nil +} + +// Windows +func wchar2NToString(s *C.wchar_t, size C.size_t) (string, error) { + var i int + var res string + N := int(size) + for i < N { + ch := C.gowchar_get(s, C.int(i)) + if ch == 0 { + break + } + r := rune(ch) + i++ + if !utf16.IsSurrogate(r) { + if !utf8.ValidRune(r) { + err := fmt.Errorf("Invalid rune at position %v", i) + return "", err + } + + res += string(r) + } else { + if i >= N { + err := fmt.Errorf("Invalid surrogate pair at position %v", i-1) + return "", err + } + ch2 := C.gowchar_get(s, C.int(i)) + r2 := rune(ch2) + r12 := utf16.DecodeRune(r, r2) + if r12 == '\uFFFD' { + err := fmt.Errorf("Invalid surrogate pair at position %v", i-1) + return "", err + } + res += string(r12) + i++ + } + } + return res, nil +} + +// Unix +func wchar4NToString(s *C.wchar_t, size C.size_t) (string, error) { + var i int + var res string + N := int(size) + for i < N { + ch := C.gowchar_get(s, C.int(i)) + r := rune(ch) + if !utf8.ValidRune(r) { + err := fmt.Errorf("Invalid rune at position %v", i) + return "", err + } + res += string(r) + i++ + } + return res, nil +} diff --git a/run.go b/run.go new file mode 100644 index 0000000..f6e5b95 --- /dev/null +++ b/run.go @@ -0,0 +1,45 @@ +package python + +import ( + "os" +) + +func (py *Interpreter) RunString(command string) error { + py.mu.Lock() + defer py.mu.Unlock() + rcode := py.r.RunString(command) + if rcode == 0 { + return nil + } + return &RunError{Code: rcode} +} + +func (py *Interpreter) RunFile(filename string) error { + f, err := os.Open(filename) + if err != nil { + return err + } + defer f.Close() + + py.mu.Lock() + defer py.mu.Unlock() + rcode := py.r.RunFile(f) + if rcode == 0 { + return nil + } + return &RunError{Code: rcode, File: filename} +} + +func (py *Interpreter) Main(args []string) error { + py.mu.Lock() + defer py.mu.Unlock() + rcode := py.r.Main(args) + if rcode == 0 { + return nil + } + return &RunError{Code: rcode} +} + +func (py *Interpreter) RunMain(name string, args ...string) error { + return py.Main(append([]string{os.Args[0], name}, args...)) +} diff --git a/runtime/runtime.go b/runtime/runtime.go new file mode 100644 index 0000000..117ede1 --- /dev/null +++ b/runtime/runtime.go @@ -0,0 +1,115 @@ +package runtime + +import ( + "fmt" + "os" +) + +type Runtime interface { + GetVersion() string + GetCompiler() string + + IsInitialized() bool + Initialize(signals bool) + Finalize() + + Main(args []string) int + + EvalRuntime + RunRuntime + SysRuntime + FileRuntime + TypeRuntime +} + +type Object interface { + Valid() bool + IsNone() bool + DecRef() + + HasAttr(name Object) bool + + AsString() string + StringCheck() bool + StringCheckExact() bool +} + +type DictObject interface { + Object +} + +type Frame interface { + GetLineNumber() int + GetFilename() Object + + Builtins() DictObject + Globals() DictObject + Locals() Object +} + +type TraceType int + +func (t TraceType) String() string { + switch t { + case TraceCall: + return "Call" + case TraceException: + return "Exception" + case TraceLine: + return "Line" + case TraceReturn: + return "Return" + case TraceCCall: + return "C Call" + case TraceCException: + return "C Exception" + case TraceCReturn: + return "C Return" + default: + return fmt.Sprintf("TraceType(%d)", int(t)) + } +} + +const ( + TraceCall = TraceType(iota + 1) + TraceException + TraceLine + TraceReturn + TraceCCall + TraceCException + TraceCReturn +) + +type TraceFunc func(obj Object, frame Frame, what TraceType, arg Object) int + +type EvalRuntime interface { + EvalThreadsInitialized() bool + EvalInitThreads() + + EvalSetTrace(fnc TraceFunc, obj Object) + EvalGetFrame() Frame +} + +type RunRuntime interface { + RunString(command string) int + RunFile(f *os.File) int +} + +type SysRuntime interface { + SysSetObject(name string, v Object) int + SysGetObject(name string) Object +} + +type FileRuntime interface { + FromFile(f *os.File, mode string) Object +} + +type TypeRuntime interface { + None() Object + True() Object + False() Object + FromString(v string) Object + FromInt64(v int64) Object + FromFloat64(v float64) Object + FromBool(v bool) Object +} diff --git a/sys.go b/sys.go new file mode 100644 index 0000000..ee2c4fe --- /dev/null +++ b/sys.go @@ -0,0 +1,25 @@ +package python + +func (py *Interpreter) setStdStreamObject(name string, obj *Object) error { + ret := py.r.SysSetObject(name, obj.ptr) + if ret != 0 { + return errCode(ret) + } + ret = py.r.SysSetObject("__"+name+"__", obj.ptr) + return errCode(ret) +} + +// SetStdinObject sets a sys.stdin to a specified python object. +func (py *Interpreter) SetStdinObject(obj *Object) error { + return py.setStdStreamObject("stdin", obj) +} + +// SetStdoutObject sets a sys.stdout to a specified python object. +func (py *Interpreter) SetStdoutObject(obj *Object) error { + return py.setStdStreamObject("stdout", obj) +} + +// SetStderrObject sets a sys.stderr to a specified python object. +func (py *Interpreter) SetStderrObject(obj *Object) error { + return py.setStdStreamObject("stderr", obj) +} diff --git a/types.go b/types.go new file mode 100644 index 0000000..a4b03ce --- /dev/null +++ b/types.go @@ -0,0 +1,7 @@ +package python + +func (py *Interpreter) FromString(s string) *Object { + o := newObject(py.r.FromString(s)) + o.setFinalizer() + return o +} diff --git a/veryhigh.go b/veryhigh.go deleted file mode 100644 index b943e4a..0000000 --- a/veryhigh.go +++ /dev/null @@ -1,60 +0,0 @@ -package python - -//#include "go-python.h" -import "C" - -import ( - "fmt" - "unsafe" -) - -// very high level interface - -// int Py_Main(int argc, char **argv) -// The main program for the standard interpreter. This is made available for programs which embed Python. The argc and argv parameters should be prepared exactly as those which are passed to a C program’s main() function. It is important to note that the argument list may be modified (but the contents of the strings pointed to by the argument list are not). The return value will be the integer passed to the sys.exit() function, 1 if the interpreter exits due to an exception, or 2 if the parameter list does not represent a valid Python command line. -// -// Note that if an otherwise unhandled SystemError is raised, this function will not return 1, but exit the process, as long as Py_InspectFlag is not set. -func Py_Main(args []string) int { - var argc C.int = C.int(len(args)) - var argv []*C.char = make([]*C.char, argc) - for idx, arg := range args { - argv[idx] = C.CString(arg) - // no need to free. Py_Main takes owner ship. - //defer C.free(unsafe.Pointer(argv[idx])) - } - return int(C.Py_Main(argc, &argv[0])) -} - -// int PyRun_SimpleString(const char *command) -// This is a simplified interface to PyRun_SimpleStringFlags() below, leaving the PyCompilerFlags* argument set to NULL. -func PyRun_SimpleString(command string) int { - c_cmd := C.CString(command) - defer C.free(unsafe.Pointer(c_cmd)) - return int(C._gopy_PyRun_SimpleString(c_cmd)) -} - -// PyRun_SimpleFile executes the given python script synchronously. Note that -// unlike the corresponding C API, this will internally open and close the file -// for you. -func PyRun_SimpleFile(filename string) error { - cfname := C.CString(filename) - defer C.free(unsafe.Pointer(cfname)) - - cronly := C.CString("r") - defer C.free(unsafe.Pointer(cronly)) - - cfile, err := C.fopen(cfname, cronly) - if err != nil || cfile == nil { - return fmt.Errorf("python: could not open %s: %v", filename, err) - } - defer C.fclose(cfile) - - retcode := C.PyRun_SimpleFileExFlags(cfile, cfname, 0, nil) - if retcode != 0 { - return fmt.Errorf("error %d executing script %s", int(retcode), - filename) - } - return nil -} - -// EOF