Skip to content

Commit cbb7913

Browse files
ondrasbinet
authored andcommitted
gopy/bind: pass Go strings to Python as Unicode objects
1 parent 7712a09 commit cbb7913

File tree

11 files changed

+57
-33
lines changed

11 files changed

+57
-33
lines changed

_examples/cgo/test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77

88
import cgo
99

10-
print("cgo.doc: %r" % (cgo.__doc__,))
11-
print("cgo.Hi()= %r" % (cgo.Hi(),))
12-
print("cgo.Hello(you)= %r" % (cgo.Hello("you"),))
10+
print("cgo.doc: %s" % repr(cgo.__doc__).lstrip('u'))
11+
print("cgo.Hi()= %s" % repr(cgo.Hi()).lstrip('u'))
12+
print("cgo.Hello(you)= %s" % repr(cgo.Hello("you")).lstrip('u'))

_examples/empty/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77

88
import empty as pkg
99

10-
print("doc(pkg):\n%s" % repr(pkg.__doc__))
10+
print("doc(pkg):\n%s" % repr(pkg.__doc__).lstrip('u'))
1111

_examples/named/test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
import named
1515

1616
### test docs
17-
print("doc(named): %r" % (named.__doc__,))
18-
print("doc(named.Float): %r" % (named.Float.__doc__,))
19-
print("doc(named.Float.Value): %r" % (named.Float.Value.__doc__,))
17+
print("doc(named): %s" % repr(named.__doc__).lstrip('u'))
18+
print("doc(named.Float): %s" % repr(named.Float.__doc__).lstrip('u'))
19+
print("doc(named.Float.Value): %s" % repr(named.Float.Value.__doc__).lstrip('u'))
2020

2121
print("v = named.Float()")
2222
v = named.Float()
@@ -83,12 +83,12 @@
8383
print("s = named.Str()")
8484
s = named.Str()
8585
print("s = %s" % (s,))
86-
print("s.Value() = %r" % (s.Value(),))
86+
print("s.Value() = %s" % repr(s.Value()).lstrip('u'))
8787

8888
print("s = named.Str('string')")
8989
s = named.Str("string")
9090
print("s = %s" % (s,))
91-
print("s.Value() = %r" % (s.Value(),))
91+
print("s.Value() = %s" % repr(s.Value()).lstrip('u'))
9292

9393
print("arr = named.Array()")
9494
arr = named.Array()

_examples/seqs/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import seqs
1414

1515
### test docs
16-
print("doc(seqs): %r" % (seqs.__doc__,))
16+
print("doc(seqs): %s" % repr(seqs.__doc__).lstrip('u'))
1717

1818
print("arr = seqs.Array(xrange(2))")
1919
arr = seqs.Array(xrange(2))

_examples/simple/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import simple as pkg
99

10-
print("doc(pkg):\n%s" % repr(pkg.__doc__))
10+
print("doc(pkg):\n%s" % repr(pkg.__doc__).lstrip('u'))
1111
print("pkg.Func()...")
1212
pkg.Func()
1313
print("fct = pkg.Func...")

_examples/structs/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
print("s = %s" % (s,))
1313
print("s.Init()")
1414
s.Init()
15-
print("s.Upper('boo')= %r" % (s.Upper("boo"),))
15+
print("s.Upper('boo')= %s" % repr(s.Upper("boo")).lstrip('u'))
1616

1717
print("s1 = structs.S1()")
1818
s1 = structs.S1()

_examples/unicode/test.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,32 @@
77
## py2/py3 compat
88
from __future__ import print_function, unicode_literals
99

10+
import sys
11+
1012
import encoding
1113

14+
# There is no portable way of outputting Unicode in Python 2/3 -- we encode
15+
# them manually and sidestep the builtin encoding machinery
16+
try:
17+
binary_stdout = sys.stdout.buffer # Python 3
18+
except AttributeError:
19+
binary_stdout = sys.stdout # Python 2
20+
1221
bytestr = b"Python byte string"
1322
unicodestr = u"Python Unicode string 🐱"
1423

1524
bytestr_ret = encoding.HandleString(bytestr)
1625
unicodestr_ret = encoding.HandleString(unicodestr)
1726

18-
print("encoding.HandleString(bytestr) ->", bytestr_ret)
19-
print("encoding.HandleString(unicodestr) ->", unicodestr_ret)
27+
binary_stdout.write(b"encoding.HandleString(bytestr) -> ")
28+
binary_stdout.write(bytestr_ret.encode('UTF-8'))
29+
binary_stdout.write(b'\n')
30+
binary_stdout.write(b"encoding.HandleString(unicodestr) -> ")
31+
binary_stdout.write(unicodestr_ret.encode('UTF-8'))
32+
binary_stdout.write(b'\n')
2033

2134
gostring_ret = encoding.GetString()
22-
print("encoding.GetString() ->", gostring_ret)
35+
binary_stdout.write(b"encoding.GetString() -> ")
36+
binary_stdout.write(gostring_ret.encode("UTF-8"))
37+
binary_stdout.write(b"\n")
2338

