Skip to content

Commit c4c2580

Browse files
committed
Close 19609: narrow scope of codec exc chaining
1 parent 91d2c56 commit c4c2580

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

Lib/test/test_codecs.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2435,22 +2435,22 @@ def test_raise_by_value(self):
24352435
self.check_wrapped(RuntimeError(msg), msg)
24362436

24372437
@contextlib.contextmanager
2438-
def assertNotWrapped(self, operation, exc_type, msg):
2438+
def assertNotWrapped(self, operation, exc_type, msg_re, msg=None):
2439+
if msg is None:
2440+
msg = msg_re
24392441
with self.assertRaisesRegex(exc_type, msg) as caught:
24402442
yield caught
2441-
actual_msg = str(caught.exception)
2442-
self.assertNotIn(operation, actual_msg)
2443-
self.assertNotIn(self.codec_name, actual_msg)
2443+
self.assertEqual(str(caught.exception), msg)
24442444

2445-
def check_not_wrapped(self, obj_to_raise, msg):
2445+
def check_not_wrapped(self, obj_to_raise, msg_re, msg=None):
24462446
self.set_codec(obj_to_raise)
2447-
with self.assertNotWrapped("encoding", RuntimeError, msg):
2447+
with self.assertNotWrapped("encoding", RuntimeError, msg_re, msg):
24482448
"str input".encode(self.codec_name)
2449-
with self.assertNotWrapped("encoding", RuntimeError, msg):
2449+
with self.assertNotWrapped("encoding", RuntimeError, msg_re, msg):
24502450
codecs.encode("str input", self.codec_name)
2451-
with self.assertNotWrapped("decoding", RuntimeError, msg):
2451+
with self.assertNotWrapped("decoding", RuntimeError, msg_re, msg):
24522452
b"bytes input".decode(self.codec_name)
2453-
with self.assertNotWrapped("decoding", RuntimeError, msg):
2453+
with self.assertNotWrapped("decoding", RuntimeError, msg_re, msg):
24542454
codecs.decode(b"bytes input", self.codec_name)
24552455

24562456
def test_init_override_is_not_wrapped(self):
@@ -2475,8 +2475,23 @@ def test_non_str_arg_is_not_wrapped(self):
24752475
self.check_not_wrapped(RuntimeError(1), "1")
24762476

24772477
def test_multiple_args_is_not_wrapped(self):
2478-
msg = "\('a', 'b', 'c'\)"
2479-
self.check_not_wrapped(RuntimeError('a', 'b', 'c'), msg)
2478+
msg_re = "\('a', 'b', 'c'\)"
2479+
msg = "('a', 'b', 'c')"
2480+
self.check_not_wrapped(RuntimeError('a', 'b', 'c'), msg_re, msg)
2481+
2482+
# http://bugs.python.org/issue19609
2483+
def test_codec_lookup_failure_not_wrapped(self):
2484+
msg = "unknown encoding: %s" % self.codec_name
2485+
# The initial codec lookup should not be wrapped
2486+
with self.assertNotWrapped("encoding", LookupError, msg):
2487+
"str input".encode(self.codec_name)
2488+
with self.assertNotWrapped("encoding", LookupError, msg):
2489+
codecs.encode("str input", self.codec_name)
2490+
with self.assertNotWrapped("decoding", LookupError, msg):
2491+
b"bytes input".decode(self.codec_name)
2492+
with self.assertNotWrapped("decoding", LookupError, msg):
2493+
codecs.decode(b"bytes input", self.codec_name)
2494+
24802495

24812496

24822497
@unittest.skipUnless(sys.platform == 'win32',

Python/codecs.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,10 @@ PyObject *PyCodec_Encode(PyObject *object,
370370
goto onError;
371371

372372
result = PyEval_CallObject(encoder, args);
373-
if (result == NULL)
373+
if (result == NULL) {
374+
wrap_codec_error("encoding", encoding);
374375
goto onError;
376+
}
375377

376378
if (!PyTuple_Check(result) ||
377379
PyTuple_GET_SIZE(result) != 2) {
@@ -392,7 +394,6 @@ PyObject *PyCodec_Encode(PyObject *object,
392394
Py_XDECREF(result);
393395
Py_XDECREF(args);
394396
Py_XDECREF(encoder);
395-
wrap_codec_error("encoding", encoding);
396397
return NULL;
397398
}
398399

@@ -418,8 +419,10 @@ PyObject *PyCodec_Decode(PyObject *object,
418419
goto onError;
419420

420421
result = PyEval_CallObject(decoder,args);
421-
if (result == NULL)
422+
if (result == NULL) {
423+
wrap_codec_error("decoding", encoding);
422424
goto onError;
425+
}
423426
if (!PyTuple_Check(result) ||
424427
PyTuple_GET_SIZE(result) != 2) {
425428
PyErr_SetString(PyExc_TypeError,
@@ -439,7 +442,6 @@ PyObject *PyCodec_Decode(PyObject *object,
439442
Py_XDECREF(args);
440443
Py_XDECREF(decoder);
441444
Py_XDECREF(result);
442-
wrap_codec_error("decoding", encoding);
443445
return NULL;
444446
}
445447

0 commit comments

Comments
 (0)