Mike Bayer avatar Mike Bayer committed b3c4390

- more fixes for #163, make sure env.py load figures out to load env.pyc or
env.pyo as well

Comments (0)

Files changed (3)

     path = os.path.join(dir_, filename)
     _, ext = os.path.splitext(filename)
     if ext == ".py":
-        module = load_module_py(module_id, path)
+        if os.path.exists(path):
+            module = load_module_py(module_id, path)
+        elif os.path.exists(simple_pyc_file_from_path(path)):
+            # look for sourceless load
+            module = load_module_pyc(module_id, simple_pyc_file_from_path(path))
+        else:
+            raise ImportError("Can't find Python file %s" % path)
     elif ext in (".pyc", ".pyo"):
         module = load_module_pyc(module_id, path)
     del sys.modules[module_id]
     return module
 
 def simple_pyc_file_from_path(path):
+    """Given a python source path, return the so-called
+    "sourceless" .pyc or .pyo path.
+
+    This just a .pyc or .pyo file where the .py file would be.
+
+    Even with PEP-3147, which normally puts .pyc/.pyo files in __pycache__,
+    this use case remains supported as a so-called "sourceless module import".
+
+    """
     if sys.flags.optimize:
         return path + "o"  # e.g. .pyo
     else:

tests/__init__.py

     return Config(os.path.join(staging_directory, 'test_alembic.ini'))
 
 
-def staging_env(create=True, template="generic"):
+def staging_env(create=True, template="generic", sourceless=False):
     from alembic import command, script
     cfg = _testing_config()
     if create:
         if os.path.exists(path):
             shutil.rmtree(path)
         command.init(cfg, path)
+        if sourceless:
+            try:
+                # do an import so that a .pyc/.pyo is generated.
+                util.load_python_file(path, 'env.py')
+            except AttributeError:
+                # we don't have the migration context set up yet
+                # so running the .env py throws this exception.
+                # theoretically we could be using py_compiler here to
+                # generate .pyc/.pyo without importing but not really
+                # worth it.
+                pass
+            make_sourceless(os.path.join(path, "env.py"))
+
     sc = script.ScriptDirectory.from_config(cfg)
     return sc
 
     script.nextrev = old.nextrev
 
     if sourceless:
-        # note that if -O is set, you'd see pyo files here,
-        # the pyc util function looks at sys.flags.optimize to handle this
-        assert os.access(pyc_path, os.F_OK)
-        # look for a non-pep3147 path here.
-        # if not present, need to copy from __pycache__
-        simple_pyc_path = util.simple_pyc_file_from_path(path)
-        if not os.access(simple_pyc_path, os.F_OK):
-            shutil.copyfile(pyc_path, simple_pyc_path)
-        os.unlink(path)
+        make_sourceless(path)
+
+def make_sourceless(path):
+    # note that if -O is set, you'd see pyo files here,
+    # the pyc util function looks at sys.flags.optimize to handle this
+    pyc_path = util.pyc_file_from_path(path)
+    assert os.access(pyc_path, os.F_OK)
+
+    # look for a non-pep3147 path here.
+    # if not present, need to copy from __pycache__
+    simple_pyc_path = util.simple_pyc_file_from_path(path)
 
+    if not os.access(simple_pyc_path, os.F_OK):
+        shutil.copyfile(pyc_path, simple_pyc_path)
+    os.unlink(path)
 
 def three_rev_fixture(cfg):
     a = util.rev_id()

tests/test_versioning.py

 
     @classmethod
     def setup_class(cls):
-        cls.env = staging_env()
+        cls.env = staging_env(sourceless=cls.sourceless)
         cls.cfg = _sqlite_testing_config()
 
     @classmethod
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.