[tor-commits] [stem/master] DictWriter.writeheader() introduced in python 2.7

atagar at torproject.org atagar at torproject.org
Mon Oct 29 16:04:29 UTC 2012


commit aac44fe0a23f409e61484f8ce6c1b5c79a312714
Author: Damian Johnson <atagar at torproject.org>
Date:   Mon Oct 29 09:03:52 2012 -0700

    DictWriter.writeheader() introduced in python 2.7
    
    The writeheader() method of the DictWriter class was added in python 2.7,
    breaking our 2.5 and 2.6 compatability...
    
    ======================================================================
    ERROR: test_multiple_descriptor_types
    ----------------------------------------------------------------------
    Traceback:
      File "/home/atagar/Desktop/stem/test/unit/descriptor/export.py", line 91, in test_multiple_descriptor_types
        self.assertRaises(ValueError, export_csv,)
      File "/usr/lib/python2.6/unittest.py", line 336, in failUnlessRaises
        callableObj(*args, **kwargs)
      File "/home/atagar/Desktop/stem/stem/descriptor/export.py", line 39, in export_csv
        export_csv_file(output_buffer, descriptors, included_fields, excluded_fields, header)
      File "/home/atagar/Desktop/stem/stem/descriptor/export.py", line 91, in export_csv_file
        writer.writeheader()
    AttributeError: DictWriter instance has no attribute 'writeheader'
    
    Noting the prereq in our pydocs and ignoring the 'header' flag if we can't
    support it.
---
 stem/descriptor/export.py      |    7 ++++---
 test/unit/descriptor/export.py |   13 +++++++++++++
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/stem/descriptor/export.py b/stem/descriptor/export.py
index 9785421..455e4ef 100644
--- a/stem/descriptor/export.py
+++ b/stem/descriptor/export.py
@@ -13,6 +13,7 @@ import csv
 import cStringIO
 
 import stem.descriptor
+import stem.prereq
 
 class _ExportDialect(csv.excel):
   lineterminator = '\n'
@@ -29,7 +30,7 @@ def export_csv(descriptors, included_fields = (), excluded_fields = (), header =
   :param list included_fields: attributes to include in the csv
   :param list excluded_fields: attributes to exclude from the csv
   :param bool header: if **True** then the first line will be a comma separated
-    list of the attribute names
+    list of the attribute names (**only supported in python 2.7 and higher**)
   
   :returns: **str** of the CSV for the descriptors, one per line
   :raises: **ValueError** if descriptors contain more than one descriptor type
@@ -50,7 +51,7 @@ def export_csv_file(output_file, descriptors, included_fields = (), excluded_fie
   :param list included_fields: attributes to include in the csv
   :param list excluded_fields: attributes to exclude from the csv
   :param bool header: if **True** then the first line will be a comma separated
-    list of the attribute names
+    list of the attribute names (**only supported in python 2.7 and higher**)
   
   :returns: **str** of the CSV for the descriptors, one per line
   :raises: **ValueError** if descriptors contain more than one descriptor type
@@ -87,7 +88,7 @@ def export_csv_file(output_file, descriptors, included_fields = (), excluded_fie
   
   writer = csv.DictWriter(output_file, included_fields, dialect = _ExportDialect(), extrasaction='ignore')
   
-  if header:
+  if header and stem.prereq.is_python_27():
     writer.writeheader()
   
   for desc in descriptors:
diff --git a/test/unit/descriptor/export.py b/test/unit/descriptor/export.py
index 463171d..5d43a8f 100644
--- a/test/unit/descriptor/export.py
+++ b/test/unit/descriptor/export.py
@@ -5,6 +5,9 @@ Unit tests for stem.descriptor.export.
 import StringIO
 import unittest
 
+import stem.prereq
+import test.runner
+
 from stem.descriptor.export import export_csv, export_csv_file
 from test.mocking import get_relay_server_descriptor, get_bridge_server_descriptor
 
@@ -14,6 +17,11 @@ class TestExport(unittest.TestCase):
     Exports a single minimal tor server descriptor.
     """
     
+    # we won't have a header prior to python 2.7
+    if not stem.prereq.is_python_27():
+      test.runner.skip(self, "(header added in python 2.7)")
+      return
+    
     desc = get_relay_server_descriptor()
     
     desc_csv = export_csv(desc, included_fields = ('nickname', 'address', 'published'), header = False)
@@ -59,6 +67,11 @@ class TestExport(unittest.TestCase):
     Checks that the default attributes for our csv output doesn't include private fields.
     """
     
+    # we won't have a header prior to python 2.7
+    if not stem.prereq.is_python_27():
+      test.runner.skip(self, "(header added in python 2.7)")
+      return
+    
     desc = get_relay_server_descriptor()
     desc_csv = export_csv(desc)
     



More information about the tor-commits mailing list