8000 gh-76785: Clean Up Interpreter ID Conversions by ericsnowcurrently · Pull Request #117048 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-76785: Clean Up Interpreter ID Conversions #117048

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
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
PyInterpreterID_LookUp() -> PyInterpreterState_LookUpIDObject()
  • Loading branch information
ericsnowcurrently committed Mar 20, 2024
commit 7154bb663c2f166f29ae50f65aff08c25c610085
1 change: 0 additions & 1 deletion Include/cpython/interpreteridobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ PyAPI_DATA(PyTypeObject) PyInterpreterID_Type;

PyAPI_FUNC(PyObject *) PyInterpreterID_New(int64_t);
PyAPI_FUNC(PyObject *) PyInterpreterState_GetIDObject(PyInterpreterState *);
PyAPI_FUNC(PyInterpreterState *) PyInterpreterID_LookUp(PyObject *);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that in 3.12 this was a private API (leading _) but in 3.13 it lost the _. It looks like you're going back to private. What about the handful of definitions above this line? Are any of them meant to be public?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good question. They needn't be public at this point and it turns out interpreteridobject.h was never added to Python.h, so there shouldn't be a problem moving them. I'll do that here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, I'm going to remove the InterpreterID type, including this header file, in a separate PR. (I made some changes a few months ago that made the class unnecessary. It can be removed with a couple small tweaks.)


#ifdef Py_BUILD_CORE
extern int64_t _PyInterpreterID_GetID(PyObject *);
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ extern int64_t _PyInterpreterState_ObjectToID(PyObject *);

// Export for the _xxinterpchannels module.
PyAPI_FUNC(PyInterpreterState *) _PyInterpreterState_LookUpID(int64_t);
PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_LookUpIDObject(PyObject *);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this have a leading _ as well, like the others below?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'll fix that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


PyAPI_FUNC(int) _PyInterpreterState_IDInitref(PyInterpreterState *);
PyAPI_FUNC(int) _PyInterpreterState_IDIncref(PyInterpreterState *);
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_capi/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2185,7 +2185,7 @@ def test_equality(self):

def test_linked_lifecycle(self):
id1 = _interpreters.create()
_testcapi.unlink_interpreter_refcount(id1)
_testinternalcapi.unlink_interpreter_refcount(id1)
self.assertEqual(
_testinternalcapi.get_interpreter_refcount(id1),
0)
Expand All @@ -2201,7 +2201,7 @@ def test_linked_lifecycle(self):
_testinternalcapi.get_interpreter_refcount(id1),
0)

_testcapi.link_interpreter_refcount(id1)
_testinternalcapi.link_interpreter_refcount(id1)
self.assertEqual(
_testinternalcapi.get_interpreter_refcount(id1),
0)
Expand Down
26 changes: 0 additions & 26 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1455,30 +1455,6 @@ get_interpreterid_type(PyObject *self, PyObject *Py_UNUSED(ignored))
return Py_NewRef(&PyInterpreterID_Type);
}

static PyObject *
link_interpreter_refcount(PyObject *self, PyObject *idobj)
{
PyInterpreterState *interp = PyInterpreterID_LookUp(idobj);
if (interp == NULL) {
assert(PyErr_Occurred());
return NULL;
}
_PyInterpreterState_RequireIDRef(interp, 1);
Py_RETURN_NONE;
}

static PyObject *
unlink_interpreter_refcount(PyObject *self, PyObject *idobj)
{
PyInterpreterState *interp = PyInterpreterID_LookUp(idobj);
if (interp == NULL) {
assert(PyErr_Occurred());
return NULL;
}
_PyInterpreterState_RequireIDRef(interp, 0);
Py_RETURN_NONE;
}

static PyMethodDef ml;

