Skip to content

[C API] Add private _PyCodec_UnregisterError to un-register custom error handlers #124665

Closed
@picnixz

Description

@picnixz

Feature or enhancement

Proposal:

In order to test the Codecs C API (#123343), I need to be able to un-register a custom codecs error policy, otherwise running the tests would leak since they won't clean the registry.

For now, I've managed to make the tests work without this but it's an ugly hack (namely, relying on the fact that the test suite is executed multiple times when searching for refleaks).

The proposed API is as follows:

/* Internal function in Python/internal/pycore_codecs.h */
extern int _PyCodec_UnregisterError(const char *name);
/* Exposed private Python function in Modules/_codecsmodule.c */
static int
_codecs__unregister_error(PyObject *module, PyObject *name);

It would:

  • return 0 if the name does not exist (converted into False at Python level)
  • return 1 if the name exists and was successfully removed (converted into True at Python level)
  • return -1 and set an exception if someone attemps to unregister a standard error policy (e.g., 'strict') or if the removal failed for whatever reason. The exception in the first case would be a ValueError while the exception in the second case would be the one raised internally (we would just propagate any exception by PyDict_PopString).

cc @vstinner

EDIT: After discussion, we decided to first make it entirely private and un-documented (documentation still exists at the code level but not in an RST file). If needs arise, we will make it public.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Metadata

Metadata

Assignees

Labels

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions