[Zope-Coders] module-scope conditionals
Chris McDonough
chrism at zopemafia.com
Wed Oct 22 17:21:50 EDT 2003
As I've been finding out during the gradual shift over to a
ZConfig-configured Zope, module scope conditionals are pretty
inflexible. For example, at module scope in ZopeSecurityPolicy.py:
if os.environ.get("ZOPE_SECURITY_POLICY", None) == "PYTHON":
_use_python_impl = 1
else:
try:
# C Optimization:
from cAccessControl import ZopeSecurityPolicy
from SimpleObjectPolicies import _noroles
except ImportError:
# Fall back to Python implementation.
_use_python_impl = 1
if _use_python_impl:
from types import StringType
...
The problem here is that there is some dynamicism that happens at import
time conditioned directly on the existence of an environment variable.
Unfortunately, there are many cases where you probably don't want this
configuration to happen at import time (such as when you want to use a
different configuration mechanism than an envvar). Coupled with the
fact that Zope's import graph is pretty unpredictable, there are timing
issues that make module-scope conditionals really bad. I might be able
to leave this one alone, but then again, I might not... not sure yet.
This example is just one of quite a few that are in Zope right now. I'm
going to try to stamp them out as I see them, but I'd suggest that we
encourage a coding style that frowns on dynamicism that happens at
import time in the future...
- C
More information about the Zope-Coders
mailing list