mirror of https://github.com/python/cpython
Use the preferred form of raise statements in the docs.
This commit is contained in:
parent
dad7b7b1cb
commit
c1edec3374
|
@ -204,8 +204,7 @@ length message::
|
||||||
while totalsent < MSGLEN:
|
while totalsent < MSGLEN:
|
||||||
sent = self.sock.send(msg[totalsent:])
|
sent = self.sock.send(msg[totalsent:])
|
||||||
if sent == 0:
|
if sent == 0:
|
||||||
raise RuntimeError, \
|
raise RuntimeError("socket connection broken")
|
||||||
"socket connection broken"
|
|
||||||
totalsent = totalsent + sent
|
totalsent = totalsent + sent
|
||||||
|
|
||||||
def myreceive(self):
|
def myreceive(self):
|
||||||
|
@ -213,8 +212,7 @@ length message::
|
||||||
while len(msg) < MSGLEN:
|
while len(msg) < MSGLEN:
|
||||||
chunk = self.sock.recv(MSGLEN-len(msg))
|
chunk = self.sock.recv(MSGLEN-len(msg))
|
||||||
if chunk == '':
|
if chunk == '':
|
||||||
raise RuntimeError, \
|
raise RuntimeError("socket connection broken")
|
||||||
"socket connection broken"
|
|
||||||
msg = msg + chunk
|
msg = msg + chunk
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
|
|
|
@ -37,7 +37,7 @@ Usage: %prog [options] msgfile
|
||||||
os.mkdir(opts.directory)
|
os.mkdir(opts.directory)
|
||||||
except OSError, e:
|
except OSError, e:
|
||||||
# Ignore directory exists error
|
# Ignore directory exists error
|
||||||
if e.errno <> errno.EEXIST:
|
if e.errno != errno.EEXIST:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
fp = open(msgfile)
|
fp = open(msgfile)
|
||||||
|
|
|
@ -149,21 +149,21 @@ def test():
|
||||||
except ZeroDivisionError:
|
except ZeroDivisionError:
|
||||||
print '\tGot ZeroDivisionError as expected from pool.apply()'
|
print '\tGot ZeroDivisionError as expected from pool.apply()'
|
||||||
else:
|
else:
|
||||||
raise AssertionError, 'expected ZeroDivisionError'
|
raise AssertionError('expected ZeroDivisionError')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
print pool.map(f, range(10))
|
print pool.map(f, range(10))
|
||||||
except ZeroDivisionError:
|
except ZeroDivisionError:
|
||||||
print '\tGot ZeroDivisionError as expected from pool.map()'
|
print '\tGot ZeroDivisionError as expected from pool.map()'
|
||||||
else:
|
else:
|
||||||
raise AssertionError, 'expected ZeroDivisionError'
|
raise AssertionError('expected ZeroDivisionError')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
print list(pool.imap(f, range(10)))
|
print list(pool.imap(f, range(10)))
|
||||||
except ZeroDivisionError:
|
except ZeroDivisionError:
|
||||||
print '\tGot ZeroDivisionError as expected from list(pool.imap())'
|
print '\tGot ZeroDivisionError as expected from list(pool.imap())'
|
||||||
else:
|
else:
|
||||||
raise AssertionError, 'expected ZeroDivisionError'
|
raise AssertionError('expected ZeroDivisionError')
|
||||||
|
|
||||||
it = pool.imap(f, range(10))
|
it = pool.imap(f, range(10))
|
||||||
for i in range(10):
|
for i in range(10):
|
||||||
|
@ -176,7 +176,7 @@ def test():
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
if i == 5:
|
if i == 5:
|
||||||
raise AssertionError, 'expected ZeroDivisionError'
|
raise AssertionError('expected ZeroDivisionError')
|
||||||
|
|
||||||
assert i == 9
|
assert i == 9
|
||||||
print '\tGot ZeroDivisionError as expected from IMapIterator.next()'
|
print '\tGot ZeroDivisionError as expected from IMapIterator.next()'
|
||||||
|
|
|
@ -249,7 +249,7 @@ def test(namespace=multiprocessing):
|
||||||
info = multiprocessing._debug_info()
|
info = multiprocessing._debug_info()
|
||||||
if info:
|
if info:
|
||||||
print info
|
print info
|
||||||
raise ValueError, 'there should be no positive refcounts left'
|
raise ValueError('there should be no positive refcounts left')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -271,6 +271,6 @@ if __name__ == '__main__':
|
||||||
import multiprocessing.dummy as namespace
|
import multiprocessing.dummy as namespace
|
||||||
else:
|
else:
|
||||||
print 'Usage:\n\t%s [processes | manager | threads]' % sys.argv[0]
|
print 'Usage:\n\t%s [processes | manager | threads]' % sys.argv[0]
|
||||||
raise SystemExit, 2
|
raise SystemExit(2)
|
||||||
|
|
||||||
test(namespace)
|
test(namespace)
|
||||||
|
|
|
@ -52,7 +52,8 @@ A simple example illustrating typical use::
|
||||||
cryptedpasswd = pwd.getpwnam(username)[1]
|
cryptedpasswd = pwd.getpwnam(username)[1]
|
||||||
if cryptedpasswd:
|
if cryptedpasswd:
|
||||||
if cryptedpasswd == 'x' or cryptedpasswd == '*':
|
if cryptedpasswd == 'x' or cryptedpasswd == '*':
|
||||||
raise "Sorry, currently no support for shadow passwords"
|
raise NotImplementedError(
|
||||||
|
"Sorry, currently no support for shadow passwords")
|
||||||
cleartext = getpass.getpass()
|
cleartext = getpass.getpass()
|
||||||
return crypt.crypt(cleartext, cryptedpasswd) == cryptedpasswd
|
return crypt.crypt(cleartext, cryptedpasswd) == cryptedpasswd
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -160,7 +160,7 @@ This code is intended to be read, not executed. However, it does work
|
||||||
parent = None
|
parent = None
|
||||||
q = import_module(head, qname, parent)
|
q = import_module(head, qname, parent)
|
||||||
if q: return q, tail
|
if q: return q, tail
|
||||||
raise ImportError, "No module named " + qname
|
raise ImportError("No module named " + qname)
|
||||||
|
|
||||||
def load_tail(q, tail):
|
def load_tail(q, tail):
|
||||||
m = q
|
m = q
|
||||||
|
@ -171,7 +171,7 @@ This code is intended to be read, not executed. However, it does work
|
||||||
mname = "%s.%s" % (m.__name__, head)
|
mname = "%s.%s" % (m.__name__, head)
|
||||||
m = import_module(head, mname, m)
|
m = import_module(head, mname, m)
|
||||||
if not m:
|
if not m:
|
||||||
raise ImportError, "No module named " + mname
|
raise ImportError("No module named " + mname)
|
||||||
return m
|
return m
|
||||||
|
|
||||||
def ensure_fromlist(m, fromlist, recursive=0):
|
def ensure_fromlist(m, fromlist, recursive=0):
|
||||||
|
@ -189,7 +189,7 @@ This code is intended to be read, not executed. However, it does work
|
||||||
subname = "%s.%s" % (m.__name__, sub)
|
subname = "%s.%s" % (m.__name__, sub)
|
||||||
submod = import_module(sub, subname, m)
|
submod = import_module(sub, subname, m)
|
||||||
if not submod:
|
if not submod:
|
||||||
raise ImportError, "No module named " + subname
|
raise ImportError("No module named " + subname)
|
||||||
|
|
||||||
def import_module(partname, fqname, parent):
|
def import_module(partname, fqname, parent):
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -272,11 +272,11 @@ Let us say that we want a slightly more relaxed policy than the standard
|
||||||
elif mode in ('w', 'wb', 'a', 'ab'):
|
elif mode in ('w', 'wb', 'a', 'ab'):
|
||||||
# check filename : must begin with /tmp/
|
# check filename : must begin with /tmp/
|
||||||
if file[:5]!='/tmp/':
|
if file[:5]!='/tmp/':
|
||||||
raise IOError, "can't write outside /tmp"
|
raise IOError("can't write outside /tmp")
|
||||||
elif (string.find(file, '/../') >= 0 or
|
elif (string.find(file, '/../') >= 0 or
|
||||||
file[:3] == '../' or file[-3:] == '/..'):
|
file[:3] == '../' or file[-3:] == '/..'):
|
||||||
raise IOError, "'..' in filename forbidden"
|
raise IOError("'..' in filename forbidden")
|
||||||
else: raise IOError, "Illegal open() mode"
|
else: raise IOError("Illegal open() mode")
|
||||||
return open(file, mode, buf)
|
return open(file, mode, buf)
|
||||||
|
|
||||||
Notice that the above code will occasionally forbid a perfectly valid filename;
|
Notice that the above code will occasionally forbid a perfectly valid filename;
|
||||||
|
|
|
@ -216,7 +216,7 @@ provided by this module. ::
|
||||||
except OSError, why:
|
except OSError, why:
|
||||||
errors.extend((src, dst, str(why)))
|
errors.extend((src, dst, str(why)))
|
||||||
if errors:
|
if errors:
|
||||||
raise Error, errors
|
raise Error(errors)
|
||||||
|
|
||||||
Another example that uses the :func:`ignore_patterns` helper::
|
Another example that uses the :func:`ignore_patterns` helper::
|
||||||
|
|
||||||
|
|
|
@ -232,7 +232,7 @@ be sent, and the handler raises an exception. ::
|
||||||
|
|
||||||
def handler(signum, frame):
|
def handler(signum, frame):
|
||||||
print 'Signal handler called with signal', signum
|
print 'Signal handler called with signal', signum
|
||||||
raise IOError, "Couldn't open device!"
|
raise IOError("Couldn't open device!")
|
||||||
|
|
||||||
# Set the signal handler and a 5-second alarm
|
# Set the signal handler and a 5-second alarm
|
||||||
signal.signal(signal.SIGALRM, handler)
|
signal.signal(signal.SIGALRM, handler)
|
||||||
|
|
|
@ -288,7 +288,7 @@ The simple form, ``assert expression``, is equivalent to ::
|
||||||
The extended form, ``assert expression1, expression2``, is equivalent to ::
|
The extended form, ``assert expression1, expression2``, is equivalent to ::
|
||||||
|
|
||||||
if __debug__:
|
if __debug__:
|
||||||
if not expression1: raise AssertionError, expression2
|
if not expression1: raise AssertionError(expression2)
|
||||||
|
|
||||||
.. index::
|
.. index::
|
||||||
single: __debug__
|
single: __debug__
|
||||||
|
|
|
@ -40,9 +40,9 @@ romanNumeralMap = (('M', 1000),
|
||||||
def toRoman(n):
|
def toRoman(n):
|
||||||
"""convert integer to Roman numeral"""
|
"""convert integer to Roman numeral"""
|
||||||
if not (0 < n < 5000):
|
if not (0 < n < 5000):
|
||||||
raise OutOfRangeError, "number out of range (must be 1..4999)"
|
raise OutOfRangeError("number out of range (must be 1..4999)")
|
||||||
if int(n) <> n:
|
if int(n) != n:
|
||||||
raise NotIntegerError, "decimals can not be converted"
|
raise NotIntegerError("decimals can not be converted")
|
||||||
|
|
||||||
result = ""
|
result = ""
|
||||||
for numeral, integer in romanNumeralMap:
|
for numeral, integer in romanNumeralMap:
|
||||||
|
@ -67,9 +67,9 @@ romanNumeralPattern = re.compile("""
|
||||||
def fromRoman(s):
|
def fromRoman(s):
|
||||||
"""convert Roman numeral to integer"""
|
"""convert Roman numeral to integer"""
|
||||||
if not s:
|
if not s:
|
||||||
raise InvalidRomanNumeralError, 'Input can not be blank'
|
raise InvalidRomanNumeralError('Input can not be blank')
|
||||||
if not romanNumeralPattern.search(s):
|
if not romanNumeralPattern.search(s):
|
||||||
raise InvalidRomanNumeralError, 'Invalid Roman numeral: %s' % s
|
raise InvalidRomanNumeralError('Invalid Roman numeral: %s' % s)
|
||||||
|
|
||||||
result = 0
|
result = 0
|
||||||
index = 0
|
index = 0
|
||||||
|
|
|
@ -159,7 +159,7 @@ class CheckSuspiciousMarkupBuilder(Builder):
|
||||||
except IOError: return
|
except IOError: return
|
||||||
for i, row in enumerate(csv.reader(f)):
|
for i, row in enumerate(csv.reader(f)):
|
||||||
if len(row) != 4:
|
if len(row) != 4:
|
||||||
raise ValueError, "wrong format in %s, line %d: %s" % (filename, i+1, row)
|
raise ValueError("wrong format in %s, line %d: %s" % (filename, i+1, row))
|
||||||
docname, lineno, issue, text = row
|
docname, lineno, issue, text = row
|
||||||
docname = docname.decode('utf-8')
|
docname = docname.decode('utf-8')
|
||||||
if lineno: lineno = int(lineno)
|
if lineno: lineno = int(lineno)
|
||||||
|
|
Loading…
Reference in New Issue