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

Skip to content

Commit 7aeb4b9

Browse files
committed
* Lib/linecache.py: don't crash on empty filename
* Lib/macpath.py: don't return trailing colon for dirname() (XXX won't do for volume names -- but otherwise glob(':*:*.py') loops forever) * Lib/traceback.py: print SyntaxError correctly * Lib/stat.py: moved to posixstat.py; added macstat.py which has the constants for the Mac; and created new stat.py which includes the right one * Lib/urllib.py: fix caching bug (by disabling the cache)
1 parent 9e1e149 commit 7aeb4b9

6 files changed

Lines changed: 131 additions & 18 deletions

File tree

Lib/linecache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def checkcache():
5959
def updatecache(filename):
6060
if cache.has_key(filename):
6161
del cache[filename]
62-
if filename[0] + filename[-1] == '<>':
62+
if not filename or filename[0] + filename[-1] == '<>':
6363
return []
6464
fullname = filename
6565
try:

Lib/macpath.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def split(s):
4343
colon = 0
4444
for i in range(len(s)):
4545
if s[i] == ':': colon = i+1
46-
return s[:colon], s[colon:]
46+
return s[:colon-1], s[colon:]
4747

4848

4949
# Short interfaces to split()

Lib/macstat.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Module 'stat'
2+
#
3+
# Defines constants and functions for interpreting stat/lstat struct
4+
# as returned by os.stat() and os.lstat() (if it exists).
5+
#
6+
# Suggested usage: from stat import *
7+
#
8+
# XXX Strictly spoken, this module may have to be adapted for each POSIX
9+
# implementation; in practice, however, the numeric constants used by
10+
# stat() are almost universal (even for stat() emulations on non-UNIX
11+
# systems like MS-DOS).
12+
13+
# Indices for stat struct members in tuple returned by os.stat()
14+
15+
ST_MODE = 0
16+
ST_INO = 1
17+
ST_DEV = 2
18+
ST_NLINK = 3
19+
ST_UID = 4
20+
ST_GID = 5
21+
ST_SIZE = 6
22+
ST_ATIME = 7
23+
ST_MTIME = 8
24+
ST_CTIME = 9
25+
26+
# Extract bits from the mode
27+
28+
def S_IMODE(mode):
29+
return 0
30+
31+
def S_IFMT(mode):
32+
return mode & 0xFFFF
33+
34+
# Constants used as S_IFMT() for various file types
35+
# (not all are implemented on all systems)
36+
37+
S_IFDIR = 0x0000
38+
S_IFREG = 0x0003
39+
40+
# Functions to test for each file type
41+
42+
def S_ISDIR(mode):
43+
return S_IFMT(mode) == S_IFDIR
44+
45+
def S_ISCHR(mode):
46+
return 0
47+
48+
def S_ISBLK(mode):
49+
return 0
50+
51+
def S_ISREG(mode):
52+
return S_IFMT(mode) == S_IFREG
53+
54+
def S_ISFIFO(mode):
55+
return 0
56+
57+
def S_ISLNK(mode):
58+
return 0
59+
60+
def S_ISSOCK(mode):
61+
return 0
62+
63+
# Names for permission bits
64+
65+
S_ISUID = 04000
66+
S_ISGID = 02000
67+
S_ENFMT = S_ISGID
68+
S_ISVTX = 01000
69+
S_IREAD = 00400
70+
S_IWRITE = 00200
71+
S_IEXEC = 00100
72+
S_IRWXU = 00700
73+
S_IRUSR = 00400
74+
S_IWUSR = 00200
75+
S_IXUSR = 00100
76+
S_IRWXG = 00070
77+
S_IRGRP = 00040
78+
S_IWGRP = 00020
79+
S_IXGRP = 00010
80+
S_IRWXO = 00007
81+
S_IROTH = 00004
82+
S_IWOTH = 00002
83+
S_IXOTH = 00001

