[Python-3000] Brainstorming: literal construction hooks

Nick Coghlan ncoghlan at gmail.com
Sun Apr 23 04:27:24 CEST 2006


Guido van Rossum wrote:
> On 4/22/06, Nick Coghlan <ncoghlan at gmail.com> wrote:
>> 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.
> 
> Oops, I have to concur.
> 
> I was thinking that it could all be done later, at run-time. That
> makes sense for things like [...] and {...} but doesn't really work
> for floating point or even string literals -- you can't afford the
> overhead of calling a default hook for each literal at run-time, and
> passing the default value to the hook doesn't work in the case of
> floats or strings.
> 
> Unfortunately a compile-time hook is much more painful to add to the
> language because it means adding new syntax just to specify the hooks.
> :-(

This is why I'm suggesting Michael look at the exec/compile plugin execution 
use case first - then we have control code that runs without the hooks that 
can ask for other code to be compiled *with* the hooks. (similar to the way 
the compile statement can already force the use of certain __future__ directives)

I think doing that cleanly is a big task in itself, but if it works out, we'll 
have a much better idea of what compiler directives might be needed so a 
module or function could request a particular compiler.

For example, we may decide that hooking individual bits and pieces from within 
the module is right out, but provide a general "from 
__compiler__.my.pkg.namespace import MyCompiler" directive that tells the 
compiler "retrieve my.pkg.namespace.MyCompiler from the list of registered 
compilers and use it to compile this module rather than the standard compiler".

It would then be up to the application to ensure the appropriate compiler was 
available before importing the module. If you tried to import such a module 
before the appropriate compiler has been registered then you'd get an ImportError.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-3000 mailing list