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
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
Next Next commit
add tests for C API codecs
  • Loading branch information
picnixz committed Aug 26, 2024
commit ce7c135c4df91b2e3b19f18da66e493b61277c9f
237 changes: 232 additions & 5 deletions Modules/_testcapi/codec.c
10000
Original file line number Diff line number Diff line change
@@ -1,17 +1,244 @@
#include "parts.h"
#include "util.h"

// === Codecs registration and un-registration ================================

static PyObject *
codec_register(PyObject *Py_UNUSED(module), PyObject *search_function)
{
if (PyCodec_Register(search_function) < 0) {
return NULL;
}
Py_RETURN_NONE;
}

static PyObject *
codec_unregister(PyObject *Py_UNUSED(module), PyObject *search_function)
{
if (PyCodec_Unregister(search_function) < 0) {
return NULL;
}
Py_RETURN_NONE;
}

static PyObject *
codec_known_encoding(PyObject *Py_UNUSED(module), PyObject *args)
{
const char *encoding; // should not be NULL
if (!PyArg_ParseTuple(args, "z", &encoding)) {
return NULL;
}
assert(encoding != NULL);
return PyCodec_KnownEncoding(encoding) ? Py_True : Py_False;
}

// === Codecs encoding and decoding interfaces ================================

static PyObject *
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)) {
return NULL;
}
assert(encoding != NULL);
return PyCodec_Encode(input, encoding, errors);
}

static PyObject *
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)) {
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)) {
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)) {
return NULL;
}
assert(encoding != NULL);
return PyCodec_Decoder(encoding);
}

static PyObject *
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)) {
return NULL;
}
assert(encoding != NULL);
assert(errors != NULL);
return PyCodec_IncrementalEncoder(encoding, errors);
}

static PyObject *
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)) {
return NULL;
}
assert(encoding != NULL);
assert(errors != NULL);
return PyCodec_IncrementalDecoder(encoding, errors);
}

static PyObject *
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)) {
return NULL;
}
assert(encoding != NULL);
assert(errors != NULL);
return PyCodec_StreamReader(encoding, stream, errors);
}

static PyObject *
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)) {
return NULL;
}
assert(encoding != NULL);
assert(errors != NULL);
return PyCodec_StreamWriter(encoding, stream, errors);
}

// === Codecs errors handlers =================================================

static PyObject *
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)) {
return NULL;
}
assert(encoding != NULL);
if (PyCodec_RegisterError(encoding, error) < 0) {
return NULL;
}
Py_RETURN_NONE;
}

static PyObject *
codec_lookup_error(PyObject *Py_UNUSED(module), PyObject *args)
{
const char *encoding; // can be NULL
if (!PyArg_ParseTuple(args, "z", &encoding)) {
return NULL;
}
return PyCodec_LookupError(encoding);
}

static PyObject *
codec_strict_errors(PyObject *Py_UNUSED(module), PyObject *exc)
{
assert(exc != NULL);
return PyCodec_StrictErrors(exc);
}

static PyObject *
codec_ignore_errors(PyObject *Py_UNUSED(module), PyObject *exc)
{
assert(exc != NULL);
return PyCodec_IgnoreErrors(exc);
}

static PyObject *
codec_replace_errors(PyObject *Py_UNUSED(module), PyObject *exc)
{
assert(exc != NULL);
return PyCodec_ReplaceErrors(exc);
}

static PyObject *
codec_xmlcharrefreplace_errors(PyObject *Py_UNUSED(module), PyObject *exc)
{
assert(exc != NULL);
return PyCodec_XMLCharRefReplaceErrors(exc);
}

static PyObject *
codec_backslashreplace_errors(PyObject *Py_UNUSED(module), PyObject *exc)
{
assert(exc != NULL);
return PyCodec_BackslashReplaceErrors(exc);
}

#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
static PyObject *
codec_namereplace_errors(PyObject *Py_UNUSED(module), PyObject *exc)
{
assert(exc != NULL);
return PyCodec_NameReplaceError 9CFD s(exc);
}
#endif

static PyMethodDef test_methods[] = {
{NULL},
/* codecs registration */
{"codec_register", codec_register, METH_O},
{"codec_unregister", codec_unregister, METH_O},
{"codec_known_encoding", codec_known_encoding, METH_VARARGS},
/* encoding and decoding interface */
{"codec_encode", codec_encode, METH_VARARGS},
{"codec_decode", codec_decode, METH_VARARGS},
{"codec_encoder", codec_encoder, METH_VARARGS},
{"codec_decoder", codec_decoder, METH_VARARGS},
{"codec_incremental_encoder", codec_incremental_encoder, METH_VARARGS},
{"codec_incremental_decoder", codec_incremental_decoder, METH_VARARGS},
{"codec_stream_reader", codec_stream_reader, METH_VARARGS},
{"codec_stream_writer", codec_stream_writer, METH_VARARGS},
/* error handling */
{"codec_register_error", codec_register_error, METH_VARARGS},
{"codec_lookup_error", codec_lookup_error, METH_VARARGS},
{"codec_strict_errors", codec_strict_errors, METH_O},
{"codec_ignore_errors", codec_ignore_errors, METH_O},
{"codec_replace_errors", codec_replace_errors, METH_O},
{"codec_xmlcharrefreplace_errors", codec_xmlcharrefreplace_errors, METH_O},
{"codec_backslashreplace_errors", codec_backslashreplace_errors, METH_O},
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
{"codec_namereplace_errors", codec_namereplace_errors, METH_O},
#endif
{NULL, NULL, 0, NULL},
};

int
_PyTestCapi_Init_Codec(PyObject *m)
_PyTestCapi_Init_Codec(PyObject *module)
{
if (PyModule_AddFunctions(m, test_methods) < 0){
if (PyModule_AddFunctions(module, test_methods) < 0) {
return -1;
}

return 0;
}
0