[Python-checkins] r56200 - in sandbox/trunk/import_in_py: _importlib.py importlib.py tests/mock_importlib.py tests/test_py_handler.py tests/test_regression.py
brett.cannon
python-checkins at python.org
Sun Jul 8 22:53:11 CEST 2007
Author: brett.cannon
Date: Sun Jul 8 22:53:11 2007
New Revision: 56200
Modified:
sandbox/trunk/import_in_py/_importlib.py
sandbox/trunk/import_in_py/importlib.py
sandbox/trunk/import_in_py/tests/mock_importlib.py
sandbox/trunk/import_in_py/tests/test_py_handler.py
sandbox/trunk/import_in_py/tests/test_regression.py
Log:
Move hand-coded functions that should really come from C version out to
importlib.
Modified: sandbox/trunk/import_in_py/_importlib.py
==============================================================================
--- sandbox/trunk/import_in_py/_importlib.py (original)
+++ sandbox/trunk/import_in_py/_importlib.py Sun Jul 8 22:53:11 2007
@@ -63,76 +63,11 @@
_required_builtins = ['imp', 'sys', 'marshal']
-def _set__import__():
- """Set __import__ to an instance of Import."""
- global original__import__
- original__import__ = __import__
- __builtins__['__import__'] = Import()
-
-
-def _reset__import__():
- """Set __import__ back to the original implementation (assumes
- _set__import__ was called previously)."""
- __builtins__['__import__'] = original__import__
-
-
-def _w_long(x):
- """Convert a 32-bit integer to little-endian.
-
- XXX Temporary until marshal's long functions are exposed.
-
- """
- bytes = []
- bytes.append(x & 0xFF)
- bytes.append((x >> 8) & 0xFF)
- bytes.append((x >> 16) & 0xFF)
- bytes.append((x >> 24) & 0xFF)
- return ''.join(chr(x) for x in bytes)
-
-
-def _r_long(bytes):
- """Convert 4 bytes in little-endian to an integer.
-
- XXX Temporary until marshal's long function are exposed.
-
- """
- x = ord(bytes[0])
- x |= ord(bytes[1]) << 8
- x |= ord(bytes[2]) << 16
- x |= ord(bytes[3]) << 24
- return x
-
-
def _path_join(*args):
"""Replacement for os.path.join so as to remove dependency on os module."""
return path_sep.join(args)
-def _case_ok(directory, file_name):
- """Verify that file_name (as found in 'directory') has the proper case.
-
- The path is assumed to already exist.
-
- XXX Temporary until imp's case_ok function can be exposed.
-
- XXX Better to roll this into a single function some how so that existence
- check can be part of case check and thus cut down on stat calls?
-
- """
- # If platform is not case-sensitive *or* the environment variable
- # PYTHONCASEOK is defined, then os.path.exists already handled the case by
- # either doing a case-sensitive check or from the user saying he does not
- # want case-sensitivity, respectively.
- if sys.platform not in ('win32', 'mac', 'riscos', 'darwin', 'cygwin',
- 'os2emx') or os.environ.get('PYTHONCASEOK'):
- return True
- directory_contents = os.listdir(directory)
- if file_name in directory_contents:
- return True
- else:
- return False
-
-
class _BuiltinFrozenBaseImporter(object):
"""Base class for meta_path importers for built-in and frozen modules.
Modified: sandbox/trunk/import_in_py/importlib.py
==============================================================================
--- sandbox/trunk/import_in_py/importlib.py (original)
+++ sandbox/trunk/import_in_py/importlib.py Sun Jul 8 22:53:11 2007
@@ -30,6 +30,72 @@
"""
import _importlib
+#XXX Temporary functions that should eventually be removed.
+def _set__import__():
+ """Set __import__ to an instance of Import."""
+ global original__import__
+ original__import__ = __import__
+ __builtins__['__import__'] = Import()
+
+
+def _reset__import__():
+ """Set __import__ back to the original implementation (assumes
+ _set__import__ was called previously)."""
+ __builtins__['__import__'] = original__import__
+
+
+def _w_long(x):
+ """Convert a 32-bit integer to little-endian.
+
+ XXX Temporary until marshal's long functions are exposed.
+
+ """
+ bytes = []
+ bytes.append(x & 0xFF)
+ bytes.append((x >> 8) & 0xFF)
+ bytes.append((x >> 16) & 0xFF)
+ bytes.append((x >> 24) & 0xFF)
+ return ''.join(chr(x) for x in bytes)
+
+
+def _r_long(bytes):
+ """Convert 4 bytes in little-endian to an integer.
+
+ XXX Temporary until marshal's long function are exposed.
+
+ """
+ x = ord(bytes[0])
+ x |= ord(bytes[1]) << 8
+ x |= ord(bytes[2]) << 16
+ x |= ord(bytes[3]) << 24
+ return x
+
+
+def _case_ok(directory, file_name):
+ """Verify that file_name (as found in 'directory') has the proper case.
+
+ The path is assumed to already exist.
+
+ XXX Temporary until imp's case_ok function can be exposed.
+
+ XXX Better to roll this into a single function some how so that existence
+ check can be part of case check and thus cut down on stat calls?
+
+ """
+ # If platform is not case-sensitive *or* the environment variable
+ # PYTHONCASEOK is defined, then os.path.exists already handled the case by
+ # either doing a case-sensitive check or from the user saying he does not
+ # want case-sensitivity, respectively.
+ if sys.platform not in ('win32', 'mac', 'riscos', 'darwin', 'cygwin',
+ 'os2emx') or os.environ.get('PYTHONCASEOK'):
+ return True
+ directory_contents = os.listdir(directory)
+ if file_name in directory_contents:
+ return True
+ else:
+ return False
+
+
# Import needed built-in modules.
for builtin_name in _importlib._required_builtins:
module = __import__(builtin_name)
@@ -41,9 +107,13 @@
import os
import warnings
+_importlib._r_long = _r_long #XXX Expose original from marshal.
+_importlib._w_long = _w_long #XXX Expose original from marshal.
+_importlib._case_ok = _case_ok #XXX Expose original from imp.
+_importlib.path_sep = os.sep # For os.path.join replacement.
+
_importlib.errno = errno
_importlib.os = os
-_importlib.path_sep = os.sep # For os.path.join replacement.
_importlib.warnings = warnings
del _importlib
Modified: sandbox/trunk/import_in_py/tests/mock_importlib.py
==============================================================================
--- sandbox/trunk/import_in_py/tests/mock_importlib.py (original)
+++ sandbox/trunk/import_in_py/tests/mock_importlib.py Sun Jul 8 22:53:11 2007
@@ -2,7 +2,7 @@
import marshal
import imp
from test import test_support
-from _importlib import _w_long
+from importlib import _w_long
def log_call(method):
"""Log method calls to self.log."""
Modified: sandbox/trunk/import_in_py/tests/test_py_handler.py
==============================================================================
--- sandbox/trunk/import_in_py/tests/test_py_handler.py (original)
+++ sandbox/trunk/import_in_py/tests/test_py_handler.py Sun Jul 8 22:53:11 2007
@@ -4,7 +4,7 @@
from tests import mock_importlib
from tests.py_help import TestPyPycFiles
-from _importlib import _r_long
+from importlib import _r_long
import imp
import marshal
Modified: sandbox/trunk/import_in_py/tests/test_regression.py
==============================================================================
--- sandbox/trunk/import_in_py/tests/test_regression.py (original)
+++ sandbox/trunk/import_in_py/tests/test_regression.py Sun Jul 8 22:53:11 2007
@@ -2,7 +2,7 @@
from tests import mock_importlib
from tests.py_help import TestPyPycPackages
-from _importlib import _r_long
+from importlib import _r_long
import imp
import marshal
More information about the Python-checkins
mailing list