Discussion:
How to disable assert statements inside a module?
Skip Montanaro
2002-03-10 21:09:29 UTC
Permalink
Pearu> So, I would like to disable the assert statements in one module
Pearu> while in others, that are under development, assert statements
Pearu> are enabled. Is it possible? I am using Python 2.2.

This comes to mind:

s/assert /pass; #assert/

:-)
--
Skip Montanaro (skip at pobox.com - http://www.mojam.com/)
"As often happens in Net communities, people find all kinds of energy to
compose incisive criticisms of the threads and postings of other members,
when they could be directing that energy towards brilliant new threads and
postings of their own." - Tom Neff
Just van Rossum
2002-03-10 21:55:42 UTC
Permalink
In article <m3henoqh5u.fsf at mira.informatik.hu-berlin.de>,
Is there a way to disable all assert statements inside a module,
and without calling python with -O switch?
You can set __debug__ to 0 on the module level; this will disable
assert statements inside this module.
That's what I thought, too, but you get a SyntaxError in 2.2.

Unhelpfully,
Just
Peter Hansen
2002-03-11 00:50:30 UTC
Permalink
Is there a way to disable all assert statements inside a module,
and without calling python with -O switch?
Why? I have developed a module using lots of assert statements (yes,
they are all relevant). Now when it is finished, I found that
running python with -O switch the speed up is approximately 4 times. I
assume that this is due to disabled assert statements. I don't want to
remove all these assert statements because I might extend this module in
future when they will be handy again for debugging.
You might look into the Test-Driven Development approach espoused
by Extreme Programming (XP) proponents. Using a proper set of
unit tests is probably going to be a better approach than scattering
asserts throughout your code like that, and you would benefit from
higher performance even without -O, an improved design, and less
need to debug at that level when you extend the module.

Just a thought, but I've never seen code that had so many relevant assert
statements that the program slowed to a quarter normal speed because of
them.

-Peter
Martin v. Loewis
2002-03-10 20:52:45 UTC
Permalink
Is there a way to disable all assert statements inside a module,
and without calling python with -O switch?
You can set __debug__ to 0 on the module level; this will disable
assert statements inside this module.

Regards,
Martin
Bengt Richter
2002-03-11 00:41:56 UTC
Permalink
[Pearu Peterson]
Is there a way to disable all assert statements inside a module,
and without calling python with -O switch?
[Martin v. Loewis]
You can set __debug__ to 0 on the module level; this will disable
assert statements inside this module.
SyntaxWarning: can not assign to __debug__
SyntaxError: can not assign to __debug__
-O is the only way left. Guido often writes code under the control of an
explicit "debug" vrbl instead. A more flexible way to control asserts would
be (IMO) prime PEP material (as a heavy assert user myself, I sympathize
with Pearu's dilemma -- "all or nothing everywhere" is too crude for large
projects with many subsystems).
Controlling logging would also be handy. I.e., not just controlling its execution,
but including/excluding it from compiled code.

Play with the idea of a built-in compiler line filter based on name token matching:

E.g., the compiler could include or exclude lines with the name token 'assert'
by default, depending on allow/deny config data set up or overridden in site.py,
and possibly overridable within a module scope.

This could be controlled globally also by command line option.

You could exclude whole imports by filtering on the module name, and thus exclude
the import effects. By suitable naming (even just using modulename.xxx for
all uses, and isolating them on single lines), you could leave a lot of
cruft in the source and not suffer performance hits in execution of .pyc's.

Conventions for default exclusions of enableable standard modules (and calls using
them) like the proposed logging module could be established.

Regards,
Bengt Richter
Pearu Peterson
2002-03-10 21:23:39 UTC
Permalink
Post by Skip Montanaro
Pearu> So, I would like to disable the assert statements in one module
Pearu> while in others, that are under development, assert statements
Pearu> are enabled. Is it possible? I am using Python 2.2.
s/assert /pass; #assert/
:-)
Thanks :-)

I now tried that. It turns out that it does not help much. python -O must
do something more than just disabling assert statements. So, let me
rephrase my question:

Is it possible to turn on the optimization in one module while
in others it is off (so that their assert statements are in effect)?

Pearu
Pearu Peterson
2002-03-10 20:06:44 UTC
Permalink
Hi,

Is there a way to disable all assert statements inside a module,
and without calling python with -O switch?

Why? I have developed a module using lots of assert statements (yes,
they are all relevant). Now when it is finished, I found that
running python with -O switch the speed up is approximately 4 times. I
assume that this is due to disabled assert statements. I don't want to
remove all these assert statements because I might extend this module in
future when they will be handy again for debugging.

Next, I want to use this module from other modules that use it
extensively. But then, I would expect a remarkable slow down because of
the irrelevant assert statements in the first module.

So, I would like to disable the assert statements in one module while in
others, that are under development, assert statements are enabled.
Is it possible? I am using Python 2.2.

Thanks,
Pearu
Tim Peters
2002-03-10 21:17:01 UTC
Permalink
[Pearu Peterson]
Is there a way to disable all assert statements inside a module,
and without calling python with -O switch?
[Martin v. Loewis]
You can set __debug__ to 0 on the module level; this will disable
assert statements inside this module.
Note that assignment to __debug__ was deprecated in 2.1:

SyntaxWarning: can not assign to __debug__

and removed in 2.2:

SyntaxError: can not assign to __debug__

-O is the only way left. Guido often writes code under the control of an
explicit "debug" vrbl instead. A more flexible way to control asserts would
be (IMO) prime PEP material (as a heavy assert user myself, I sympathize
with Pearu's dilemma -- "all or nothing everywhere" is too crude for large
projects with many subsystems).
Martin v. Loewis
2002-03-11 05:29:10 UTC
Permalink
SyntaxWarning: can not assign to __debug__
SyntaxError: can not assign to __debug__
Of course,

def foo(x):
assert x

globals()['__debug__'] = 0
foo(0)

continues to work.

Regards,
Martin

Loading...