[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Avoiding the 'with' keyword for tarfile



commit e51fea598dced5e3c4a05c4cd0ad509beed530fe
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Thu Jun 21 09:45:14 2012 -0700

    Avoiding the 'with' keyword for tarfile
    
    The tarfile's __exit__() method was added sometime after python 2.5, causing it
    to produce a stacktrace if used via the 'with' keyword. Pity, yet another hack
    to accomidate a six-year-old version of python...
---
 stem/descriptor/reader.py |   31 ++++++++++++++++++++-----------
 1 files changed, 20 insertions(+), 11 deletions(-)

diff --git a/stem/descriptor/reader.py b/stem/descriptor/reader.py
index 7a7e565..716b58c 100644
--- a/stem/descriptor/reader.py
+++ b/stem/descriptor/reader.py
@@ -443,22 +443,31 @@ class DescriptorReader:
       self._notify_skip_listeners(target, ReadFailed(exc))
   
   def _handle_archive(self, target):
+    # TODO: This would be nicer via the 'with' keyword, but tarfile's __exit__
+    # method was added sometime after python 2.5. We should change this when
+    # we drop python 2.5 support.
+    
+    tar_file = None
+    
     try:
-      with tarfile.open(target) as tar_file:
-        for tar_entry in tar_file:
-          if tar_entry.isfile():
-            entry = tar_file.extractfile(tar_entry)
-            
-            for desc in stem.descriptor.parse_file(target, entry):
-              if self._is_stopped.isSet(): return
-              self._unreturned_descriptors.put(desc)
-              self._iter_notice.set()
-            
-            entry.close()
+      tar_file = tarfile.open(target)
+      
+      for tar_entry in tar_file:
+        if tar_entry.isfile():
+          entry = tar_file.extractfile(tar_entry)
+          
+          for desc in stem.descriptor.parse_file(target, entry):
+            if self._is_stopped.isSet(): return
+            self._unreturned_descriptors.put(desc)
+            self._iter_notice.set()
+          
+          entry.close()
     except TypeError, exc:
       self._notify_skip_listeners(target, ParsingFailure(exc))
     except IOError, exc:
       self._notify_skip_listeners(target, ReadFailed(exc))
+    finally:
+      if tar_file: tar_file.close()
   
   def _notify_skip_listeners(self, path, exception):
     for listener in self._skip_listeners:



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits