A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2010-July/101856.html below:

[Python-Dev] How to block a module import

[Python-Dev] How to block a module import [Python-Dev] How to block a module importAlexander Belopolsky alexander.belopolsky at gmail.com
Tue Jul 13 02:37:14 CEST 2010
I thought that in order to block a module from being imported, one
would need to assign None to the corresponding entry in sys.modules.
However, it looks like the code in test.support uses 0 instead of
None:

def _save_and_block_module(name, orig_modules):
    """Helper function to save and block a module in sys.modules

       Return value is True if the module was in sys.modules and
       False otherwise."""
    saved = True
    try:
        orig_modules[name] = sys.modules[name]
    except KeyError:
        saved = False
    sys.modules[name] = 0  # <--- I would expect None here
    return saved


In my experiments, 0 is not equivalent to None:

>>> import sys, time
>>> sys.modules['time'] = 0
>>> __import__('time')
0
>>> sys.modules['time'] = None
>>> __import__('time')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named time

 Am I missing something?
More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4