qqwqqw689
(qqwqqw689)
February 12, 2024, 4:07pm
1
In file Include/pythonrun.h, we have code
#if defined(WIN32) && !defined(MS_WIN64) && !defined(_M_ARM) && defined(_MSC_VER) && _MSC_VER >= 1300
/* Enable stack checking under Microsoft C */
// When changing the platforms, ensure PyOS_CheckStack() docs are still correct
#define USE_STACKCHECK
#endif
But I only see _WIN32 as a predefined macro in MSVC and I didn’t find “#define WIN32” in cpython (maybe I miss it?).
Maybe WIN32 should be _WIN32.
qqwqqw689
(qqwqqw689)
February 13, 2024, 12:29am
3
But if we never define WIN32 macro, we will never have USE_STACKCHECK macro, then we can never use function PyOS_CheckStack().
See the doc .
qqwqqw689
(qqwqqw689)
February 13, 2024, 12:35am
4
For example, we have code in file object.c
int
PyObject_Print(PyObject *op, FILE *fp, int flags)
{
int ret = 0;
if (PyErr_CheckSignals())
return -1;
#ifdef USE_STACKCHECK
if (PyOS_CheckStack()) {
PyErr_SetString(PyExc_MemoryError, "stack overflow");
return -1;
}
#endif
clearerr(fp); /* Clear any previous error condition */
if (op == NULL) {
Py_BEGIN_ALLOW_THREADS
fprintf(fp, "<nil>");
Py_END_ALLOW_THREADS
}
else {
if (Py_REFCNT(op) <= 0) {
Py_BEGIN_ALLOW_THREADS
fprintf(fp, "<refcnt %zd at %p>", Py_REFCNT(op), (void *)op);
Py_END_ALLOW_THREADS
}
else {
PyObject *s;
if (flags & Py_PRINT_RAW)
s = PyObject_Str(op);
else
s = PyObject_Repr(op);
if (s == NULL) {
ret = -1;
}
else {
assert(PyUnicode_Check(s));
const char *t;
Py_ssize_t len;
t = PyUnicode_AsUTF8AndSize(s, &len);
if (t == NULL) {
ret = -1;
}
else {
fwrite(t, 1, len, fp);
}
Py_DECREF(s);
}
}
}
if (ret == 0) {
if (ferror(fp)) {
PyErr_SetFromErrno(PyExc_OSError);
clearerr(fp);
ret = -1;
}
}
return ret;
}
Only if we have USE_STACKCHECK macro, we can use PyOS_CheckStack().
Because it is only a check, it will not affect cpython in most situations.