[Python-3000] Brainstorming: literal construction hooks
Nick Coghlan
ncoghlan at gmail.com
Sat Apr 22 16:28:06 CEST 2006
Guido van Rossum wrote:
> On 4/22/06, Nick Coghlan <ncoghlan at gmail.com> wrote:
>> Michael Urman wrote:
>>> There's a lot of holes to this story, including at least how these
>>> functions are registered, and which additional arguments (if any) are
>>> necessary. Shall we try to fill these holes in?
>> Answering without checking the source (which is undoubtedly a bad idea), but
>> this sounds like something else that could be addressed if it was possible to
>> either register an alternate AST compiler for a scope, or else get hold of an
>> AST and recompile it. (The former works for modules and functions, the latter
>> works only for functions)
>>
>> Even if it was only a matter of some additional keyword arguments to compile
>> and/or exec, it could at least be of benefit for plugin code or an interactive
>> interpreter loop.
>
> Hm... Using the AST seems overkill for this (unless you want to do it
> without modifying the Python interpreter in any way).
Strawman. . .
from ast import Compiler
from decimal import Decimal
class MyCompiler(Compiler):
def build_float(self, literal):
return Decimal(literal)
# Pass this to compile or exec via an ast_compiler argument
# It may even be possible to permit it as an argument to __import__
The basic idea would be to have a compiler object that delegated compilation
operations to the internal AST compiler, but gave subclasses the ability to
easily override certain aspects of the process - like processing different
kinds of literal.
No matter what, permitting these kinds of hooks is going to require
alterations to the process of compiling the AST to the bytecode - and
subclassing is an excellent way of allowing some aspects of an implementation
to be overridden while leaving other aspects alone.
This is just an idea, as I doubt I'd ever use the ability no matter how it was
implemented :)
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
http://www.boredomandlaziness.org
More information about the Python-3000
mailing list