(self)
| 105 | return out |
| 106 | |
| 107 | def test_reload(self): |
| 108 | main = """\ |
| 109 | import sys |
| 110 | |
| 111 | # In module mode, the path is set to the parent directory and we can import testapp. |
| 112 | try: |
| 113 | import testapp |
| 114 | except ImportError: |
| 115 | print("import testapp failed") |
| 116 | else: |
| 117 | print("import testapp succeeded") |
| 118 | |
| 119 | spec = getattr(sys.modules[__name__], '__spec__', None) |
| 120 | print(f"Starting {__name__=}, __spec__.name={getattr(spec, 'name', None)}") |
| 121 | exec(open("run_twice_magic.py", encoding="utf-8").read()) |
| 122 | """ |
| 123 | |
| 124 | # Create temporary test application |
| 125 | self.write_files( |
| 126 | { |
| 127 | "testapp": { |
| 128 | "__init__.py": "", |
| 129 | "__main__.py": main, |
| 130 | }, |
| 131 | } |
| 132 | ) |
| 133 | |
| 134 | # The autoreload wrapper should support all the same modes as the python interpreter. |
| 135 | # The wrapper itself should have no effect on this test so we try all modes with and |
| 136 | # without it. |
| 137 | for wrapper in [False, True]: |
| 138 | with self.subTest(wrapper=wrapper): |
| 139 | with self.subTest(mode="module"): |
| 140 | if wrapper: |
| 141 | base_args = [sys.executable, "-m", "tornado.autoreload"] |
| 142 | else: |
| 143 | base_args = [sys.executable] |
| 144 | # In module mode, the path is set to the parent directory and we can import |
| 145 | # testapp. Also, the __spec__.name is set to the fully qualified module name. |
| 146 | out = self.run_subprocess(base_args + ["-m", "testapp"]) |
| 147 | self.assertEqual( |
| 148 | out, |
| 149 | ( |
| 150 | "import testapp succeeded\n" |
| 151 | + "Starting __name__='__main__', __spec__.name=testapp.__main__\n" |
| 152 | ) |
| 153 | * 2, |
| 154 | ) |
| 155 | |
| 156 | with self.subTest(mode="file"): |
| 157 | out = self.run_subprocess(base_args + ["testapp/__main__.py"]) |
| 158 | # In file mode, we do not expect the path to be set so we can import testapp, |
| 159 | # but when the wrapper is used the -m argument to the python interpreter |
| 160 | # does this for us. |
| 161 | expect_import = ( |
| 162 | "import testapp succeeded" |
| 163 | if wrapper |
| 164 | else "import testapp failed" |
nothing calls this directly
no test coverage detected