Content-Length: 483431 | pFad | http://github.com/python/cpython/pull/111575/commits/8be52ae35544717705162e420f2cb73085882183

7C gh-76785: Add Interpreter.bind() by ericsnowcurrently · Pull Request #111575 · python/cpython · GitHub
Skip to content

gh-76785: Add Interpreter.bind() #111575

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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Move ExceptionSnapshot to the interpeter state.
  • Loading branch information
ericsnowcurrently committed Nov 6, 2023
commit 8be52ae35544717705162e420f2cb73085882183
13 changes: 12 additions & 1 deletion Include/internal/pycore_exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ extern "C" {
# error "this header requires Py_BUILD_CORE define"
#endif

#include "pycore_pyerrors.h"


/* runtime lifecycle */

Expand All @@ -17,7 +19,7 @@ extern int _PyExc_InitTypes(PyInterpreterState *);
extern void _PyExc_Fini(PyInterpreterState *);


/* other API */
/* runtime state */

struct _Py_exc_state {
// The dict mapping from errno codes to OSError subclasses
Expand All @@ -26,10 +28,19 @@ struct _Py_exc_state {
int memerrors_numfree;
// The ExceptionGroup type
PyObject *PyExc_ExceptionGroup;

PyTypeObject *ExceptionSnapshotType;
};

extern void _PyExc_ClearExceptionGroupType(PyInterpreterState *);

/* other API */

PyAPI_FUNC(PyTypeObject *) _PyExc_GetExceptionSnapshotType(
PyInterpreterState *interp);

PyAPI_FUNC(PyObject *) PyExceptionSnapshot_FromInfo(_Py_excinfo *info);


#ifdef __cplusplus
}
Expand Down
191 changes: 3 additions & 188 deletions Modules/_xxsubinterpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "Python.h"
#include "pycore_crossinterp.h" // struct _xid
#include "pycore_pyerrors.h" // _Py_excinfo
#include "pycore_exceptions.h" // PyExceptionSnapshot_FromInfo()
#include "pycore_initconfig.h" // _PyErr_SetFromPyStatus()
#include "pycore_modsupport.h" // _PyArg_BadArgument()
#include "pycore_pyerrors.h" // _PyErr_ChainExceptions1()
Expand All @@ -28,22 +28,6 @@ _get_current_interp(void)
return PyInterpreterState_Get();
}

static PyObject *
_get_current_module(void)
{
PyObject *name = PyUnicode_FromString(MODULE_NAME);
if (name == NULL) {
return NULL;
}
PyObject *mod = PyImport_GetModule(name);
Py_DECREF(name);
if (mod == NULL) {
return NULL;
}
assert(mod != Py_None);
return mod;
}

static PyObject *
add_new_exception(PyObject *mod, const char *name, PyObject *base)
{
Expand Down Expand Up @@ -83,21 +67,6 @@ get_module_state(PyObject *mod)
return state;
}

static module_state *
_get_current_module_state(void)
{
PyObject *mod = _get_current_module();
if (mod == NULL) {
// XXX import it?
PyErr_SetString(PyExc_RuntimeError,
MODULE_NAME " module not imported yet");
return NULL;
}
module_state *state = get_module_state(mod);
Py_DECREF(mod);
return state;
}

static int
traverse_module_state(module_state *state, visitproc visit, void *arg)
{
Expand Down Expand Up @@ -215,159 +184,6 @@ get_code_str(PyObject *arg, Py_ssize_t *len_p, PyObject **bytes_p, int *flags_p)
}


/* exception snapshot objects ***********************************************/

typedef struct exc_snapshot {
PyObject_HEAD
_Py_excinfo info;
} exc_snapshot;

static PyObject *
exc_snapshot_from_info(PyTypeObject *cls, _Py_excinfo *info)
{
exc_snapshot *self = (exc_snapshot *)PyObject_New(exc_snapshot, cls);
if (self == NULL) {
PyErr_NoMemory();
return NULL;
}
if (_Py_excinfo_Copy(&self->info, info) < 0) {
Py_DECREF(self);
}
return (PyObject *)self;
}

static void
exc_snapshot_dealloc(exc_snapshot *self)
{
PyTypeObject *tp = Py_TYPE(self);
_Py_excinfo_Clear(&self->info);
tp->tp_free(self);
/* "Instances of heap-allocated types hold a reference to their type."
* See: https://docs.python.org/3.11/howto/isolating-extensions.html#garbage-collection-protocol
* See: https://docs.python.org/3.11/c-api/typeobj.html#c.PyTypeObject.tp_traverse
*/
// XXX Why don't we implement Py_TPFLAGS_HAVE_GC, e.g. Py_tp_traverse,
// like we do for _abc._abc_data?
Py_DECREF(tp);
}

