[2.7] bpo-30223: Add Lib/test/__main__.py. (#1373)
To unify running tests in Python 2.7 and Python 3, the test
package can be run as a script. This is equivalent to running the
test.regrtest module as a script.
diff --git a/Lib/test/__main__.py b/Lib/test/__main__.py
new file mode 100644
index 0000000..d5fbe15
--- /dev/null
+++ b/Lib/test/__main__.py
@@ -0,0 +1,3 @@
+from test import regrtest
+
+regrtest.main_in_temp_cwd()
diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py
index 138e17e..4268394 100644
--- a/Lib/test/pickletester.py
+++ b/Lib/test/pickletester.py
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
+from __future__ import absolute_import
+
import unittest
import pickle
import cPickle
diff --git a/Lib/test/regrtest.py b/Lib/test/regrtest.py
index 8017de9..cbd9185 100755
--- a/Lib/test/regrtest.py
+++ b/Lib/test/regrtest.py
@@ -1645,16 +1645,8 @@
assert self.isvalid()
return self.expected
-if __name__ == '__main__':
- # findtestdir() gets the dirname out of __file__, so we have to make it
- # absolute before changing the working directory.
- # For example __file__ may be relative when running trace or profile.
- # See issue #9323.
- __file__ = os.path.abspath(__file__)
-
- # sanity check
- assert __file__ == os.path.abspath(sys.argv[0])
-
+def main_in_temp_cwd():
+ """Run main() in a temporary working directory."""
# When tests are run from the Python build directory, it is best practice
# to keep the test files in a subfolder. It eases the cleanup of leftover
# files using command "make distclean".
@@ -1677,3 +1669,16 @@
# available from test_support.SAVEDCWD.
with test_support.temp_cwd(TESTCWD, quiet=True):
main()
+
+if __name__ == '__main__':
+ # findtestdir() gets the dirname out of __file__, so we have to make it
+ # absolute before changing the working directory.
+ # For example __file__ may be relative when running trace or profile.
+ # See issue #9323.
+ global __file__
+ __file__ = os.path.abspath(__file__)
+
+ # sanity check
+ assert __file__ == os.path.abspath(sys.argv[0])
+
+ main_in_temp_cwd()
diff --git a/Lib/test/test_memoryio.py b/Lib/test/test_memoryio.py
index 2c18ad7..d29f148 100644
--- a/Lib/test/test_memoryio.py
+++ b/Lib/test/test_memoryio.py
@@ -5,6 +5,7 @@
from __future__ import unicode_literals
from __future__ import print_function
+from __future__ import absolute_import
import unittest
from test import test_support as support