Skip to content

Commit 543a395

Browse files
bpo-38005: Remove support of string argument in InterpreterID(). (GH-16227)
Make negative interpreter id to raise ValueError instead of RuntimeError.
1 parent 15ccc4f commit 543a395

File tree

2 files changed

+37
-59
lines changed

2 files changed

+37
-59
lines changed

Lib/test/test__xxsubinterpreters.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ def test_does_not_exist(self):
514514
interpreters.is_running(1_000_000)
515515

516516
def test_bad_id(self):
517-
with self.assertRaises(RuntimeError):
517+
with self.assertRaises(ValueError):
518518
interpreters.is_running(-1)
519519

520520

@@ -530,18 +530,15 @@ class Int(str):
530530
def __index__(self):
531531
return 10
532532

533-
for id in ('10', '1_0', Int()):
534-
with self.subTest(id=id):
535-
id = interpreters.InterpreterID(id, force=True)
536-
self.assertEqual(int(id), 10)
533+
id = interpreters.InterpreterID(Int(), force=True)
534+
self.assertEqual(int(id), 10)
537535

538536
def test_bad_id(self):
539537
self.assertRaises(TypeError, interpreters.InterpreterID, object())
540538
self.assertRaises(TypeError, interpreters.InterpreterID, 10.0)
539+
self.assertRaises(TypeError, interpreters.InterpreterID, '10')
541540
self.assertRaises(TypeError, interpreters.InterpreterID, b'10')
542541
self.assertRaises(ValueError, interpreters.InterpreterID, -1)
543-
self.assertRaises(ValueError, interpreters.InterpreterID, '-1')
544-
self.assertRaises(ValueError, interpreters.InterpreterID, 'spam')
545542
self.assertRaises(OverflowError, interpreters.InterpreterID, 2**64)
546543

547544
def test_does_not_exist(self):
@@ -720,7 +717,7 @@ def test_does_not_exist(self):
720717
interpreters.destroy(1_000_000)
721718

722719
def test_bad_id(self):
723-
with self.assertRaises(RuntimeError):
720+
with self.assertRaises(ValueError):
724721
interpreters.destroy(-1)
725722

726723
def test_from_current(self):
@@ -863,7 +860,7 @@ def test_does_not_exist(self):
863860
interpreters.run_string(id, 'print("spam")')
864861

865862
def test_error_id(self):
866-
with self.assertRaises(RuntimeError):
863+
with self.assertRaises(ValueError):
867864
interpreters.run_string(-1, 'print("spam")')
868865

869866
def test_bad_id(self):

Objects/interpreteridobject.c

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -35,52 +35,45 @@ newinterpid(PyTypeObject *cls, int64_t id, int force)
3535
return self;
3636
}
3737

38-
static PyObject *
39-
interpid_new(PyTypeObject *cls, PyObject *args, PyObject *kwds)
38+
static int
39+
interp_id_converter(PyObject *arg, void *ptr)
4040
{
41-
static char *kwlist[] = {"id", "force", NULL};
42-
PyObject *idobj;
43-
int force = 0;
44-
if (!PyArg_ParseTupleAndKeywords(args, kwds,
45-
"O|$p:InterpreterID.__init__", kwlist,
46-
&idobj, &force)) {
47-
return NULL;
48-
}
49-
50-
// Coerce and check the ID.
5141
int64_t id;
52-
if (PyObject_TypeCheck(idobj, &_PyInterpreterID_Type)) {
53-
id = ((interpid *)idobj)->id;
42+
if (PyObject_TypeCheck(arg, &_PyInterpreterID_Type)) {
43+
id = ((interpid *)arg)->id;
5444
}
55-
else {
56-
PyObject *pyid;
57-
if (PyIndex_Check(idobj)) {
58-
pyid = idobj;
59-
Py_INCREF(pyid);
60-
}
61-
else if (PyUnicode_Check(idobj)) {
62-
pyid = PyNumber_Long(idobj);
63-
if (pyid == NULL) {
64-
return NULL;
65-
}
66-
}
67-
else {
68-
PyErr_Format(PyExc_TypeError,
69-
"interpreter ID must be an int, got %.100s",
70-
idobj->ob_type->tp_name);
71-
return NULL;
72-
}
73-
id = PyLong_AsLongLong(pyid);
74-
Py_DECREF(pyid);
45+
else if (PyIndex_Check(arg)) {
46+
id = PyLong_AsLongLong(arg);
7547
if (id == -1 && PyErr_Occurred()) {
76-
return NULL;
48+
return 0;
7749
}
7850
if (id < 0) {
7951
PyErr_Format(PyExc_ValueError,
80-
"interpreter ID must be a non-negative int, got %R", idobj);
81-
return NULL;
52+
"interpreter ID must be a non-negative int, got %R", arg);
53+
return 0;
8254
}
8355
}
56+
else {
57+
PyErr_Format(PyExc_TypeError,
58+
"interpreter ID must be an int, got %.100s",
59+
arg->ob_type->tp_name);
60+
return 0;
61+
}
62+
*(int64_t *)ptr = id;
63+
return 1;
64+
}
65+
66+
static PyObject *
67+
interpid_new(PyTypeObject *cls, PyObject *args, PyObject *kwds)
68+
{
69+
static char *kwlist[] = {"id", "force", NULL};
70+
int64_t id;
71+
int force = 0;
72+
if (!PyArg_ParseTupleAndKeywords(args, kwds,
73+
"O&|$p:InterpreterID.__init__", kwlist,
74+
interp_id_converter, &id, &force)) {
75+
return NULL;
76+
}
8477

8578
return (PyObject *)newinterpid(cls, id, force);
8679
}
@@ -287,19 +280,7 @@ PyInterpreterState *
287280
_PyInterpreterID_LookUp(PyObject *requested_id)
288281
{
289282
int64_t id;
290-
if (PyObject_TypeCheck(requested_id, &_PyInterpreterID_Type)) {
291-
id = ((interpid *)requested_id)->id;
292-
}
293-
else if (PyIndex_Check(requested_id)) {
294-
id = PyLong_AsLongLong(requested_id);
295-
if (id == -1 && PyErr_Occurred() != NULL) {
296-
return NULL;
297-
}
298-
assert(id <= INT64_MAX);
299-
}
300-
else {
301-
PyErr_Format(PyExc_TypeError, "interpreter ID must be an int, got %.100s",
302-
requested_id->ob_type->tp_name);
283+
if (!interp_id_converter(requested_id, &id)) {
303284
return NULL;
304285
}
305286
return _PyInterpreterState_LookUpID(id);

0 commit comments

Comments
 (0)