[gtk-doc] tests/common.py: Make compatible with Python 2



commit d70bc839e6072d30b3892b4873a4590a541e6637
Author: Jan Alexander Steffens (heftig) <jan steffens gmail com>
Date:   Sat Aug 12 15:36:23 2017 +0200

    tests/common.py: Make compatible with Python 2
    
    56213947 doesn't actually work on Python 2, as mock can't find the
    'builtins.open' in the current scope.
    
    Try another method of naming 'open' with the help of six, which we
    already depend on.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=786174

 tests/common.py |   14 ++++++++------
 1 files changed, 8 insertions(+), 6 deletions(-)
---
diff --git a/tests/common.py b/tests/common.py
index ae47b31..26dd16b 100755
--- a/tests/common.py
+++ b/tests/common.py
@@ -23,16 +23,18 @@ try:
 except ImportError:
     import mock
 
-try:
-    import builtins
-except ImportError:
-    import __builtin__ as builtins
-
 import unittest
 
+from six import PY2
 from gtkdoc import common
 
 
+if PY2:
+    openname = '__builtin__.open'
+else:
+    openname = 'builtins.open'
+
+
 class TestUpdateFileIfChanged(unittest.TestCase):
 
     @mock.patch('os.path.exists')
@@ -44,7 +46,7 @@ class TestUpdateFileIfChanged(unittest.TestCase):
         self.assertTrue(res)
 
     @mock.patch('os.path.exists')
-    @mock.patch('builtins.open', mock.mock_open(read_data='bar'))
+    @mock.patch(openname, mock.mock_open(read_data='bar'))
     @mock.patch('os.unlink')
     def test_FilesAreTheSame(self, os_unlink, os_path_exists):
         os_path_exists.return_value = True


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