Commit 53772108 authored by da-woods's avatar da-woods Committed by GitHub

Cleanup and fix string initialization code for LIMITED_API (GH-3378)

* Cleanup of string initialization code for limited API

Now appears in a few large blocks rather than

```
 # if CYTHON_LIMITED_API
   individual string line
 # endif
```

* Fixed issue with some unicode strings, e.g.

```
# cython: language_level=3str, binding=True

def non_ascii_str():
    s = 'ø\x20\u0020'
    assert isinstance(s, str)
    return s
```
parent 0d272a51
...@@ -1512,6 +1512,17 @@ class GlobalState(object): ...@@ -1512,6 +1512,17 @@ class GlobalState(object):
w = self.parts['pystring_table'] w = self.parts['pystring_table']
w.putln("") w.putln("")
w.putln("static __Pyx_StringTabEntry %s[] = {" % Naming.stringtab_cname) w.putln("static __Pyx_StringTabEntry %s[] = {" % Naming.stringtab_cname)
w.putln("#if CYTHON_COMPILING_IN_LIMITED_API")
w_limited_writer = w.insertion_point()
w.putln("#else")
w_not_limited_writer = w.insertion_point()
w.putln("#endif")
decls_writer.putln("#if !CYTHON_COMPILING_IN_LIMITED_API")
not_limited_api_decls_writer = decls_writer.insertion_point()
decls_writer.putln("#endif")
init_globals.putln("#if CYTHON_COMPILING_IN_LIMITED_API")
init_globals_limited_api = init_globals.insertion_point()
init_globals.putln("#endif")
for idx, py_string_args in enumerate(py_strings): for idx, py_string_args in enumerate(py_strings):
c_cname, _, py_string = py_string_args c_cname, _, py_string = py_string_args
if not py_string.is_str or not py_string.encoding or \ if not py_string.is_str or not py_string.encoding or \
...@@ -1530,23 +1541,20 @@ class GlobalState(object): ...@@ -1530,23 +1541,20 @@ class GlobalState(object):
py_string.cname) py_string.cname)
self.parts['module_state_traverse'].putln("Py_VISIT(traverse_module_state->%s);" % self.parts['module_state_traverse'].putln("Py_VISIT(traverse_module_state->%s);" %
py_string.cname) py_string.cname)
decls_writer.putln("#if !CYTHON_COMPILING_IN_LIMITED_API") not_limited_api_decls_writer.putln(
decls_writer.putln(
"static PyObject *%s;" % py_string.cname) "static PyObject *%s;" % py_string.cname)
decls_writer.putln("#endif")
if py_string.py3str_cstring: if py_string.py3str_cstring:
w.putln("#if PY_MAJOR_VERSION >= 3") w_not_limited_writer.putln("#if PY_MAJOR_VERSION >= 3")
w.putln("{&%s, %s, sizeof(%s), %s, %d, %d, %d}," % ( w_not_limited_writer.putln("{&%s, %s, sizeof(%s), %s, %d, %d, %d}," % (
py_string.cname, py_string.cname,
py_string.py3str_cstring.cname, py_string.py3str_cstring.cname,
py_string.py3str_cstring.cname, py_string.py3str_cstring.cname,
'0', 1, 0, '0', 1, 0,
py_string.intern py_string.intern
)) ))
w.putln("#else") w_not_limited_writer.putln("#else")
w_not_limited_writer.putln("{&%s, %s, sizeof(%s), %s, %d, %d, %d}," % (
w.putln("#if CYTHON_COMPILING_IN_LIMITED_API") py_string.cname,
w.putln("{0, %s, sizeof(%s), %s, %d, %d, %d}," % (
c_cname, c_cname,
c_cname, c_cname,
encoding, encoding,
...@@ -1554,26 +1562,21 @@ class GlobalState(object): ...@@ -1554,26 +1562,21 @@ class GlobalState(object):
py_string.is_str, py_string.is_str,
py_string.intern py_string.intern
)) ))
w.putln("#else") if py_string.py3str_cstring:
w.putln("{&%s, %s, sizeof(%s), %s, %d, %d, %d}," % ( w_not_limited_writer.putln("#endif")
py_string.cname, w_limited_writer.putln("{0, %s, sizeof(%s), %s, %d, %d, %d}," % (
c_cname, c_cname if not py_string.py3str_cstring else py_string.py3str_cstring.cname,
c_cname, c_cname if not py_string.py3str_cstring else py_string.py3str_cstring.cname,
encoding, encoding if not py_string.py3str_cstring else '0',
py_string.is_unicode, py_string.is_unicode,
py_string.is_str, py_string.is_str,
py_string.intern py_string.intern
)) ))
w.putln("#endif") init_globals_limited_api.putln("if (__Pyx_InitString(%s[%d], &%s) < 0) %s;" % (
init_globals.putln("#if CYTHON_COMPILING_IN_LIMITED_API")
init_globals.putln("if (__Pyx_InitString(%s[%d], &%s) < 0) %s;" % (
Naming.stringtab_cname, Naming.stringtab_cname,
idx, idx,
py_string.cname, py_string.cname,
init_globals.error_goto(self.module_pos))) init_globals.error_goto(self.module_pos)))
init_globals.putln("#endif")
if py_string.py3str_cstring:
w.putln("#endif")
w.putln("{0, 0, 0, 0, 0, 0, 0}") w.putln("{0, 0, 0, 0, 0, 0, 0}")
w.putln("};") w.putln("};")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment