Skip to content

Commit

Permalink
Final review comments, and add missing newline after _PyCodec_InitReg…
Browse files Browse the repository at this point in the history
…istry's return type
  • Loading branch information
swtaarrs committed Apr 15, 2024
1 parent 137cf27 commit 4d5bffc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
10 changes: 7 additions & 3 deletions Include/internal/pycore_codecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ extern "C" {

#include "pycore_lock.h" // PyMutex

/* Initialize codecs-related state for the given interpreter. Must be called
before any other _PyCodec* functions, and while only one thread is
active. */
/* Initialize codecs-related state for the given interpreter, including
registering the first codec search function. Must be called before any other
PyCodec-related functions, and while only one thread is active. */
extern PyStatus _PyCodec_InitRegistry(PyInterpreterState *interp);

/* Finalize codecs-related state for the given interpreter. No PyCodec-related
functions other than PyCodec_Unregister() may be called after this. */
extern void _PyCodec_Fini(PyInterpreterState *interp);

extern PyObject* _PyCodec_Lookup(const char *encoding);

/* Text codec specific encoding and decoding API.
Expand Down
1 change: 1 addition & 0 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ void
PyObject_CallFinalizer(PyObject *self)
{
PyTypeObject *tp = Py_TYPE(self);

if (tp->tp_finalize == NULL)
return;
/* tp_finalize should only be called once. */
Expand Down
23 changes: 11 additions & 12 deletions Python/codecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,6 @@ const char *Py_hexdigits = "0123456789abcdef";

/* --- Codec Registry ----------------------------------------------------- */

/* Import the standard encodings package which will register the first
codec search function.
This is done in a lazy way so that the Unicode implementation does
not downgrade startup time of scripts not needing it.
ImportErrors are silently ignored by this function. Only one try is
made.
*/

int PyCodec_Register(PyObject *search_function)
{
PyInterpreterState *interp = _PyInterpreterState_GET();
Expand Down Expand Up @@ -1389,7 +1378,8 @@ static PyObject *surrogateescape_errors(PyObject *self, PyObject *exc)
return PyCodec_SurrogateEscapeErrors(exc);
}

PyStatus _PyCodec_InitRegistry(PyInterpreterState *interp)
PyStatus
_PyCodec_InitRegistry(PyInterpreterState *interp)
{
static struct {
const char *name;
Expand Down Expand Up @@ -1516,3 +1506,12 @@ PyStatus _PyCodec_InitRegistry(PyInterpreterState *interp)

return PyStatus_Ok();
}

void
_PyCodec_Fini(PyInterpreterState *interp)
{
Py_CLEAR(interp->codecs.search_path);
Py_CLEAR(interp->codecs.search_cache);
Py_CLEAR(interp->codecs.error_registry);
interp->codecs.initialized = 0;
}
5 changes: 1 addition & 4 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -838,10 +838,7 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
}

PyConfig_Clear(&interp->config);
Py_CLEAR(interp->codecs.search_path);
Py_CLEAR(interp->codecs.search_cache);
Py_CLEAR(interp->codecs.error_registry);
interp->codecs.initialized = 0;
_PyCodec_Fini(interp);

assert(interp->imports.modules == NULL);
assert(interp->imports.modules_by_index == NULL);
Expand Down

0 comments on commit 4d5bffc

Please sign in to comment.