8000 Issue #16714: use 'raise' exceptions, don't 'throw'. · python/cpython@a191959 · GitHub
[go: up one dir, main page]

Skip to content

Commit a191959

Browse files
committed
Issue #16714: use 'raise' exceptions, don't 'throw'.
Patch by Serhiy Storchaka.
2 parents f61dc4c + 5b89840 commit a191959

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+64
-64
lines changed

Doc/howto/cporting.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ behave slightly differently from real Capsules. Specifically:
253253

254254
* :c:func:`PyCapsule_GetName` always returns NULL.
255255

256-
* :c:func:`PyCapsule_SetName` always throws an exception and
256+
* :c:func:`PyCapsule_SetName` always raises an exception and
257257
returns failure. (Since there's no way to store a name
258258
in a CObject, noisy failure of :c:func:`PyCapsule_SetName`
259259
was deemed preferable to silent failure here. If this is

Doc/library/contextlib.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ Functions and classes provided:
184184
files = [stack.enter_context(open(fname)) for fname in filenames]
185185
# All opened files will automatically be closed at the end of
186186
# the with statement, even if attempts to open files later
187-
# in the list throw an exception
187+
# in the list raise an exception
188188

189189
Each instance maintains a stack of registered callbacks that are called in
190190
reverse order when the instance is closed (either explicitly or implicitly

Doc/library/imaplib.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ There's also a subclass for secure connections:
7575
:class:`ssl.SSLContext` object which allows bundling SSL configuration
7676
options, certificates and private keys into a single (potentially long-lived)
7777
structure. Note that the *keyfile*/*certfile* parameters are mutually exclusive with *ssl_context*,
78-
a :class:`ValueError` is thrown if *keyfile*/*certfile* is provided along with *ssl_context*.
78+
a :class:`ValueError` is raised if *keyfile*/*certfile* is provided along with *ssl_context*.
7979

8080
.. versionchanged:: 3.3
8181
*ssl_context* parameter added.

Doc/library/os.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,7 @@ Querying the size of a terminal
11711171
output) specifies which file descriptor should be queried.
11721172

11731173
If the file descriptor is not connected to a terminal, an :exc:`OSError`
1174-
is thrown.
1174+
is raised.
11751175

11761176
:func:`shutil.get_terminal_size` is the high-level function which
11771177
should normally be used, ``os.get_terminal_size`` is the low-level
@@ -1945,7 +1945,7 @@ features:
19451945
:mod:`os` module permit use of their *dir_fd* parameter. Different platforms
19461946
provide different functionality, and an option that might work on one might
19471947
be unsupported on another. For consistency's sakes, functions that support
1948-
*dir_fd* always allow specifying the parameter, but will throw an exception
1948+
*dir_fd* always allow specifying the parameter, but will raise an exception
19491949
if the functionality is not actually available.
19501950

19511951
To check whether a particular function permits use of its *dir_fd*
@@ -1986,7 +1986,7 @@ features:
19861986
descriptor. Different platforms provide different functionality, and an
19871987
option that might work on one might be unsupported on another. For
19881988
consistency's sakes, functions that support *fd* always allow specifying
1989-
the parameter, but will throw an exception if the functionality is not
1989+
the parameter, but will raise an exception if the functionality is not
19901990
actually available.
19911991

19921992
To check whether a particular function permits specifying an open file
@@ -2007,7 +2007,7 @@ features:
20072007
platforms provide different functionality, and an option that might work on
20082008
one might be unsupported on another. For consistency's sakes, functions that
20092009
support *follow_symlinks* always allow specifying the parameter, but will
2010-
throw an exception if the functionality is not actually available.
2010+
raise an exception if the functionality is not actually available.
20112011

20122012
To check whether a particular function permits use of its *follow_symlinks*
20132013
parameter, use the ``in`` operator on ``supports_follow_symlinks``. As an

Lib/asyncore.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ def recv(self, buffer_size):
385385
else:
386386
return data
387387
except socket.error as why:
388-
# winsock sometimes throws ENOTCONN
388+
# winsock sometimes raises ENOTCONN
389389
if why.args[0] in _DISCONNECTED:
390390
self.handle_close()
391391
return b''

Lib/contextlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class ExitStack(object):
151151
files = [stack.enter_context(open(fname)) for fname in filenames]
152152
# All opened files will automatically be closed at the end of
153153
# the with statement, even if attempts to open files later
154-
# in the list throw an exception
154+
# in the list raise an exception
155155
156156
"""
157157
def __init__(self):

Lib/distutils/tests/test_msvc9compiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class msvc9compilerTestCase(support.TempdirManager,
104104
unittest.TestCase):
105105

106106
def test_no_compiler(self):
107-
# makes sure query_vcvarsall throws
107+
# makes sure query_vcvarsall raises
108108
# a DistutilsPlatformError if the compiler
109109
# is not found
110110
from distutils.msvc9compiler import query_vcvarsall

Lib/email/feedparser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
data. When you have no more data to push into the parser, call .close().
1414
This completes the parsing and returns the root message object.
1515
16-
The other advantage of this parser is that it will never throw a parsing
16+
The other advantage of this parser is that it will never raise a parsing
1717
exception. Instead, when it finds something unexpected, it adds a 'defect' to
1818
the current message. Defects are just instances that live on the message
1919
object's .defects attribute.
@@ -228,7 +228,7 @@ def _parsegen(self):
228228
# supposed to see in the body of the message.
229229
self._parse_headers(headers)
230230
# Headers-only parsing is a backwards compatibility hack, which was
231-
# necessary in the older parser, which could throw errors. All
231+
# necessary in the older parser, which could raise errors. All
232232
# remaining lines in the input are thrown into the message body.
233233
if self._headersonly:
234234
lines = []

Lib/email/header.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ def append(self, s, charset=None, errors='strict'):
298298
else:
299299
s = s.decode(input_charset, errors)
300300
# Ensure that the bytes we're storing can be decoded to the output
301-
# character set, otherwise an early error is thrown.
301+
# character set, otherwise an early error is raised.
302302
output_charset = charset.output_codec or 'us-ascii'
303303
if output_charset != _charset.UNKNOWN8BIT:
304304
try:

Lib/email/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def formataddr(pair, charset='utf-8'):
8383
'utf-8'.
8484
"""
8585
name, address = pair
86-
# The address MUST (per RFC) be ascii, so throw a UnicodeError if it isn't.
86+
# The address MUST (per RFC) be ascii, so raise an UnicodeError if it isn't.
8787
address.encode('ascii')
8888
if name:
8989
try:

Lib/imaplib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,7 @@ class IMAP4_SSL(IMAP4):
11781178
ssl_context - a SSLContext object that contains your certificate chain
11791179
and private key (default: None)
11801180
Note: if ssl_context is provided, then parameters keyfile or
1181-
certfile should not be set otherwise ValueError is thrown.
1181+
certfile should not be set otherwise ValueError is raised.
11821182
11831183
for more documentation see the docstring of the parent class IMAP4.
11841184
"""

Lib/io.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
At the top of the I/O hierarchy is the abstract base class IOBase. It
55
defines the basic interface to a stream. Note, however, that there is no
66
separation between reading and writing to streams; implementations are
7-
allowed to throw an IOError if they do not support a given operation.
7+
allowed to raise an IOError if they do not support a given operation.
88
99
Extending IOBase is RawIOBase which deals simply with the reading and
1010
writing of raw bytes to a stream. FileIO subclasses RawIOBase to provide

Lib/logging/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1358,7 +1358,7 @@ def _log(self, level, msg, args, exc_info=None, extra=None, stack_info=False):
13581358
"""
13591359
sinfo = None
13601360
if _srcfile:
1361-
#IronPython doesn't track Python frames, so findCaller throws an
1361+
#IronPython doesn't track Python frames, so findCaller raises an
13621362
#exception on some versions of IronPython. We trap it here so that
13631363
#IronPython can use logging.
13641364
try:

Lib/multiprocessing/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ def _exit_function(info=info, debug=debug, _run_finalizers=_run_finalizers,
290290

291291
if current_process() is not None:
292292
# We check if the current process is None here because if
293-
# it's None, any call to ``active_children()`` will throw
293+
# it's None, any call to ``active_children()`` will raise
294294
# an AttributeError (active_children winds up trying to
295295
# get attributes from util._current_process). One
296296
# situation where this can happen is if someone has

Lib/pkgutil.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ def find_loader(fullname):
502502
return importlib.find_loader(fullname, path)
503503
except (ImportError, AttributeError, TypeError, ValueError) as ex:
504504
# This hack fixes an impedance mismatch between pkgutil and
505-
# importlib, where the latter throws other errors for cases where
505+
# importlib, where the latter raises other errors for cases where
506506
# pkgutil previously threw ImportError
507507
msg = "Error while finding loader for {!r} ({}: {})"
508508
raise ImportError(msg.format(fullname, type(ex), ex)) from ex

Lib/tempfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ class TemporaryDirectory(object):
621621

622622
def __init__(self, suffix="", prefix=template, dir=None):
623623
self._closed = False
624-
self.name = None # Handle mkdtemp throwing an exception
624+
self.name = None # Handle mkdtemp raising an exception
625625
self.name = mkdtemp(suffix, prefix, dir)
626626

627627
def __repr__(self):

Lib/test/test_codeop.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def assertInvalid(self, str, symbol='single', is_syntax=1):
5050
'''succeed iff str is the start of an invalid piece of code'''
5151
try:
5252
compile_command(str,symbol=symbol)
53-
self.fail("No exception thrown for invalid code")
53+
self.fail("No exception raised for invalid code")
5454
except SyntaxError:
5555
self.assertTrue(is_syntax)
5656
except OverflowError:

Lib/test/test_docxmlrpc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def test_valid_get_response(self):
100100
self.assertEqual(response.status, 200)
101101
self.assertEqual(response.getheader("Content-type"), "text/html")
102102

103-
# Server throws an exception if we don't start to read the data
103+
# Server raises an exception if we don't start to read the data
104104
response.read()
105105

106106
def test_invalid_get_response(self):

Lib/test/test_imaplib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def handle(self):
115115
return
116116
line += part
117117
except IOError:
118-
# ..but SSLSockets throw exceptions.
118+
# ..but SSLSockets raise exceptions.
119119
return
120120
if line.endswith(b'\r\n'):
121121
break

Lib/test/test_minidom.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ def testEncodings(self):
10731073
'<?xml version="1.0" encoding="utf-16"?>'
10741074
'<foo>\u20ac</foo>'.encode('utf-16'))
10751075

1076-
# Verify that character decoding errors throw exceptions instead
1076+
# Verify that character decoding errors raise exceptions instead
10771077
# of crashing
10781078
self.assertRaises(UnicodeDecodeError, parseString,
10791079
b'<fran\xe7ais>Comment \xe7a va ? Tr\xe8s bien ?</fran\xe7ais>')

Lib/test/test_os.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,33 +202,33 @@ def trunc(x): return x
202202

203203
try:
204204
result[200]
205-
self.fail("No exception thrown")
205+
self.fail("No exception raised")
206206
except IndexError:
207207
pass
208208

209209
# Make sure that assignment fails
210210
try:
211211
result.st_mode = 1
212-
self.fail("No exception thrown")
212+
self.fail("No exception raised")
213213
except AttributeError:
214214
pass
215215

216216
try:
217217
result.st_rdev = 1
218-
self.fail("No exception thrown")
218+
self.fail("No exception raised")
219219
except (AttributeError, TypeError):
220220
pass
221221

222222
try:
223223
result.parrot = 1
224-
self.fail("No exception thrown")
224+
self.fail("No exception raised")
225225
except AttributeError:
226226
pass
227227

228228
# Use the stat_result constructor with a too-short tuple.
229229
try:
230230
result2 = os.stat_result((10,))
231-
self.fail("No exception thrown")
231+
self.fail("No exception raised")
232232
except TypeError:
233233
pass
234234

@@ -273,20 +273,20 @@ def test_statvfs_attributes(self):
273273
# Make sure that assignment really fails
274274
try:
275275
result.f_bfree = 1
276-
self.fail("No exception thrown")
276+
self.fail("No exception raised")
277277
except AttributeError:
278278
pass
279279

280280
try:
281281
result.parrot = 1
282-
self.fail("No exception thrown")
282+
self.fail("No exception raised")
283283
except AttributeError:
284284
pass
285285

286286
# Use the constructor with a too-short tuple.
287287
try:
288288
result2 = os.statvfs_result((10,))
289-
self.fail("No exception thrown")
289+
self.fail("No exception raised")
290290
except TypeError:
291291
pass
292292

Li 17AE b/test/test_posix.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ def test_rename_dir_fd(self):
824824
posix.rename(support.TESTFN + 'ren', support.TESTFN)
825825
raise
826826
else:
827-
posix.stat(support.TESTFN) # should not throw exception
827+
posix.stat(support.TESTFN) # should not raise exception
828828
finally:
829829
posix.close(f)
830830

@@ -842,7 +842,7 @@ def test_symlink_dir_fd(self):
842842
def test_unlink_dir_fd(self):
843843
f = posix.open(posix.getcwd(), posix.O_RDONLY)
844844
support.create_empty_file(support.TESTFN + 'del')
845-
posix.stat(support.TESTFN + 'del') # should not throw exception
845+
posix.stat(support.TESTFN + 'del') # should not raise exception
846846
try:
847847
posix.unlink(support.TESTFN + 'del', dir_fd=f)
848848
except:

Lib/test/test_pty.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def test_fork(self):
152152
# platform-dependent amount of data is written to its fd. On
153153
# Linux 2.6, it's 4000 bytes and the child won't block, but on OS
154154
# X even the small writes in the child above will block it. Also
155-
# on Linux, the read() will throw an OSError (input/output error)
155+
# on Linux, the read() will raise an OSError (input/output error)
156156
# when it tries to read past the end of the buffer but the child's
157157
# already exited, so catch and discard those exceptions. It's not
158158
# worth checking for EIO.

Lib/test/test_sax.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ def test_1463026_3_empty(self):
389389
def test_5027_1(self):
390390
# The xml prefix (as in xml:lang below) is reserved and bound by
391391
# definition to http://www.w3.org/XML/1998/namespace. XMLGenerator had
392-
# a bug whereby a KeyError is thrown because this namespace is missing
392+
# a bug whereby a KeyError is raised because this namespace is missing
393393
# from a dictionary.
394394
#
395395
# This test demonstrates the bug by parsing a document.
@@ -415,7 +415,7 @@ def test_5027_1(self):
415415
def test_5027_2(self):
416416
# The xml prefix (as in xml:lang below) is reserved and bound by
417417
# definition to http://www.w3.org/XML/1998/namespace. XMLGenerator had
418-
# a bug whereby a KeyError is thrown because this namespace is missing
418+
# a bug whereby a KeyError is raised because this namespace is missing
419419
# from a dictionary.
420420
#
421421
# This test demonstrates the bug by direct manipulation of the

Lib/test/test_signal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def run_test(self):
104104
# This wait should be interrupted by the signal's exception.
105105
self.wait(child)
106106
time.sleep(1) # Give the signal time to be delivered.
107-
self.fail('HandlerBCalled exception not thrown')
107+
self.fail('HandlerBCalled exception not raised')
108108
except HandlerBCalled:
109109
self.assertTrue(self.b_called)
110110
self.assertFalse(self.a_called)
@@ -140,7 +140,7 @@ def test_main(self):
140140
# test-running process from all the signals. It then
141141
# communicates with that child process over a pipe and
142142
# re-raises information about any exceptions the child
143-
# throws. The real work happens in self.run_test().
143+
# raises. The real work happens in self.run_test().
144144
os_done_r, os_done_w = os.pipe()
145145
with closing(os.fdopen(os_done_r, 'rb')) as done_r, \
146146
closing(os.fdopen(os_done_w, 'wb')) as done_w:

Lib/test/test_socketserver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class ForkingUnixDatagramServer(socketserver.ForkingMixIn,
5858
def simple_subprocess(testcase):
5959
pid = os.fork()
6060
if pid == 0:
61-
# Don't throw an exception; it would be caught by the test harness.
61+
# Don't raise an exception; it would be caught by the test harness.
6262
os._exit(72)
6363
yield None
6464
pid2, status = os.waitpid(pid, 0)

Lib/test/test_sys_settrace.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ def run_test_for_event(self, event):
422422
except ValueError:
423423
pass
424424
else:
425-
self.fail("exception not thrown!")
425+
self.fail("exception not raised!")
426426
except RuntimeError:
427427
self.fail("recursion counter not reset")
428428

Lib/test/test_time.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def test_default_values_for_zero(self):
175175

176176
def test_strptime(self):
177177
# Should be able to go round-trip from strftime to strptime without
178-
# throwing an exception.
178+
# raising an exception.
179179
tt = time.gmtime(self.t)
180180
for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
181181
'j', 'm', 'M', 'p', 'S',

Lib/test/test_uu.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def test_truncatedinput(self):
8080
out = io.BytesIO()
8181
try:
8282
uu.decode(inp, out)
83-
self.fail("No exception thrown")
83+
self.fail("No exception raised")
8484
except uu.Error as e:
8585
self.assertEqual(str(e), "Truncated input file")
8686

@@ -89,7 +89,7 @@ def test_missingbegin(self):
8989
out = io.BytesIO()
9090
try:
9191
uu.decode(inp, out)
92-
self.fail("No exception thrown")
92+
self.fail("No exception raised")
9393
except uu.Error as e:
9494
self.assertEqual(str(e), "No valid begin line found in input file")
9595

0 commit comments

Comments
 (0)
0