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

Skip to content

Commit 64e736b

Browse files
committed
Some new blood and some updated versions.
1 parent fdb8fb8 commit 64e736b

7 files changed

Lines changed: 1105 additions & 14 deletions

File tree

Lib/dos-8x3/exceptio.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
"""Class based built-in exception hierarchy.
22
3-
This is a new feature whereby all the standard built-in exceptions,
4-
traditionally string objects, are replaced with classes. This gives
5-
Python's exception handling mechanism a more object-oriented feel.
3+
New with Python 1.5, all standard built-in exceptions are now class objects by
4+
default. This gives Python's exception handling mechanism a more
5+
object-oriented feel. Traditionally they were string objects. Python will
6+
fallback to string based exceptions if the interpreter is invoked with the -X
7+
option, or if some failure occurs during class exception initialization (in
8+
this case a warning will be printed).
69
7-
Most existing code should continue to work with class based
8-
exceptions. Some tricky uses of IOError may break, but the most
9-
common uses should work.
10+
Most existing code should continue to work with class based exceptions. Some
11+
tricky uses of IOError may break, but the most common uses should work.
1012
11-
To disable this feature, start the Python executable with the -X option.
13+
Here is a rundown of the class hierarchy. You can change this by editing this
14+
file, but it isn't recommended. The class names described here are expected
15+
to be found by the bltinmodule.c file.
1216
13-
Here is a rundown of the class hierarchy. You can change this by
14-
editing this file, but it isn't recommended. The classes with a `*'
15-
are new with this feature. They are defined as tuples containing the
16-
derived exceptions when string-based exceptions are used.
17+
The classes with a `*' are new as of Python 1.5. They are defined as tuples
18+
containing the derived exceptions when string-based exceptions are used. If
19+
you define your own class based exceptions, they should be derived from
20+
Exception.
1721
1822
Exception(*)
1923
|
@@ -22,7 +26,11 @@
2226
+-- SystemExit
2327
+-- KeyboardInterrupt
2428
+-- ImportError
25-
+-- IOError
29+
+-- EnvironmentError(*)
30+
| |
31+
| +-- IOError
32+
| +-- OSError(*)
33+
|
2634
+-- EOFError
2735
+-- RuntimeError
2836
+-- NameError

Lib/dos-8x3/posixpat.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ def normpath(path):
354354
while i < len(comps):
355355
if comps[i] == '.':
356356
del comps[i]
357+
while i < len(comps) and comps[i] == '':
358+
del comps[i]
357359
elif comps[i] == '..' and i > 0 and comps[i-1] not in ('', '..'):
358360
del comps[i-1:i+1]
359361
i = i-1

Lib/dos-8x3/py_compi.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,15 @@ def compile(file, cfile=None, dfile=None):
5151
f.close()
5252
if codestring and codestring[-1] != '\n':
5353
codestring = codestring + '\n'
54-
codeobject = __builtin__.compile(codestring, dfile or file, 'exec')
54+
try:
55+
codeobject = __builtin__.compile(codestring, dfile or file, 'exec')
56+
except SyntaxError, detail:
57+
import traceback, sys, string
58+
lines = traceback.format_exception_only(SyntaxError, detail)
59+
for line in lines:
60+
sys.stderr.write(string.replace(line, 'File "<string>"',
61+
'File "%s"' % (dfile or file)))
62+
return
5563
if not cfile:
5664
cfile = file + (__debug__ and 'c' or 'o')
5765
fc = open(cfile, 'wb')

Lib/dos-8x3/stringio.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@ def close(self):
4141
self.closed = 1
4242
del self.buf, self.pos
4343
def isatty(self):
44+
if self.closed:
45+
raise ValueError, "I/O operation on closed file"
4446
return 0
4547
def seek(self, pos, mode = 0):
48+
if self.closed:
49+
raise ValueError, "I/O operation on closed file"
4650
if self.buflist:
4751
self.buf = self.buf + string.joinfields(self.buflist, '')
4852
self.buflist = []
@@ -52,8 +56,12 @@ def seek(self, pos, mode = 0):
5256
pos = pos + self.len
5357
self.pos = max(0, pos)
5458
def tell(self):
59+
if self.closed:
60+
raise ValueError, "I/O operation on closed file"
5561
return self.pos
5662
def read(self, n = -1):
63+
if self.closed:
64+
raise ValueError, "I/O operation on closed file"
5765
if self.buflist:
5866
self.buf = self.buf + string.joinfields(self.buflist, '')
5967
self.buflist = []
@@ -65,6 +73,8 @@ def read(self, n = -1):
6573
self.pos = newpos
6674
return r
6775
def readline(self, length=None):
76+
if self.closed:
77+
raise ValueError, "I/O operation on closed file"
6878
if self.buflist:
6979
self.buf = self.buf + string.joinfields(self.buflist, '')
7080
self.buflist = []
@@ -87,6 +97,8 @@ def readlines(self):
8797
line = self.readline()
8898
return lines
8999
def write(self, s):
100+
if self.closed:
101+
raise ValueError, "I/O operation on closed file"
90102
if not s: return
91103
if self.pos > self.len:
92104
self.buflist.append('\0'*(self.pos - self.len))
@@ -105,7 +117,8 @@ def write(self, s):
105117
def writelines(self, list):
106118
self.write(string.joinfields(list, ''))
107119
def flush(self):
108-
pass
120+
if self.closed:
121+
raise ValueError, "I/O operation on closed file"
109122
def getvalue(self):
110123
if self.buflist:
111124
self.buf = self.buf + string.joinfields(self.buflist, '')

Lib/dos-8x3/test_lon.py

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
from test_support import TestFailed, verbose
2+
from string import join
3+
from random import random, randint
4+
5+
# SHIFT should match the value in longintrepr.h for best testing.
6+
SHIFT = 15
7+
BASE = 2 ** SHIFT
8+
MASK = BASE - 1
9+
10+
# Max number of base BASE digits to use in test cases. Doubling
11+
# this will at least quadruple the runtime.
12+
MAXDIGITS = 10
13+
14+
# build some special values
15+
special = map(long, [0, 1, 2, BASE, BASE >> 1])
16+
special.append(0x5555555555555555L)
17+
special.append(0xaaaaaaaaaaaaaaaaL)
18+
# some solid strings of one bits
19+
p2 = 4L # 0 and 1 already added
20+
for i in range(2*SHIFT):
21+
special.append(p2 - 1)
22+
p2 = p2 << 1
23+
del p2
24+
# add complements & negations
25+
special = special + map(lambda x: ~x, special) + \
26+
map(lambda x: -x, special)
27+
28+
# ------------------------------------------------------------ utilities
29+
30+
# Use check instead of assert so the test still does something
31+
# under -O.
32+
33+
def check(ok, *args):
34+
if not ok:
35+
raise TestFailed, join(map(str, args), " ")
36+
37+
# Get quasi-random long consisting of ndigits digits (in base BASE).
38+
# quasi == the most-significant digit will not be 0, and the number
39+
# is constructed to contain long strings of 0 and 1 bits. These are
40+
# more likely than random bits to provoke digit-boundary errors.
41+
# The sign of the number is also random.
42+
43+
def getran(ndigits):
44+
assert ndigits > 0
45+
nbits_hi = ndigits * SHIFT
46+
nbits_lo = nbits_hi - SHIFT + 1
47+
answer = 0L
48+
nbits = 0
49+
r = int(random() * (SHIFT * 2)) | 1 # force 1 bits to start
50+
while nbits < nbits_lo:
51+
bits = (r >> 1) + 1
52+
bits = min(bits, nbits_hi - nbits)
53+
assert 1 <= bits <= SHIFT
54+
nbits = nbits + bits
55+
answer = answer << bits
56+
if r & 1:
57+
answer = answer | ((1 << bits) - 1)
58+
r = int(random() * (SHIFT * 2))
59+
assert nbits_lo <= nbits <= nbits_hi
60+
if random() < 0.5:
61+
answer = -answer
62+
return answer
63+
64+
# Get random long consisting of ndigits random digits (relative to base
65+
# BASE). The sign bit is also random.
66+
67+
def getran2(ndigits):
68+
answer = 0L
69+
for i in range(ndigits):
70+
answer = (answer << SHIFT) | randint(0, MASK)
71+
if random() < 0.5:
72+
answer = -answer
73+
return answer
74+
75+
# --------------------------------------------------------------- divmod
76+
77+
def test_division_2(x, y):
78+
q, r = divmod(x, y)
79+
q2, r2 = x/y, x%y
80+
check(q == q2, "divmod returns different quotient than / for", x, y)
81+
check(r == r2, "divmod returns different mod than % for", x, y)
82+
check(x == q*y + r, "x != q*y + r after divmod on", x, y)
83+
if y > 0:
84+
check(0 <= r < y, "bad mod from divmod on", x, y)
85+
else:
86+
check(y < r <= 0, "bad mod from divmod on", x, y)
87+
88+
def test_division(maxdigits=MAXDIGITS):
89+
print "long / * % divmod"
90+
digits = range(1, maxdigits+1)
91+
for lenx in digits:
92+
x = getran(lenx)
93+
for leny in digits:
94+
y = getran(leny) or 1L
95+
test_division_2(x, y)
96+
97+
# -------------------------------------------------------------- ~ & | ^
98+
99+
def test_bitop_identities_1(x):
100+
check(x & 0 == 0, "x & 0 != 0 for", x)
101+
check(x | 0 == x, "x | 0 != x for", x)
102+
check(x ^ 0 == x, "x ^ 0 != x for", x)
103+
check(x & -1 == x, "x & -1 != x for", x)
104+
check(x | -1 == -1, "x | -1 != -1 for", x)
105+
check(x ^ -1 == ~x, "x ^ -1 != ~x for", x)
106+
check(x == ~~x, "x != ~~x for", x)
107+
check(x & x == x, "x & x != x for", x)
108+
check(x | x == x, "x | x != x for", x)
109+
check(x ^ x == 0, "x ^ x != 0 for", x)
110+
check(x & ~x == 0, "x & ~x != 0 for", x)
111+
check(x | ~x == -1, "x | ~x != -1 for", x)
112+
check(x ^ ~x == -1, "x ^ ~x != -1 for", x)
113+
check(-x == 1 + ~x == ~(x-1), "not -x == 1 + ~x == ~(x-1) for", x)
114+
for n in range(2*SHIFT):
115+
p2 = 2L ** n
116+
check(x << n >> n == x, "x << n >> n != x for", x, n)
117+
check(x / p2 == x >> n, "x / p2 != x >> n for x n p2", x, n, p2)
118+
check(x * p2 == x << n, "x * p2 != x << n for x n p2", x, n, p2)
119+
check(x & -p2 == x >> n << n == x & ~(p2 - 1),
120+
"not x & -p2 == x >> n << n == x & ~(p2 - 1) for x n p2",
121+
x, n, p2)
122+
123+
def test_bitop_identities_2(x, y):
124+
check(x & y == y & x, "x & y != y & x for", x, y)
125+
check(x | y == y | x, "x | y != y | x for", x, y)
126+
check(x ^ y == y ^ x, "x ^ y != y ^ x for", x, y)
127+
check(x ^ y ^ x == y, "x ^ y ^ x != y for", x, y)
128+
check(x & y == ~(~x | ~y), "x & y != ~(~x | ~y) for", x, y)
129+
check(x | y == ~(~x & ~y), "x | y != ~(~x & ~y) for", x, y)
130+
check(x ^ y == (x | y) & ~(x & y),
131+
"x ^ y != (x | y) & ~(x & y) for", x, y)
132+
check(x ^ y == (x & ~y) | (~x & y),
133+
"x ^ y == (x & ~y) | (~x & y) for", x, y)
134+
check(x ^ y == (x | y) & (~x | ~y),
135+
"x ^ y == (x | y) & (~x | ~y) for", x, y)
136+
137+
def test_bitop_identities_3(x, y, z):
138+
check((x & y) & z == x & (y & z),
139+
"(x & y) & z != x & (y & z) for", x, y, z)
140+
check((x | y) | z == x | (y | z),
141+
"(x | y) | z != x | (y | z) for", x, y, z)
142+
check((x ^ y) ^ z == x ^ (y ^ z),
143+
"(x ^ y) ^ z != x ^ (y ^ z) for", x, y, z)
144+
check(x & (y | z) == (x & y) | (x & z),
145+
"x & (y | z) != (x & y) | (x & z) for", x, y, z)
146+
check(x | (y & z) == (x | y) & (x | z),
147+
"x | (y & z) != (x | y) & (x | z) for", x, y, z)
148+
149+
def test_bitop_identities(maxdigits=MAXDIGITS):
150+
print "long bit-operation identities"
151+
for x in special:
152+
test_bitop_identities_1(x)
153+
digits = range(1, maxdigits+1)
154+
for lenx in digits:
155+
x = getran(lenx)
156+
test_bitop_identities_1(x)
157+
for leny in digits:
158+
y = getran(leny)
159+
test_bitop_identities_2(x, y)
160+
test_bitop_identities_3(x, y, getran((lenx + leny)/2))
161+
162+
# ------------------------------------------------------ hex oct str atol
163+
164+
def slow_format(x, base):
165+
if (x, base) == (0, 8):
166+
# this is an oddball!
167+
return "0L"
168+
digits = []
169+
sign = 0
170+
if x < 0:
171+
sign, x = 1, -x
172+
while x:
173+
x, r = divmod(x, base)
174+
digits.append(int(r))
175+
digits.reverse()
176+
digits = digits or [0]
177+
return '-'[:sign] + \
178+
{8: '0', 10: '', 16: '0x'}[base] + \
179+
join(map(lambda i: "0123456789ABCDEF"[i], digits), '') + \
180+
"L"
181+
182+
def test_format_1(x):
183+
from string import atol
184+
for base, mapper in (8, oct), (10, str), (16, hex):
185+
got = mapper(x)
186+
expected = slow_format(x, base)
187+
check(got == expected, mapper.__name__, "returned",
188+
got, "but expected", expected, "for", x)
189+
check(atol(got, 0) == x, 'atol("%s", 0) !=' % got, x)
190+
191+
def test_format(maxdigits=MAXDIGITS):
192+
print "long str/hex/oct/atol"
193+
for x in special:
194+
test_format_1(x)
195+
for i in range(10):
196+
for lenx in range(1, maxdigits+1):
197+
x = getran(lenx)
198+
test_format_1(x)
199+
200+
# ----------------------------------------------------------------- misc
201+
202+
def test_misc(maxdigits=MAXDIGITS):
203+
print "long miscellaneous operations"
204+
import sys
205+
206+
# check the extremes in int<->long conversion
207+
hugepos = sys.maxint
208+
hugeneg = -hugepos - 1
209+
hugepos_aslong = long(hugepos)
210+
hugeneg_aslong = long(hugeneg)
211+
check(hugepos == hugepos_aslong, "long(sys.maxint) != sys.maxint")
212+
check(hugeneg == hugeneg_aslong,
213+
"long(-sys.maxint-1) != -sys.maxint-1")
214+
215+
# long -> int should not fail for hugepos_aslong or hugeneg_aslong
216+
try:
217+
check(int(hugepos_aslong) == hugepos,
218+
"converting sys.maxint to long and back to int fails")
219+
except OverflowError:
220+
raise TestFailed, "int(long(sys.maxint)) overflowed!"
221+
try:
222+
check(int(hugeneg_aslong) == hugeneg,
223+
"converting -sys.maxint-1 to long and back to int fails")
224+
except OverflowError:
225+
raise TestFailed, "int(long(-sys.maxint-1)) overflowed!"
226+
227+
# but long -> int should overflow for hugepos+1 and hugeneg-1
228+
x = hugepos_aslong + 1
229+
try:
230+
int(x)
231+
raise ValueError
232+
except OverflowError:
233+
pass
234+
except:
235+
raise TestFailed, "int(long(sys.maxint) + 1) didn't overflow"
236+
237+
x = hugeneg_aslong - 1
238+
try:
239+
int(x)
240+
raise ValueError
241+
except OverflowError:
242+
pass
243+
except:
244+
raise TestFailed, "int(long(-sys.maxint-1) - 1) didn't overflow"
245+
246+
# ---------------------------------------------------------------- do it
247+
248+
test_division()
249+
test_bitop_identities()
250+
test_format()
251+
test_misc()
252+

0 commit comments

Comments
 (0)