Announcing bytecodehacks 0.10

Michael Hudson mwh21 at cam.ac.uk
Thu May 13 07:07:54 EDT 1999


Michael Scharf <Michael.Scharf at kosmos.rhein-neckar.de> writes:
> Michael Hudson wrote:
> > 
> > If you've been following the "Python is too slow" thread of late, you
> > might have noticed my sneaky bytecode rewriting functions.
> 
> But all this does *not* work for JPython I think.

No, I very much doubt it. JPython compiles to Java bytecodes doesn't
it? Anyway I doubt the runtime permits functions to play with
bytecodes on the fly given Java's security concerns. These functions
are comprehensively not goint to work under RExec either, and fair
enough too - it's quite easy to core Python with them (try inserting a
LOAD_CONST(30000) somewhere).

> The most interesting functionality (for me), is hidden in 
> bytecodehacks.closure:
> 
>   bind
>   bind_locals
>   bind_now
> 
> With those methods (it seems), I can speedup my functions.
> From the documentation it's not clear to me, which one to use.
> What should I do, if I just want to "burn" the globals and 
> built-ins into my function?

Oh yeah, builtins. The live off in a separate namespace. I'd forgotten
that.

For now try this:

func=apply(bind,(func,),globals())
func=apply(bind,(func,),__builtins__.__dict__)

If you want to bind things like "string.split" you'll need to use the
attr_freeze module.

It's used like this:

def f(s):
    return string.split(s)

_ = attr_freeze.Ref()

f = attr_freeze(f,_.string.split,string.split)

which, yes, is something of an odd interface.

Maybe I could write a function which looks for global references that
resolve to builtins functions and freeze them. That might work nicely.

> Looks like great work.
> 

Thanks!

Michael






More information about the Python-list mailing list