]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Clear __package__ in script run by autoreload CLI.
authorBen Darnell <ben@bendarnell.com>
Sun, 21 Jun 2015 17:57:28 +0000 (13:57 -0400)
committerBen Darnell <ben@bendarnell.com>
Sun, 21 Jun 2015 17:57:28 +0000 (13:57 -0400)
This prevents imports from incorrectly being interpreted as
relative to the 'tornado package, which would make the standard
'platform' module unimportable.

See bdarnell/plop#21

tornado/autoreload.py

index a52ddde40d497d92a89c1ea6fe29981b0a129a8e..1cbf26c6cb091327d2f481ce1380d42bf6701cc4 100644 (file)
@@ -289,11 +289,16 @@ def main():
             runpy.run_module(module, run_name="__main__", alter_sys=True)
         elif mode == "script":
             with open(script) as f:
+                # Execute the script in our namespace instead of creating
+                # a new one so that something that tries to import __main__
+                # (e.g. the unittest module) will see names defined in the
+                # script instead of just those defined in this module.
                 global __file__
                 __file__ = script
-                # Use globals as our "locals" dictionary so that
-                # something that tries to import __main__ (e.g. the unittest
-                # module) will see the right things.
+                # If __package__ is defined, imports may be incorrectly
+                # interpreted as relative to this module.
+                global __package__
+                del __package__
                 exec_in(f.read(), globals(), globals())
     except SystemExit as e:
         logging.basicConfig()