Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d54b678

Browse files
committed
Merge pull request #4305 from charris/fix-gh-4256
BUG: #4256: f2py, PyString_FromStringAndSize is undefined in Python3.
2 parents 0178b12 + ddcb49e commit d54b678

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

numpy/f2py/cfuncs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@
312312
needs['pyobj_from_string1']=['string']
313313
cppmacros['pyobj_from_string1']='#define pyobj_from_string1(v) (PyString_FromString((char *)v))'
314314
needs['pyobj_from_string1size']=['string']
315-
cppmacros['pyobj_from_string1size']='#define pyobj_from_string1size(v,len) (PyString_FromStringAndSize((char *)v, len))'
315+
cppmacros['pyobj_from_string1size']='#define pyobj_from_string1size(v,len) (PyUString_FromStringAndSize((char *)v, len))'
316316
needs['TRYPYARRAYTEMPLATE']=['PRINTPYOBJERR']
317317
cppmacros['TRYPYARRAYTEMPLATE']="""\
318318
/* New SciPy */

numpy/f2py/src/fortranobject.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ extern "C" {
2020
#define PyString_GET_SIZE PyBytes_GET_SIZE
2121
#define PyString_AS_STRING PyBytes_AS_STRING
2222
#define PyString_FromString PyBytes_FromString
23+
#define PyUString_FromStringAndSize PyUnicode_FromStringAndSize
2324
#define PyString_ConcatAndDel PyBytes_ConcatAndDel
2425
#define PyString_AsString PyBytes_AsString
2526

@@ -29,6 +30,10 @@ extern "C" {
2930
#define PyInt_AsLong PyLong_AsLong
3031

3132
#define PyNumber_Int PyNumber_Long
33+
34+
#else
35+
36+
#define PyUString_FromStringAndSize PyString_FromStringAndSize
3237
#endif
3338

3439

numpy/f2py/tests/test_callback.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ class TestF77Callback(util.F2PyTest):
3434
external fun
3535
call fun(a)
3636
end
37+
38+
subroutine string_callback(callback, a)
39+
external callback
40+
double precision callback
41+
double precision a
42+
character*1 r
43+
cf2py intent(out) a
44+
r = 'r'
45+
a = callback(r)
46+
end
47+
3748
"""
3849

3950
@dec.slow
@@ -103,6 +114,19 @@ def mth(self):
103114
r = t(a.mth)
104115
assert_( r==9, repr(r))
105116

117+
def test_string_callback(self):
118+
119+
def callback(code):
120+
if code == 'r':
121+
return 0
122+
else:
123+
return 1
124+
125+
f = getattr(self.module, 'string_callback')
126+
r = f(callback)
127+
assert_(r == 0, repr(r))
128+
129+
106130
if __name__ == "__main__":
107131
import nose
108132
nose.runmodule()

0 commit comments

Comments
 (0)