[Python-Dev] Freeze hacks
Thomas Heller
thomas.heller@ion-tof.com
Wed, 18 Jul 2001 18:10:24 +0200
From: "Martin von Loewis" <loewis@informatik.hu-berlin.de>
> > Very good idea IMO, but 'if 0:' is optimized away.
>
> I'm not sure I understand. freeze does not optimize away such a code
> block. Under which condition is that optimized away?
>
The Python compiler itself. 'if 0: import whatever' does
not generate any byte code. Modulefinder (used by freeze,
py2exe, and Gordon's installer) checks the compiled byte code
for import statements.
Thomas
C:\Python21>c:\python21\python.exe
ActivePython 2.1, build 211 (ActiveState)
based on Python 2.1 (#15, Jun 18 2001, 21:42:28) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import dis
>>> _FAKE=0
>>> def f():
... if 0:
... import win32api
...
>>> def g():
... if _FAKE:
... import win32api
...
>>> dis.dis(f)
0 SET_LINENO 1
3 SET_LINENO 2
6 LOAD_CONST 0 (None)
9 RETURN_VALUE
>>> dis.dis(g)
0 SET_LINENO 1
3 SET_LINENO 2
6 LOAD_GLOBAL 0 (_FAKE)
9 JUMP_IF_FALSE 16 (to 28)
12 POP_TOP
13 SET_LINENO 3
16 LOAD_CONST 0 (None)
19 IMPORT_NAME 1 (win32api)
22 STORE_FAST 0 (win32api)
25 JUMP_FORWARD 1 (to 29)
>> 28 POP_TOP
>> 29 LOAD_CONST 0 (None)
32 RETURN_VALUE
>>> ^Z