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 <rober...@gmail.com> wrote: > On Tue, Apr 19, 2016 at 2:13 AM, Xuancong Wang <xuancon...@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-global-namespace-or-a-reason-this-is-a-bad-idea > > 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