[Python-checkins] r53771 - in python/trunk: Lib/gzip.py Lib/test/test_gzip.py Misc/NEWS

lars.gustaebel python-checkins at python.org
Tue Feb 13 17:09:25 CET 2007


Author: lars.gustaebel
Date: Tue Feb 13 17:09:24 2007
New Revision: 53771

Modified:
   python/trunk/Lib/gzip.py
   python/trunk/Lib/test/test_gzip.py
   python/trunk/Misc/NEWS
Log:
Patch #1647484: Renamed GzipFile's filename attribute to name. The
filename attribute is still accessible as a property that emits a
DeprecationWarning.


Modified: python/trunk/Lib/gzip.py
==============================================================================
--- python/trunk/Lib/gzip.py	(original)
+++ python/trunk/Lib/gzip.py	Tue Feb 13 17:09:24 2007
@@ -106,7 +106,7 @@
             self._new_member = True
             self.extrabuf = ""
             self.extrasize = 0
-            self.filename = filename
+            self.name = filename
             # Starts small, scales exponentially
             self.min_readsize = 100
 
@@ -127,14 +127,20 @@
         if self.mode == WRITE:
             self._write_gzip_header()
 
+    @property
+    def filename(self):
+        import warnings
+        warnings.warn("use the name attribute", DeprecationWarning)
+        if self.mode == WRITE and self.name[-3:] != ".gz":
+            return self.name + ".gz"
+        return self.name
+
     def __repr__(self):
         s = repr(self.fileobj)
         return '<gzip ' + s[1:-1] + ' ' + hex(id(self)) + '>'
 
     def _init_write(self, filename):
-        if filename[-3:] != '.gz':
-            filename = filename + '.gz'
-        self.filename = filename
+        self.name = filename
         self.crc = zlib.crc32("")
         self.size = 0
         self.writebuf = []
@@ -143,16 +149,15 @@
     def _write_gzip_header(self):
         self.fileobj.write('\037\213')             # magic header
         self.fileobj.write('\010')                 # compression method
-        fname = self.filename[:-3]
         flags = 0
-        if fname:
+        if self.name:
             flags = FNAME
         self.fileobj.write(chr(flags))
         write32u(self.fileobj, long(time.time()))
         self.fileobj.write('\002')
         self.fileobj.write('\377')
-        if fname:
-            self.fileobj.write(fname + '\000')
+        if self.name:
+            self.fileobj.write(self.name + '\000')
 
     def _init_read(self):
         self.crc = zlib.crc32("")

Modified: python/trunk/Lib/test/test_gzip.py
==============================================================================
--- python/trunk/Lib/test/test_gzip.py	(original)
+++ python/trunk/Lib/test/test_gzip.py	Tue Feb 13 17:09:24 2007
@@ -153,6 +153,13 @@
         self.assertEqual(f.myfileobj.mode, 'rb')
         f.close()
 
+    def test_1647484(self):
+        for mode in ('wb', 'rb'):
+            f = gzip.GzipFile(self.filename, mode)
+            self.assert_(hasattr(f, "name"))
+            self.assertEqual(f.name, self.filename)
+            f.close()
+
 def test_main(verbose=None):
     test_support.run_unittest(TestGzip)
 

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Tue Feb 13 17:09:24 2007
@@ -128,6 +128,8 @@
 Library
 -------
 
+- Patch #1647484: Renamed GzipFile's filename attribute to name.
+
 - Patch #1517891: Mode 'a' for ZipFile now creates the file if it
   doesn't exist.
 


More information about the Python-checkins mailing list