berkerpeksag / astor

Python AST read/write
https://pypi.org/project/astor/
BSD 3-Clause "New" or "Revised" License
810 stars 102 forks source link

Add a utility function for getting filename info from code objects #29

Closed berkerpeksag closed 9 years ago

berkerpeksag commented 9 years ago

I think it would be useful to have https://github.com/berkerpeksag/astor/blob/master/astor/misc.py#L185-190 as an helper in astor.

pmaupin commented 9 years ago

Maybe something like

    def finfo(codeobj):
        """Returns the file and line number of a code object.
            If the code object has a __file__ attribute (e.g. if
            it is a module), then the returned line number will
            be 0
        """
        fname = getattr(codeobj, '__file__', None)
        linenum = 0
        if fname is None:
            func_code = codeobj.__code__
            fname = func_code.co_filename
            linenum = func_code.co_firstlineno
        fname = fname.replace('.pyc', '.py')
        return fname, linenum
berkerpeksag commented 9 years ago

Yep, looks good!

pmaupin commented 9 years ago

I'll add it. I think I'll add a couple of things for anti8 as well.

Thanks, Pat

berkerpeksag commented 9 years ago

Thanks! Can you also add documentation for the new function?

pmaupin commented 9 years ago

I'll try to figure that out :)

pmaupin commented 9 years ago

Done in the code cleanup.