Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix static locals in stdlib extension modules.
  • Loading branch information
ericsnowcurrently committed May 17, 2019
commit c4689e0e63c075528d468676452b6c796ee5635f
16 changes: 8 additions & 8 deletions Modules/_bisectmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ bisect_right(PyObject *self, PyObject *args, PyObject *kw)
Py_ssize_t lo = 0;
Py_ssize_t hi = -1;
Py_ssize_t index;
static char *keywords[] = {"a", "x", "lo", "hi", NULL};
static char *kwlist[] = {"a", "x", "lo", "hi", NULL};

if (kw == NULL && PyTuple_GET_SIZE(args) == 2) {
list = PyTuple_GET_ITEM(args, 0);
item = PyTuple_GET_ITEM(args, 1);
}
else {
if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:bisect_right",
keywords, &list, &item, &lo, &hi))
kwlist, &list, &item, &lo, &hi))
return NULL;
}
index = internal_bisect_right(list, item, lo, hi);
Expand Down Expand Up @@ -87,15 +87,15 @@ insort_right(PyObject *self, PyObject *args, PyObject *kw)
Py_ssize_t lo = 0;
Py_ssize_t hi = -1;
Py_ssize_t index;
static char *keywords[] = {"a", "x", "lo", "hi", NULL};
static char *kwlist[] = {"a", "x", "lo", "hi", NULL};

if (kw == NULL && PyTuple_GET_SIZE(args) == 2) {
list = PyTuple_GET_ITEM(args, 0);
item = PyTuple_GET_ITEM(args, 1);
}
else {
if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:insort_right",
keywords, &list, &item, &lo, &hi))
kwlist, &list, &item, &lo, &hi))
return NULL;
}
index = internal_bisect_right(list, item, lo, hi);
Expand Down Expand Up @@ -168,15 +168,15 @@ bisect_left(PyObject *self, PyObject *args, PyObject *kw)
Py_ssize_t lo = 0;
Py_ssize_t hi = -1;
Py_ssize_t index;
static char *keywords[] = {"a", "x", "lo", "hi", NULL};
static char *kwlist[] = {"a", "x", "lo", "hi", NULL};

if (kw == NULL && PyTuple_GET_SIZE(args) == 2) {
list = PyTuple_GET_ITEM(args, 0);
item = PyTuple_GET_ITEM(args, 1);
}
else {
if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:bisect_left",
keywords, &list, &item, &lo, &hi))
kwlist, &list, &item, &lo, &hi))
return NULL;
}
index = internal_bisect_left(list, item, lo, hi);
Expand Down Expand Up @@ -204,14 +204,14 @@ insort_left(PyObject *self, PyObject *args, PyObject *kw)
Py_ssize_t lo = 0;
Py_ssize_t hi = -1;
Py_ssize_t index;
static char *keywords[] = {"a", "x", "lo", "hi", NULL};
static char *kwlist[] = {"a", "x", "lo", "hi", NULL};

if (kw == NULL && PyTuple_GET_SIZE(args) == 2) {
list = PyTuple_GET_ITEM(args, 0);
item = PyTuple_GET_ITEM(args, 1);
} else {
if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:insort_left",
keywords, &list, &item, &lo, &hi))
kwlist, &list, &item, &lo, &hi))
return NULL;
}
index = internal_bisect_left(list, item, lo, hi);
Expand Down
2 changes: 1 addition & 1 deletion Modules/_blake2/impl/blake2-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ BLAKE2_LOCAL_INLINE(uint64_t) rotr64( const uint64_t w, const unsigned c )
/* prevents compiler optimizing out memset() */
BLAKE2_LOCAL_INLINE(void) secure_zero_memory(void *v, size_t n)
{
static void *(*const volatile memset_v)(void *, int, size_t) = &memset;
static void *(*const volatile memset_v)(void *, int, size_t) = &memset; // Static is okay here (process-global).
memset_v(v, 0, n);
}