static PyObject *
Expand Down Expand Up @@ -3297,8 +3273,6 @@ static PyMethodDef TestMethods[] = {
{"test_current_tstate_matches", test_current_tstate_matches, METH_NOARGS},
{"run_in_subinterp", run_in_subinterp, METH_VARARGS},
{"get_interpreterid_type", get_interpreterid_type, METH_NOARGS},
{"link_interpreter_refcount", link_interpreter_refcount, METH_O},
{"unlink_interpreter_refcount", unlink_interpreter_refcount, METH_O},
{"create_cfunction", create_cfunction, METH_NOARGS},
{"call_in_temporary_c_thread", call_in_temporary_c_thread, METH_VARARGS,
PyDoc_STR("set_error_class(error_class) -> None")},
Expand Down
33 changes: 29 additions & 4 deletions Modules/_testinternalcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
#include "pycore_pyerrors.h" // _PyErr_ChainExceptions1()
#include "pycore_pystate.h" // _PyThreadState_GET()

#include "interpreteridobject.h" // PyInterpreterID_LookUp()

#include "clinic/_testinternalcapi.c.h"

// Include test definitions from _testinternalcapi/
Expand Down Expand Up @@ -1112,7 +1110,7 @@ pending_identify(PyObject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "O:pending_identify", &interpid)) {
return NULL;
}
PyInterpreterState *interp = PyInterpreterID_LookUp(interpid);
PyInterpreterState *interp = PyInterpreterState_LookUpIDObject(interpid);
if (interp == NULL) {
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_ValueError, "interpreter not found");
Expand Down Expand Up @@ -1378,6 +1376,31 @@ dict_getitem_knownhash(PyObject *self, PyObject *args)
}


static PyObject *
link_interpreter_refcount(PyObject *self, PyObject *idobj)
{
PyInterpreterState *interp = PyInterpreterState_LookUpIDObject(idobj);
if (interp == NULL) {
assert(PyErr_Occurred());
return NULL;
}
_PyInterpreterState_RequireIDRef(interp, 1);
Py_RETURN_NONE;
}

static PyObject *
unlink_interpreter_refcount(PyObject *self, PyObject *idobj)
{
PyInterpreterState *interp = PyInterpreterState_LookUpIDObject(idobj);
if (interp == NULL) {
assert(PyErr_Occurred());
return NULL;
}
_PyInterpreterState_RequireIDRef(interp, 0);
Py_RETURN_NONE;
}


/* To run some code in a sub-interpreter. */
static PyObject *
run_in_subinterp_with_config(PyObject *self, PyObject *args, PyObject *kwargs)
Expand Down Expand Up @@ -1480,7 +1503,7 @@ run_in_subinterp_with_config(PyObject *self, PyObject *args, PyObject *kwargs)
static PyObject *
get_interpreter_refcount(PyObject *self, PyObject *idobj)
{
PyInterpreterState *interp = PyInterpreterID_LookUp(idobj);
PyInterpreterState *interp = PyInterpreterState_LookUpIDObject(idobj);
if (interp == NULL) {
return NULL;
}
Expand Down Expand Up @@ -1724,6 +1747,8 @@ static PyMethodDef module_functions[] = {
{"get_object_dict_values", get_object_dict_values, METH_O},
{"hamt", new_hamt, METH_NOARGS},
{"dict_getitem_knownhash", dict_getitem_knownhash, METH_VARARGS},
{"link_interpreter_refcount", link_interpreter_refcount, METH_O},
{"unlink_interpreter_refcount", unlink_interpreter_refcount, METH_O},
{"run_in_subinterp_with_config",
_PyCFunction_CAST(run_in_subinterp_with_config),
METH_VARARGS | METH_KEYWORDS},
Expand Down
4 changes: 2 additions & 2 deletions Modules/_xxsubinterpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ _get_current_interp(void)
return PyInterpreterState_Get();
}

#define look_up_interp PyInterpreterID_LookUp
#define look_up_interp PyInterpreterState_LookUpIDObject


static PyObject *
Expand Down Expand Up @@ -625,7 +625,7 @@ interp_set___main___attrs(PyObject *self, PyObject *args)
}

// Look up the interpreter.
PyInterpreterState *interp = PyInterpreterID_LookUp(id);
PyInterpreterState *interp = look_up_interp(id);
if (interp == NULL) {
return NULL;
}
Expand Down
10 changes: 0 additions & 10 deletions Objects/interpreteridobject.c
D448
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,3 @@ PyInterpreterState_GetIDObject(PyInterpreterState *interp)
}
return (PyObject *)newinterpid(&PyInterpreterID_Type, id, 0);
}

PyInterpreterState *
PyInterpreterID_LookUp(PyObject *requested_id)
{
int64_t id = _PyInterpreterState_ObjectToID(requested_id);
if (id < 0) {
return NULL;
}
return _PyInterpreterState_LookUpID(id);
}
10 changes: 10 additions & 0 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,16 @@ _PyInterpreterState_LookUpID(int64_t requested_id)
return interp;
}

PyInterpreterState *
PyInterpreterState_LookUpIDObject(PyObject *requested_id)
{
int64_t id = _PyInterpreterState_ObjectToID(requested_id);
if (id < 0) {
return NULL;
}
return _PyInterpreterState_LookUpID(id);
}


/********************************/
/* the per-thread runtime state */
Expand Down
0