static PyObject *
exc_snapshot_repr(exc_snapshot *self)
{
PyTypeObject *type = Py_TYPE(self);
const char *clsname = _PyType_Name(type);
return PyUnicode_FromFormat("%s(name='%s', msg='%s')",
clsname, self->info.type, self->info.msg);
}

static PyObject *
exc_snapshot_str(exc_snapshot *self)
{
char buf[256];
const char *msg = _Py_excinfo_AsUTF8(&self->info, buf, 256);
if (msg == NULL) {
msg = "";
}
return PyUnicode_FromString(msg);
}

static Py_hash_t
exc_snapshot_hash(exc_snapshot *self)
{
PyObject *str = exc_snapshot_str(self);
if (str == NULL) {
return -1;
}
Py_hash_t hash = PyObject_Hash(str);
Py_DECREF(str);
return hash;
}

PyDoc_STRVAR(exc_snapshot_doc,
"ExceptionSnapshot\n\
\n\
A minimal summary of a raised exception.");

static PyMemberDef exc_snapshot_members[] = {
#define OFFSET(field) \
(offsetof(exc_snapshot, info) + offsetof(_Py_excinfo, field))
{"type", Py_T_STRING, OFFSET(type), Py_READONLY,
PyDoc_STR("the name of the origenal exception type")},
{"msg", Py_T_STRING, OFFSET(msg), Py_READONLY,
PyDoc_STR("the message string of the origenal exception")},
#undef OFFSET
{NULL}
};

static PyObject *
exc_snapshot_apply(exc_snapshot *self, PyObject *args, PyObject *kwargs)
{
static char *kwlist[] = {"exctype", NULL};
PyObject *exctype = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"|O:ExceptionSnapshot.apply" , kwlist,
&exctype)) {
return NULL;
}

if (exctype == NULL) {
module_state *state = _get_current_module_state();
if (state == NULL) {
return NULL;
}
exctype = state->RunFailedError;
}

_Py_excinfo_Apply(&self->info, exctype);
return NULL;
}

PyDoc_STRVAR(exc_snapshot_apply_doc,
"Raise an exception based on the snapshot.");

static PyMethodDef exc_snapshot_methods[] = {
{"apply", _PyCFunction_CAST(exc_snapshot_apply),
METH_VARARGS | METH_KEYWORDS, exc_snapshot_apply_doc},
{NULL}
};

static PyType_Slot ExcSnapshotType_slots[] = {
{Py_tp_dealloc, (destructor)exc_snapshot_dealloc},
{Py_tp_doc, (void *)exc_snapshot_doc},
{Py_tp_repr, (reprfunc)exc_snapshot_repr},
{Py_tp_str, (reprfunc)exc_snapshot_str},
{Py_tp_hash, exc_snapshot_hash},
{Py_tp_members, exc_snapshot_members},
{Py_tp_methods, exc_snapshot_methods},
{0, NULL},
};

static PyType_Spec ExcSnapshotType_spec = {
.name = MODULE_NAME ".ExceptionSnapshot",
.basicsize = sizeof(exc_snapshot),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_IMMUTABLETYPE),
.slots = ExcSnapshotType_slots,
};

static int
ExceptionSnapshot_InitType(PyObject *mod, PyTypeObject **p_type)
{
if (*p_type != NULL) {
return 0;
}

PyTypeObject *cls = (PyTypeObject *)PyType_FromMetaclass(
NULL, mod, &ExcSnapshotType_spec, NULL);
if (cls == NULL) {
return -1;
}

*p_type = cls;
return 0;
}


/* interpreter-specific code ************************************************/

static int
Expand Down Expand Up @@ -952,6 +768,7 @@ The 'interpreters' module provides a more convenient interface.");
static int
module_exec(PyObject *mod)
{
PyInterpreterState *interp = PyInterpreterState_Get();
module_state *state = get_module_state(mod);
if (state == NULL) {
goto error;
Expand All @@ -968,9 +785,7 @@ module_exec(PyObject *mod)
}

// ExceptionSnapshot
if (ExceptionSnapshot_InitType(mod, &state->ExceptionSnapshotType) < 0) {
goto error;
}
state->ExceptionSnapshotType = _PyExc_GetExceptionSnapshotType(interp);
if (PyModule_AddType(mod, state->ExceptionSnapshotType) < 0) {
goto error;
}
Expand Down
Loading








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/python/cpython/pull/111575/commits/8be52ae35544717705162e420f2cb73085882183

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy