Skip to content

Commit 373afb0

Browse files
committed
Support python3.5
1 parent 2255a89 commit 373afb0

10 files changed

+34
-48
lines changed

_test_py2go.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def return_unicode():
2727

2828

2929
def return_bytearray():
30-
return bytearray('abcdefg')
30+
return bytearray(b'abcdefg')
3131

3232

3333
def return_array():

cgolinks.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package py
22

33
/*
4-
#cgo pkg-config: python-2.7
4+
#cgo pkg-config: python-3.5
55
*/
66
import "C"

error.go

+6-15
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ void fetchPythonError(PyObject* excInfo)
1313
PyObject *type, *value, *traceback;
1414
1515
PyErr_Fetch(&type, &value, &traceback);
16+
PyErr_NormalizeException(&type, &value, &traceback);
17+
if (traceback != NULL) {
18+
PyException_SetTraceback(value, traceback);
19+
}
1620
PyTuple_SetItem(excInfo, 0, idOrNone(type));
1721
PyTuple_SetItem(excInfo, 1, idOrNone(value));
1822
PyTuple_SetItem(excInfo, 2, idOrNone(traceback));
@@ -44,20 +48,7 @@ func init() {
4448
}
4549
tracebackFormatExceptionFunc = formatException
4650

47-
exceptions, err := loadModule("exceptions")
48-
if err != nil {
49-
ch <- err
50-
return
51-
}
52-
defer exceptions.decRef()
53-
syntaxErrorCString := C.CString("SyntaxError")
54-
defer C.free(unsafe.Pointer(syntaxErrorCString))
55-
syntaxError := C.PyObject_GetAttrString(exceptions.p, syntaxErrorCString)
56-
if syntaxError == nil {
57-
ch <- errors.New("cannot load exceptions.SyntaxError")
58-
return
59-
}
60-
syntaxErrorType.p = syntaxError
51+
syntaxErrorType.p = C.PyExc_SyntaxError
6152

6253
ch <- nil
6354
})
@@ -152,7 +143,7 @@ func getPyErr() error {
152143

153144
func extractLineFromFormattedErrorMessage(formatted Object, n C.Py_ssize_t) string {
154145
line := C.PyList_GetItem(formatted.p, n)
155-
return C.GoString(C.PyString_AsString(line))
146+
return C.GoString(C.PyUnicode_AsUTF8(line))
156147
}
157148

158149
// pyErr represents an exception of python.

error_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
)
77

