def test_install_aliases(self): """ Does the install_aliases() interface monkey-patch urllib etc. successfully? """ from future.standard_library import remove_hooks, install_aliases remove_hooks() install_aliases() from collections import Counter, OrderedDict # backported to Py2.6 from collections import UserDict, UserList, UserString # Requires Python dbm support: # import dbm # import dbm.dumb # import dbm.gnu # import dbm.ndbm from itertools import filterfalse, zip_longest from subprocess import check_output # backported to Py2.6 from subprocess import getoutput, getstatusoutput from sys import intern # test_support may not be available (e.g. on Anaconda Py2.6): # import test.support import urllib.error import urllib.parse import urllib.request import urllib.response import urllib.robotparser self.assertTrue('urlopen' in dir(urllib.request))
def test_remove_hooks2(self): """ As above, but with the new names """ example_PY2_check = False standard_library.install_hooks() old_meta_path = copy.copy(sys.meta_path) standard_library.remove_hooks() standard_library.scrub_future_sys_modules() if utils.PY2: self.assertTrue(len(old_meta_path) == len(sys.meta_path) + 1) else: self.assertTrue(len(old_meta_path) == len(sys.meta_path)) # An example of fragile import code that we don't want to break: try: import builtins except ImportError: example_PY2_check = True if utils.PY2: self.assertTrue(example_PY2_check) else: self.assertFalse(example_PY2_check) standard_library.install_hooks() # The import should succeed again now: import builtins self.assertTrue(len(old_meta_path) == len(sys.meta_path))
def test_urllib_imports_install_hooks(self): standard_library.remove_hooks() standard_library.install_hooks() import urllib import urllib.parse import urllib.request import urllib.robotparser import urllib.error import urllib.response self.assertTrue(True)
def test_import_future_standard_library(self): """ Does futurized Py3-like code like this work under autotranslation?? """ code = """ from future import standard_library standard_library.install_hooks() import configparser """ module = self.write_and_import(code, 'future_standard_library') self.assertTrue('configparser' in dir(module)) from future import standard_library standard_library.remove_hooks()
def test_remove_hooks2(self): """ This verifies that modules like http.client are no longer accessible after disabling import hooks, even if they have been previously imported. The reason for this test is that Python caches imported modules in sys.modules. """ standard_library.remove_hooks() try: from . import verify_remove_hooks_affects_imported_modules except RuntimeError as e: self.fail(e.message) finally: standard_library.install_hooks()
def test_requests(self): code = """ from future import standard_library standard_library.install_hooks() import urllib.response import html.parser """ with write_module(code, self.tempdir): import test_imports_future_stdlib standard_library.remove_hooks() import requests r = requests.get('http://google.com') self.assertTrue(True)
def test_detect_hooks(self): """ Tests whether the future.standard_library.detect_hooks is doing its job. """ standard_library.install_hooks() if utils.PY2: self.assertTrue(standard_library.detect_hooks()) meta_path = copy.copy(sys.meta_path) standard_library.remove_hooks() if utils.PY2: self.assertEqual(len(meta_path), len(sys.meta_path) + 1) self.assertFalse(standard_library.detect_hooks())
def test_requests(self): with open(self.tempdir + 'test_imports_future_stdlib.py', 'w') as f: f.write('from future import standard_library') # print('sys.meta_path is: ', sys.meta_path) sys.path.insert(0, self.tempdir) print('Importing test_imports_future_stdlib ...') try: import test_imports_future_stdlib standard_library.remove_hooks() import requests r = requests.get('http://google.com') self.assertTrue(True) except Exception as e: raise e else: print('Succeeded!') finally: sys.path.remove(self.tempdir)
def test_remove_hooks_then_requests(self): code = """ from future import standard_library standard_library.install_hooks() import builtins import http.client import html.parser """ with write_module(code, self.tempdir): import test_imports_future_stdlib standard_library.remove_hooks() try: import requests except ImportError: print("Requests doesn't seem to be available. Skipping requests test ...") else: r = requests.get('http://google.com') self.assertTrue(r) self.assertTrue(True)
def test_remove_hooks_then_requests(self): code = """ from future import standard_library standard_library.install_hooks() import builtins import http.client import html.parser """ with write_module(code, self.tempdir): import test_imports_future_stdlib standard_library.remove_hooks() try: import requests except ImportError: print( "Requests doesn't seem to be available. Skipping requests test ..." ) else: r = requests.get('http://google.com') self.assertTrue(r) self.assertTrue(True)
def test_remove_hooks(self): example_PY2_check = False standard_library.install_hooks() old_meta_path = copy.copy(sys.meta_path) import builtins standard_library.remove_hooks() self.assertTrue(len(old_meta_path) == len(sys.meta_path) + 1) # An example of fragile import code that we don't want to break: try: import builtins except ImportError: example_PY2_check = True if utils.PY2: self.assertTrue(example_PY2_check) else: self.assertFalse(example_PY2_check) standard_library.install_hooks() # The import should succeed again now: import builtins self.assertTrue(len(old_meta_path) == len(sys.meta_path))
def tearDown(self): standard_library.remove_hooks()
from future.tests.base import unittest from future.utils import PY26 import sys import random standard_library.install_hooks() try: from test import support except ImportError: def cpython_only(f): return f else: cpython_only = support.cpython_only standard_library.remove_hooks() L = [ ('0', 0), ('1', 1), ('9', 9), ('10', 10), ('99', 99), ('100', 100), ('314', 314), (' 314', 314), ('314 ', 314), (' \t\t 314 \t\t ', 314), (repr(sys.maxsize), sys.maxsize), (' 1x', ValueError), (' 1 ', 1),