Cython compiler directives: c_string_encoding
Dear mailing list I am trying to compile a program with Cython using the compiler directives and I am running into some trouble. Specifically, I am trying to port a Cython 0.18 program to Cython 0.19 and due to changes how char * and Python str are handled, I need to set the c_string_encoding directive. Unfortunately, this fails for my project and also in the following testcase when I try to do it locally: cimport cython cdef class TestClass: def foo(self): with cython.c_string_encoding("ascii"): return If I replace the directive with "with cython.boundscheck(True):" the program compiles fine. Also adding "#cython: c_string_encoding=ascii" as the first line of the file works fine. However, adding a decorator '@cython.c_string_encoding("ascii")' to foo also crashes the compiler. Finally, compiling with "-X c_string_encoding=ascii" also works. I was following the documentation provided here http://docs.cython.org/src/reference/compilation.html . The error message that I get is attached at the end. Did I do something wrong or can somebody point me in the right direction? Best regards Hannes Roest Error message: inimalcase.pyx:6:13: Compiler crash in InterpretCompilerDirectives ModuleNode.body = StatListNode(minimalcase.pyx:1:0) StatListNode.stats[1] = StatListNode(minimalcase.pyx:3:5) StatListNode.stats[0] = CClassDefNode(minimalcase.pyx:3:5, as_name = u'TestClass', class_name = u'TestClass', module_name = '', visibility = 'private') CClassDefNode.body = StatListNode(minimalcase.pyx:5:4) StatListNode.stats[0] = DefNode(minimalcase.pyx:5:4, modifiers = [...]/0, name = u'foo', num_required_args = 1, py_wrapper_required = True, reqd_kw_flags_cname = '0') DefNode.body = StatListNode(minimalcase.pyx:6:13) StatListNode.stats[0] = WithStatNode(minimalcase.pyx:6:13) Compiler crash traceback from this point on: File "/home/hr/lib/Cython-0.19.1/Cython/Compiler/Visitor.py", line 168, in _visit return handler_method(obj) File "/home/hr/lib/Cython-0.19.1/Cython/Compiler/ParseTreeTransforms.py", line 1002, in visit_WithStatNode for directive in self.try_to_parse_directives(node.manager) or []: File "/home/hr/lib/Cython-0.19.1/Cython/Compiler/ParseTreeTransforms.py", line 861, in try_to_parse_directives directives.append(self.try_to_parse_directive(optname, args, kwds, node.function.pos)) File "/home/hr/lib/Cython-0.19.1/Cython/Compiler/ParseTreeTransforms.py", line 913, in try_to_parse_directive assert False AssertionError:
Hannes Röst, 17.07.2013 14:45:
I am trying to compile a program with Cython using the compiler directives and I am running into some trouble. Specifically, I am trying to port a Cython 0.18 program to Cython 0.19 and due to changes how char * and Python str are handled, I need to set the c_string_encoding directive.
This may or may not be so. The fact that it gives an error in 0.19 might also hint at problems in your code. There's some documentation available: http://docs.cython.org/src/tutorial/strings.html If you need further help, then the cython-users mailing list is the right place to ask.
Unfortunately, this fails for my project and also in the following testcase when I try to do it locally:
cimport cython cdef class TestClass: def foo(self): with cython.c_string_encoding("ascii"): return
This can't work. The string encoding is a module global option.
The error message that I get is attached at the end. Did I do something wrong or can somebody point me in the right direction?
Error message:
inimalcase.pyx:6:13: Compiler crash in InterpretCompilerDirectives [...] File "/home/hr/lib/Cython-0.19.1/Cython/Compiler/ParseTreeTransforms.py", line 913, in try_to_parse_directive assert False AssertionError:
That's a bug. It shouldn't crash and instead give a compile error. Thanks for the report. Stefan
Hi Stefan Thank for your fast and detailed answer, this is helping us a lot. On 17 July 2013 18:24, Stefan Behnel <stefan_ml@behnel.de> wrote:
Hannes Röst, 17.07.2013 14:45:
I am trying to compile a program with Cython using the compiler directives and I am running into some trouble. Specifically, I am trying to port a Cython 0.18 program to Cython 0.19 and due to changes how char * and Python str are handled, I need to set the c_string_encoding directive.
This may or may not be so. The fact that it gives an error in 0.19 might also hint at problems in your code. There's some documentation available:
http://docs.cython.org/src/tutorial/strings.html
If you need further help, then the cython-users mailing list is the right place to ask.
Thanks, I will have a look at it. Currently we have strong guarantees that our strings only contain ascii since they are part of a controlled vocabulary which does not contain non-ASCII. Also there is no other option to convert since our code is talking to external C code. However, we might want to test for the assumptions that we make here.
Unfortunately, this fails for my project and also in the following testcase when I try to do it locally:
cimport cython cdef class TestClass: def foo(self): with cython.c_string_encoding("ascii"): return
This can't work. The string encoding is a module global option.
Thank you, I wasn’t aware of that. Maybe the documentation could be more clear on this point and specifically state the that the "Locally" method at the bottom of the page will only work with certain flags (and maybe list which ones work).
The error message that I get is attached at the end. Did I do something wrong or can somebody point me in the right direction?
Error message:
inimalcase.pyx:6:13: Compiler crash in InterpretCompilerDirectives [...] File "/home/hr/lib/Cython-0.19.1/Cython/Compiler/ParseTreeTransforms.py", line 913, in try_to_parse_directive assert False AssertionError:
That's a bug. It shouldn't crash and instead give a compile error. Thanks for the report.
Stefan
_______________________________________________ cython-devel mailing list cython-devel@python.org http://mail.python.org/mailman/listinfo/cython-devel
participants (2)
-
Hannes Röst -
Stefan Behnel