Detect from a running python script if the optimize flag is -O or -OO
Asked Answered
D

1

14

Sometime I'd like to spawn a child process with the same optimization flags used to start the parent.

I can use something like:

optimize = not __debug__

But this way I match both -O and -OO flags.

Is there some python internal status that contains that info?

Delphinedelphinia answered 26/6, 2013 at 10:40 Comment(0)
D
14

After some digging in the documentation I've found that the sys.flags struct sequence (http://docs.python.org/dev/library/sys#sys.flags) that has an optimize attribute containing the information I was searching for.

python -c "import sys; print sys.flags.optimize" -> 0

python -O -c "import sys; print sys.flags.optimize" -> 1

python -OO -c "import sys; print sys.flags.optimize" -> 2

Delphinedelphinia answered 26/6, 2013 at 10:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.