88
func TestPyError(t *testing.T) {
9-
Convey("When importing exceptions module and extract SyntaxError type", t, func() {
10-
mdl, err := LoadModule("exceptions")
9+
Convey("When importing builtins module and extract SyntaxError type", t, func() {
10+
mdl, err := LoadModule("builtins")
1111
So(err, ShouldBeNil)
1212
So(mdl, ShouldNotBeNil)
1313
Reset(func() {

func_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func TestPyFunc(t *testing.T) {
105105
Convey("it should return an error.", func() {
106106
So(err, ShouldNotBeNil)
107107
So(ret2.p, ShouldBeNil)
108-
So(err.Error(), ShouldContainSubstring, "__call__() takes exactly 2 arguments (1 given)")
108+
So(err.Error(), ShouldContainSubstring, "__call__() missing 1 required positional argument")
109109
})
110110
})
111111

go2py_converter.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func newPyObj(v data.Value) (Object, error) {
2727
pyobj = C.PyBool_FromLong(C.long(b))
2828
case data.TypeInt:
2929
i, _ := data.AsInt(v)
30-
pyobj = C.PyInt_FromLong(C.long(i))
30+
pyobj = C.PyLong_FromLong(C.long(i))
3131
case data.TypeFloat:
3232
f, _ := data.AsFloat(v)
3333
pyobj = C.PyFloat_FromDouble(C.double(f))
@@ -37,7 +37,7 @@ func newPyObj(v data.Value) (Object, error) {
3737
case data.TypeBlob:
3838
b, _ := data.AsBlob(v)
3939
cb := (*C.char)(unsafe.Pointer(&b[0]))
40-
pyobj = C.PyByteArray_FromStringAndSize(cb, C.Py_ssize_t(len(b)))
40+
pyobj = C.PyBytes_FromStringAndSize(cb, C.Py_ssize_t(len(b)))
4141
case data.TypeTimestamp:
4242
t, _ := data.AsTimestamp(v)
4343
pyobj = getPyDateTime(t)
@@ -62,7 +62,7 @@ func newPyObj(v data.Value) (Object, error) {
6262
func newPyString(s string) *C.PyObject {
6363
cs := C.CString(s)
6464
defer C.free(unsafe.Pointer(cs))
65-
return C.PyString_FromString(cs)
65+
return C.PyUnicode_FromString(cs)
6666
}
6767

6868
func newPyArray(a data.Array) (*C.PyObject, error) {

go2py_converter_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestConvertGo2PyObject(t *testing.T) {
2727
"string": argAndExpected{data.String("test"), "test"},
2828
"int": argAndExpected{data.Int(9), "9"},
2929
"float": argAndExpected{data.Float(0.9), "0.9"},
30-
"byte": argAndExpected{data.Blob([]byte("ABC")), "ABC"},
30+
"byte": argAndExpected{data.Blob([]byte("ABC")), "b'ABC'"},
3131
"true": argAndExpected{data.True, "True"},
3232
"false": argAndExpected{data.False, "False"},
3333
"null": argAndExpected{data.Null{}, "None"},

mainthread/init.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ this package is executed before all other init functions in py package.
1010
package mainthread
1111

1212
/*
13-
#cgo pkg-config: python-2.7
13+
#cgo pkg-config: python-3.5
1414
#include "Python.h"
1515
*/
1616
import "C"

module_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ func TestNewInstanceWithKeywordArgument(t *testing.T) {
249249

250250
actual, err := ins.Call("confirm_init")
251251
So(err, ShouldBeNil)
252-
So(actual, ShouldEqual, "1_2_{'c': 3, 'd': 4}")
252+
So(actual, ShouldBeIn, []string{"1_2_{'c': 3, 'd': 4}", "1_2_{'d': 4, 'c': 3}"})
253253
})
254254
})
255255

py2go_converter.go

+17-22
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ int IsPyTypeFalse(PyObject *o) {
1212
return o == Py_False;
1313
}
1414
15-
int IsPyTypeInt(PyObject *o) {
16-
return PyInt_CheckExact(o);
15+
int IsPyTypeLong(PyObject *o) {
16+
return PyLong_CheckExact(o);
1717
}
1818
1919
int IsPyTypeFloat(PyObject *o) {
@@ -24,8 +24,8 @@ int IsPyTypeByteArray(PyObject *o) {
2424
return PyByteArray_CheckExact(o);
2525
}
2626
27-
int IsPyTypeString(PyObject *o) {
28-
return PyString_CheckExact(o);
27+
int IsPyTypeBytes(PyObject *o) {
28+
return PyBytes_CheckExact(o);
2929
}
3030
3131
int IsPyTypeList(PyObject *o) {
@@ -76,37 +76,32 @@ func fromPyTypeObject(o *C.PyObject) (data.Value, error) {
7676
case C.IsPyTypeFalse(o) > 0:
7777
return data.Bool(false), nil
7878

79-
case C.IsPyTypeInt(o) > 0:
80-
return data.Int(C.PyInt_AsLong(o)), nil
79+
case C.IsPyTypeLong(o) > 0:
80+
return data.Int(C.PyLong_AsLong(o)), nil
8181

8282
case C.IsPyTypeFloat(o) > 0:
8383
return data.Float(C.PyFloat_AsDouble(o)), nil
8484

8585
case C.IsPyTypeByteArray(o) > 0:
8686
bytePtr := C.PyByteArray_FromObject(o)
8787
charPtr := C.PyByteArray_AsString(bytePtr)
88-
l := C.PyByteArray_Size(o)
89-
return data.Blob(C.GoBytes(unsafe.Pointer(charPtr), C.int(l))), nil
88+
size := C.int(C.PyByteArray_Size(o))
89+
return data.Blob(C.GoBytes(unsafe.Pointer(charPtr), size)), nil
9090

91-
case C.IsPyTypeString(o) > 0:
92-
size := C.int(C.PyString_Size(o))
93-
charPtr := C.PyString_AsString(o)
94-
return data.String(string(C.GoBytes(unsafe.Pointer(charPtr), size))), nil
91+
case C.IsPyTypeBytes(o) > 0:
92+
charPtr := C.PyBytes_AsString(o)
93+
size := C.int(C.PyBytes_Size(o))
94+
return data.Blob(C.GoBytes(unsafe.Pointer(charPtr), size)), nil
9595

9696
case C.IsPyTypeUnicode(o) > 0:
9797
// Use unicode string as UTF-8 in py because
9898
// Go's source code is defined to be UTF-8 text and string literal is too.
99-
utf8 := C.CString("UTF-8")
100-
defer C.free(unsafe.Pointer(utf8))
101-
102-
strObj := C.PyUnicode_AsEncodedString(o, utf8, nil)
103-
if strObj == nil {
99+
var size C.Py_ssize_t
100+
charPtr := C.PyUnicode_AsUTF8AndSize(o, &size)
101+
if charPtr == nil {
104102
return data.Null{}, getPyErr()
105103
}
106-
str := Object{p: strObj}
107-
defer str.decRef()
108-
109-
return fromPyTypeObject(str.p)
104+
return data.String(string(C.GoBytes(unsafe.Pointer(charPtr), C.int(size)))), nil
110105

111106
case isPyTypeDateTime(o):
112107
return fromTimestamp(o), nil
@@ -156,7 +151,7 @@ func fromPyMap(o *C.PyObject) (data.Map, error) {
156151

157152
for C.int(C.PyDict_Next(o, &pos, &key, &value)) > 0 {
158153
// data.Map's key is only allowed string or unicode
159-
if C.IsPyTypeString(key) == 0 && C.IsPyTypeUnicode(key) == 0 {
154+
if C.IsPyTypeBytes(key) == 0 && C.IsPyTypeUnicode(key) == 0 {
160155
continue
161156
}
162157
k, _ := fromPyTypeObject(key)

0 commit comments

Comments
 (0)