unsupported meta-programming-related features
Dear Cython developers, Python supports meta-programming, in which a variable with name specified in a string can be created at run-time. One built-in library which make use of this is argparse. For example: parser.add_argument('-N', '--max_threads', help='maximum number of concurrent decoding threads', type=int, default=16) in this case, the variable max_threads is created from the string argument. And then Cython will generate an incorrect C program with the following error: smt.py:78:88: undeclared name not builtin: headtail smt.c:1:2: error: #error Do not use this file, it is the result of a failed Cython compilation. In comparison, I found that nuitka can convert this kind of Python programs sucessfully. I hope Cython can be improved. Thanks! Cheers, Xuancong
On Tue, Apr 19, 2016 at 2:13 AM, Xuancong Wang <xuancong84@gmail.com> wrote:
Dear Cython developers,
Python supports meta-programming, in which a variable with name specified in a string can be created at run-time. One built-in library which make use of this is argparse.
For example:
parser.add_argument('-N', '--max_threads', help='maximum number of concurrent decoding threads', type=int, default=16)
in this case, the variable max_threads is created from the string argument. And then Cython will generate an incorrect C program with the following error:
smt.py:78:88: undeclared name not builtin: headtail smt.c:1:2: error: #error Do not use this file, it is the result of a failed Cython compilation.
Argparse works just fine in Cython import argparse parser = argparse.ArgumentParser() parser.add_argument('-N', '--max_threads', help='maximum number of concurrent decoding threads', type=int, default=16) args = parser.parse_args() print args.max_threads Could you provide a full example that you think should work? Where is headtail defined? Is it another argument?
In comparison, I found that nuitka can convert this kind of Python programs sucessfully. I hope Cython can be improved. Thanks!
argsparse dynamically adds the attributes to the returned object, which works fine in Cython. Unless you're doing something like http://stackoverflow.com/questions/19299635/python-argparse-parse-args-into-... That's one of the few places we diverge, because people strongly preferred compile-time errors to runtime errors in this case.
Sorry, I have a few extra steps which map all parser symbols into the global space. parser = argparse.ArgumentParser(...) parser.add_argument('-ht', '--headtail', help='add headtail', default=False, action='store_true') opt=parser.parse_args() globals().update(vars(opt)) xuancong On Wed, Apr 20, 2016 at 1:41 AM, Robert Bradshaw <robertwb@gmail.com> wrote:
On Tue, Apr 19, 2016 at 2:13 AM, Xuancong Wang <xuancong84@gmail.com> wrote:
Dear Cython developers,
Python supports meta-programming, in which a variable with name specified in a string can be created at run-time. One built-in library which make use of this is argparse.
For example:
parser.add_argument('-N', '--max_threads', help='maximum number of concurrent decoding threads', type=int, default=16)
in this case, the variable max_threads is created from the string argument. And then Cython will generate an incorrect C program with the following error:
smt.py:78:88: undeclared name not builtin: headtail smt.c:1:2: error: #error Do not use this file, it is the result of a failed Cython compilation.
Argparse works just fine in Cython
import argparse parser = argparse.ArgumentParser() parser.add_argument('-N', '--max_threads', help='maximum number of concurrent decoding threads', type=int, default=16) args = parser.parse_args() print args.max_threads
Could you provide a full example that you think should work? Where is headtail defined? Is it another argument?
In comparison, I found that nuitka can convert this kind of Python programs sucessfully. I hope Cython can be improved. Thanks!
argsparse dynamically adds the attributes to the returned object, which works fine in Cython. Unless you're doing something like
http://stackoverflow.com/questions/19299635/python-argparse-parse-args-into-...
That's one of the few places we diverge, because people strongly preferred compile-time errors to runtime errors in this case.
_______________________________________________ cython-devel mailing list cython-devel@python.org https://mail.python.org/mailman/listinfo/cython-devel
On Tue, Apr 19, 2016 at 11:22 PM, Xuancong Wang <xuancong84@gmail.com> wrote:
Sorry, I have a few extra steps which map all parser symbols into the global space.
parser = argparse.ArgumentParser(...) parser.add_argument('-ht', '--headtail', help='add headtail', default=False, action='store_true') opt=parser.parse_args() globals().update(vars(opt))
Symbols injected into globals() manually are not supported by default, see Stefan's comments on how to support this. Documented at https://github.com/cython/cython/wiki/Unsupported On Wed, Apr 20, 2016 at 1:41 AM, Robert Bradshaw <robertwb@gmail.com> wrote:
On Tue, Apr 19, 2016 at 2:13 AM, Xuancong Wang <xuancong84@gmail.com> wrote:
Dear Cython developers,
Python supports meta-programming, in which a variable with name specified in a string can be created at run-time. One built-in library which make use of this is argparse.
For example:
parser.add_argument('-N', '--max_threads', help='maximum number of concurrent decoding threads', type=int, default=16)
in this case, the variable max_threads is created from the string argument. And then Cython will generate an incorrect C program with the following error:
smt.py:78:88: undeclared name not builtin: headtail smt.c:1:2: error: #error Do not use this file, it is the result of a failed Cython compilation.
Argparse works just fine in Cython
import argparse parser = argparse.ArgumentParser() parser.add_argument('-N', '--max_threads', help='maximum number of concurrent decoding threads', type=int, default=16) args = parser.parse_args() print args.max_threads
Could you provide a full example that you think should work? Where is headtail defined? Is it another argument?
In comparison, I found that nuitka can convert this kind of Python programs sucessfully. I hope Cython can be improved. Thanks!
argsparse dynamically adds the attributes to the returned object, which works fine in Cython. Unless you're doing something like
http://stackoverflow.com/questions/19299635/python-argparse-parse-args-into-...
That's one of the few places we diverge, because people strongly
preferred
compile-time errors to runtime errors in this case.
_______________________________________________ cython-devel mailing list cython-devel@python.org https://mail.python.org/mailman/listinfo/cython-devel
_______________________________________________ cython-devel mailing list cython-devel@python.org https://mail.python.org/mailman/listinfo/cython-devel
Xuancong Wang schrieb am 19.04.2016 um 11:13:
Python supports meta-programming, in which a variable with name specified in a string can be created at run-time. One built-in library which make use of this is argparse.
For example:
parser.add_argument('-N', '--max_threads', help='maximum number of concurrent decoding threads', type=int, default=16)
in this case, the variable max_threads is created from the string argument.
Actually, no. What argparse returns on parsing is an object with dynamically created attributes. It doesn't magically inject any global variables into your module namespace. Or were you trying to compile argparse itself? Then I don't see a reason either why that shouldn't work.
And then Cython will generate an incorrect C program with the following error:
smt.py:78:88: undeclared name not builtin: headtail smt.c:1:2: error: #error Do not use this file, it is the result of a failed Cython compilation.
In comparison, I found that nuitka can convert this kind of Python programs sucessfully. I hope Cython can be improved. Thanks!
Cython makes this an error because in almost all cases, with very few exceptions, it is an actual bug in your program. For me personally, this already helped me find several bugs in the past, both in my code as well as other people's code, including at least one in Python's standard library. If you think you know better, you can disable the check by setting Cython.Compiler.Options.error_on_unknown_names = False Stefan
participants (3)
-
Robert Bradshaw -
Stefan Behnel -
Xuancong Wang