Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
import_test.py69 linesDownload Raw Back to test
1# flake8: noqa
2import subprocess
3import sys
4import unittest
5
6_import_everything = b"""
7# The event loop is not fork-safe, and it's easy to initialize an asyncio.Future
8# at startup, which in turn creates the default event loop and prevents forking.
9# Explicitly disallow the default event loop so that an error will be raised
10# if something tries to touch it.
11import asyncio
12import warnings
13with warnings.catch_warnings():
14    warnings.simplefilter("ignore", DeprecationWarning)
15    asyncio.set_event_loop(None)
16
17import importlib
18import tornado
19
20for mod in tornado.__all__:
21    if mod == "curl_httpclient":
22        # This module has extra dependencies; skip it if they're not installed.
23        try:
24            import pycurl
25        except ImportError:
26            continue
27    importlib.import_module(f"tornado.{mod}")
28"""
29
30_import_lazy = b"""
31import sys
32import tornado
33
34if "tornado.web" in sys.modules:
35    raise Exception("unexpected eager import")
36
37# Trigger a lazy import by referring to something in a submodule.
38tornado.web.RequestHandler
39
40if "tornado.web" not in sys.modules:
41    raise Exception("lazy import did not update sys.modules")
42"""
43
44
45class ImportTest(unittest.TestCase):
46    def test_import_everything(self):
47        # Test that all Tornado modules can be imported without side effects,
48        # specifically without initializing the default asyncio event loop.
49        # Since we can't tell which modules may have already beein imported
50        # in our process, do it in a subprocess for a clean slate.
51        proc = subprocess.Popen([sys.executable], stdin=subprocess.PIPE)
52        proc.communicate(_import_everything)
53        self.assertEqual(proc.returncode, 0)
54
55    def test_lazy_import(self):
56        # Test that submodules can be referenced lazily after "import tornado"
57        proc = subprocess.Popen([sys.executable], stdin=subprocess.PIPE)
58        proc.communicate(_import_lazy)
59        self.assertEqual(proc.returncode, 0)
60
61    def test_import_aliases(self):
62        # Ensure we don't delete formerly-documented aliases accidentally.
63        import tornado
64        import asyncio
65
66        self.assertIs(tornado.ioloop.TimeoutError, tornado.util.TimeoutError)
67        self.assertIs(tornado.gen.TimeoutError, tornado.util.TimeoutError)
68        self.assertIs(tornado.util.TimeoutError, asyncio.TimeoutError)
69 
codekingpro/portable-devtools · Team Ai