Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c173137

Browse files
committed
Derived from SF patch #446899 Permit import of .pyw under Windows, from
David Bolen.
1 parent 289898c commit c173137

4 files changed

Lines changed: 68 additions & 46 deletions

File tree

Lib/test/test_import.py

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,56 @@
1616
# Another brief digression to test the accuracy of manifest float constants.
1717
import double_const # don't blink -- that *was* the test
1818

19-
sys.path.insert(0, os.curdir)
20-
21-
source = TESTFN + ".py"
22-
pyo = TESTFN + ".pyo"
23-
if sys.platform.startswith('java'):
24-
pyc = TESTFN + "$py.class"
25-
else:
26-
pyc = TESTFN + ".pyc"
19+
def test_with_extension(ext): # ext normally ".py"; perhaps ".pyw"
20+
source = TESTFN + ext
21+
pyo = TESTFN + ".pyo"
22+
if sys.platform.startswith('java'):
23+
pyc = TESTFN + "$py.class"
24+
else:
25+
pyc = TESTFN + ".pyc"
2726

28-
f = open(source, "w")
29-
print >> f, "# This will test Python's ability to import a .py file"
30-
a = random.randrange(1000)
31-
b = random.randrange(1000)
32-
print >> f, "a =", a
33-
print >> f, "b =", b
34-
f.close()
27+
f = open(source, "w")
28+
print >> f, "# This tests Python's ability to import a", ext, "file."
29+
a = random.randrange(1000)
30+
b = random.randrange(1000)
31+
print >> f, "a =", a
32+
print >> f, "b =", b
33+
f.close()
3534

36-
try:
3735
try:
38-
mod = __import__(TESTFN)
39-
except ImportError, err:
40-
raise ValueError, "import from .py failed: %s" % err
36+
try:
37+
mod = __import__(TESTFN)
38+
except ImportError, err:
39+
raise ValueError("import from %s failed: %s" % (ext, err))
4140

42-
if mod.a != a or mod.b != b:
43-
print a, "!=", mod.a
44-
print b, "!=", mod.b
45-
raise ValueError, "module loaded (%s) but contents invalid" % mod
46-
finally:
47-
os.unlink(source)
41+
if mod.a != a or mod.b != b:
42+
print a, "!=", mod.a
43+
print b, "!=", mod.b
44+
raise ValueError("module loaded (%s) but contents invalid" % mod)
45+
finally:
46+
os.unlink(source)
4847

49-
try:
5048
try:
51-
reload(mod)
52-
except ImportError, err:
53-
raise ValueError, "import from .pyc/.pyo failed: %s" % err
54-
finally:
55-
try:
56-
os.unlink(pyc)
57-
except os.error:
58-
pass
59-
try:
60-
os.unlink(pyo)
61-
except os.error:
62-
pass
49+
try:
50+
reload(mod)
51+
except ImportError, err:
52+
raise ValueError("import from .pyc/.pyo failed: %s" % err)
53+
finally:
54+
try:
55+
os.unlink(pyc)
56+
except os.error:
57+
pass
58+
try:
59+
os.unlink(pyo)
60+
except os.error:
61+
pass
62+
del sys.modules[TESTFN]
6363

64-
del sys.path[0]
64+
sys.path.insert(0, os.curdir)
65+
try:
66+
test_with_extension(".py")
67+
if sys.platform.startswith("win"):
68+
for ext in ".PY", ".Py", ".pY", ".pyw", ".PYW", ".pYw":
69+
test_with_extension(ext)
70+
finally:
71+
del sys.path[0]

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Roy Bixler
4242
Pablo Bleyer
4343
Finn Bock
4444
Paul Boddie
45+
David Bolen
4546
Jurjen Bos
4647
Peter Bosch
4748
Eric Bouck

Misc/NEWS

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,17 @@ C API
3232
against buffer overruns.
3333

3434
- Unicode APIs now use name mangling to assure that mixing interpreters
35-
and extensions using different Unicode widths is rendered next to
36-
impossible. Trying to import an incompatible Unicode-aware extension
35+
and extensions using different Unicode widths is rendered next to
36+
impossible. Trying to import an incompatible Unicode-aware extension
3737
will result in an ImportError. Unicode extensions writers must make
3838
sure to check the Unicode width compatibility in their extensions by
3939
using at least one of the mangled Unicode APIs in the extension.
4040

41+
Windows
42+
43+
- "import module" now compiles module.pyw if it exists and nothing else
44+
relevant is found.
45+
4146

4247
What's New in Python 2.2a1?
4348
===========================

Python/import.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
7070
#else
7171
static const struct filedescr _PyImport_StandardFiletab[] = {
7272
{".py", "r", PY_SOURCE},
73+
#ifdef MS_WIN32
74+
{".pyw", "r", PY_SOURCE},
75+
#endif
7376
{".pyc", "rb", PY_COMPILED},
7477
{0, 0}
7578
};
@@ -513,13 +516,19 @@ PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
513516
static char *
514517
make_compiled_pathname(char *pathname, char *buf, size_t buflen)
515518
{
516-
size_t len;
517-
518-
len = strlen(pathname);
519+
size_t len = strlen(pathname);
519520
if (len+2 > buflen)
520521
return NULL;
521-
strcpy(buf, pathname);
522-
strcpy(buf+len, Py_OptimizeFlag ? "o" : "c");
522+
523+
#ifdef MS_WIN32
524+
/* Treat .pyw as if it were .py. The case of ".pyw" must match
525+
that used in _PyImport_StandardFiletab. */
526+
if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
527+
--len; /* pretend 'w' isn't there */
528+
#endif
529+
memcpy(buf, pathname, len);
530+
buf[len] = Py_OptimizeFlag ? 'o' : 'c';
531+
buf[len+1] = '\0';
523532

524533
return buf;
525534
}

0 commit comments

Comments
 (0)