8000 BUG: f2py: thread-safe forcomb (#29091) · numpy/numpy@a01a14e · GitHub
[go: up one dir, main page]

Skip to content

Commit a01a14e

Browse files
crusaderkycharris
authored andcommitted
BUG: f2py: thread-safe forcomb (#29091)
1 parent 141fb34 commit a01a14e

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

numpy/f2py/cfuncs.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -598,32 +598,37 @@ def errmess(s: str) -> None:
598598
return ii;
599599
}"""
600600
cfuncs['forcomb'] = """
601-
static struct { int nd;npy_intp *d;int *i,*i_tr,tr; } forcombcache;
602-
static int initforcomb(npy_intp *dims,int nd,int tr) {
601+
struct ForcombCache { int nd;npy_intp *d;int *i,*i_tr,tr; };
602+
static int initforcomb(struct ForcombCache *cache, npy_intp *dims,int nd,int tr) {
603603
int k;
604604
if (dims==NULL) return 0;
605605
if (nd<0) return 0;
606-
forcombcache.nd = nd;
607-
forcombcache.d = dims;
608-
forcombcache.tr = tr;
609-
if ((forcombcache.i = (int *)malloc(sizeof(int)*nd))==NULL) return 0;
610-
if ((forcombcache.i_tr = (int *)malloc(sizeof(int)*nd))==NULL) return 0;
606+
cache->nd = nd;
607+
cache->d = dims;
608+
cache->tr = tr;
609+
610+
cache->i = (int *)malloc(sizeof(int)*nd);
611+
if (cache->i==NULL) return 0;
612+
cache->i_tr = (int *)malloc(sizeof(int)*nd);
613+
if (cache->i_tr==NULL) {free(cache->i); return 0;};
614+
611615
for (k=1;k<nd;k++) {
612-
forcombcache.i[k] = forcombcache.i_tr[nd-k-1] = 0;
616+
cache->i[k] = cache->i_tr[nd-k-1] = 0;
613617
}
614-
forcombcache.i[0] = forcombcache.i_tr[nd-1] = -1;
618+
cache->i[0] = cache->i_tr[nd-1] = -1;
615619
return 1;
616620
}
617-
static int *nextforcomb(void) {
621+
static int *nextforcomb(struct ForcombCache *cache) {
622+
if (cache==NULL) return NULL;
618623
int j,*i,*i_tr,k;
619-
int nd=forcombcache.nd;
620-
if ((i=forcombcache.i) == NULL) return NULL;
621-
if ((i_tr=forcombcache.i_tr) == NULL) return NULL;
622-
if (forcombcache.d == NULL) return NULL;
624+
int nd=cache->nd;
625+
if ((i=cache->i) == NULL) return NULL;
626+
if ((i_tr=cache->i_tr) == NULL) return NULL;
627+
if (cache->d == NULL) return NULL;
623628
i[0]++;
624-
if (i[0]==forcombcache.d[0]) {
629+
if (i[0]==cache->d[0]) {
625630
j=1;
626-
while ((j<nd) && (i[j]==forcombcache.d[j]-1)) j++;
631+
while ((j<nd) && (i[j]==cache->d[j]-1)) j++;
627632
if (j==nd) {
628633
free(i);
629634
free(i_tr);
@@ -634,7 +639,7 @@ def errmess(s: str) -> None:
634639
i_tr[nd-j-1]++;
635640
} else
636641
i_tr[nd-1]++;
637-
if (forcombcache.tr) return i_tr;
642+
if (cache->tr) return i_tr;
638643
return i;
639644
}"""
640645
needs['try_pyarr_from_string'] = ['STRINGCOPYN', 'PRINTPYOBJERR', 'string']

numpy/f2py/rules.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,9 +1184,10 @@
11841184
"""\
11851185
int *_i,capi_i=0;
11861186
CFUNCSMESS(\"#name#: Initializing #varname#=#init#\\n\");
1187-
if (initforcomb(PyArray_DIMS(capi_#varname#_as_array),
1187+
struct ForcombCache cache;
1188+
if (initforcomb(&cache, PyArray_DIMS(capi_#varname#_as_array),
11881189
PyArray_NDIM(capi_#varname#_as_array),1)) {
1189-
while ((_i = nextforcomb()))
1190+
while ((_i = nextforcomb(&cache)))
11901191
#varname#[capi_i++] = #init#; /* fortran way */
11911192
} else {
11921193
PyObject *exc, *val, *tb;

0 commit comments

Comments
 (0)
0