]> granicus.if.org Git - python/commitdiff
Derived from SF patch #446899 Permit import of .pyw under Windows, from
authorTim Peters <tim.peters@gmail.com>
Sat, 4 Aug 2001 08:12:36 +0000 (08:12 +0000)
committerTim Peters <tim.peters@gmail.com>
Sat, 4 Aug 2001 08:12:36 +0000 (08:12 +0000)
David Bolen.

Lib/test/test_import.py
Misc/ACKS
Misc/NEWS
Python/import.c

index d0536095059c9da7903c6ab42508e4b404ea7d0d..b2e3fb6d3d46585768bd73367543019b414a1b07 100644 (file)
@@ -16,49 +16,56 @@ else:
 # Another brief digression to test the accuracy of manifest float constants.
 import double_const  # don't blink -- that *was* the test
 
-sys.path.insert(0, os.curdir)
-
-source = TESTFN + ".py"
-pyo = TESTFN + ".pyo"
-if sys.platform.startswith('java'):
-    pyc = TESTFN + "$py.class"
-else:
-    pyc = TESTFN + ".pyc"
+def test_with_extension(ext): # ext normally ".py"; perhaps ".pyw"
+    source = TESTFN + ext
+    pyo = TESTFN + ".pyo"
+    if sys.platform.startswith('java'):
+        pyc = TESTFN + "$py.class"
+    else:
+        pyc = TESTFN + ".pyc"
 
-f = open(source, "w")
-print >> f, "# This will test Python's ability to import a .py file"
-a = random.randrange(1000)
-b = random.randrange(1000)
-print >> f, "a =", a
-print >> f, "b =", b
-f.close()
+    f = open(source, "w")
+    print >> f, "# This tests Python's ability to import a", ext, "file."
+    a = random.randrange(1000)
+    b = random.randrange(1000)
+    print >> f, "a =", a
+    print >> f, "b =", b
+    f.close()
 
-try:
     try:
-        mod = __import__(TESTFN)
-    except ImportError, err:
-        raise ValueError, "import from .py failed: %s" % err
+        try:
+            mod = __import__(TESTFN)
+        except ImportError, err:
+            raise ValueError("import from %s failed: %s" % (ext, err))
 
-    if mod.a != a or mod.b != b:
-        print a, "!=", mod.a
-        print b, "!=", mod.b
-        raise ValueError, "module loaded (%s) but contents invalid" % mod
-finally:
-    os.unlink(source)
+        if mod.a != a or mod.b != b:
+            print a, "!=", mod.a
+            print b, "!=", mod.b
+            raise ValueError("module loaded (%s) but contents invalid" % mod)
+    finally:
+        os.unlink(source)
 
-try:
     try:
-        reload(mod)
-    except ImportError, err:
-        raise ValueError, "import from .pyc/.pyo failed: %s" % err
-finally:
-    try:
-        os.unlink(pyc)
-    except os.error:
-        pass
-    try:
-        os.unlink(pyo)
-    except os.error:
-        pass
+        try:
+            reload(mod)
+        except ImportError, err:
+            raise ValueError("import from .pyc/.pyo failed: %s" % err)
+    finally:
+        try:
+            os.unlink(pyc)
+        except os.error:
+            pass
+        try:
+            os.unlink(pyo)
+        except os.error:
+            pass
+        del sys.modules[TESTFN]
 
-del sys.path[0]
+sys.path.insert(0, os.curdir)
+try:
+    test_with_extension(".py")
+    if sys.platform.startswith("win"):
+        for ext in ".PY", ".Py", ".pY", ".pyw", ".PYW", ".pYw":
+            test_with_extension(ext)
+finally:
+    del sys.path[0]
index ddaf16a7859f7af1dce7b99198d354e3253a845c..1a4d223957330a03d9d82d69965be20e2a61dc02 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -42,6 +42,7 @@ Roy Bixler
 Pablo Bleyer
 Finn Bock
 Paul Boddie
+David Bolen
 Jurjen Bos
 Peter Bosch
 Eric Bouck
index d93ab8658a3146e19ff941b53032654ad9c0c359..a1e9fc0d9ded779e58eb44db217c9ff89cfa1ed3 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -32,12 +32,17 @@ C API
   against buffer overruns.
 
 - Unicode APIs now use name mangling to assure that mixing interpreters
-  and extensions using different Unicode widths is rendered next to 
-  impossible. Trying to import an incompatible Unicode-aware extension 
+  and extensions using different Unicode widths is rendered next to
+  impossible. Trying to import an incompatible Unicode-aware extension
   will result in an ImportError.  Unicode extensions writers must make
   sure to check the Unicode width compatibility in their extensions by
   using at least one of the mangled Unicode APIs in the extension.
 
+Windows
+
+- "import module" now compiles module.pyw if it exists and nothing else
+  relevant is found.
+
 
 What's New in Python 2.2a1?
 ===========================
index a2106de370dfbbd8387dfd2f6aeb2c6e04d2483d..8e08164d98e7e442bca91b37adb15a58a9bf9725 100644 (file)
@@ -70,6 +70,9 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
 #else
 static const struct filedescr _PyImport_StandardFiletab[] = {
        {".py", "r", PY_SOURCE},
+#ifdef MS_WIN32
+       {".pyw", "r", PY_SOURCE},
+#endif 
        {".pyc", "rb", PY_COMPILED},
        {0, 0}
 };
@@ -513,13 +516,19 @@ PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
 static char *
 make_compiled_pathname(char *pathname, char *buf, size_t buflen)
 {
-       size_t len;
-
-       len = strlen(pathname);
+       size_t len = strlen(pathname);
        if (len+2 > buflen)
                return NULL;
-       strcpy(buf, pathname);
-       strcpy(buf+len, Py_OptimizeFlag ? "o" : "c");
+
+#ifdef MS_WIN32
+       /* Treat .pyw as if it were .py.  The case of ".pyw" must match
+          that used in _PyImport_StandardFiletab. */
+       if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
+               --len;  /* pretend 'w' isn't there */
+#endif
+       memcpy(buf, pathname, len);
+       buf[len] = Py_OptimizeFlag ? 'o' : 'c';
+       buf[len+1] = '\0';
 
        return buf;
 }