[Python-ideas] parser in stdlib

Aaron Brady castironpi at comcast.net
Fri May 11 01:11:47 CEST 2007


> -----Original Message-----
> From: python-ideas-bounces at python.org [mailto:python-ideas-
> bounces at python.org] On Behalf Of Aaron Brady
> Sent: Thursday, May 10, 2007 6:01 PM
> 
> > -----Original Message-----
> > From: Collin Winter [mailto:collinw at gmail.com]
> > Sent: Thursday, May 10, 2007 5:57 PM
> >
> > On 5/10/07, Aaron Brady <castironpi at comcast.net> wrote:
> > > > -----Original Message-----
> > > > From: python-ideas-bounces at python.org [mailto:python-ideas-
> > > > bounces at python.org] On Behalf Of Adam Atlas
> > > > Sent: Thursday, May 10, 2007 5:47 PM
> > > >
> > > > On 10 May 2007, at 18.39, Aaron Brady wrote:
> > > > > There is, but I want access to it.
> > > > >>>> import parser
> > > > >>>> parser.__file__
> > > > > Traceback (most recent call last):
> > > > >   File "<stdin>", line 1, in <module>
> > > > > AttributeError: 'module' object has no attribute '__file__'
> > > >
> > > > That's because it's a builtin module, written in C. It's Python/
> > > > parsermodule.c in the source distributions.
> > > >
> > > > What exactly are you suggesting should be possible? Are you trying
> to
> > > > programmatically change the parser?
> > >
> > > Yes.  The relevant code snippet:
> > >
> > > class Transformer:
> > >     <snip>
> > >     def parsesuite(self, text):
> > >         """Return a modified parse tree for the given suite text."""
> > >         return self.transform(parser.suite(text))
> > >
> > > Hence to allusion to Van Rossum's note, for the record.
> >
> > That is not and will not be possible using Python's built-in parser.
> >
> > Collin Winter
> 
> I'm 25, FTR, heavy experience.  Why not?  PEP or FAQ if there is one.
> 
> Aaron Brady
> 
I'd also like to get an attribute in class objects containing the first line
number, and can't see why not.  Code objects have them; new.code() requires
them.  I've treated dynamic creation on the newsgroup, and
inspect.findsource(object):

    if isclass(object):
        name = object.__name__
        pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
        # make some effort to find the best matching class definition:
        # use the one with the least indentation, which is the one
        # that's most probably not inside a function definition.
        candidates = []

cleans up quite a bit.

Aaron Brady




More information about the Python-ideas mailing list