Expand Down
31 changes: 17 additions & 14 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1900,6 +1900,8 @@ static PyMethodDef c_void_p_method = { "from_param", c_void_p_from_param, METH_O
static PyMethodDef c_char_p_method = { "from_param", c_char_p_from_param, METH_O };
static PyMethodDef c_wchar_p_method = { "from_param", c_wchar_p_from_param, METH_O };

static PyObject *swapped_type_suffix = NULL;

static PyObject *CreateSwappedType(PyTypeObject *type, PyObject *args, PyObject *kwds,
PyObject *proto, struct fielddesc *fmt)
{
Expand All @@ -1908,25 +1910,25 @@ static PyObject *CreateSwappedType(PyTypeObject *type, PyObject *args, PyObject
PyObject *name = PyTuple_GET_ITEM(args, 0);
PyObject *newname;
PyObject *swapped_args;
static PyObject *suffix;
Py_ssize_t i;

swapped_args = PyTuple_New(PyTuple_GET_SIZE(args));
if (!swapped_args)
return NULL;

if (suffix == NULL)
if (swapped_type_suffix == NULL) {
#ifdef WORDS_BIGENDIAN
suffix = PyUnicode_InternFromString("_le");
swapped_type_suffix = PyUnicode_InternFromString("_le");
#else
suffix = PyUnicode_InternFromString("_be");
swapped_type_suffix = PyUnicode_InternFromString("_be");
#endif
if (suffix == NULL) {
Py_DECREF(swapped_args);
return NULL;
if (swapped_type_suffix == NULL) {
Py_DECREF(swapped_args);
return NULL;
}
}

newname = PyUnicode_Concat(name, suffix);
newname = PyUnicode_Concat(name, swapped_type_suffix);
if (newname == NULL) {
Py_DECREF(swapped_args);
return NULL;
Expand Down Expand Up @@ -4731,18 +4733,19 @@ PyTypeObject PyCArray_Type = {
0, /* tp_free */
};

static PyObject *array_type_cache;

PyObject *
PyCArrayType_from_ctype(PyObject *itemtype, Py_ssize_t length)
{
static PyObject *cache;
PyObject *key;
PyObject *result;
char name[256];
PyObject *len;

if (cache == NULL) {
cache = PyDict_New();
if (cache == NULL)
if (array_type_cache == NULL) {
array_type_cache = PyDict_New();
if (array_type_cache == NULL)
return NULL;
}
len = PyLong_FromSsize_t(length);
Expand All @@ -4752,7 +4755,7 @@ PyCArrayType_from_ctype(PyObject *itemtype, Py_ssize_t length)
Py_DECREF(len);
if (!key)
return NULL;
result = PyDict_GetItemProxy(cache, key);
result = PyDict_GetItemProxy(array_type_cache, key);
if (result) {
Py_INCREF(result);
Py_DECREF(key);
Expand Down Expand Up @@ -4790,7 +4793,7 @@ PyCArrayType_from_ctype(PyObject *itemtype, Py_ssize_t length)
Py_DECREF(key);
return NULL;
}
if (-1 == PyDict_SetItemProxy(cache, key, result)) {
if (-1 == PyDict_SetItemProxy(array_type_cache, key, result)) {
Py_DECREF(key);
Py_DECREF(result);
return NULL;
Expand Down
18 changes: 12 additions & 6 deletions Modules/_ctypes/callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -429,14 +429,17 @@ static void LoadPython(void)

/******************************************************************/

static PyObject *context_dll_get_cls = NULL;

long Call_GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
{
PyObject *mod, *func, *result;
long retval;
static PyObject *context;

if (context == NULL)
context = PyUnicode_InternFromString("_ctypes.DllGetClassObject");
if (context_dll_get_cls == NULL) {
context_dll_get_cls = PyUnicode_InternFromString("_ctypes.DllGetClassObject");
}
PyObject *context = context_dll_get_cls;

mod = PyImport_ImportModuleNoBlock("ctypes");
if (!mod) {
Expand Down Expand Up @@ -502,14 +505,17 @@ STDAPI DllGetClassObject(REFCLSID rclsid,
return result;
}

static PyObject *context_dll_can_unload = NULL;

long Call_CanUnloadNow(void)
{
PyObject *mod, *func, *result;
long retval;
static PyObject *context;

if (context == NULL)
context = PyUnicode_InternFromString("_ctypes.DllCanUnloadNow");
if (context_dll_can_unload == NULL) {
context_dll_can_unload = PyUnicode_InternFromString("_ctypes.DllCanUnloadNow");
}
PyObject *context = context_dll_can_unload;

mod = PyImport_ImportModuleNoBlock("ctypes");
if (!mod) {
Expand Down
3 changes: 2 additions & 1 deletion Modules/_ctypes/callproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ static void pymem_destructor(PyObject *ptr)
private copies value.
*/

static PyObject *error_object_name = NULL;

/*
This function creates and returns a thread-local Python object that has
space to store two integer error numbers; once created the Python object is
Expand All @@ -135,7 +137,6 @@ _ctypes_get_errobj(int **pspace)
{
PyObject *dict = PyThreadState_GetDict();
PyObject *errobj;
static PyObject *error_object_name;
if (dict == NULL) {
PyErr_SetString(PyExc_RuntimeError,
"cannot get thread state");
Expand Down
6 changes: 3 additions & 3 deletions Modules/_ctypes/cfield.c
Original file line number Diff line number Diff line change
Expand Up @@ -1569,11 +1569,11 @@ static struct fielddesc formattable[] = {
struct fielddesc *
_ctypes_get_fielddesc(const char *fmt)
{
static int initialized = 0;
struct fielddesc *table = formattable;

if (!initialized) {
initialized = 1;
static int ctypes_fielddesc_initialized = 0; // Static is okay here (process-global).
if (!ctypes_fielddesc_initialized) {
ctypes_fielddesc_initialized = 1;
#ifdef CTYPES_UNICODE
if (sizeof(wchar_t) == sizeof(short))
_ctypes_get_fielddesc("u")->pffi_type = &ffi_type_sshort;
Expand Down
7 changes: 4 additions & 3 deletions Modules/_ctypes/darwin/dlfcn_simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ static void *dlsymIntern(void *handle, const char *symbol);

static const char *error(int setget, const char *str, ...);

static int err_filled = 0;
static char errstr[ERR_STR_LEN];

/* Set and get the error string for use by dlerror */
static const char *error(int setget, const char *str, ...)
{
static char errstr[ERR_STR_LEN];
static int err_filled = 0;
const char *retval;
va_list arg;
if (setget == 0)
Expand Down Expand Up @@ -213,7 +214,7 @@ static int darwin_dlclose(void *handle)
/* dlsym, prepend the underscore and call dlsymIntern */
static void *darwin_dlsym(void *handle, const char *symbol)
{
static char undersym[257]; /* Saves calls to malloc(3) */
static char undersym[257]; // Static is okay here (saves calls to malloc(3)).
int sym_len = strlen(symbol);
void *value = NULL;
char *malloc_sym = NULL;
Expand Down
11 changes: 6 additions & 5 deletions Modules/_cursesmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -4441,17 +4441,18 @@ PyMODINIT_FUNC
PyInit__curses(void)
{
PyObject *m, *d, *v, *c_api_object;
static void *PyCurses_API[PyCurses_API_pointers];

/* Initialize object type */
if (PyType_Ready(&PyCursesWindow_Type) < 0)
return NULL;

/* Initialize the C API pointer array */
PyCurses_API[0] = (void *)&PyCursesWindow_Type;
PyCurses_API[1] = (void *)func_PyCursesSetupTermCalled;
PyCurses_API[2] = (void *)func_PyCursesInitialised;
PyCurses_API[3] = (void *)func_PyCursesInitialisedColor;
void *PyCurses_API[PyCurses_API_pointers] = {
(void *)&PyCursesWindow_Type,
(void *)func_PyCursesSetupTermCalled,
(void *)func_PyCursesInitialised,
(void *)func_PyCursesInitialisedColor,
};

/* Create the module and add the functions */
m = PyModule_Create(&_cursesmodule);
Expand Down
Loading