Skip to content
Closed
Prev Previous commit
Next Next commit
Fix static locals in builtin modules.
  • Loading branch information
ericsnowcurrently committed May 17, 2019
commit a9cfc159da9386976ed2f0c41d268ec5b99212ca
14 changes: 7 additions & 7 deletions Modules/_functoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -510,9 +510,9 @@ keyobject_call(keyobject *ko, PyObject *args, PyObject *kwds)
{
PyObject *object;
keyobject *result;
static char *kwargs[] = {"obj", NULL};
static char *kwlist[] = {"obj", NULL};

if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:K", kwargs, &object))
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:K", kwlist, &object))
return NULL;
result = PyObject_New(keyobject, &keyobject_type);
if (!result)
Expand Down Expand Up @@ -566,10 +566,10 @@ static PyObject *
functools_cmp_to_key(PyObject *self, PyObject *args, PyObject *kwds)
{
PyObject *cmp;
static char *kwargs[] = {"mycmp", NULL};
static char *kwlist[] = {"mycmp", NULL};
keyobject *object;

if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:cmp_to_key", kwargs, &cmp))
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:cmp_to_key", kwlist, &cmp))
return NULL;
object = PyObject_New(keyobject, &keyobject_type);
if (!object)
Expand Down Expand Up @@ -1096,10 +1096,10 @@ lru_cache_new(PyTypeObject *type, PyObject *args, PyObject *kw)
lru_cache_object *obj;
Py_ssize_t maxsize;
PyObject *(*wrapper)(lru_cache_object *, PyObject *, PyObject *);
static char *keywords[] = {"user_function", "maxsize", "typed",
"cache_info_type", NULL};
static char *kwlist[] = {"user_function", "maxsize", "typed",
"cache_info_type", NULL};

if (!PyArg_ParseTupleAndKeywords(args, kw, "OOpO:lru_cache", keywords,
if (!PyArg_ParseTupleAndKeywords(args, kw, "OOpO:lru_cache", kwlist,
&func, &maxsize_O, &typed,
&cache_info_type)) {
return NULL;
Expand Down
11 changes: 6 additions & 5 deletions Modules/_io/bufferedio.c
Original file line number Diff line number Diff line change
Expand Up @@ -756,20 +756,21 @@ _buffered_init(buffered *self)
return 0;
}

static PyObject *cached_eintr_int = NULL;

/* Return 1 if an OSError with errno == EINTR is set (and then
clears the error indicator), 0 otherwise.
Should only be called when PyErr_Occurred() is true.
*/
int
_PyIO_trap_eintr(void)
{
static PyObject *eintr_int = NULL;
PyObject *typ, *val, *tb;
PyOSErrorObject *env_err;

if (eintr_int == NULL) {
eintr_int = PyLong_FromLong(EINTR);
assert(eintr_int != NULL);
if (cached_eintr_int == NULL) {
cached_eintr_int = PyLong_FromLong(EINTR);
assert(cached_eintr_int != NULL);
}
if (!PyErr_ExceptionMatches(PyExc_OSError))
return 0;
Expand All @@ -778,7 +779,7 @@ _PyIO_trap_eintr(void)
env_err = (PyOSErrorObject *) val;
assert(env_err != NULL);
if (env_err->myerrno != NULL &&
PyObject_RichCompareBool(env_err->myerrno, eintr_int, Py_EQ) > 0) {
PyObject_RichCompareBool(env_err->myerrno, cached_eintr_int, Py_EQ) > 0) {
Py_DECREF(typ);
Py_DECREF(val);
Py_XDECREF(tb);
Expand Down
2 changes: 1 addition & 1 deletion Modules/_sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -1224,7 +1224,7 @@ _sre_SRE_Pattern___deepcopy__(PatternObject *self, PyObject *memo)
static PyObject *
pattern_repr(PatternObject *obj)
{
static const struct {
static const struct { // Static is okay here (immutable data).
const char *name;
int value;
} flag_names[] = {
Expand Down
2 changes: 1 addition & 1 deletion Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ local_new(PyTypeObject *type, PyObject *args, PyObject *kw)
{
localobject *self;
PyObject *wr;
static PyMethodDef wr_callback_def = {
static PyMethodDef wr_callback_def = { // Static is okay here (dynamic method def).
"_localdummy_destroyed", (PyCFunction) _localdummy_destroyed, METH_O
};

Expand Down
4 changes: 2 additions & 2 deletions Modules/itertoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -4245,9 +4245,9 @@ repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
repeatobject *ro;
PyObject *element;
Py_ssize_t cnt = -1, n_kwds = 0;
static char *kwargs[] = {"object", "times", NULL};
static char *kwlist[] = {"object", "times", NULL};

if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|n:repeat", kwargs,
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|n:repeat", kwlist,
&element, &cnt))
return NULL;

Expand Down
33 changes: 19 additions & 14 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -7360,11 +7360,12 @@ os_setgroups(PyObject *module, PyObject *groups)
#endif /* HAVE_SETGROUPS */

#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
static PyObject *struct_rusage = NULL;

static PyObject *
wait_helper(pid_t pid, int status, struct rusage *ru)
{
PyObject *result;
static PyObject *struct_rusage;
_Py_IDENTIFIER(struct_rusage);

if (pid == -1)
Expand Down Expand Up @@ -7871,7 +7872,7 @@ os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
DWORD flags = 0;

/* Assumed true, set to false if detected to not be available. */
static int windows_has_symlink_unprivileged_flag = TRUE;
static int windows_has_symlink_unprivileged_flag = TRUE; // Static is okay here (process-global).
#else
int result;
#endif
Expand Down Expand Up @@ -8344,7 +8345,7 @@ os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
#if defined(HAVE_DUP3) && \
!(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC))
/* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
static int dup3_works = -1;
static int dup3_works = -1; // Static is okay here (process-global).
#endif

if (fd < 0 || fd2 < 0) {
Expand Down Expand Up @@ -8845,19 +8846,19 @@ posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
struct sf_hdtr sf;
int flags = 0;
/* Beware that "in" clashes with Python's own "in" operator keyword */
static char *keywords[] = {"out", "in",
"offset", "count",
"headers", "trailers", "flags", NULL};
static char *kwlist[] = {"out", "in",
"offset", "count",
"headers", "trailers", "flags", NULL};

sf.headers = NULL;
sf.trailers = NULL;

#ifdef __APPLE__
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
keywords, &out, &in, Py_off_t_converter, &offset, Py_off_t_converter, &sbytes,
kwlist, &out, &in, Py_off_t_converter, &offset, Py_off_t_converter, &sbytes,
#else
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
keywords, &out, &in, Py_off_t_converter, &offset, &len,
kwlist, &out, &in, Py_off_t_converter, &offset, &len,
#endif
&headers, &trailers, &flags))
return NULL;
Expand Down Expand Up @@ -8961,10 +8962,10 @@ posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
#else
Py_ssize_t count;
PyObject *offobj;
static char *keywords[] = {"out", "in",
"offset", "count", NULL};
static char *kwlist[] = {"out", "in",
"offset", "count", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
keywords, &out, &in, &offobj, &count))
kwlist, &out, &in, &offobj, &count))
return NULL;
#ifdef __linux__
if (offobj == Py_None) {
Expand Down Expand Up @@ -11554,7 +11555,9 @@ os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
for (i = 0; ; i++) {
void *ptr;
ssize_t result;
static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
static const Py_ssize_t buffer_sizes[] = { // Static is okay here (immutable data).
128, XATTR_SIZE_MAX, 0
};
Py_ssize_t buffer_size = buffer_sizes[i];
if (!buffer_size) {
path_error(path);
Expand Down Expand Up @@ -11720,7 +11723,9 @@ os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
for (i = 0; ; i++) {
const char *start, *trace, *end;
ssize_t length;
static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
static const Py_ssize_t buffer_sizes[] = { // Static is okay here (immutable data).
256, XATTR_LIST_MAX, 0
};
Py_ssize_t buffer_size = buffer_sizes[i];
if (!buffer_size) {
/* ERANGE */
Expand Down Expand Up @@ -11946,7 +11951,7 @@ os_cpu_count_impl(PyObject *module)
/* Vista is supported and the GetMaximumProcessorCount API is Win7+
Need to fallback to Vista behavior if this call isn't present */
HINSTANCE hKernel32;
static DWORD(CALLBACK *_GetMaximumProcessorCount)(WORD) = NULL;
static DWORD(CALLBACK *_GetMaximumProcessorCount)(WORD) = NULL; // Static is okay here (buffer, non-threaded).
Py_BEGIN_ALLOW_THREADS
hKernel32 = GetModuleHandleW(L"KERNEL32");
*(FARPROC*)&_GetMaximumProcessorCount = GetProcAddress(hKernel32,
Expand Down
10 changes: 5 additions & 5 deletions Modules/timemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Return the current time in nanoseconds since the Epoch.");
static int
_PyTime_GetClockWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
{
static int initialized = 0;
static int initialized = 0; // Static is okay here (process-global).
clock_t ticks;

if (!initialized) {
Expand Down Expand Up @@ -886,10 +886,10 @@ _asctime(struct tm *timeptr)
{
/* Inspired by Open Group reference implementation available at
* http://pubs.opengroup.org/onlinepubs/009695399/functions/asctime.html */
static const char wday_name[7][4] = {
static const char wday_name[7][4] = { // Static is okay here (immutable data).
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
static const char mon_name[12][4] = {
static const char mon_name[12][4] = { // Static is okay here (immutable data).
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
Expand Down Expand Up @@ -1219,7 +1219,7 @@ _PyTime_GetProcessTimeWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
struct tms t;

if (times(&t) != (clock_t)-1) {
static long ticks_per_second = -1;
static long ticks_per_second = -1; // Static is okay here (process-global).

if (ticks_per_second == -1) {
long freq;
Expand Down Expand Up @@ -1596,7 +1596,7 @@ init_timezone(PyObject *m)
}
PyModule_AddObject(m, "tzname", tzname_obj);
#else // !HAVE_DECL_TZNAME
static const time_t YEAR = (365 * 24 + 6) * 3600;
static const time_t YEAR = (365 * 24 + 6) * 3600; // Static is okay here (immutable data).
time_t t;
struct tm p;
time_t janzone_t, julyzone_t;
Expand Down