]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Move autoreload's sys.path manipulations earlier in the file, before
authorBen Darnell <ben@bendarnell.com>
Tue, 21 Feb 2012 04:31:33 +0000 (20:31 -0800)
committerBen Darnell <ben@bendarnell.com>
Tue, 6 Mar 2012 05:52:56 +0000 (21:52 -0800)
as many imports as possible.

This fixes an edge case discovered in a branch where I introduced
a module named tornado.concurrent.

tornado/autoreload.py

index 7da8df8880a84ececf31efb6b742ca2b8888bc25..84975067d0eb7bf6f40f42ed34c2bf918f040656 100644 (file)
@@ -28,6 +28,34 @@ multi-process mode is used.
 
 from __future__ import absolute_import, division, with_statement
 
+import os
+import sys
+
+if __name__ == "__main__":
+    # If this module is run with "python -m tornado.autoreload", the current
+    # directory is automatically prepended to sys.path, but not if it is
+    # run as "path/to/tornado/autoreload.py".  The processing for "-m" rewrites
+    # the former to the latter, so subsequent executions won't have the same
+    # path as the original.  Modify os.environ here to ensure that the
+    # re-executed process will have the same path.
+    #
+    # Conversely, when run as path/to/tornado/autoreload.py, the directory
+    # containing autoreload.py gets added to the path, but we don't want
+    # tornado modules importable at top level, so remove it.
+    #
+    # This sys.path manipulation must come before our imports (as much
+    # as possible - if we introduced a tornado.sys or tornado.os
+    # module we'd be in trouble), or else our imports would become
+    # relative again despite the future import.
+    #
+    # There is a separate __main__ block at the end of the file to call main().
+    path_prefix = '.' + os.pathsep
+    if (sys.path[0] == '' and
+        not os.environ.get("PYTHONPATH", "").startswith(path_prefix)):
+        os.environ["PYTHONPATH"] = path_prefix + os.environ.get("PYTHONPATH", "")
+    if sys.path[0] == os.path.dirname(__file__):
+        del sys.path[0]
+
 import functools
 import logging
 import os
@@ -244,19 +272,6 @@ def main():
 
 
 if __name__ == "__main__":
-    # If this module is run with "python -m tornado.autoreload", the current
-    # directory is automatically prepended to sys.path, but not if it is
-    # run as "path/to/tornado/autoreload.py".  The processing for "-m" rewrites
-    # the former to the latter, so subsequent executions won't have the same
-    # path as the original.  Modify os.environ here to ensure that the
-    # re-executed process will have the same path.
-    # Conversely, when run as path/to/tornado/autoreload.py, the directory
-    # containing autoreload.py gets added to the path, but we don't want
-    # tornado modules importable at top level, so remove it.
-    path_prefix = '.' + os.pathsep
-    if (sys.path[0] == '' and
-        not os.environ.get("PYTHONPATH", "").startswith(path_prefix)):
-        os.environ["PYTHONPATH"] = path_prefix + os.environ.get("PYTHONPATH", "")
-    elif sys.path[0] == os.path.dirname(__file__):
-        del sys.path[0]
+    # See also the other __main__ block at the top of the file, which modifies
+    # sys.path before our imports
     main()