Author: Matti Picus <[email protected]>
Branch: win32-cleanup2
Changeset: r54964:d409ef556bc6
Date: 2012-05-08 19:46 +0300
http://bitbucket.org/pypy/pypy/changeset/d409ef556bc6/
Log: back out changes to lib-python
diff --git a/lib-python/2.7/ctypes/__init__.py
b/lib-python/2.7/ctypes/__init__.py
--- a/lib-python/2.7/ctypes/__init__.py
+++ b/lib-python/2.7/ctypes/__init__.py
@@ -451,7 +451,6 @@
GetLastError = windll.kernel32.GetLastError
else:
GetLastError = windll.coredll.GetLastError
- GetLastError.argtypes=[]
from _ctypes import get_last_error, set_last_error
def WinError(code=None, descr=None):
diff --git a/lib-python/2.7/mailbox.py b/lib-python/2.7/mailbox.py
--- a/lib-python/2.7/mailbox.py
+++ b/lib-python/2.7/mailbox.py
@@ -619,9 +619,7 @@
"""Write any pending changes to disk."""
if not self._pending:
return
- if self._file.closed:
- self._pending = False
- return
+
# In order to be writing anything out at all, self._toc must
# already have been generated (and presumably has been modified
# by adding or deleting an item).
@@ -749,7 +747,6 @@
"""Return a file-like representation or raise a KeyError."""
start, stop = self._lookup(key)
self._file.seek(start)
-
if not from_:
self._file.readline()
return _PartialFile(self._file, self._file.tell(), stop)
@@ -1821,10 +1818,6 @@
else:
self._pos = pos
- def __del__(self):
- if hasattr(self,'_file'):
- self.close()
-
def read(self, size=None):
"""Read bytes."""
return self._read(size, self._file.read)
@@ -1861,7 +1854,6 @@
def close(self):
"""Close the file."""
- self._file.close()
del self._file
def _read(self, size, read_method):
diff --git a/lib-python/2.7/tarfile.py b/lib-python/2.7/tarfile.py
--- a/lib-python/2.7/tarfile.py
+++ b/lib-python/2.7/tarfile.py
@@ -425,16 +425,10 @@
raise CompressionError("zlib module is not available")
self.zlib = zlib
self.crc = zlib.crc32("") & 0xffffffffL
- try:
- if mode == "r":
- self._init_read_gz()
- else:
- self._init_write_gz()
- except:
- if not self._extfileobj:
- fileobj.close()
- self.closed = True
- raise
+ if mode == "r":
+ self._init_read_gz()
+ else:
+ self._init_write_gz()
if comptype == "bz2":
try:
@@ -1688,14 +1682,13 @@
if filemode not in "rw":
raise ValueError("mode must be 'r' or 'w'")
- fid = _Stream(name, filemode, comptype, fileobj, bufsize)
- try:
- t = cls(name, filemode, fid, **kwargs)
- t._extfileobj = False
- return t
- except:
- fid.close()
- raise
+
+ t = cls(name, filemode,
+ _Stream(name, filemode, comptype, fileobj, bufsize),
+ **kwargs)
+ t._extfileobj = False
+ return t
+
elif mode in "aw":
return cls.taropen(name, mode, fileobj, **kwargs)
@@ -1722,18 +1715,13 @@
gzip.GzipFile
except (ImportError, AttributeError):
raise CompressionError("gzip module is not available")
- gz_fid = None
+
try:
- gz_fid = gzip.GzipFile(name, mode, compresslevel, fileobj)
- t = cls.taropen(name, mode, gz_fid, **kwargs)
+ t = cls.taropen(name, mode,
+ gzip.GzipFile(name, mode, compresslevel, fileobj),
+ **kwargs)
except IOError:
- if gz_fid:
- gz_fid.close()
raise ReadError("not a gzip file")
- except:
- if gz_fid:
- gz_fid.close()
- raise
t._extfileobj = False
return t
@@ -1750,21 +1738,15 @@
except ImportError:
raise CompressionError("bz2 module is not available")
+ if fileobj is not None:
+ fileobj = _BZ2Proxy(fileobj, mode)
+ else:
+ fileobj = bz2.BZ2File(name, mode, compresslevel=compresslevel)
+
try:
- if fileobj is not None:
- bzfileobj = _BZ2Proxy(fileobj, mode)
- else:
- bzfileobj = bz2.BZ2File(name, mode,
compresslevel=compresslevel)
- t = cls.taropen(name, mode, bzfileobj, **kwargs)
-
+ t = cls.taropen(name, mode, fileobj, **kwargs)
except (IOError, EOFError):
- if fileobj is None:
- bzfileobj.close()
raise ReadError("not a bzip2 file")
- except:
- if fileobj is None:
- bzfileobj.close()
- raise
t._extfileobj = False
return t
diff --git a/lib-python/2.7/test/test_mailbox.py
b/lib-python/2.7/test/test_mailbox.py
--- a/lib-python/2.7/test/test_mailbox.py
+++ b/lib-python/2.7/test/test_mailbox.py
@@ -60,8 +60,6 @@
def tearDown(self):
self._box.close()
- if os.name == 'nt':
- time.sleep(0.1) #Allow all syncing to take place
self._delete_recursively(self._path)
def test_add(self):
@@ -139,7 +137,6 @@
msg = self._box.get(key1)
self.assertEqual(msg['from'], 'foo')
self.assertEqual(msg.fp.read(), '1')
- msg.fp.close()
def test_getitem(self):
# Retrieve message using __getitem__()
@@ -172,12 +169,10 @@
# Get file representations of messages
key0 = self._box.add(self._template % 0)
key1 = self._box.add(_sample_message)
- msg0 = self._box.get_file(key0)
- self.assertEqual(msg0.read().replace(os.linesep, '\n'), self._template
% 0)
- msg1 = self._box.get_file(key1)
- self.assertEqual(msg1.read().replace(os.linesep, '\n'),
_sample_message)
- msg0.close()
- msg1.close()
+ self.assertEqual(self._box.get_file(key0).read().replace(os.linesep,
'\n'),
+ self._template % 0)
+ self.assertEqual(self._box.get_file(key1).read().replace(os.linesep,
'\n'),
+ _sample_message)
def test_iterkeys(self):
# Get keys using iterkeys()
@@ -791,8 +786,6 @@
class _TestMboxMMDF(TestMailbox):
def tearDown(self):
- if os.name == 'nt':
- time.sleep(0.1) #Allow os to sync files
self._box.close()
self._delete_recursively(self._path)
for lock_remnant in glob.glob(self._path + '.*'):
@@ -1844,9 +1837,7 @@
self.createMessage("cur")
self.mbox = mailbox.Maildir(test_support.TESTFN)
#self.assertTrue(len(self.mbox.boxes) == 1)
- msg = self.mbox.next()
- self.assertIsNot(msg, None)
- msg.fp.close()
+ self.assertIsNot(self.mbox.next(), None)
self.assertIs(self.mbox.next(), None)
self.assertIs(self.mbox.next(), None)
@@ -1854,9 +1845,7 @@
self.createMessage("new")
self.mbox = mailbox.Maildir(test_support.TESTFN)
#self.assertTrue(len(self.mbox.boxes) == 1)
- msg = self.mbox.next()
- self.assertIsNot(msg, None)
- msg.fp.close()
+ self.assertIsNot(self.mbox.next(), None)
self.assertIs(self.mbox.next(), None)
self.assertIs(self.mbox.next(), None)
@@ -1865,12 +1854,8 @@
self.createMessage("new")
self.mbox = mailbox.Maildir(test_support.TESTFN)
#self.assertTrue(len(self.mbox.boxes) == 2)
- msg = self.mbox.next()
- self.assertIsNot(msg, None)
- msg.fp.close()
- msg = self.mbox.next()
- self.assertIsNot(msg, None)
- msg.fp.close()
+ self.assertIsNot(self.mbox.next(), None)
+ self.assertIsNot(self.mbox.next(), None)
self.assertIs(self.mbox.next(), None)
self.assertIs(self.mbox.next(), None)
@@ -1879,13 +1864,11 @@
import email.parser
fname = self.createMessage("cur", True)
n = 0
- fid = open(fname)
- for msg in mailbox.PortableUnixMailbox(fid,
+ for msg in mailbox.PortableUnixMailbox(open(fname),
email.parser.Parser().parse):
n += 1
self.assertEqual(msg["subject"], "Simple Test")
self.assertEqual(len(str(msg)), len(FROM_)+len(DUMMY_MESSAGE))
- fid.close()
self.assertEqual(n, 1)
## End: classes from the original module (for backward compatibility).
diff --git a/lib-python/2.7/test/test_old_mailbox.py
b/lib-python/2.7/test/test_old_mailbox.py
--- a/lib-python/2.7/test/test_old_mailbox.py
+++ b/lib-python/2.7/test/test_old_mailbox.py
@@ -73,9 +73,7 @@
self.createMessage("cur")
self.mbox = mailbox.Maildir(test_support.TESTFN)
self.assertTrue(len(self.mbox) == 1)
- msg = self.mbox.next()
- self.assertTrue(msg is not None)
- msg.fp.close()
+ self.assertTrue(self.mbox.next() is not None)
self.assertTrue(self.mbox.next() is None)
self.assertTrue(self.mbox.next() is None)
@@ -83,9 +81,7 @@
self.createMessage("new")
self.mbox = mailbox.Maildir(test_support.TESTFN)
self.assertTrue(len(self.mbox) == 1)
- msg = self.mbox.next()
- self.assertTrue(msg is not None)
- msg.fp.close()
+ self.assertTrue(self.mbox.next() is not None)
self.assertTrue(self.mbox.next() is None)
self.assertTrue(self.mbox.next() is None)
@@ -94,12 +90,8 @@
self.createMessage("new")
self.mbox = mailbox.Maildir(test_support.TESTFN)
self.assertTrue(len(self.mbox) == 2)
- msg = self.mbox.next()
- self.assertTrue(msg is not None)
- msg.fp.close()
- msg = self.mbox.next()
- self.assertTrue(msg is not None)
- msg.fp.close()
+ self.assertTrue(self.mbox.next() is not None)
+ self.assertTrue(self.mbox.next() is not None)
self.assertTrue(self.mbox.next() is None)
self.assertTrue(self.mbox.next() is None)
diff --git a/lib-python/2.7/test/test_os.py b/lib-python/2.7/test/test_os.py
--- a/lib-python/2.7/test/test_os.py
+++ b/lib-python/2.7/test/test_os.py
@@ -74,8 +74,7 @@
self.assertFalse(os.path.exists(name),
"file already exists for temporary file")
# make sure we can create the file
- fid = open(name, "w")
- fid.close()
+ open(name, "w")
self.files.append(name)
def test_tempnam(self):
diff --git a/lib-python/2.7/test/test_tarfile.py
b/lib-python/2.7/test/test_tarfile.py
--- a/lib-python/2.7/test/test_tarfile.py
+++ b/lib-python/2.7/test/test_tarfile.py
@@ -340,8 +340,7 @@
# constructor in case of an error. For the test we rely on
# the fact that opening an empty file raises a ReadError.
empty = os.path.join(TEMPDIR, "empty")
- with open(empty, "wb") as fid:
- fid.write("")
+ open(empty, "wb").write("")
try:
tar = object.__new__(tarfile.TarFile)
diff --git a/lib-python/2.7/test/test_zipfile.py
b/lib-python/2.7/test/test_zipfile.py
--- a/lib-python/2.7/test/test_zipfile.py
+++ b/lib-python/2.7/test/test_zipfile.py
@@ -234,9 +234,8 @@
# Read the ZIP archive
with zipfile.ZipFile(f, "r") as zipfp:
- with zipfp.open(TESTFN) as f:
- for line, zipline in zip(self.line_gen, f):
- self.assertEqual(zipline, line + '\n')
+ for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)):
+ self.assertEqual(zipline, line + '\n')
def test_readline_read_stored(self):
# Issue #7610: calls to readline() interleaved with calls to read().
@@ -341,8 +340,7 @@
produces the expected result."""
with zipfile.ZipFile(TESTFN2, "w") as zipfp:
zipfp.write(TESTFN)
- with open(TESTFN) as f:
- self.assertEqual(zipfp.read(TESTFN), f.read())
+ self.assertEqual(zipfp.read(TESTFN), open(TESTFN).read())
@skipUnless(zlib, "requires zlib")
def test_per_file_compression(self):
@@ -384,8 +382,7 @@
self.assertEqual(writtenfile, correctfile)
# make sure correct data is in correct file
- with open(writtenfile, "rb") as fid:
- self.assertEqual(fdata, fid.read())
+ self.assertEqual(fdata, open(writtenfile, "rb").read())
os.remove(writtenfile)
# remove the test file subdirectories
@@ -404,25 +401,24 @@
else:
outfile = os.path.join(os.getcwd(), fpath)
- with open(outfile, "rb") as fid:
- self.assertEqual(fdata, fid.read())
+ self.assertEqual(fdata, open(outfile, "rb").read())
os.remove(outfile)
# remove the test file subdirectories
shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
def test_writestr_compression(self):
- with zipfile.ZipFile(TESTFN2, "w") as zipfp:
- zipfp.writestr("a.txt", "hello world",
compress_type=zipfile.ZIP_STORED)
- if zlib:
- zipfp.writestr("b.txt", "hello world",
compress_type=zipfile.ZIP_DEFLATED)
+ zipfp = zipfile.ZipFile(TESTFN2, "w")
+ zipfp.writestr("a.txt", "hello world",
compress_type=zipfile.ZIP_STORED)
+ if zlib:
+ zipfp.writestr("b.txt", "hello world",
compress_type=zipfile.ZIP_DEFLATED)
- info = zipfp.getinfo('a.txt')
- self.assertEqual(info.compress_type, zipfile.ZIP_STORED)
+ info = zipfp.getinfo('a.txt')
+ self.assertEqual(info.compress_type, zipfile.ZIP_STORED)
- if zlib:
- info = zipfp.getinfo('b.txt')
- self.assertEqual(info.compress_type, zipfile.ZIP_DEFLATED)
+ if zlib:
+ info = zipfp.getinfo('b.txt')
+ self.assertEqual(info.compress_type, zipfile.ZIP_DEFLATED)
def zip_test_writestr_permissions(self, f, compression):
@@ -650,8 +646,7 @@
def test_write_non_pyfile(self):
with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
- with open(TESTFN, 'w') as f:
- f.write('most definitely not a python file')
+ open(TESTFN, 'w').write('most definitely not a python file')
self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
os.remove(TESTFN)
@@ -800,8 +795,7 @@
self.assertRaises(RuntimeError, zipf.open, "foo.txt")
self.assertRaises(RuntimeError, zipf.testzip)
self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
- with open(TESTFN, 'w') as fp:
- fp.write('zipfile test data')
+ open(TESTFN, 'w').write('zipfile test data')
self.assertRaises(RuntimeError, zipf.write, TESTFN)
def test_bad_constructor_mode(self):
@@ -809,6 +803,7 @@
self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q")
def test_bad_open_mode(self):
+ """Check that bad modes passed to ZipFile.open are caught."""
with zipfile.ZipFile(TESTFN, mode="w") as zipf:
zipf.writestr("foo.txt", "O, for a Muse of Fire!")
@@ -856,6 +851,7 @@
def test_comments(self):
"""Check that comments on the archive are handled properly."""
+
# check default comment is empty
with zipfile.ZipFile(TESTFN, mode="w") as zipf:
self.assertEqual(zipf.comment, '')
@@ -957,16 +953,14 @@
with zipfile.ZipFile(TESTFN, mode="w") as zipf:
pass
try:
- with zipfile.ZipFile(TESTFN, mode="r") as zipf:
- pass
+ zipf = zipfile.ZipFile(TESTFN, mode="r")
except zipfile.BadZipfile:
self.fail("Unable to create empty ZIP file in 'w' mode")
with zipfile.ZipFile(TESTFN, mode="a") as zipf:
pass
try:
- with zipfile.ZipFile(TESTFN, mode="r") as zipf:
- pass
+ zipf = zipfile.ZipFile(TESTFN, mode="r")
except:
self.fail("Unable to create empty ZIP file in 'a' mode")
@@ -1166,8 +1160,6 @@
data1 += zopen1.read(500)
data2 += zopen2.read(500)
self.assertEqual(data1, data2)
- zopen1.close()
- zopen2.close()
def test_different_file(self):
# Verify that (when the ZipFile is in control of creating file objects)
@@ -1215,9 +1207,9 @@
def test_store_dir(self):
os.mkdir(os.path.join(TESTFN2, "x"))
- with zipfile.ZipFile(TESTFN, "w") as zipf:
- zipf.write(os.path.join(TESTFN2, "x"), "x")
- self.assertTrue(zipf.filelist[0].filename.endswith("x/"))
+ zipf = zipfile.ZipFile(TESTFN, "w")
+ zipf.write(os.path.join(TESTFN2, "x"), "x")
+ self.assertTrue(zipf.filelist[0].filename.endswith("x/"))
def tearDown(self):
shutil.rmtree(TESTFN2)
@@ -1234,8 +1226,7 @@
for n, s in enumerate(self.seps):
self.arcdata[s] = s.join(self.line_gen) + s
self.arcfiles[s] = '%s-%d' % (TESTFN, n)
- with open(self.arcfiles[s], "wb") as f:
- f.write(self.arcdata[s])
+ open(self.arcfiles[s], "wb").write(self.arcdata[s])
def make_test_archive(self, f, compression):
# Create the ZIP archive
@@ -1304,9 +1295,8 @@
# Read the ZIP archive
with zipfile.ZipFile(f, "r") as zipfp:
for sep, fn in self.arcfiles.items():
- with zipfp.open(fn, "rU") as f:
- for line, zipline in zip(self.line_gen, f):
- self.assertEqual(zipline, line + '\n')
+ for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")):
+ self.assertEqual(zipline, line + '\n')
def test_read_stored(self):
for f in (TESTFN2, TemporaryFile(), StringIO()):
diff --git a/lib-python/2.7/zipfile.py b/lib-python/2.7/zipfile.py
--- a/lib-python/2.7/zipfile.py
+++ b/lib-python/2.7/zipfile.py
@@ -648,10 +648,6 @@
return data
-class ZipExtFileWithClose(ZipExtFile):
- def close(self):
- self._fileobj.close()
-
class ZipFile:
""" Class with methods to open, read, write, close, list zip files.
@@ -847,9 +843,9 @@
try:
# Read by chunks, to avoid an OverflowError or a
# MemoryError with very large embedded files.
- with self.open(zinfo.filename, "r") as f:
- while f.read(chunk_size): # Check CRC-32
- pass
+ f = self.open(zinfo.filename, "r")
+ while f.read(chunk_size): # Check CRC-32
+ pass
except BadZipfile:
return zinfo.filename
@@ -868,9 +864,7 @@
def read(self, name, pwd=None):
"""Return file bytes (as a string) for name."""
- with self.open(name, "r", pwd) as f:
- retval = f.read()
- return retval
+ return self.open(name, "r", pwd).read()
def open(self, name, mode="r", pwd=None):
"""Return file-like object for 'name'."""
@@ -887,66 +881,59 @@
else:
zef_file = open(self.filename, 'rb')
- try:
- # Make sure we have an info object
- if isinstance(name, ZipInfo):
- # 'name' is already an info object
- zinfo = name
+ # Make sure we have an info object
+ if isinstance(name, ZipInfo):
+ # 'name' is already an info object
+ zinfo = name
+ else:
+ # Get info object for name
+ zinfo = self.getinfo(name)
+
+ zef_file.seek(zinfo.header_offset, 0)
+
+ # Skip the file header:
+ fheader = zef_file.read(sizeFileHeader)
+ if fheader[0:4] != stringFileHeader:
+ raise BadZipfile, "Bad magic number for file header"
+
+ fheader = struct.unpack(structFileHeader, fheader)
+ fname = zef_file.read(fheader[_FH_FILENAME_LENGTH])
+ if fheader[_FH_EXTRA_FIELD_LENGTH]:
+ zef_file.read(fheader[_FH_EXTRA_FIELD_LENGTH])
+
+ if fname != zinfo.orig_filename:
+ raise BadZipfile, \
+ 'File name in directory "%s" and header "%s" differ.' % (
+ zinfo.orig_filename, fname)
+
+ # check for encrypted flag & handle password
+ is_encrypted = zinfo.flag_bits & 0x1
+ zd = None
+ if is_encrypted:
+ if not pwd:
+ pwd = self.pwd
+ if not pwd:
+ raise RuntimeError, "File %s is encrypted, " \
+ "password required for extraction" % name
+
+ zd = _ZipDecrypter(pwd)
+ # The first 12 bytes in the cypher stream is an encryption header
+ # used to strengthen the algorithm. The first 11 bytes are
+ # completely random, while the 12th contains the MSB of the CRC,
+ # or the MSB of the file time depending on the header type
+ # and is used to check the correctness of the password.
+ bytes = zef_file.read(12)
+ h = map(zd, bytes[0:12])
+ if zinfo.flag_bits & 0x8:
+ # compare against the file type from extended local headers
+ check_byte = (zinfo._raw_time >> 8) & 0xff
else:
- # Get info object for name
- zinfo = self.getinfo(name)
+ # compare against the CRC otherwise
+ check_byte = (zinfo.CRC >> 24) & 0xff
+ if ord(h[11]) != check_byte:
+ raise RuntimeError("Bad password for file", name)
- zef_file.seek(zinfo.header_offset, 0)
-
- # Skip the file header:
- fheader = zef_file.read(sizeFileHeader)
- if fheader[0:4] != stringFileHeader:
- raise BadZipfile, "Bad magic number for file header"
-
- fheader = struct.unpack(structFileHeader, fheader)
- fname = zef_file.read(fheader[_FH_FILENAME_LENGTH])
- if fheader[_FH_EXTRA_FIELD_LENGTH]:
- zef_file.read(fheader[_FH_EXTRA_FIELD_LENGTH])
-
- if fname != zinfo.orig_filename:
- raise BadZipfile, \
- 'File name in directory "%s" and header "%s"
differ.' % (
- zinfo.orig_filename, fname)
-
- # check for encrypted flag & handle password
- is_encrypted = zinfo.flag_bits & 0x1
- zd = None
- if is_encrypted:
- if not pwd:
- pwd = self.pwd
- if not pwd:
- raise RuntimeError, "File %s is encrypted, " \
- "password required for extraction" % name
-
- zd = _ZipDecrypter(pwd)
- # The first 12 bytes in the cypher stream is an encryption
header
- # used to strengthen the algorithm. The first 11 bytes are
- # completely random, while the 12th contains the MSB of the
CRC,
- # or the MSB of the file time depending on the header type
- # and is used to check the correctness of the password.
- bytes = zef_file.read(12)
- h = map(zd, bytes[0:12])
- if zinfo.flag_bits & 0x8:
- # compare against the file type from extended local headers
- check_byte = (zinfo._raw_time >> 8) & 0xff
- else:
- # compare against the CRC otherwise
- check_byte = (zinfo.CRC >> 24) & 0xff
- if ord(h[11]) != check_byte:
- raise RuntimeError("Bad password for file", name)
- except:
- if not self._filePassed:
- zef_file.close()
- raise
- if self._filePassed:
- return ZipExtFile(zef_file, mode, zinfo, zd)
- else:
- return ZipExtFileWithClose(zef_file, mode, zinfo, zd)
+ return ZipExtFile(zef_file, mode, zinfo, zd)
def extract(self, member, path=None, pwd=None):
"""Extract a member from the archive to the current working directory,
@@ -1002,6 +989,7 @@
if not os.path.isdir(targetpath):
os.mkdir(targetpath)
return targetpath
+
source = self.open(member, pwd=pwd)
target = file(targetpath, "wb")
shutil.copyfileobj(source, target)
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit