Commits

Tetsuya Morimoto committed 74be668

added Python 3 support and dropped under Python 2.5 support

  • Participants
  • Parent commits 7500f94

Comments (0)

Files changed (1)

 constructor).  These are all the core parts of a mock library.  The
 implementation is simple because most of the work is done by doctest.
 """
+from __future__ import print_function
 
 __all__ = ["mock", "restore", "Mock", "TraceTracker", "assert_same_trace"]
 
-import __builtin__
 import sys
 import inspect
 import doctest
 import re
 import textwrap
+
 try:
-    from cStringIO import StringIO
+    # for Python 3
+    import __builtin__ as builtins
+    from StringIO import StringIO
 except ImportError:
-    from StringIO import StringIO
+    # for Python 2.6 or later
+    import builtins
+    from io import StringIO
+
 
 # A list of mocked objects. Each item is a tuple of (original object,
 # namespace dict, object name, and a list of object attributes).
 
     Test mocking a built-in function::
 
-        >>> mock("raw_input", returns="okay")
-        >>> raw_input()
-        Called raw_input()
+        >>> try:
+        ...     input = raw_input
+        ... except NameError:
+        ...     pass  # for Python 3
+        >>> mock("input", returns="okay")
+        >>> input()
+        Called input()
         'okay'
         >>> restore()
 
             # stack[1][0] is the frame object of the caller to this function
             globals_ = stack[1][0].f_globals
             locals_ = stack[1][0].f_locals
-            nsdicts = (locals_, globals_, __builtin__.__dict__)
+            nsdicts = (locals_, globals_, builtins.__dict__)
         finally:
             del(stack)
 
     # because if tmp is itself a mock object, tmp.func_globals will
     # return another mock object
     if isinstance(getattr(tmp, 'func_globals', None), dict):
-        nsdict = tmp.func_globals
+        nsdict = tmp.__globals__
     if not attrs:
         original = tmp
         nsdict[obj_name] = mock_obj
         if len(msg) > 80:
             msg = 'Called %s(\n    %s)' % (
                 func_name, ',\n    '.join(parts))
-        print >> self.file, msg
+        print(msg, file=self.file)
 
     def set(self, obj_name, attr, value): 
         """
         >>> z.a = 2
         Set z.a = 2
         """
-        print >> self.file, 'Set %s.%s = %r' % (obj_name, attr, value)
+        print('Set %s.%s = %r' % (obj_name, attr, value), file=self.file)
         
 class TraceTracker(Printer):
     """
 
     def clear(self):
         """Clear the MiniMock object usage that has been tracked so far.
+
+        truncate() was modified not to change the file position anymore
+        in Python 3.1.2, so should be sought explicitly.
+        http://bugs.python.org/issue8558
         """
         self.out.truncate(0)
+        self.out.seek(0)
 
 
 def normalize_function_parameters(text):
             return self.mock_returns
         elif self.mock_returns_iter is not None:
             try:
-                return self.mock_returns_iter.next()
+                return next(self.mock_returns_iter)
             except StopIteration:
                 raise Exception("No more mock return values are present.")
         elif self.mock_returns_func is not None: