[Python-checkins] r58428 - sandbox/trunk/import_in_py/Py3K/_importlib.py
brett.cannon
python-checkins at python.org
Fri Oct 12 09:17:23 CEST 2007
Author: brett.cannon
Date: Fri Oct 12 09:17:22 2007
New Revision: 58428
Modified:
sandbox/trunk/import_in_py/Py3K/_importlib.py
Log:
Handle the issue of when open() is not defined.
Modified: sandbox/trunk/import_in_py/Py3K/_importlib.py
==============================================================================
--- sandbox/trunk/import_in_py/Py3K/_importlib.py (original)
+++ sandbox/trunk/import_in_py/Py3K/_importlib.py Fri Oct 12 09:17:22 2007
@@ -131,6 +131,63 @@
trimmed_path_len = len(path) - len(ext)
return imp._case_ok(path, trimmed_path_len, module_name)
+def open_(path, flags):
+ """Stand-in replacement for open() in case it is not available yet."""
+ try:
+ raise NameError
+ return open(path, flags)
+ except NameError:
+ return DinkyFile(path, flags)
+
+class DinkyFile(object):
+
+ """Dinky replacement of a file object."""
+
+ def __init__(self, path, flags):
+ self.writing = 'w' in flags
+ self.binary = 'b' in flags
+ if self.writing:
+ posix_flags = _os.O_WRONLY | _os.O_CREAT
+ else:
+ posix_flags = _os.O_RDONLY
+ self.fd = _os.open(path, posix_flags)
+
+ def read(self):
+ if self.writing:
+ raise TypeError("file not opened for reading")
+ hunk_size = 8 * 1024
+ read_out = bytes()
+ while True:
+ just_read = _os.read(self.fd, hunk_size)
+ if not just_read:
+ break
+ read_out.extend(just_read)
+ if len(just_read) < hunk_size:
+ break
+ if self.binary:
+ return read_out
+ else:
+ return read_out.decode("utf8")
+
+ def write(self, data):
+ if not self.writing or not self.binary:
+ raise TypeError("file not opened for writing binary")
+ _os.write(self.fd, data)
+
+ def close(self):
+ if hasattr(self, 'fd') and self.fd != -1:
+ _os.close(self.fd)
+ self.fd = -1
+
+ def __del__(self):
+ self.close()
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *args):
+ self.close()
+
class _BuiltinFrozenBaseImporter(object):
@@ -482,7 +539,7 @@
"""
source_path = self._source_path()
if source_path:
- return open(source_path, 'U').read()
+ return open_(source_path, 'U').read()
elif self._bytecode_path():
return None
else:
@@ -498,7 +555,7 @@
"""
try:
- with open(self._bytecode_path(), 'rb') as bytecode_file:
+ with open_(self._bytecode_path(), 'rb') as bytecode_file:
data = bytecode_file.read()
return data[:4], marshal._r_long(data[4:8]), data[8:]
except AttributeError:
@@ -518,7 +575,7 @@
if not bytecode_path:
bytecode_path = self._base_path + suffix_list(imp.PY_COMPILED)[0]
try:
- with open(bytecode_path, 'wb') as bytecode_file:
+ with open_(bytecode_path, 'wb') as bytecode_file:
bytecode_file.write(imp.get_magic())
bytecode_file.write(marshal._w_long(timestamp))
bytecode_file.write(data)
@@ -531,7 +588,7 @@
def get_data(self, path):
"""Return the data from path as raw bytes."""
- return open(path, 'rb').read()
+ return open_(path, 'rb').read()
@check_name
def is_package(self, fullname):
More information about the Python-checkins
mailing list