diff options
author | Georg Brandl <georg@python.org> | 2008-07-16 12:55:28 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2008-07-16 12:55:28 (GMT) |
commit | 2ee470f7f99f78348baa1e7a13007540d89d9c92 (patch) | |
tree | c8c65be74a06e665375c0d0de7ee7514e61cdb49 /Lib | |
parent | 38bbc4888a1fcbcedd7eb8b83ac4b070f2aeb5f2 (diff) | |
download | cpython-2ee470f7f99f78348baa1e7a13007540d89d9c92.zip cpython-2ee470f7f99f78348baa1e7a13007540d89d9c92.tar.gz cpython-2ee470f7f99f78348baa1e7a13007540d89d9c92.tar.bz2 |
Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r64722 | georg.brandl | 2008-07-05 12:13:36 +0200 (Sat, 05 Jul 2008) | 4 lines
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
This is a new feature, but Barry authorized adding it in the beta period.
........
r64729 | mark.dickinson | 2008-07-05 13:33:52 +0200 (Sat, 05 Jul 2008) | 5 lines
Issue 3188: accept float('infinity') as well as float('inf'). This
makes the float constructor behave in the same way as specified
by various other language standards, including C99, IEEE 754r,
and the IBM Decimal standard.
........
r64753 | gregory.p.smith | 2008-07-06 05:35:58 +0200 (Sun, 06 Jul 2008) | 4 lines
- Issue #2862: Make int and float freelist management consistent with other
freelists. Changes their CompactFreeList apis into ClearFreeList apis and
calls them via gc.collect().
........
r64845 | raymond.hettinger | 2008-07-10 16:03:19 +0200 (Thu, 10 Jul 2008) | 1 line
Issue 3301: Bisect functions behaved badly when lo was negative.
........
r64846 | raymond.hettinger | 2008-07-10 16:34:57 +0200 (Thu, 10 Jul 2008) | 1 line
Issue 3285: Fractions from_float() and from_decimal() accept Integral arguments.
........
r64849 | andrew.kuchling | 2008-07-10 16:43:31 +0200 (Thu, 10 Jul 2008) | 1 line
Wording changes
........
r64871 | raymond.hettinger | 2008-07-11 14:00:21 +0200 (Fri, 11 Jul 2008) | 1 line
Add cautionary note on the use of PySequence_Fast_ITEMS.
........
r64880 | amaury.forgeotdarc | 2008-07-11 23:28:25 +0200 (Fri, 11 Jul 2008) | 5 lines
#3317 in zipfile module, restore the previous names of global variables:
some applications relied on them.
Also remove duplicated lines.
........
r64881 | amaury.forgeotdarc | 2008-07-11 23:45:06 +0200 (Fri, 11 Jul 2008) | 3 lines
#3342: In tracebacks, printed source lines were not indented since r62555.
#3343: Py_DisplaySourceLine should be a private function. Rename it to _Py_DisplaySourceLine.
........
r64882 | josiah.carlson | 2008-07-12 00:17:14 +0200 (Sat, 12 Jul 2008) | 2 lines
Fix for the AttributeError in test_asynchat.
........
r64885 | josiah.carlson | 2008-07-12 01:26:59 +0200 (Sat, 12 Jul 2008) | 2 lines
Fixed test for asyncore.
........
r64888 | matthias.klose | 2008-07-12 09:51:48 +0200 (Sat, 12 Jul 2008) | 2 lines
- Fix bashisms in Tools/faqwiz/move-faqwiz.sh
........
r64897 | benjamin.peterson | 2008-07-12 22:16:19 +0200 (Sat, 12 Jul 2008) | 1 line
fix various doc typos #3320
........
r64900 | alexandre.vassalotti | 2008-07-13 00:06:53 +0200 (Sun, 13 Jul 2008) | 2 lines
Fixed typo.
........
r64901 | benjamin.peterson | 2008-07-13 01:41:19 +0200 (Sun, 13 Jul 2008) | 1 line
#1778443 robotparser fixes from Aristotelis Mikropoulos
........
r64915 | nick.coghlan | 2008-07-13 16:52:36 +0200 (Sun, 13 Jul 2008) | 1 line
Fix issue 3221 by emitting a RuntimeWarning instead of raising SystemError when the parent module can't be found during an absolute import (likely due to non-PEP 361 aware code which sets a module level __package__ attribute)
........
r64926 | martin.v.loewis | 2008-07-13 22:31:49 +0200 (Sun, 13 Jul 2008) | 2 lines
Add turtle into the module index.
........
r64927 | alexandre.vassalotti | 2008-07-13 22:42:44 +0200 (Sun, 13 Jul 2008) | 3 lines
Issue #3274: Use a less common identifier for the temporary variable
in Py_CLEAR().
........
r64928 | andrew.kuchling | 2008-07-13 23:43:25 +0200 (Sun, 13 Jul 2008) | 1 line
Re-word
........
r64929 | andrew.kuchling | 2008-07-13 23:43:52 +0200 (Sun, 13 Jul 2008) | 1 line
Add various items; move ctypes items into a subsection of their own
........
r64938 | andrew.kuchling | 2008-07-14 02:35:32 +0200 (Mon, 14 Jul 2008) | 1 line
Typo fixes
........
r64939 | andrew.kuchling | 2008-07-14 02:40:55 +0200 (Mon, 14 Jul 2008) | 1 line
Typo fix
........
r64940 | andrew.kuchling | 2008-07-14 03:18:16 +0200 (Mon, 14 Jul 2008) | 1 line
Typo fix
........
r64941 | andrew.kuchling | 2008-07-14 03:18:31 +0200 (Mon, 14 Jul 2008) | 1 line
Expand the multiprocessing section
........
r64944 | gregory.p.smith | 2008-07-14 08:06:48 +0200 (Mon, 14 Jul 2008) | 7 lines
Fix posix.fork1() / os.fork1() to only call PyOS_AfterFork() in the child
process rather than both parent and child.
Does anyone actually use fork1()? It appears to be a Solaris thing
but if Python is built with pthreads on Solaris, fork1() and fork()
should be the same.
........
r64961 | jesse.noller | 2008-07-15 15:47:33 +0200 (Tue, 15 Jul 2008) | 1 line
multiprocessing/connection.py patch to remove fqdn oddness for issue 3270
........
r64966 | nick.coghlan | 2008-07-15 17:40:22 +0200 (Tue, 15 Jul 2008) | 1 line
Add missing NEWS entry for r64962
........
r64973 | jesse.noller | 2008-07-15 20:29:18 +0200 (Tue, 15 Jul 2008) | 1 line
Revert 3270 patch: self._address is in pretty widespread use, need to revisit
........
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/bisect.py | 8 | ||||
-rwxr-xr-x | Lib/fractions.py | 8 | ||||
-rw-r--r-- | Lib/multiprocessing/connection.py | 4 | ||||
-rw-r--r-- | Lib/shutil.py | 35 | ||||
-rw-r--r-- | Lib/test/test_bisect.py | 8 | ||||
-rw-r--r-- | Lib/test/test_float.py | 18 | ||||
-rw-r--r-- | Lib/test/test_fractions.py | 12 | ||||
-rw-r--r-- | Lib/test/test_import.py | 23 | ||||
-rw-r--r-- | Lib/test/test_shutil.py | 76 | ||||
-rw-r--r-- | Lib/zipfile.py | 82 |
10 files changed, 200 insertions, 74 deletions
diff --git a/Lib/bisect.py b/Lib/bisect.py index e4a2133..fe84255 100644 --- a/Lib/bisect.py +++ b/Lib/bisect.py @@ -9,6 +9,8 @@ def insort_right(a, x, lo=0, hi=None): slice of a to be searched. """ + if lo < 0: + raise ValueError('lo must be non-negative') if hi is None: hi = len(a) while lo < hi: @@ -30,6 +32,8 @@ def bisect_right(a, x, lo=0, hi=None): slice of a to be searched. """ + if lo < 0: + raise ValueError('lo must be non-negative') if hi is None: hi = len(a) while lo < hi: @@ -49,6 +53,8 @@ def insort_left(a, x, lo=0, hi=None): slice of a to be searched. """ + if lo < 0: + raise ValueError('lo must be non-negative') if hi is None: hi = len(a) while lo < hi: @@ -69,6 +75,8 @@ def bisect_left(a, x, lo=0, hi=None): slice of a to be searched. """ + if lo < 0: + raise ValueError('lo must be non-negative') if hi is None: hi = len(a) while lo < hi: diff --git a/Lib/fractions.py b/Lib/fractions.py index 3df628a..bc06524 100755 --- a/Lib/fractions.py +++ b/Lib/fractions.py @@ -108,7 +108,9 @@ class Fraction(numbers.Rational): Beware that Fraction.from_float(0.3) != Fraction(3, 10). """ - if not isinstance(f, float): + if isinstance(f, numbers.Integral): + f = float(f) + elif not isinstance(f, float): raise TypeError("%s.from_float() only takes floats, not %r (%s)" % (cls.__name__, f, type(f).__name__)) if math.isnan(f) or math.isinf(f): @@ -119,7 +121,9 @@ class Fraction(numbers.Rational): def from_decimal(cls, dec): """Converts a finite Decimal instance to a rational number, exactly.""" from decimal import Decimal - if not isinstance(dec, Decimal): + if isinstance(dec, numbers.Integral): + dec = Decimal(int(dec)) + elif not isinstance(dec, Decimal): raise TypeError( "%s.from_decimal() only takes Decimals, not %r (%s)" % (cls.__name__, dec, type(dec).__name__)) diff --git a/Lib/multiprocessing/connection.py b/Lib/multiprocessing/connection.py index 8493ac0..717e14e 100644 --- a/Lib/multiprocessing/connection.py +++ b/Lib/multiprocessing/connection.py @@ -222,11 +222,9 @@ class SocketListener(object): self._family = family self._last_accepted = None - sub_debug('listener bound to address %r', self._address) - if family == 'AF_UNIX': self._unlink = Finalize( - self, os.unlink, args=(self._address,), exitpriority=0 + self, os.unlink, args=(address,), exitpriority=0 ) else: self._unlink = None diff --git a/Lib/shutil.py b/Lib/shutil.py index e9cebae..56ea7ec 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -8,6 +8,7 @@ import os import sys import stat from os.path import abspath +import fnmatch __all__ = ["copyfileobj","copyfile","copymode","copystat","copy","copy2", "copytree","move","rmtree","Error"] @@ -93,8 +94,19 @@ def copy2(src, dst): copyfile(src, dst) copystat(src, dst) +def ignore_patterns(*patterns): + """Function that can be used as copytree() ignore parameter. -def copytree(src, dst, symlinks=False): + Patterns is a sequence of glob-style patterns + that are used to exclude files""" + def _ignore_patterns(path, names): + ignored_names = [] + for pattern in patterns: + ignored_names.extend(fnmatch.filter(names, pattern)) + return set(ignored_names) + return _ignore_patterns + +def copytree(src, dst, symlinks=False, ignore=None): """Recursively copy a directory tree using copy2(). The destination directory must not already exist. @@ -105,13 +117,32 @@ def copytree(src, dst, symlinks=False): it is false, the contents of the files pointed to by symbolic links are copied. + The optional ignore argument is a callable. If given, it + is called with the `src` parameter, which is the directory + being visited by copytree(), and `names` which is the list of + `src` contents, as returned by os.listdir(): + + callable(src, names) -> ignored_names + + Since copytree() is called recursively, the callable will be + called once for each directory that is copied. It returns a + list of names relative to the `src` directory that should + not be copied. + XXX Consider this example code rather than the ultimate tool. """ names = os.listdir(src) + if ignore is not None: + ignored_names = ignore(src, names) + else: + ignored_names = set() + os.makedirs(dst) errors = [] for name in names: + if name in ignored_names: + continue srcname = os.path.join(src, name) dstname = os.path.join(dst, name) try: @@ -119,7 +150,7 @@ def copytree(src, dst, symlinks=False): linkto = os.readlink(srcname) os.symlink(linkto, dstname) elif os.path.isdir(srcname): - copytree(srcname, dstname, symlinks) + copytree(srcname, dstname, symlinks, ignore) else: copy2(srcname, dstname) # XXX What about devices, sockets etc.? diff --git a/Lib/test/test_bisect.py b/Lib/test/test_bisect.py index af9f1f7..987f33c 100644 --- a/Lib/test/test_bisect.py +++ b/Lib/test/test_bisect.py @@ -114,6 +114,14 @@ class TestBisect(unittest.TestCase): self.assertEqual(func(data, elem), expected) self.assertEqual(func(UserList(data), elem), expected) + def test_negative_lo(self): + # Issue 3301 + mod = self.module + self.assertRaises(ValueError, mod.bisect_left, [1, 2, 3], 5, -1, 3), + self.assertRaises(ValueError, mod.bisect_right, [1, 2, 3], 5, -1, 3), + self.assertRaises(ValueError, mod.insort_left, [1, 2, 3], 5, -1, 3), + self.assertRaises(ValueError, mod.insort_right, [1, 2, 3], 5, -1, 3), + def test_random(self, n=25): from random import randrange for i in range(n): diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py index 7de5320..db91e5e 100644 --- a/Lib/test/test_float.py +++ b/Lib/test/test_float.py @@ -284,24 +284,36 @@ class ReprTestCase(unittest.TestCase): floats_file.close() # Beginning with Python 2.6 float has cross platform compatible -# ways to create and representate inf and nan +# ways to create and represent inf and nan class InfNanTest(unittest.TestCase): def test_inf_from_str(self): self.assert_(isinf(float("inf"))) self.assert_(isinf(float("+inf"))) self.assert_(isinf(float("-inf"))) + self.assert_(isinf(float("infinity"))) + self.assert_(isinf(float("+infinity"))) + self.assert_(isinf(float("-infinity"))) self.assertEqual(repr(float("inf")), "inf") self.assertEqual(repr(float("+inf")), "inf") self.assertEqual(repr(float("-inf")), "-inf") + self.assertEqual(repr(float("infinity")), "inf") + self.assertEqual(repr(float("+infinity")), "inf") + self.assertEqual(repr(float("-infinity")), "-inf") self.assertEqual(repr(float("INF")), "inf") self.assertEqual(repr(float("+Inf")), "inf") self.assertEqual(repr(float("-iNF")), "-inf") + self.assertEqual(repr(float("Infinity")), "inf") + self.assertEqual(repr(float("+iNfInItY")), "inf") + self.assertEqual(repr(float("-INFINITY")), "-inf") self.assertEqual(str(float("inf")), "inf") self.assertEqual(str(float("+inf")), "inf") self.assertEqual(str(float("-inf")), "-inf") + self.assertEqual(str(float("infinity")), "inf") + self.assertEqual(str(float("+infinity")), "inf") + self.assertEqual(str(float("-infinity")), "-inf") self.assertRaises(ValueError, float, "info") self.assertRaises(ValueError, float, "+info") @@ -309,6 +321,10 @@ class InfNanTest(unittest.TestCase): self.assertRaises(ValueError, float, "in") self.assertRaises(ValueError, float, "+in") self.assertRaises(ValueError, float, "-in") + self.assertRaises(ValueError, float, "infinit") + self.assertRaises(ValueError, float, "+Infin") + self.assertRaises(ValueError, float, "-INFI") + self.assertRaises(ValueError, float, "infinitys") def test_inf_as_str(self): self.assertEqual(repr(1e300 * 1e300), "inf") diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py index 4f8defb..4fadf6c 100644 --- a/Lib/test/test_fractions.py +++ b/Lib/test/test_fractions.py @@ -134,10 +134,8 @@ class FractionTest(unittest.TestCase): self.assertNotEquals(F(4, 2), r) def testFromFloat(self): - self.assertRaisesMessage( - TypeError, "Fraction.from_float() only takes floats, not 3 (int)", - F.from_float, 3) - + self.assertRaises(TypeError, F.from_float, 3+4j) + self.assertEquals((10, 1), _components(F.from_float(10))) self.assertEquals((0, 1), _components(F.from_float(-0.0))) self.assertEquals((10, 1), _components(F.from_float(10.0))) self.assertEquals((-5, 2), _components(F.from_float(-2.5))) @@ -161,10 +159,8 @@ class FractionTest(unittest.TestCase): F.from_float, nan) def testFromDecimal(self): - self.assertRaisesMessage( - TypeError, - "Fraction.from_decimal() only takes Decimals, not 3 (int)", - F.from_decimal, 3) + self.assertRaises(TypeError, F.from_decimal, 3+4j) + self.assertEquals(F(10, 1), F.from_decimal(10)) self.assertEquals(F(0), F.from_decimal(Decimal("-0"))) self.assertEquals(F(5, 10), F.from_decimal(Decimal("0.5"))) self.assertEquals(F(5, 1000), F.from_decimal(Decimal("5e-3"))) diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py index 81e221d..aa65c7a 100644 --- a/Lib/test/test_import.py +++ b/Lib/test/test_import.py @@ -1,5 +1,3 @@ -from test.support import TESTFN, run_unittest, catch_warning - import unittest import os import random @@ -8,7 +6,7 @@ import sys import py_compile import warnings import imp -from test.support import unlink, TESTFN, unload +from test.support import unlink, TESTFN, unload, run_unittest, catch_warning def remove_files(name): @@ -267,6 +265,25 @@ class RelativeImport(unittest.TestCase): from . import relimport self.assertTrue(hasattr(relimport, "RelativeImport")) + def test_issue3221(self): + def check_relative(): + exec("from . import relimport", ns) + # Check both OK with __package__ and __name__ correct + ns = dict(__package__='test', __name__='test.notarealmodule') + check_relative() + # Check both OK with only __name__ wrong + ns = dict(__package__='test', __name__='notarealpkg.notarealmodule') + check_relative() + # Check relative fails with only __package__ wrong + ns = dict(__package__='foo', __name__='test.notarealmodule') + self.assertRaises(SystemError, check_relative) + # Check relative fails with __package__ and __name__ wrong + ns = dict(__package__='foo', __name__='notarealpkg.notarealmodule') + self.assertRaises(SystemError, check_relative) + # Check both fail with package set to a non-string + ns = dict(__package__=object()) + self.assertRaises(ValueError, check_relative) + def test_main(verbose=None): run_unittest(ImportTest, PathsTests, RelativeImport) diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py index 2e680a9..ad60a44 100644 --- a/Lib/test/test_shutil.py +++ b/Lib/test/test_shutil.py @@ -108,6 +108,82 @@ class TestShutil(unittest.TestCase): if os.path.exists(path): shutil.rmtree(path) + def test_copytree_with_exclude(self): + + def write_data(path, data): + f = open(path, "w") + f.write(data) + f.close() + + def read_data(path): + f = open(path) + data = f.read() + f.close() + return data + + # creating data + join = os.path.join + exists = os.path.exists + src_dir = tempfile.mkdtemp() + dst_dir = join(tempfile.mkdtemp(), 'destination') + write_data(join(src_dir, 'test.txt'), '123') + write_data(join(src_dir, 'test.tmp'), '123') + os.mkdir(join(src_dir, 'test_dir')) + write_data(join(src_dir, 'test_dir', 'test.txt'), '456') + os.mkdir(join(src_dir, 'test_dir2')) + write_data(join(src_dir, 'test_dir2', 'test.txt'), '456') + os.mkdir(join(src_dir, 'test_dir2', 'subdir')) + os.mkdir(join(src_dir, 'test_dir2', 'subdir2')) + write_data(join(src_dir, 'test_dir2', 'subdir', 'test.txt'), '456') + write_data(join(src_dir, 'test_dir2', 'subdir2', 'test.py'), '456') + + + # testing glob-like patterns + try: + patterns = shutil.ignore_patterns('*.tmp', 'test_dir2') + shutil.copytree(src_dir, dst_dir, ignore=patterns) + # checking the result: some elements should not be copied + self.assert_(exists(join(dst_dir, 'test.txt'))) + self.assert_(not exists(join(dst_dir, 'test.tmp'))) + self.assert_(not exists(join(dst_dir, 'test_dir2'))) + finally: + if os.path.exists(dst_dir): + shutil.rmtree(dst_dir) + try: + patterns = shutil.ignore_patterns('*.tmp', 'subdir*') + shutil.copytree(src_dir, dst_dir, ignore=patterns) + # checking the result: some elements should not be copied + self.assert_(not exists(join(dst_dir, 'test.tmp'))) + self.assert_(not exists(join(dst_dir, 'test_dir2', 'subdir2'))) + self.assert_(not exists(join(dst_dir, 'test_dir2', 'subdir'))) + finally: + if os.path.exists(dst_dir): + shutil.rmtree(dst_dir) + + # testing callable-style + try: + def _filter(src, names): + res = [] + for name in names: + path = os.path.join(src, name) + + if (os.path.isdir(path) and + path.split()[-1] == 'subdir'): + res.append(name) + elif os.path.splitext(path)[-1] in ('.py'): + res.append(name) + return res + + shutil.copytree(src_dir, dst_dir, ignore=_filter) + + # checking the result: some elements should not be copied + self.assert_(not exists(join(dst_dir, 'test_dir2', 'subdir2', + 'test.py'))) + self.assert_(not exists(join(dst_dir, 'test_dir2', 'subdir'))) + + finally: + if os.path.exists(dst_dir): + shutil.rmtree(dst_dir) if hasattr(os, "symlink"): def test_dont_copy_file_onto_link_to_itself(self): diff --git a/Lib/zipfile.py b/Lib/zipfile.py index 59a86e2..fda9903 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -45,9 +45,9 @@ ZIP_DEFLATED = 8 # The "end of central directory" structure, magic number, size, and indices # (section V.I in the format document) -structEndCentDir = b"<4s4H2LH" -magicEndCentDir = b"PK\005\006" -sizeEndCentDir = struct.calcsize(structEndCentDir) +structEndArchive = b"<4s4H2LH" +stringEndArchive = b"PK\005\006" +sizeEndCentDir = struct.calcsize(structEndArchive) _ECD_SIGNATURE = 0 _ECD_DISK_NUMBER = 1 @@ -65,37 +65,9 @@ _ECD_LOCATION = 9 # The "central directory" structure, magic number, size, and indices # of entries in the structure (section V.F in the format document) structCentralDir = "<4s4B4HL2L5H2L" -magicCentralDir = b"PK\001\002" +stringCentralDir = b"PK\001\002" sizeCentralDir = struct.calcsize(structCentralDir) -# The "local file header" structure, magic number, size, and indices -# (section V.A in the format document) -structFileHeader = "<4s2B4HL2L2H" -magicFileHeader = b"PK\003\004" -sizeFileHeader = struct.calcsize(structFileHeader) - -# The "Zip64 end of central directory locator" structure, magic number, and size -structEndCentDir64Locator = "<4sLQL" -magicEndCentDir64Locator = b"PK\x06\x07" -sizeEndCentDir64Locator = struct.calcsize(structEndCentDir64Locator) - -# The "Zip64 end of central directory" record, magic number, size, and indices -# (section V.G in the format document) -structEndCentDir64 = "<4sQ2H2L4Q" -magicEndCentDir64 = b"PK\x06\x06" -sizeEndCentDir64 = struct.calcsize(structEndCentDir64) - -_CD64_SIGNATURE = 0 -_CD64_DIRECTORY_RECSIZE = 1 -_CD64_CREATE_VERSION = 2 -_CD64_EXTRACT_VERSION = 3 -_CD64_DISK_NUMBER = 4 -_CD64_DISK_NUMBER_START = 5 -_CD64_NUMBER_ENTRIES_THIS_DISK = 6 -_CD64_NUMBER_ENTRIES_TOTAL = 7 -_CD64_DIRECTORY_SIZE = 8 -_CD64_OFFSET_START_CENTDIR = 9 - # indexes of entries in the central directory structure _CD_SIGNATURE = 0 _CD_CREATE_VERSION = 1 @@ -120,7 +92,7 @@ _CD_LOCAL_HEADER_OFFSET = 18 # The "local file header" structure, magic number, size, and indices # (section V.A in the format document) structFileHeader = "<4s2B4HL2L2H" -magicFileHeader = b"PK\003\004" +stringFileHeader = b"PK\003\004" sizeFileHeader = struct.calcsize(structFileHeader) _FH_SIGNATURE = 0 @@ -137,15 +109,15 @@ _FH_FILENAME_LENGTH = 10 _FH_EXTRA_FIELD_LENGTH = 11 # The "Zip64 end of central directory locator" structure, magic number, and size -structEndCentDir64Locator = "<4sLQL" -magicEndCentDir64Locator = b"PK\x06\x07" -sizeEndCentDir64Locator = struct.calcsize(structEndCentDir64Locator) +structEndArchive64Locator = "<4sLQL" +stringEndArchive64Locator = b"PK\x06\x07" +sizeEndCentDir64Locator = struct.calcsize(structEndArchive64Locator) # The "Zip64 end of central directory" record, magic number, size, and indices # (section V.G in the format document) -structEndCentDir64 = "<4sQ2H2L4Q" -magicEndCentDir64 = b"PK\x06\x06" -sizeEndCentDir64 = struct.calcsize(structEndCentDir64) +structEndArchive64 = "<4sQ2H2L4Q" +stringEndArchive64 = b"PK\x06\x06" +sizeEndCentDir64 = struct.calcsize(structEndArchive64) _CD64_SIGNATURE = 0 _CD64_DIRECTORY_RECSIZE = 1 @@ -176,8 +148,8 @@ def _EndRecData64(fpin, offset, endrec): """ fpin.seek(offset - sizeEndCentDir64Locator, 2) data = fpin.read(sizeEndCentDir64Locator) - sig, diskno, reloff, disks = struct.unpack(structEndCentDir64Locator, data) - if sig != magicEndCentDir64Locator: + sig, diskno, reloff, disks = struct.unpack(structEndArchive64Locator, data) + if sig != stringEndArchive64Locator: return endrec if diskno != 0 or disks != 1: @@ -188,8 +160,8 @@ def _EndRecData64(fpin, offset, endrec): data = fpin.read(sizeEndCentDir64) sig, sz, create_version, read_version, disk_num, disk_dir, \ dircount, dircount2, dirsize, diroffset = \ - struct.unpack(structEndCentDir64, data) - if sig != magicEndCentDir64: + struct.unpack(structEndArchive64, data) + if sig != stringEndArchive64: return endrec # Update the original endrec using data from the ZIP64 record @@ -217,9 +189,9 @@ def _EndRecData(fpin): # file if this is the case). fpin.seek(-sizeEndCentDir, 2) data = fpin.read() - if data[0:4] == magicEndCentDir and data[-2:] == b"\000\000": + if data[0:4] == stringEndArchive and data[-2:] == b"\000\000": # the signature is correct and there's no comment, unpack structure - endrec = struct.unpack(structEndCentDir, data) + endrec = struct.unpack(structEndArchive, data) endrec=list(endrec) # Append a blank comment and record start offset @@ -241,11 +213,11 @@ def _EndRecData(fpin): maxCommentStart = max(filesize - (1 << 16) - sizeEndCentDir, 0) fpin.seek(maxCommentStart, 0) data = fpin.read() - start = data.rfind(magicEndCentDir) + start = data.rfind(stringEndArchive) if start >= 0: # found the magic number; attempt to unpack and interpret recData = data[start:start+sizeEndCentDir] - endrec = list(struct.unpack(structEndCentDir, recData)) + endrec = list(struct.unpack(structEndArchive, recData)) comment = data[start+sizeEndCentDir:] # check that comment length is correct if endrec[_ECD_COMMENT_SIZE] == len(comment): @@ -352,7 +324,7 @@ class ZipInfo (object): self.create_version = max(45, self.extract_version) filename, flag_bits = self._encodeFilenameFlags() - header = struct.pack(structFileHeader, magicFileHeader, + header = struct.pack(structFileHeader, stringFileHeader, self.extract_version, self.reserved, flag_bits, self.compress_type, dostime, dosdate, CRC, compress_size, file_size, @@ -777,7 +749,7 @@ class ZipFile: total = 0 while total < size_cd: centdir = fp.read(sizeCentralDir) - if centdir[0:4] != magicCentralDir: + if centdir[0:4] != stringCentralDir: raise BadZipfile("Bad magic number for central directory") centdir = struct.unpack(structCentralDir, centdir) if self.debug > 2: @@ -892,7 +864,7 @@ class ZipFile: # Skip the file header: fheader = zef_file.read(sizeFileHeader) - if fheader[0:4] != magicFileHeader: + if fheader[0:4] != stringFileHeader: raise BadZipfile("Bad magic number for file header") fheader = struct.unpack(structFileHeader, fheader) @@ -1184,7 +1156,7 @@ class ZipFile: try: filename, flag_bits = zinfo._encodeFilenameFlags() centdir = struct.pack(structCentralDir, - magicCentralDir, create_version, + stringCentralDir, create_version, zinfo.create_system, extract_version, zinfo.reserved, flag_bits, zinfo.compress_type, dostime, dosdate, zinfo.CRC, compress_size, file_size, @@ -1212,13 +1184,13 @@ class ZipFile: if pos1 > ZIP64_LIMIT: # Need to write the ZIP64 end-of-archive records zip64endrec = struct.pack( - structEndCentDir64, magicEndCentDir64, + structEndArchive64, stringEndArchive64, 44, 45, 45, 0, 0, count, count, pos2 - pos1, pos1) self.fp.write(zip64endrec) zip64locrec = struct.pack( - structEndCentDir64Locator, - magicEndCentDir64Locator, 0, pos2, 1) + structEndArchive64Locator, + stringEndArchive64Locator, 0, pos2, 1) self.fp.write(zip64locrec) centDirOffset = 0xFFFFFFFF @@ -1229,7 +1201,7 @@ class ZipFile: % ZIP_MAX_COMMENT self.comment = self.comment[:ZIP_MAX_COMMENT] - endrec = struct.pack(structEndCentDir, magicEndCentDir, + endrec = struct.pack(structEndArchive, stringEndArchive, 0, 0, count % ZIP_FILECOUNT_LIMIT, count % ZIP_FILECOUNT_LIMIT, pos2 - pos1, centDirOffset, len(self.comment)) |