From: Ben Darnell Date: Sun, 21 Jun 2015 17:57:28 +0000 (-0400) Subject: Clear __package__ in script run by autoreload CLI. X-Git-Tag: v4.3.0b1~95 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b647b842b80f812dbc1e6b692278dcd2f28129c0;p=thirdparty%2Ftornado.git Clear __package__ in script run by autoreload CLI. 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 --- diff --git a/tornado/autoreload.py b/tornado/autoreload.py index a52ddde40..1cbf26c6c 100644 --- a/tornado/autoreload.py +++ b/tornado/autoreload.py @@ -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()