[tor-commits] [stem/master] Mocking for open() broken in python 3

atagar at torproject.org atagar at torproject.org
Sat Feb 2 18:20:49 UTC 2013


commit 8b95332960e5343dd9b716ef25682548f0de9d7e
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Jan 27 16:32:06 2013 -0800

    Mocking for open() broken in python 3
    
    In python 2.x there's both __builtin__.open() and io.open(), but in python 3.x
    there's just io.open() (which is aliased by builtin). Accounting for this when
    we mock it for the descriptor reader unit tests.
    
    I'm a little puzzled why the mocking wasn't failing more spectacularly before
    (the __builtin__ import should always fail on python 3), but oh well - fixed
    now.
    
    With this the unit tests finally pass with python 3!!!
    
    ======================================================================
    ERROR: test_load_processed_files
    ----------------------------------------------------------------------
    Traceback:
      File "/home/atagar/Desktop/stem/test/data/python3/test/unit/descriptor/reader.py", line 52, in test_load_processed_files
        self.assertEquals(expected_value, stem.descriptor.reader.load_processed_files(""))
      File "/home/atagar/Desktop/stem/test/data/python3/stem/descriptor/reader.py", line 179, in load_processed_files
        with open(path) as input_file:
    IOError: [Errno 2] No such file or directory: ''
    
    ======================================================================
    FAIL: test_get_connections
    ----------------------------------------------------------------------
    Traceback:
      File "/home/atagar/Desktop/stem/test/data/python3/test/unit/util/proc.py", line 171, in test_get_connections
        self.assertEquals(expected_results, proc.get_connections(pid))
    AssertionError: Lists differ: [('17.17.17.17', 4369, '34.34.... != []
    
    First list contains 2 additional elements.
    First extra element 0:
    ('17.17.17.17', 4369, '34.34.34.34', 8738)
    
    + []
    - [('17.17.17.17', 4369, '34.34.34.34', 8738),
    - ]
---
 test/mocking.py                |    5 +++--
 test/unit/descriptor/reader.py |   36 ++++++++++++++++++------------------
 test/unit/util/proc.py         |   18 ++++++++++++++----
 3 files changed, 35 insertions(+), 24 deletions(-)

diff --git a/test/mocking.py b/test/mocking.py
index ea88992..6472659 100644
--- a/test/mocking.py
+++ b/test/mocking.py
@@ -52,7 +52,6 @@ import hashlib
 import inspect
 import itertools
 import StringIO
-import __builtin__
 
 import stem.descriptor.extrainfo_descriptor
 import stem.descriptor.networkstatus
@@ -409,12 +408,14 @@ def revert_mocking():
     # makes the following check fail. Haven't a clue why.
 
     if stem.prereq.is_python_3():
+      import builtins
       builtin_module = builtins
     else:
+      import __builtin__
       builtin_module = __builtin__
 
     if module == builtin_module:
-      setattr(__builtin__, function, impl)
+      setattr(builtin_module, function, impl)
     else:
       setattr(module, function, impl)
 
diff --git a/test/unit/descriptor/reader.py b/test/unit/descriptor/reader.py
index acd77b5..bc24805 100644
--- a/test/unit/descriptor/reader.py
+++ b/test/unit/descriptor/reader.py
@@ -6,9 +6,21 @@ import StringIO
 import unittest
 
 import stem.descriptor.reader
+import stem.prereq
 import test.mocking as mocking
 
 
+def _mock_open(content):
+  test_content = StringIO.StringIO(content)
+  mocking.support_with(test_content)
+
+  if stem.prereq.is_python_3():
+    import builtins
+    mocking.mock(builtins.open, mocking.return_value(test_content), builtins)
+  else:
+    mocking.mock(open, mocking.return_value(test_content))
+
+
 class TestDescriptorReader(unittest.TestCase):
   def tearDown(self):
     mocking.revert_mocking()
@@ -36,9 +48,7 @@ class TestDescriptorReader(unittest.TestCase):
       "/dir/after empty line": 12345,
     }
 
-    test_content = StringIO.StringIO("\n".join(test_lines))
-    mocking.support_with(test_content)
-    mocking.mock(open, mocking.return_value(test_content))
+    _mock_open("\n".join(test_lines))
     self.assertEquals(expected_value, stem.descriptor.reader.load_processed_files(""))
 
   def test_load_processed_files_empty(self):
@@ -46,9 +56,7 @@ class TestDescriptorReader(unittest.TestCase):
     Tests the load_processed_files() function with an empty file.
     """
 
-    test_content = StringIO.StringIO("")
-    mocking.support_with(test_content)
-    mocking.mock(open, mocking.return_value(test_content))
+    _mock_open("")
     self.assertEquals({}, stem.descriptor.reader.load_processed_files(""))
 
   def test_load_processed_files_no_file(self):
@@ -57,9 +65,7 @@ class TestDescriptorReader(unittest.TestCase):
     it is missing the file path.
     """
 
-    test_content = StringIO.StringIO(" 12345")
-    mocking.support_with(test_content)
-    mocking.mock(open, mocking.return_value(test_content))
+    _mock_open(" 12345")
     self.assertRaises(TypeError, stem.descriptor.reader.load_processed_files, "")
 
   def test_load_processed_files_no_timestamp(self):
@@ -68,9 +74,7 @@ class TestDescriptorReader(unittest.TestCase):
     it is missing the timestamp.
     """
 
-    test_content = StringIO.StringIO("/dir/file ")
-    mocking.support_with(test_content)
-    mocking.mock(open, mocking.return_value(test_content))
+    _mock_open("/dir/file ")
     self.assertRaises(TypeError, stem.descriptor.reader.load_processed_files, "")
 
   def test_load_processed_files_malformed_file(self):
@@ -79,9 +83,7 @@ class TestDescriptorReader(unittest.TestCase):
     it has an invalid file path.
     """
 
-    test_content = StringIO.StringIO("not_an_absolute_file 12345")
-    mocking.support_with(test_content)
-    mocking.mock(open, mocking.return_value(test_content))
+    _mock_open("not_an_absolute_file 12345")
     self.assertRaises(TypeError, stem.descriptor.reader.load_processed_files, "")
 
   def test_load_processed_files_malformed_timestamp(self):
@@ -90,7 +92,5 @@ class TestDescriptorReader(unittest.TestCase):
     it has a non-numeric timestamp.
     """
 
-    test_content = StringIO.StringIO("/dir/file 123a")
-    mocking.support_with(test_content)
-    mocking.mock(open, mocking.return_value(test_content))
+    _mock_open("/dir/file 123a")
     self.assertRaises(TypeError, stem.descriptor.reader.load_processed_files, "")
diff --git a/test/unit/util/proc.py b/test/unit/util/proc.py
index 1ba2c60..0a6c0a0 100644
--- a/test/unit/util/proc.py
+++ b/test/unit/util/proc.py
@@ -6,6 +6,8 @@ import os
 import StringIO
 import unittest
 
+import stem.prereq
+
 from stem.util import proc
 from test import mocking
 
@@ -155,10 +157,18 @@ class TestProc(unittest.TestCase):
     tcp = '\n 0: 11111111:1111 22222222:2222 01 44444444:44444444 55:55555555 66666666 1111 8 99999999'
     udp = '\n A: BBBBBBBB:BBBB CCCCCCCC:CCCC DD EEEEEEEE:EEEEEEEE FF:FFFFFFFF GGGGGGGG 1111 H IIIIIIII'
 
-    mocking.mock(open, mocking.return_for_args({
-      ('/proc/net/tcp',): StringIO.StringIO(tcp),
-      ('/proc/net/udp',): StringIO.StringIO(udp)
-    }))
+    if stem.prereq.is_python_3():
+      import builtins
+
+      mocking.mock(builtins.open, mocking.return_for_args({
+        ('/proc/net/tcp',): StringIO.StringIO(tcp),
+        ('/proc/net/udp',): StringIO.StringIO(udp)
+      }), builtins)
+    else:
+      mocking.mock(open, mocking.return_for_args({
+        ('/proc/net/tcp',): StringIO.StringIO(tcp),
+        ('/proc/net/udp',): StringIO.StringIO(udp)
+      }))
 
     # tests the edge case of pid = 0
     self.assertEquals([], proc.get_connections(0))





More information about the tor-commits mailing list