Skip to content

Commit 303b13c

Browse files
committed
address Victor's review
1 parent 0097f2a commit 303b13c

File tree

2 files changed

+21
-28
lines changed

2 files changed

+21
-28
lines changed

Lib/test/test_capi/test_codecs.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -592,14 +592,21 @@ def use_custom_encoder(self):
592592

593593
def test_codec_register(self):
594594
search_function, encoding = self.search_function, self.encoding_name
595+
# register the search function using the C API
595596
self.assertIsNone(_testcapi.codec_register(search_function))
596597
self.assertIs(self.codecs.lookup(encoding), search_function(encoding))
597598
self.assertEqual(self.codecs.encode('123', encoding=encoding), '321')
599+
# unregister the search function using the regular API
600+
self.codecs.unregister(search_function)
601+
self.assertRaises(LookupError, self.codecs.lookup, encoding)
598602

599603
def test_codec_unregister(self):
600604
search_function, encoding = self.search_function, self.encoding_name
601605
self.assertRaises(LookupError, self.codecs.lookup, encoding)
606+
# register the search function using the regular API
602607
self.codecs.register(search_function)
608+
self.assertIsNotNone(self.codecs.lookup(encoding))
609+
# unregister the search function using the C API
603610
self.assertIsNone(_testcapi.codec_unregister(search_function))
604611
self.assertRaises(LookupError, self.codecs.lookup, encoding)
605612

@@ -625,24 +632,23 @@ def test_codec_encode(self):
625632
encode = _testcapi.codec_encode
626633
self.assertEqual(encode('a', 'utf-8', NULL), b'a')
627634
self.assertEqual(encode('a', 'utf-8', 'strict'), b'a')
628-
self.assertEqual(encode('é', 'ascii', 'ignore'), b'')
629-
# todo: add more cases
635+
self.assertEqual(encode('[é]', 'ascii', 'ignore'), b'[]')
636+
630637
self.assertRaises(TypeError, encode, NULL, 'ascii', 'strict')
631638
# CRASHES encode('a', NULL, 'strict')
632639

633640
def test_codec_decode(self):
634641
decode = _testcapi.codec_decode
635642

636-
b = b'a\xc2\xa1\xe4\xbd\xa0\xf0\x9f\x98\x80'
637643
s = 'a\xa1\u4f60\U0001f600'
644+
b = s.encode()
638645

639646
self.assertEqual(decode(b, 'utf-8', 'strict'), s)
640647
self.assertEqual(decode(b, 'utf-8', NULL), s)
641648
self.assertEqual(decode(b, 'latin1', 'strict'), b.decode('latin1'))
642649
self.assertRaises(UnicodeDecodeError, decode, b, 'ascii', 'strict')
643650
self.assertRaises(UnicodeDecodeError, decode, b, 'ascii', NULL)
644651
self.assertEqual(decode(b, 'ascii', 'replace'), 'a' + '\ufffd'*9)
645-
# todo: add more cases
646652

647653
# _codecs.decode only reports unknown errors policy when they are
648654
# used (it has a fast path for empty bytes); this is different from
@@ -685,6 +691,7 @@ def test_codec_stream_writer(self):
685691
writer = _testcapi.codec_stream_writer(encoding, stream, 'strict')
686692
self.assertIsInstance(writer, self.codec_info.streamwriter)
687693

694+
688695
class CAPICodecErrors(unittest.TestCase):
689696

690697
def setUp(self):

Modules/_testcapi/codec.c

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@ static PyObject *
2424
codec_known_encoding(PyObject *Py_UNUSED(module), PyObject *args)
2525
{
2626
const char *encoding; // should not be NULL
27-
if (!PyArg_ParseTuple(args, "z", &encoding)) {
27+
if (!PyArg_ParseTuple(args, "s", &encoding)) {
2828
return NULL;
2929
}
30-
assert(encoding != NULL);
3130
return PyCodec_KnownEncoding(encoding) ? Py_True : Py_False;
3231
}
3332

@@ -39,10 +38,9 @@ codec_encode(PyObject *Py_UNUSED(module), PyObject *args)
3938
PyObject *input;
4039
const char *encoding; // should not be NULL
4140
const char *errors; // can be NULL
42-
if (!PyArg_ParseTuple(args, "O|zz", &input, &encoding, &errors)) {
41+
if (!PyArg_ParseTuple(args, "O|sz", &input, &encoding, &errors)) {
4342
return NULL;
4443
}
45-
assert(encoding != NULL);
4644
return PyCodec_Encode(input, encoding, errors);
4745
}
4846

@@ -52,32 +50,29 @@ codec_decode(PyObject *Py_UNUSED(module), PyObject *args)
5250
PyObject *input;
5351
const char *encoding; // should not be NULL
5452
const char *errors; // can be NULL
55-
if (!PyArg_ParseTuple(args, "O|zz", &input, &encoding, &errors)) {
53+
if (!PyArg_ParseTuple(args, "O|sz", &input, &encoding, &errors)) {
5654
return NULL;
5755
}
58-
assert(encoding != NULL);
5956
return PyCodec_Decode(input, encoding, errors);
6057
}
6158

6259
static PyObject *
6360
codec_encoder(PyObject *Py_UNUSED(module), PyObject *args)
6461
{
6562
const char *encoding; // should not be NULL
66-
if (!PyArg_ParseTuple(args, "z", &encoding)) {
63+
if (!PyArg_ParseTuple(args, "s", &encoding)) {
6764
return NULL;
6865
}
69-
assert(encoding != NULL);
7066
return PyCodec_Encoder(encoding);
7167
}
7268

7369
static PyObject *
7470
codec_decoder(PyObject *Py_UNUSED(module), PyObject *args)
7571
{
7672
const char *encoding; // should not be NULL
77-
if (!PyArg_ParseTuple(args, "z", &encoding)) {
73+
if (!PyArg_ParseTuple(args, "s", &encoding)) {
7874
return NULL;
7975
}
80-
assert(encoding != NULL);
8176
return PyCodec_Decoder(encoding);
8277
}
8378

@@ -86,11 +81,9 @@ codec_incremental_encoder(PyObject *Py_UNUSED(module), PyObject *args)
8681
{
8782
const char *encoding; // should not be NULL
8883
const char *errors; // should not be NULL
89-
if (!PyArg_ParseTuple(args, "zz", &encoding, &errors)) {
84+
if (!PyArg_ParseTuple(args, "ss", &encoding, &errors)) {
9085
return NULL;
9186
}
92-
assert(encoding != NULL);
93-
assert(errors != NULL);
9487
return PyCodec_IncrementalEncoder(encoding, errors);
9588
}
9689

@@ -99,11 +92,9 @@ codec_incremental_decoder(PyObject *Py_UNUSED(module), PyObject *args)
9992
{
10093
const char *encoding; // should not be NULL
10194
const char *errors; // should not be NULL
102-
if (!PyArg_ParseTuple(args, "zz", &encoding, &errors)) {
95+
if (!PyArg_ParseTuple(args, "ss", &encoding, &errors)) {
10396
return NULL;
10497
}
105-
assert(encoding != NULL);
106-
assert(errors != NULL);
10798
return PyCodec_IncrementalDecoder(encoding, errors);
10899
}
109100

@@ -113,11 +104,9 @@ codec_stream_reader(PyObject *Py_UNUSED(module), PyObject *args)
113104
const char *encoding; // should not be NULL
114105
PyObject *stream;
115106
const char *errors; // should not be NULL
116-
if (!PyArg_ParseTuple(args, "zOz", &encoding, &stream, &errors)) {
107+
if (!PyArg_ParseTuple(args, "sOs", &encoding, &stream, &errors)) {
117108
return NULL;
118109
}
119-
assert(encoding != NULL);
120-
assert(errors != NULL);
121110
return PyCodec_StreamReader(encoding, stream, errors);
122111
}
123112

@@ -127,11 +116,9 @@ codec_stream_writer(PyObject *Py_UNUSED(module), PyObject *args)
127116
const char *encoding; // should not be NULL
128117
PyObject *stream;
129118
const char *errors; // should not be NULL
130-
if (!PyArg_ParseTuple(args, "zOz", &encoding, &stream, &errors)) {
119+
if (!PyArg_ParseTuple(args, "sOs", &encoding, &stream, &errors)) {
131120
return NULL;
132121
}
133-
assert(encoding != NULL);
134-
assert(errors != NULL);
135122
return PyCodec_StreamWriter(encoding, stream, errors);
136123
}
137124

@@ -142,10 +129,9 @@ codec_register_error(PyObject *Py_UNUSED(module), PyObject *args)
142129
{
143130
const char *encoding; // should not be NULL
144131
PyObject *error;
145-
if (!PyArg_ParseTuple(args, "zO", &encoding, &error)) {
132+
if (!PyArg_ParseTuple(args, "sO", &encoding, &error)) {
146133
return NULL;
147134
}
148-
assert(encoding != NULL);
149135
if (PyCodec_RegisterError(encoding, error) < 0) {
150136
return NULL;
151137
}

0 commit comments

Comments
 (0)