[Python-Dev] Release of astoptimizer 0.3
victor.stinner at gmail.com
Tue Sep 11 23:46:19 CEST 2012
2012/9/11 Nick Coghlan <ncoghlan at gmail.com>:
> This is fine in an external project, but should never be added to the
> standard library. The barrier to semantic changes that break
> monkeypatching should be high.
The version 0.3 has a known bug: "len=chr; print(len('A'))" is
optimized, whereas it should not. It is now fixed in the new version
>> * Call methods of builtin types if the object and arguments are constants.
>> - u"h\\xe9ho".encode("utf-8") => b"h\\xc3\\xa9ho"
>> - "python2.7".startswith("python") => True
>> - (32).bit_length() => 6
>> - float.fromhex("0x1.8p+0") => 1.5
> That last one isn't constant, it's a name lookup.
Well, yes, but in CPython, it is not possible to modify float.fromhex
attribute, nor unicode.encode.
>> - print(1.5) => print("1.5")
> The print example runs afoul of the general rule above: not in the
> standard library, because you're changing the values seen by a mocked
> version of print()
Ah yes, you found a bug. I forgot to disable this optimization by
default (when builtin_funcs feature is disabled). Fixed in 0.3.1.
>> - not(x in y) => x not in y
> This (and the "is") equivalent should be OK
Note: not(x is y) is also implemented.
>> * Loop: replace range() with xrange() on Python 2, and list with
>> tuple. Examples:
>> - for x in range(n): ... => for x in xrange(n): ...
>> - for x in [1, 2, 3]: ... => for x in (1, 2, 3): ...
> Name lookup optimisations again: not in the standard library.
Correct, same issue than print(): forgot in version 0.3, and also
fixed in 0.3.1.
> def f(): return 1; yield
> if DEBUG: yield
> while 0: yield
I'm using the test suite of Python 2.7 and 3.3 using my optimizer. "if
0: yield" is a known captcha (there is a test) and it is handled
correctly by astoptimizer.
By the way, "return 3" is not removed in a generator because it must
raise a SyntaxError.
>>>> def f():
> ... if 0:
> ... global x
> ... return x
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "<stdin>", line 4, in f
> NameError: global name 'x' is not defined
Oh, nice catch. But it is possible to detect the usage of global and
nonlocal, and don't remove an expression if they are present. Fixed in
More information about the Python-Dev