mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-02-07 15:03:34 +00:00
Building o//third_party/python now takes 5 seconds on my PC This change works towards modifying Python to use runtime dispatching when appropriate. For example, when loading the magnums in the socket module, it's a good idea to check if the magnum is zero, because that means the local system platform doesn't support it.
66 lines
1.5 KiB
C
66 lines
1.5 KiB
C
/* clang-format off */
|
|
|
|
#include "third_party/python/Include/Python.h"
|
|
#include "third_party/python/Include/asdl.h"
|
|
|
|
asdl_seq *
|
|
_Py_asdl_seq_new(Py_ssize_t size, PyArena *arena)
|
|
{
|
|
asdl_seq *seq = NULL;
|
|
size_t n;
|
|
|
|
/* check size is sane */
|
|
if (size < 0 ||
|
|
(size && (((size_t)size - 1) > (SIZE_MAX / sizeof(void *))))) {
|
|
PyErr_NoMemory();
|
|
return NULL;
|
|
}
|
|
n = (size ? (sizeof(void *) * (size - 1)) : 0);
|
|
|
|
/* check if size can be added safely */
|
|
if (n > SIZE_MAX - sizeof(asdl_seq)) {
|
|
PyErr_NoMemory();
|
|
return NULL;
|
|
}
|
|
n += sizeof(asdl_seq);
|
|
|
|
seq = (asdl_seq *)PyArena_Malloc(arena, n);
|
|
if (!seq) {
|
|
PyErr_NoMemory();
|
|
return NULL;
|
|
}
|
|
memset(seq, 0, n);
|
|
seq->size = size;
|
|
return seq;
|
|
}
|
|
|
|
asdl_int_seq *
|
|
_Py_asdl_int_seq_new(Py_ssize_t size, PyArena *arena)
|
|
{
|
|
asdl_int_seq *seq = NULL;
|
|
size_t n;
|
|
|
|
/* check size is sane */
|
|
if (size < 0 ||
|
|
(size && (((size_t)size - 1) > (SIZE_MAX / sizeof(void *))))) {
|
|
PyErr_NoMemory();
|
|
return NULL;
|
|
}
|
|
n = (size ? (sizeof(void *) * (size - 1)) : 0);
|
|
|
|
/* check if size can be added safely */
|
|
if (n > SIZE_MAX - sizeof(asdl_seq)) {
|
|
PyErr_NoMemory();
|
|
return NULL;
|
|
}
|
|
n += sizeof(asdl_seq);
|
|
|
|
seq = (asdl_int_seq *)PyArena_Malloc(arena, n);
|
|
if (!seq) {
|
|
PyErr_NoMemory();
|
|
return NULL;
|
|
}
|
|
memset(seq, 0, n);
|
|
seq->size = size;
|
|
return seq;
|
|
}
|