Lib/stat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# XXX Strictly spoken, this module may have to be adapted for each POSIX
99
# implementation; in practice, however, the numeric constants used by
1010
# stat() are almost universal (even for stat() emulations on non-UNIX
11-
# systems like Macintosh or MS-DOS).
11+
# systems like MS-DOS).
1212

1313
# Indices for stat struct members in tuple returned by os.stat()
1414

Lib/traceback.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,33 @@ def extract_tb(tb, limit = None):
4242
return list
4343

4444
def print_exception(type, value, tb, limit = None):
45-
print 'Traceback (innermost last):'
46-
print_tb(tb, limit)
47-
print type,
48-
if value is not None: print ':', value,
49-
print
45+
if tb:
46+
print 'Traceback (innermost last):'
47+
print_tb(tb, limit)
48+
if value is None:
49+
print type
50+
else:
51+
if type is SyntaxError:
52+
try:
53+
msg, (filename, lineno, offset, line) = value
54+
except:
55+
pass
56+
else:
57+
if not filename: filename = "<string>"
58+
print ' File "%s", line %d' % (filename, lineno)
59+
i = 0
60+
while i < len(line) and line[i] in string.whitespace:
61+
i = i+1
62+
s = ' '
63+
print s + string.strip(line)
64+
for c in line[i:offset-1]:
65+
if c in string.whitespace:
66+
s = s + c
67+
else:
68+
s = s + ' '
69+
print s + '^'
70+
value = msg
71+
print '%s: %s' % (type, value)
5072

5173
def print_exc(limit = None):
5274
print_exception(sys.exc_type, sys.exc_value, sys.exc_traceback,

Lib/urllib.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ class URLopener:
5252
# Constructor
5353
def __init__(self):
5454
self.addheaders = []
55-
self.tempcache = {}
55+
self.tempcache = None
56+
# Undocumented feature: if you assign {} to tempcache,
57+
# it is used to cache files retrieved with
58+
# self.retrieve(). This is not enabled by default
59+
# since it does not work for changing documents (and I
60+
# haven't got the logic to check expiration headers
61+
# yet).
5662
self.ftpcache = ftpcache
5763
# Undocumented feature: you can use a different
5864
# ftp cache by assigning to the .ftpcache member;
@@ -66,12 +72,13 @@ def close(self):
6672

6773
def cleanup(self):
6874
import os
69-
for url in self.tempcache.keys():
70-
try:
71-
os.unlink(self.tempcache[url][0])
72-
except os.error:
73-
pass
74-
del self.tempcache[url]
75+
if self.tempcache:
76+
for url in self.tempcache.keys():
77+
try:
78+
os.unlink(self.tempcache[url][0])
79+
except os.error:
80+
pass
81+
del self.tempcache[url]
7582

7683
# Add a header to be used by the HTTP interface only
7784
# e.g. u.addheader('Accept', 'sound/basic')
@@ -98,10 +105,10 @@ def open(self, url):
98105
# retrieve(url) returns (filename, None) for a local object
99106
# or (tempfilename, headers) for a remote object
100107
def retrieve(self, url):
101-
if self.tempcache.has_key(url):
108+
if self.tempcache and self.tempcache.has_key(url):
102109
return self.tempcache[url]
103110
url1 = unwrap(url)
104-
if self.tempcache.has_key(url1):
111+
if self.tempcache and self.tempcache.has_key(url1):
105112
self.tempcache[url] = self.tempcache[url1]
106113
return self.tempcache[url1]
107114
type, url1 = splittype(url1)
@@ -116,7 +123,8 @@ def retrieve(self, url):
116123
headers = fp.info()
117124
import tempfile
118125
tfn = tempfile.mktemp()
119-
self.tempcache[url] = result = tfn, headers
126+
if self.tempcache is not None:
127+
self.tempcache[url] = result = tfn, headers
120128
tfp = open(tfn, 'w')
121129
bs = 1024*8
122130
block = fp.read(bs)

0 commit comments

Comments
 (0)