_examples/vars/test.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
import vars
99

10-
print("doc(vars):\n%s" % repr(vars.__doc__))
11-
print("doc(vars.GetV1()):\n%s" % repr(vars.GetV1.__doc__))
12-
print("doc(vars.SetV1()):\n%s" % repr(vars.SetV1.__doc__))
10+
print("doc(vars):\n%s" % repr(vars.__doc__).lstrip('u'))
11+
print("doc(vars.GetV1()):\n%s" % repr(vars.GetV1.__doc__).lstrip('u'))
12+
print("doc(vars.SetV1()):\n%s" % repr(vars.SetV1.__doc__).lstrip('u'))
1313

1414
print("Initial values")
1515
print("v1 = %s" % vars.GetV1())
@@ -48,6 +48,6 @@
4848
print("k1 = %s" % vars.GetKind1())
4949
print("k2 = %s" % vars.GetKind2())
5050

51-
print("vars.GetDoc() = %s" % repr(vars.GetDoc()))
52-
print("doc of vars.GetDoc = %s" % (repr(vars.GetDoc.__doc__),))
53-
print("doc of vars.SetDoc = %s" % (repr(vars.SetDoc.__doc__),))
51+
print("vars.GetDoc() = %s" % repr(vars.GetDoc()).lstrip('u'))
52+
print("doc of vars.GetDoc = %s" % repr(vars.GetDoc.__doc__).lstrip('u'))
53+
print("doc of vars.SetDoc = %s" % repr(vars.SetDoc.__doc__).lstrip('u'))

bind/gencffi.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ const (
1515
// #include "%[3]s.h"
1616
// """)
1717
// discuss: https://github.com/go-python/gopy/pull/93#discussion_r119652220
18-
cffiPreamble = `"""%[1]s"""
18+
cffiPreamble = `%[1]s
19+
from __future__ import unicode_literals
1920
import collections
2021
import os
2122
import sys
@@ -157,17 +158,15 @@ class _cffi_helper(object):
157158
s = _cffi_helper.lib._cgopy_CString(c)
158159
pystr = ffi.string(s)
159160
_cffi_helper.lib._cgopy_FreeCString(s)
160-
if _PY3:
161-
pystr = pystr.decode('utf8')
161+
pystr = pystr.decode('utf8')
162162
return pystr
163163
164164
@staticmethod
165165
def cffi_cgopy_cnv_c2py_errstring(c):
166166
s = _cffi_helper.lib._cgopy_ErrorString(c)
167167
pystr = ffi.string(s)
168168
_cffi_helper.lib._cgopy_FreeCString(s)
169-
if _PY3:
170-
pystr = pystr.decode('utf8')
169+
pystr = pystr.decode('utf8')
171170
return pystr
172171
173172
@staticmethod
@@ -242,7 +241,11 @@ func (g *cffiGen) gen() error {
242241
func (g *cffiGen) genCffiPreamble() {
243242
n := g.pkg.pkg.Name()
244243
pkgDoc := g.pkg.doc.Doc
245-
g.wrapper.Printf(cffiPreamble, pkgDoc, n)
244+
if pkgDoc != "" {
245+
g.wrapper.Printf(cffiPreamble, `"""`+pkgDoc+`"""`, n)
246+
} else {
247+
g.wrapper.Printf(cffiPreamble, "", n)
248+
}
246249
}
247250

248251
func (g *cffiGen) genCffiCdef() {

bind/gencpy.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ cgopy_cnv_py2c_string(PyObject *o, GoString *addr) {
128128
static PyObject*
129129
cgopy_cnv_c2py_string(GoString *addr) {
130130
const char *str = _cgopy_CString(*addr);
131-
PyObject *pystr = PyString_FromString(str);
131+
PyObject *pystr = PyUnicode_FromString(str);
132132
free((void*)str);
133133
return pystr;
134134
}
@@ -298,10 +298,16 @@ func (g *cpyGen) gen() error {
298298
)
299299
}
300300

301-
g.impl.Printf("module = Py_InitModule3(%[1]q, cpy_%[1]s_methods, %[2]q);\n\n",
302-
g.pkg.pkg.Name(),
303-
g.pkg.doc.Doc,
304-
)
301+
if g.pkg.doc.Doc != "" {
302+
g.impl.Printf("module = Py_InitModule3(%[1]q, cpy_%[1]s_methods, %[2]q);\n\n",
303+
g.pkg.pkg.Name(),
304+
g.pkg.doc.Doc,
305+
)
306+
} else {
307+
g.impl.Printf("module = Py_InitModule3(%[1]q, cpy_%[1]s_methods, NULL);\n\n",
308+
g.pkg.pkg.Name(),
309+
)
310+
}
305311

306312
for _, n := range g.pkg.syms.names() {
307313
sym := g.pkg.syms.sym(n)

0 commit comments

Comments
 (0)