python assert fires with -O -
i trying make sure assert not executed python when using -o. test program indicates executed. use -o on command line , used -o when ran setup.py both build , install. before submit bug report wanted make sure did not rookie mistakes...
so need else or different assert not executed?
my simple script:
print __debug__ if __debug__: print "true branch" else: print "false branch" assert(false)
works when run standalone. prints:
false false branch
when copy snippet in main program (that cannot include here...) get:
false true branch assertionerror
i utterly confused how can come about. python 2.7.6 on mac. (sorry mac, have use work.)
you can achieve effect describe running .pyc
file directly -o
flag. that's abuse of way things supposed work. want either:
- run
.py
file or without-o
flag (the usual approach), or - run
.pyc
file without-o
flag, or - run
.pyo
file with-o
flag.
if run .pyc
file -o
flag, or .pyo
file without it, you're going surprises one.
what's happening @ compile time peephole optimiser has optimised away if __debug__
branches, .pyc
or .pyo
files execute appropriate branch unconditionally. when run wrong -o
specification, you'll running value of __debug__
doesn't match optimisation applied @ compile time.
there similar issue reported on python issue tracker while back, though opposite situation: running .pyo
file without using -o
flag.
a quick example: suppose i've got file yours named "debug_example.py" sitting in current directory:
noether:desktop mdickinson$ cat debug_example.py def main(): print "__debug__ {}".format(__debug__) if __debug__: print "__debug__ true" else: print "__debug__ false" if __name__ == '__main__': main()
if execute file directly, or without -o
flag, see expected results:
noether:desktop mdickinson$ python2 debug_example.py __debug__ true __debug__ true noether:desktop mdickinson$ python2 -o debug_example.py __debug__ false __debug__ false
now let's compile file "debug_example.pyc" file using handy py_compile
module. (in case compilation being performed part of setup.py
installation.):
noether:desktop mdickinson$ python2 -m py_compile debug_example.py noether:desktop mdickinson$ ls -l debug_example.pyc -rw-r--r-- 1 mdickinson staff 350 24 mar 21:41 debug_example.pyc
now execute debug_example.pyc
file, (wrongly) using -o
flag, , python gets confused:
noether:desktop mdickinson$ python2 -o debug_example.pyc __debug__ false __debug__ true
we can use python's dis
module see bytecode inside module:
python 2.7.6 (default, nov 18 2013, 15:12:51) [gcc 4.2.1 compatible apple llvm 5.0 (clang-500.2.79)] on darwin type "help", "copyright", "credits" or "license" more information. >>> import debug_example >>> import dis >>> dis.dis(debug_example) disassembly of main: 2 0 load_const 1 ('__debug__ {}') 3 load_attr 0 (format) 6 load_global 1 (__debug__) 9 call_function 1 12 print_item 13 print_newline 4 14 load_const 2 ('__debug__ true') 17 print_item 18 print_newline 19 load_const 0 (none) 22 return_value
note there's no bytecode corresponding if
statement there @ all: see unconditional printing of '__debug__ true'
.
solution: don't execute .pyc
or .pyo
files directly: execute .py
file , let python figure out whether use .pyc
or .pyo
appropriate.
Comments
Post a Comment