Apply fixes and speedups

This commit is contained in:
Justine Tunney 2021-10-04 03:23:31 -07:00
parent 7521bf9e73
commit 725f4d79f6
36 changed files with 682 additions and 334 deletions

View file

@ -647,42 +647,32 @@ Py_SetRecursionLimit(int new_limit)
_Py_CheckRecursionLimit = recursion_limit;
}
/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
if the recursion_depth reaches _Py_CheckRecursionLimit.
If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
to guarantee that _Py_CheckRecursiveCall() is regularly called.
Without USE_STACKCHECK, there is no need for this. */
int
_Py_CheckRecursiveCall(const char *where)
{
PyThreadState *tstate = PyThreadState_GET();
#ifdef USE_STACKCHECK
if (PyOS_CheckStack()) {
--tstate->recursion_depth;
PyErr_SetString(PyExc_MemoryError, "Stack overflow");
return -1;
}
#endif
_Py_CheckRecursionLimit = recursion_limit;
if (tstate->recursion_critical)
/* Somebody asked that we don't check for recursion. */
return 0;
if (tstate->overflowed) {
if (tstate->recursion_depth > recursion_limit + 50) {
/* Overflowing while handling an overflow. Give up. */
Py_FatalError("Cannot recover from stack overflow.");
PyThreadState *t;
const char *rsp, *bot;
rsp = __builtin_frame_address(0);
asm(".weak\tape_stack_vaddr\n\t"
"movabs\t$ape_stack_vaddr+32768,%0" : "=r"(bot));
if (rsp > bot) {
t = PyThreadState_GET();
_Py_CheckRecursionLimit = recursion_limit;
if (t->recursion_depth > recursion_limit && !t->recursion_critical) {
--t->recursion_depth;
t->overflowed = 1;
PyErr_Format(PyExc_RecursionError,
"maximum recursion depth exceeded%s",
where);
return -1;
}
return 0;
}
if (tstate->recursion_depth > recursion_limit) {
--tstate->recursion_depth;
tstate->overflowed = 1;
PyErr_Format(PyExc_RecursionError,
"maximum recursion depth exceeded%s",
where);
} else if (rsp > bot - 20480) {
PyErr_Format(PyExc_MemoryError, "Stack overflow%s", where);
return -1;
} else {
Py_FatalError("Cannot recover from stack overflow");
}
return 0;
}
/* Status code for main loop (reason for stack unwind) */
@ -3553,13 +3543,9 @@ error:
why = WHY_EXCEPTION;
/* Double-check exception status. */
#ifdef NDEBUG
if (!PyErr_Occurred())
PyErr_SetString(PyExc_SystemError,
"error return without exception set");
#else
assert(PyErr_Occurred());
#endif
/* Log traceback info. */
PyTraceBack_Here(f);

View file

@ -9,7 +9,6 @@
/*[clinic input]
preserve
[clinic start generated code]*/
#include "third_party/python/Include/unicodeobject.h"
PyDoc_STRVAR(builtin_abs__doc__,
"abs($module, x, /)\n"
@ -128,16 +127,9 @@ builtin_chr_impl(PyObject *module, int i);
static PyObject *
builtin_chr(PyObject *module, PyObject *arg)
{
PyObject *return_value = NULL;
int i;
if (!PyArg_Parse(arg, "i:chr", &i)) {
goto exit;
}
return_value = builtin_chr_impl(module, i);
exit:
return return_value;
if (!PyArg_Parse(arg, "i:chr", &i)) return 0;
return builtin_chr_impl(module, i);
}
PyDoc_STRVAR(builtin_compile__doc__,

View file

@ -40,6 +40,15 @@
/* clang-format off */
PYTHON_PROVIDE("cosmo");
PYTHON_PROVIDE("cosmo.exit1");
PYTHON_PROVIDE("cosmo.rdtsc");
PYTHON_PROVIDE("cosmo.crc32c");
PYTHON_PROVIDE("cosmo.syscount");
PYTHON_PROVIDE("cosmo.popcount");
PYTHON_PROVIDE("cosmo.decimate");
PYTHON_PROVIDE("cosmo.getcpucore");
PYTHON_PROVIDE("cosmo.getcpunode");
PYTHON_PROVIDE("cosmo.ftrace");
PyDoc_STRVAR(cosmo_doc,
"Cosmopolitan Libc Module\n\
@ -111,7 +120,7 @@ Enables logging of C function calls to stderr, e.g.\n\
\n\
cosmo.ftrace()\n\
WeirdFunction()\n\
os._exit(1)\n\
cosmo.exit1()\n\
\n\
Please be warned this prints massive amount of text. In order for it\n\
to work, the concomitant .com.dbg binary needs to be present.");
@ -194,7 +203,22 @@ cosmo_popcount(PyObject *self, PyObject *args)
return PyLong_FromSize_t(_countbits(p, n));
}
PyDoc_STRVAR(exit1_doc,
"exit1($module)\n\
--\n\n\
Calls _Exit(1).\n\
\n\
This function is intended to abruptly end the process with less\n\
function trace output compared to os._exit(1).");
static PyObject *
cosmo_exit1(PyObject *self, PyObject *args)
{
_Exit(1);
}
static PyMethodDef cosmo_methods[] = {
{"exit1", cosmo_exit1, METH_NOARGS, exit1_doc},
{"rdtsc", cosmo_rdtsc, METH_NOARGS, rdtsc_doc},
{"crc32c", cosmo_crc32c, METH_VARARGS, crc32c_doc},
{"syscount", cosmo_syscount, METH_NOARGS, syscount_doc},

View file

@ -62,6 +62,16 @@ static int vgetargskeywordsfast_impl(PyObject **, Py_ssize_t,
struct _PyArg_Parser *,
va_list *, int );
/**
* Deconstruct argument lists of old-style functions.
*
* These are functions which use the METH_O parameter parsing method,
* which has been removed in Python 3. This is not recommended for use
* in parameter parsing in new code, and most code in the standard
* interpreter has been modified to no longer use this for that purpose.
* It does remain a convenient way to decompose other tuples, however,
* and may continue to be used for that purpose.
*/
int
PyArg_Parse(PyObject *args, const char *format, ...)
{

View file

@ -28,6 +28,7 @@
/* clang-format off */
PYTHON_PROVIDE("xed");
PYTHON_PROVIDE("xed.ild");
PyDoc_STRVAR(xed_doc, "Xed Module\n\
\n\

View file

@ -27,6 +27,7 @@
/* clang-format off */
PYTHON_PROVIDE("xterm");
PYTHON_PROVIDE("xterm.rgb2xterm256");
PyDoc_STRVAR(xterm_doc, "Xterm Module\n\
\n\