moduleFromGrammar coupled to parse tree

Bug #928486 reported by Robert Collins
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
PyMeta
Fix Committed
Undecided
Unassigned

Bug Description

Which is fine, but it would rock if there was a
moduleFromSource(...)

and moduleFromGrammar became:

return moduleFromSource(writePython(tree), className, superclass, globalsDict)

(I've had to write nearly the identical function, for a very similar reason, for a language implemented via pymeta).

Revision history for this message
Robert Collins (lifeless) wrote :

Oh, it would need modname, filename and the superclasss bits generalised too for purity. But not for practicality ;)

Revision history for this message
Robert Collins (lifeless) wrote :

def moduleFromSource(source, className, superclass=None, globalsDict=None,
    registerModule=True):
    modname = "pymeta_grammar__" + className
    filename = "/pymeta_generated_code/" + modname + ".py"
    mod = module(modname)
    if globalsDict:
        mod.__dict__.update(globalsDict)
    mod.__name__ = modname
    if superclass:
        mod.__dict__[superclass.__name__] = superclass
        mod.__dict__["GrammarBase"] = superclass
    mod.__loader__ = GeneratedCodeLoader(source)
    code = compile(source, filename, "exec")
    eval(code, mod.__dict__)
    fullGlobals = dict(getattr(mod.__dict__[className], "globals", None) or {})
    fullGlobals.update(globalsDict)
    mod.__dict__[className].globals = fullGlobals
    if registerModule:
        sys.modules[modname] = mod
    linecache.getlines(filename, mod.__dict__)
    return mod.__dict__[className]

This is a sketch - it is what I'm using. Note that the modname and filename are not parameterised - functionally, it all works :)

Revision history for this message
Robert Collins (lifeless) wrote :

hah, I lie. I need one further change - a filename decoupled from the entry point I think; my language has a bunch of anonymous closures I'm compiling separately, so they all walk over each other atm. Any of the obvious fixes would do :)

Revision history for this message
Allen Short (washort) wrote :
Changed in pymeta:
status: New → Fix Committed
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.