diff options
author | Ralf Gommers <ralf.gommers@gmail.com> | 2022-02-01 18:26:34 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-01 18:26:34 +0100 |
commit | bcdfd208a9b99036cc6e379180c3242b08dedc47 (patch) | |
tree | a946e69e22a96704332177a3b0592b1919c9d58f /numpy/f2py/cb_rules.py | |
parent | c65bc212ec1987caefba0ea7efe6a55803318de9 (diff) | |
download | numpy-bcdfd208a9b99036cc6e379180c3242b08dedc47.tar.gz |
MAINT: f2py: don't generate code that triggers `-Wsometimes-uninitialized` (#20940)
* MAINT: f2py: don't generate code that triggers `-Wsometimes-uninitialized`
Warnings look like:
```
scipy/linalg/_flapackmodule.c:2200:9: warning: variable 'return_value' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
if (capi_j>capi_i)
^~~~~~~~~~~~~
scipy/linalg/_flapackmodule.c:2217:12: note: uninitialized use occurs here
return return_value;
^~~~~~~~~~~~
scipy/linalg/_flapackmodule.c:2200:5: note: remove the 'if' if its condition is always true
if (capi_j>capi_i)
^~~~~~~~~~~~~~~~~~
scipy/linalg/_flapackmodule.c:2099:21: note: initialize the variable 'return_value' to silence this warning
int return_value;
^
= 0
```
Also:
- Initialize complex return value.
- Warn on non-void callback returning None.
- Use brackets in if-else block.
This makes the code more readable.
Co-authored-by: Pearu Peterson <pearu.peterson@gmail.com>
Diffstat (limited to 'numpy/f2py/cb_rules.py')
-rw-r--r-- | numpy/f2py/cb_rules.py | 67 |
1 files changed, 45 insertions, 22 deletions
diff --git a/numpy/f2py/cb_rules.py b/numpy/f2py/cb_rules.py index 4848233d4..c2d7faaad 100644 --- a/numpy/f2py/cb_rules.py +++ b/numpy/f2py/cb_rules.py @@ -230,12 +230,21 @@ cb_rout_rules = [ 'latexdocstrcbs': '\\noindent Call-back functions:', 'routnote': {hasnote: '--- #note#', l_not(hasnote): ''}, }, { # Function - 'decl': ' #ctype# return_value;', - 'frompyobj': [{debugcapi: ' CFUNCSMESS("cb:Getting return_value->");'}, - ' if (capi_j>capi_i)\n GETSCALARFROMPYTUPLE(capi_return,capi_i++,&return_value,#ctype#,"#ctype#_from_pyobj failed in converting return_value of call-back function #name# to C #ctype#\\n");', - {debugcapi: - ' fprintf(stderr,"#showvalueformat#.\\n",return_value);'} - ], + 'decl': ' #ctype# return_value = 0;', + 'frompyobj': [ + {debugcapi: ' CFUNCSMESS("cb:Getting return_value->");'}, + '''\ + if (capi_j>capi_i) { + GETSCALARFROMPYTUPLE(capi_return,capi_i++,&return_value,#ctype#, + "#ctype#_from_pyobj failed in converting return_value of" + " call-back function #name# to C #ctype#\\n"); + } else { + fprintf(stderr,"Warning: call-back function #name# did not provide" + " return value (index=%d, type=#ctype#)\\n",capi_i); + }''', + {debugcapi: + ' fprintf(stderr,"#showvalueformat#.\\n",return_value);'} + ], 'need': ['#ctype#_from_pyobj', {debugcapi: 'CFUNCSMESS'}, 'GETSCALARFROMPYTUPLE'], 'return': ' return return_value;', '_check': l_and(isfunction, l_not(isstringfunction), l_not(iscomplexfunction)) @@ -245,12 +254,18 @@ cb_rout_rules = [ 'args': '#ctype# return_value,int return_value_len', 'args_nm': 'return_value,&return_value_len', 'args_td': '#ctype# ,int', - 'frompyobj': [{debugcapi: ' CFUNCSMESS("cb:Getting return_value->\\"");'}, - """ if (capi_j>capi_i) - GETSTRFROMPYTUPLE(capi_return,capi_i++,return_value,return_value_len);""", - {debugcapi: - ' fprintf(stderr,"#showvalueformat#\\".\\n",return_value);'} - ], + 'frompyobj': [ + {debugcapi: ' CFUNCSMESS("cb:Getting return_value->\\"");'}, + """\ + if (capi_j>capi_i) { + GETSTRFROMPYTUPLE(capi_return,capi_i++,return_value,return_value_len); + } else { + fprintf(stderr,"Warning: call-back function #name# did not provide" + " return value (index=%d, type=#ctype#)\\n",capi_i); + }""", + {debugcapi: + ' fprintf(stderr,"#showvalueformat#\\".\\n",return_value);'} + ], 'need': ['#ctype#_from_pyobj', {debugcapi: 'CFUNCSMESS'}, 'string.h', 'GETSTRFROMPYTUPLE'], 'return': 'return;', @@ -274,27 +289,35 @@ return_value """, 'decl': """ #ifdef F2PY_CB_RETURNCOMPLEX - #ctype# return_value; + #ctype# return_value = {0, 0}; #endif """, - 'frompyobj': [{debugcapi: ' CFUNCSMESS("cb:Getting return_value->");'}, - """\ - if (capi_j>capi_i) + 'frompyobj': [ + {debugcapi: ' CFUNCSMESS("cb:Getting return_value->");'}, + """\ + if (capi_j>capi_i) { #ifdef F2PY_CB_RETURNCOMPLEX - GETSCALARFROMPYTUPLE(capi_return,capi_i++,&return_value,#ctype#,\"#ctype#_from_pyobj failed in converting return_value of call-back function #name# to C #ctype#\\n\"); + GETSCALARFROMPYTUPLE(capi_return,capi_i++,&return_value,#ctype#, + \"#ctype#_from_pyobj failed in converting return_value of call-back\" + \" function #name# to C #ctype#\\n\"); #else - GETSCALARFROMPYTUPLE(capi_return,capi_i++,return_value,#ctype#,\"#ctype#_from_pyobj failed in converting return_value of call-back function #name# to C #ctype#\\n\"); + GETSCALARFROMPYTUPLE(capi_return,capi_i++,return_value,#ctype#, + \"#ctype#_from_pyobj failed in converting return_value of call-back\" + \" function #name# to C #ctype#\\n\"); #endif -""", - {debugcapi: """ + } else { + fprintf(stderr, + \"Warning: call-back function #name# did not provide\" + \" return value (index=%d, type=#ctype#)\\n\",capi_i); + }""", + {debugcapi: """\ #ifdef F2PY_CB_RETURNCOMPLEX fprintf(stderr,\"#showvalueformat#.\\n\",(return_value).r,(return_value).i); #else fprintf(stderr,\"#showvalueformat#.\\n\",(*return_value).r,(*return_value).i); #endif - """} - ], + ], 'return': """ #ifdef F2PY_CB_RETURNCOMPLEX return return_value; |