Commit c1731373 authored by Tim Peters's avatar Tim Peters

Derived from SF patch #446899 Permit import of .pyw under Windows, from

David Bolen.
parent 289898cd
...@@ -16,42 +16,41 @@ else: ...@@ -16,42 +16,41 @@ else:
# Another brief digression to test the accuracy of manifest float constants. # Another brief digression to test the accuracy of manifest float constants.
import double_const # don't blink -- that *was* the test import double_const # don't blink -- that *was* the test
sys.path.insert(0, os.curdir) def test_with_extension(ext): # ext normally ".py"; perhaps ".pyw"
source = TESTFN + ext
source = TESTFN + ".py" pyo = TESTFN + ".pyo"
pyo = TESTFN + ".pyo" if sys.platform.startswith('java'):
if sys.platform.startswith('java'):
pyc = TESTFN + "$py.class" pyc = TESTFN + "$py.class"
else: else:
pyc = TESTFN + ".pyc" pyc = TESTFN + ".pyc"
f = open(source, "w") f = open(source, "w")
print >> f, "# This will test Python's ability to import a .py file" print >> f, "# This tests Python's ability to import a", ext, "file."
a = random.randrange(1000) a = random.randrange(1000)
b = random.randrange(1000) b = random.randrange(1000)
print >> f, "a =", a print >> f, "a =", a
print >> f, "b =", b print >> f, "b =", b
f.close() f.close()
try: try:
try: try:
mod = __import__(TESTFN) mod = __import__(TESTFN)
except ImportError, err: except ImportError, err:
raise ValueError, "import from .py failed: %s" % err raise ValueError("import from %s failed: %s" % (ext, err))
if mod.a != a or mod.b != b: if mod.a != a or mod.b != b:
print a, "!=", mod.a print a, "!=", mod.a
print b, "!=", mod.b print b, "!=", mod.b
raise ValueError, "module loaded (%s) but contents invalid" % mod raise ValueError("module loaded (%s) but contents invalid" % mod)
finally: finally:
os.unlink(source) os.unlink(source)
try: try:
try: try:
reload(mod) reload(mod)
except ImportError, err: except ImportError, err:
raise ValueError, "import from .pyc/.pyo failed: %s" % err raise ValueError("import from .pyc/.pyo failed: %s" % err)
finally: finally:
try: try:
os.unlink(pyc) os.unlink(pyc)
except os.error: except os.error:
...@@ -60,5 +59,13 @@ finally: ...@@ -60,5 +59,13 @@ finally:
os.unlink(pyo) os.unlink(pyo)
except os.error: except os.error:
pass 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]
...@@ -42,6 +42,7 @@ Roy Bixler ...@@ -42,6 +42,7 @@ Roy Bixler
Pablo Bleyer Pablo Bleyer
Finn Bock Finn Bock
Paul Boddie Paul Boddie
David Bolen
Jurjen Bos Jurjen Bos
Peter Bosch Peter Bosch
Eric Bouck Eric Bouck
......
...@@ -38,6 +38,11 @@ C API ...@@ -38,6 +38,11 @@ C API
sure to check the Unicode width compatibility in their extensions by sure to check the Unicode width compatibility in their extensions by
using at least one of the mangled Unicode APIs in the extension. 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? What's New in Python 2.2a1?
=========================== ===========================
......
...@@ -70,6 +70,9 @@ static const struct filedescr _PyImport_StandardFiletab[] = { ...@@ -70,6 +70,9 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
#else #else
static const struct filedescr _PyImport_StandardFiletab[] = { static const struct filedescr _PyImport_StandardFiletab[] = {
{".py", "r", PY_SOURCE}, {".py", "r", PY_SOURCE},
#ifdef MS_WIN32
{".pyw", "r", PY_SOURCE},
#endif
{".pyc", "rb", PY_COMPILED}, {".pyc", "rb", PY_COMPILED},
{0, 0} {0, 0}
}; };
...@@ -513,13 +516,19 @@ PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname) ...@@ -513,13 +516,19 @@ PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
static char * static char *
make_compiled_pathname(char *pathname, char *buf, size_t buflen) make_compiled_pathname(char *pathname, char *buf, size_t buflen)
{ {
size_t len; size_t len = strlen(pathname);
len = strlen(pathname);
if (len+2 > buflen) if (len+2 > buflen)
return NULL; 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; return buf;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment