8000 gh-111495: Add tests for `PyCodec_*` C API by picnixz · Pull Request #123343 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-111495: Add tests for PyCodec_* C API #123343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
ce7c135
add tests for C API `codecs`
picnixz Aug 26, 2024
f9e350a
add Python tests for `_codecs`
picnixz Aug 26, 2024
15b6811
fix size bug
picnixz Aug 26, 2024
8048ae1
rename test class
picnixz Aug 26, 2024
8487b46
Revert "fix size bug"
picnixz Sep 25, 2024
2dbe09a
Merge branch 'main' into test/c-api-codec-111495
picnixz Sep 25, 2024
0097f2a
Disable tests that are known to crash.
picnixz Sep 25, 2024
303b13c
address Victor's review
picnixz Sep 25, 2024
4f474dd
update tests to reflect user errors
picnixz Sep 25, 2024
d49743c
Merge remote-tracking branch 'upstream/main' into test/c-api-codec-11…
picnixz Sep 27, 2024
87ee0d2
fix C API codec tests
picnixz Sep 27, 2024
6a36eb0
small hack to make the test suite correct
picnixz Sep 27, 2024
145b285
remove un-necessary imports
picnixz Sep 28, 2024
dc9af16
Merge remote-tracking branch 'upstream/main' into test/c-api-codec-11…
picnixz Sep 29, 2024
7be1f55
use `_codecs._unregister_error` to cleanup test state
picnixz Sep 29, 2024
f72be5c
indicate some semantics for NULL case being tested
picnixz Sep 29, 2024
4d02c6c
revert a cosmetic change
picnixz Sep 29, 2024
0f26ca7
Move `PyCodec_NameReplaceErrors` test to the `_testlimitedcapi` module
picnixz Sep 29, 2024
1399779
add comment for why we do not test `_PyCodec_UnregisterError`
picnixz Sep 29, 2024
914151e
update a comment
picnixz Sep 29, 2024
8dd7e8d
revert one cosmetic change
picnixz Sep 29, 2024
1e6a5ce
Fix Windows compilation
picnixz Sep 29, 2024
2ba5f03
address Victor's review
picnixz Sep 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
address Victor's review
  • Loading branch information
picnixz committed Sep 25, 2024
commit 303b13c4c61aa2a6d13fd027c3c31a0b4cef7f9a
15 changes: 11 additions & 4 deletions Lib/test/test_capi/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,14 +592,21 @@ def use_custom_encoder(self):

def test_codec_register(self):
search_function, encoding = self.search_function, self.encoding_name
# register the search function using the C API
self.assertIsNone(_testcapi.codec_register(search_function))
self.assertIs(self.codecs.lookup(encoding), search_function(encoding))
self.assertEqual(self.codecs.encode('123', encoding=encoding), '321')
# unregister the search function using the regular API
self.codecs.unregister(search_function)
self.assertRaises(LookupError, self.codecs.lookup, encoding)

def test_codec_unregister(self):
search_function, encoding = self.search_function, self.encoding_name
self.assertRaises(LookupError, self.codecs.lookup, encoding)
# register the search function using the regular API
self.codecs.register(search_function)
self.assertIsNotNone(self.codecs.lookup(encoding))
# unregister the search function using the C API
self.assertIsNone(_testcapi.codec_unregister(search_function))
self.assertRaises(LookupError, self.codecs.lookup, encoding)

Expand All @@ -625,24 +632,23 @@ def test_codec_encode(self):
encode = _testcapi.codec_encode
self.assertEqual(encode('a', 'utf-8', NULL), b'a')
self.assertEqual(encode('a', 'utf-8', 'strict'), b'a')
self.assertEqual(encode('é', 'ascii', 'ignore'), b'')
# todo: add more cases
self.assertEqual(encode('[é]', 'ascii', 'ignore'), b'[]')

self.assertRaises(TypeError, encode, NULL, 'ascii', 'strict')
# CRASHES encode('a', NULL, 'strict')

def test_codec_decode(self):
decode = _testcapi.codec_decode

b = b'a\xc2\xa1\xe4\xbd\xa0\xf0\x9f\x98\x80'
s = 'a\xa1\u4f60\U0001f600'
b = s.encode()

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

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


class CAPICodecErrors(unittest.TestCase):

def setUp(self):
Expand Down
34 changes: 10 additions & 24 deletions Modules/_testcapi/codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ static PyObject *
codec_known_encoding(PyObject *Py_UNUSED(module), PyObject *args)
{
const char *encoding; // should not be NULL
if (!PyArg_ParseTuple(args, "z", &encoding)) {
if (!PyArg_ParseTuple(args, "s", &encoding)) {
return NULL;
}
assert(encoding != NULL);
return PyCodec_KnownEncoding(encoding) ? Py_True : Py_False;
}

Expand All @@ -39,10 +38,9 @@ codec_encode(PyObject *Py_UNUSED(module), PyObject *args)
PyObject *input;
const char *encoding; // should not be NULL
const char *errors; // can be NULL
if (!PyArg_ParseTuple(args, "O|zz", &input, &encoding, &errors)) {
if (!PyArg_ParseTuple(args, "O|sz", &input, &encoding, &errors)) {
return NULL;
}
assert(encoding != NULL);
return PyCodec_Encode(input, encoding, errors);
}

Expand All @@ -52,32 +50,29 @@ codec_decode(PyObject *Py_UNUSED(module), PyObject *args)
PyObject *input;
const char *encoding; // should not be NULL
const char *errors; // can be NULL
if (!PyArg_ParseTuple(args, "O|zz", &input, &encoding, &errors)) {
if (!PyArg_ParseTuple(args, "O|sz", &input, &encoding, &errors)) {
return NULL;
}
assert(encoding != NULL);
return PyCodec_Decode(input, encoding, errors);
}

static PyObject *
codec_encoder(PyObject *Py_UNUSED(module), PyObject *args)
{
const char *encoding; // should not be NULL
if (!PyArg_ParseTuple(args, "z", &encoding)) {
if (!PyArg_ParseTuple(args, "s", &encoding)) {
return NULL;
}
assert(encoding != NULL);
return PyCodec_Encoder(encoding);
}

static PyObject *
codec_decoder(PyObject *Py_UNUSED(module), PyObject *args)
{
const char *encoding; // should not be NULL
if (!PyArg_ParseTuple(args, "z", &encoding)) {
if (!PyArg_ParseTuple(args, "s", &encoding)) {
return NULL;
}
assert(encoding != NULL);
return PyCodec_Decoder(encoding);
}

Expand All @@ -86,11 +81,9 @@ codec_incremental_encoder(PyObject *Py_UNUSED(module), PyObject *args)
{
const char *encoding; // should not be NULL
const char *errors; // should not be NULL
if (!PyArg_ParseTuple(args, "zz", &encoding, &errors)) {
if (!PyArg_ParseTuple(args, "ss", &encoding, &errors)) {
return NULL;
}
assert(encoding != NULL);
assert(errors != NULL);
return PyCodec_IncrementalEncoder(encoding, errors);
}

Expand All @@ -99,11 +92,9 @@ codec_incremental_decoder(PyObject *Py_UNUSED(module), PyObject *args)
{
const char *encoding; // should not be NULL
const char *errors; // should not be NULL
if (!PyArg_ParseTuple(args, "zz", &encoding, &errors)) {
if (!PyArg_ParseTuple(args, "ss", &encoding, &errors)) {
return NULL;
}
assert(encoding != NULL);
assert(errors != NULL);
return PyCodec_IncrementalDecoder(encoding, errors);
}

Expand All @@ -113,11 +104,9 @@ codec_stream_reader(PyObject *Py_UNUSED(module), PyObject *args)
const char *encoding; // should not be NULL
PyObject *stream;
const char *errors; // should not be NULL
if (!PyArg_ParseTuple(args, "zOz", &encoding, &stream, &errors)) {
if (!PyArg_ParseTuple(args, "sOs", &encoding, &stream, &errors)) {
return NULL;
}
assert(encoding != NULL);
assert(errors != NULL);
return PyCodec_StreamReader(encoding, stream, errors);
}

Expand All @@ -127,11 +116,9 @@ codec_stream_writer(PyObject *Py_UNUSED(module), PyObject *args)
const char *encoding; // should not be NULL
PyObject *stream;
const char *errors; // should not be NULL
if (!PyArg_ParseTuple(args, "zOz", &encoding, &stream, &errors)) {
if (!PyArg_ParseTuple(args, "sOs", &encoding, &stream, &errors)) {
return NULL;
}
assert(encoding != NULL);
assert(errors != NULL);
return PyCodec_StreamWriter(encoding, stream, errors);
}

Expand All @@ -142,10 +129,9 @@ codec_register_error(PyObject *Py_UNUSED(module), PyObject *args)
{
const char *encoding; // should not be NULL
PyObject *error;
if (!PyArg_ParseTuple(args, "zO", &encoding, &error)) {
if (!PyArg_ParseTuple(args, "sO", &encoding, &error)) {
return NULL;
}
assert(encoding != NULL);
if (PyCodec_RegisterError(encoding, error) < 0) {
return NULL;
}
Expand Down
Loading
0