|
18 | 18 | #define PY_SSIZE_T_CLEAN |
19 | 19 |
|
20 | 20 | #include "Python.h" |
21 | | -#include "datetime.h" |
22 | | -#include "marshal.h" |
| 21 | +#include "frameobject.h" // PyFrame_Check() |
| 22 | +#include "datetime.h" // PyDateTimeAPI |
| 23 | +#include "marshal.h" // PyMarshal_WriteLongToFile |
23 | 24 | #include "structmember.h" // PyMemberDef |
24 | | -#include <float.h> |
| 25 | +#include <float.h> // FLT_MAX |
25 | 26 | #include <signal.h> |
26 | 27 |
|
27 | 28 | #ifdef MS_WINDOWS |
28 | | -# include <winsock2.h> /* struct timeval */ |
| 29 | +# include <winsock2.h> // struct timeval |
29 | 30 | #endif |
30 | 31 |
|
31 | 32 | #ifdef HAVE_SYS_WAIT_H |
32 | | -#include <sys/wait.h> /* For W_STOPCODE */ |
| 33 | +#include <sys/wait.h> // W_STOPCODE |
33 | 34 | #endif |
34 | 35 |
|
35 | 36 | #ifdef Py_BUILD_CORE |
36 | 37 | # error "_testcapi must test the public Python C API, not CPython internal C API" |
37 | 38 | #endif |
38 | 39 |
|
| 40 | + |
| 41 | +// Forward declarations |
39 | 42 | static struct PyModuleDef _testcapimodule; |
40 | 43 | static PyType_Spec HeapTypeNameType_Spec; |
41 | | - |
42 | 44 | static PyObject *TestError; /* set to exception object in init */ |
43 | 45 |
|
| 46 | + |
44 | 47 | /* Raise TestError with test_name + ": " + msg, and return NULL. */ |
45 | 48 |
|
46 | 49 | static PyObject * |
@@ -5674,6 +5677,57 @@ type_get_version(PyObject *self, PyObject *type) |
5674 | 5677 | } |
5675 | 5678 |
|
5676 | 5679 |
|
| 5680 | +// Test PyThreadState C API |
| 5681 | +static PyObject * |
| 5682 | +test_tstate_capi(PyObject *self, PyObject *Py_UNUSED(args)) |
| 5683 | +{ |
| 5684 | + // PyThreadState_Get() |
| 5685 | + PyThreadState *tstate = PyThreadState_Get(); |
| 5686 | + assert(tstate != NULL); |
| 5687 | + |
| 5688 | + // PyThreadState_GET() |
| 5689 | + PyThreadState *tstate2 = PyThreadState_Get(); |
| 5690 | + assert(tstate2 == tstate); |
| 5691 | + |
| 5692 | + // private _PyThreadState_UncheckedGet() |
| 5693 | + PyThreadState *tstate3 = _PyThreadState_UncheckedGet(); |
| 5694 | + assert(tstate3 == tstate); |
| 5695 | + |
| 5696 | + // PyThreadState_EnterTracing(), PyThreadState_LeaveTracing() |
| 5697 | + PyThreadState_EnterTracing(tstate); |
| 5698 | + PyThreadState_LeaveTracing(tstate); |
| 5699 | + |
| 5700 | + // PyThreadState_GetDict(): no tstate argument |
| 5701 | + PyObject *dict = PyThreadState_GetDict(); |
| 5702 | + // PyThreadState_GetDict() API can return NULL if PyDict_New() fails, |
| 5703 | + // but it should not occur in practice. |
| 5704 | + assert(dict != NULL); |
| 5705 | + assert(PyDict_Check(dict)); |
| 5706 | + // dict is a borrowed reference |
| 5707 | + |
| 5708 | + // private _PyThreadState_GetDict() |
| 5709 | + PyObject *dict2 = _PyThreadState_GetDict(tstate); |
| 5710 | + assert(dict2 == dict); |
| 5711 | + // dict2 is a borrowed reference |
| 5712 | + |
| 5713 | + // PyThreadState_GetInterpreter() |
| 5714 | + PyInterpreterState *interp = PyThreadState_GetInterpreter(tstate); |
| 5715 | + assert(interp != NULL); |
| 5716 | + |
| 5717 | + // PyThreadState_GetFrame() |
| 5718 | + PyFrameObject*frame = PyThreadState_GetFrame(tstate); |
| 5719 | + assert(frame != NULL); |
| 5720 | + assert(PyFrame_Check(frame)); |
| 5721 | + Py_DECREF(frame); |
| 5722 | + |
| 5723 | + // PyThreadState_GetID() |
| 5724 | + uint64_t id = PyThreadState_GetID(tstate); |
| 5725 | + assert(id >= 1); |
| 5726 | + |
| 5727 | + Py_RETURN_NONE; |
| 5728 | +} |
| 5729 | + |
| 5730 | + |
5677 | 5731 | static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *); |
5678 | 5732 | static PyObject *getargs_s_hash_int(PyObject *, PyObject *, PyObject*); |
5679 | 5733 |
|
@@ -5957,6 +6011,7 @@ static PyMethodDef TestMethods[] = { |
5957 | 6011 | {"fatal_error", test_fatal_error, METH_VARARGS, |
5958 | 6012 | PyDoc_STR("fatal_error(message, release_gil=False): call Py_FatalError(message)")}, |
5959 | 6013 | {"type_get_version", type_get_version, METH_O, PyDoc_STR("type->tp_version_tag")}, |
| 6014 | + {"test_tstate_capi", test_tstate_capi, METH_NOARGS, NULL}, |
5960 | 6015 | {NULL, NULL} /* sentinel */ |
5961 | 6016 | }; |
5962 | 6017 |
|
|
0 commit comments