[Cython] unsupported meta-programming-related features

Robert Bradshaw robertwb at gmail.com
Thu Apr 21 01:51:51 EDT 2016


On Tue, Apr 19, 2016 at 11:22 PM, Xuancong Wang <xuancong84 at 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 at gmail.com> wrote:
> > On Tue, Apr 19, 2016 at 2:13 AM, Xuancong Wang <xuancong84 at 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 at python.org
> > https://mail.python.org/mailman/listinfo/cython-devel
> >
> _______________________________________________
> cython-devel mailing list
> cython-devel at python.org
> https://mail.python.org/mailman/listinfo/cython-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cython-devel/attachments/20160420/d66bee41/attachment.html>


More information about the cython-devel mailing list