Skip to content

Commit 1b8598f

Browse files
automatically convert char* function return type to string #51
1 parent e0201a2 commit 1b8598f

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

pyray/__init__.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ def pointer(self, struct):
5252
RAYWHITE =( 245, 245, 245, 255 )
5353

5454

55+
# I'm concerned that we are doing a lot of string comparisons on every function call to detect types.
56+
# Quickest way would probably be isinstance(result, ffi._backend._CDataBase) but that class name varies
57+
# depending on if binding is static/dynamic
58+
# (and possibly also different on pypy implementations?).
59+
# which makes me reluctant to rely on it.
60+
# Another possibility is ffi.typeof() but that will throw an exception if you give it a type that isn't a ctype
61+
# Another way to improve performance might be to special-case simple types before doing the string comparisons
62+
5563
def makefunc(a):
5664
#print("makefunc ",a, ffi.typeof(a).args)
5765
def func(*args):
@@ -65,7 +73,12 @@ def func(*args):
6573
modified_args.append(ffi.addressof(arg))
6674
else:
6775
modified_args.append(arg)
68-
return a(*modified_args)
76+
result = a(*modified_args)
77+
if result is None:
78+
return
79+
if str(type(result)) == "<class '_cffi_backend._CDataBase'>" and str(result).startswith("<cdata 'char *'"):
80+
result = ffi.string(result).decode('utf-8')
81+
return result
6982
return func
7083

7184
def makeStructHelper(struct):

version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "4.0.0"
1+
__version__ = "4.0.0.post1"

0 commit comments

Comments
 (0)