Conditional Compilation for bytecode?

Shae Erisson shapr at uab.edu
Wed Jun 21 15:08:40 EDT 2000


Don wrote:
> 
> I have some code that needs to run on Win32 and IRIX.
> 
> I thought I read somewhere that python had a sort of conditional
> compilation that would generate different .pyc files depending on the
> directives in the .py file.
> 
> i.e.
> #ifdef WIN32
> CreateProcess(...)
> #else UNIX
> fork()
> #endif
> 
> But I can't seem to find this anywhere.  Do I need to do this?

Nope, you don't need this, UNLESS your code uses calls that are platform
specific, then you have to deal with it yourself :) This means
os.system('ls') will most likely work on a *nix box, but almost never on
a win32 box.
Very little of Python changes from OS to OS. There are platform specific
modules like win32com and the irix GL stuff which obviously won't work
on another platform.

> Does the byte code compiler optimize this so it doesn't check every
> time?

Yup, it checks. If you haven't modified the python source since the last
time the python source was compiled, then python uses the bytecode
compiled file. bytecode compiled files are .pyc files.

The conditional bytecode compilation that I'm aware of has only to do
with optimization. python -O removes assert statements and if __debug__:
blocks, python -OO removes assert, __debug__, and docstrings. As I
remember, python compiles source to .pyo files when -O or -OO is used.
-- 
sHae mAtijs eRisson (sHae at wEbwitchEs.coM) gEnius fOr hIre
   bRing mE fIve sQuirrels aNd nO oNe wIll gEt hUrt
13:00pm up 1 season, 1 squirrel, load average: 1+1j 0.5+2j 0.2+0.5j



More information about the Python-list mailing list