1

If I define my own debugging module with functions that should only get executed while developing, is there a way to completely disable those functions when I'm ready to release the final version?

I was hoping there was a clever way to have the environment completely skip over the function calls during the byte-code conversion? I searched for this, but may be using the wrong search inputs.

I'm developing an add-on for Blender, so I don't believe I have any control over the compilation or conversion.

Robert
  • 413
  • 4
  • 12
  • Wat do yo mean by a "debugigng module" and skipping function calls. What sort of functions are you referring to that are only debugging? Like tests? Maybe offer some code examples. – RightmireM Oct 19 '19 at 12:46
  • Yeah, extensive testing calls. Making sure parameters have expected values, comparing data, trying to prevent myself from making dumb mistakes (a pattern I seem to repeat). – Robert Oct 19 '19 at 12:51
  • Maybe look into the Python [unittest](https://docs.python.org/3/library/unittest.html) module. It probably has the capability to do what you need. – RightmireM Oct 19 '19 at 12:54
  • Sorry, by skipping function calls, I mean something such as converting the function calls into comments. Something like that would work. I will take a look at that. Thanks – Robert Oct 19 '19 at 12:55

1 Answers1

2

Yes, Python has a -O flag, which means:

Remove assert statements and any code conditional on the value of __debug__

So basically if you write your debug code using either assert statements, or you check the value of __debug__ before running your debugging functions, you can use -O to switch on a production mode:

if __debug__:
    run_debug_function()

You can also enable optimization by setting the PYTHONOPTIMIZE environment variable to some non-empty string, e.g. export PYTHONOPTIMIZE=1 in your shell.

For more info refer to the Python command line documentation

Migwell
  • 18,631
  • 21
  • 91
  • 160
  • This would be great. I just need to figure out if I can turn `__debug__` on or off in Blender. Thanks. – Robert Oct 19 '19 at 12:59
  • 1
    It seems that you might be able to set `PYTHONOPTIMIZE` in your shell before you run Blender, as in this answer: https://stackoverflow.com/a/46025811/2148718 – Migwell Oct 19 '19 at 13:02
  • 1
    Actually this is a more relevant answer: https://stackoverflow.com/a/44060884/2148718 – Migwell Oct 19 '19 at 13:03