Skip to content

Commit 0ea3f1b

Browse files
pyray return more useful errors when types are wrong
1 parent 1799c58 commit 0ea3f1b

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

pyray/__init__.py

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,37 +38,28 @@ def _underscore(word: str) -> str:
3838
return word.lower()
3939

4040

41-
def pointer(struct):
42-
return ffi.addressof(struct)
43-
44-
45-
# I'm concerned that we are doing a lot of string comparisons on every function call to detect types.
46-
# Quickest way would probably be isinstance(result, ffi._backend._CDataBase) but that class name varies
47-
# depending on if binding is static/dynamic
48-
# (and possibly also different on pypy implementations?).
49-
# which makes me reluctant to rely on it.
50-
# Another possibility is ffi.typeof() but that will throw an exception if you give it a type that isn't a ctype
51-
# Another way to improve performance might be to special-case simple types before doing the string comparisons
52-
5341
def _wrap_function(original_func):
5442
c_args = [str(x) for x in ffi.typeof(original_func).args]
55-
number_of_args=len(c_args)
43+
number_of_args = len(c_args)
5644
c_arg_is_pointer = [x.kind == 'pointer' for x in ffi.typeof(original_func).args]
57-
c_arg_is_string = [str(x) == "<ctype 'char *'>" for x in ffi.typeof(original_func).args]
58-
# print("makefunc ",a, ffi.typeof(a).args)
45+
c_arg_is_string = [str(x) == "<ctype 'char *'>" for x in ffi.typeof(original_func).args]
46+
# c_arg_is_void_pointer = [str(x) == "<ctype 'void *'>" for x in ffi.typeof(original_func).args]
47+
5948
def wrapped_func(*args):
60-
args=list(args) # tuple is immutable, converting it to mutable list is faster than constructing new list!
61-
for i in range(0, number_of_args):
49+
args = list(args) # tuple is immutable, converting it to mutable list is faster than constructing new list!
50+
for i in range(number_of_args):
6251
try:
63-
arg=args[i]
52+
arg = args[i]
6453
except IndexError:
6554
raise RuntimeError(f"function requires {number_of_args} arguments but you supplied {len(args)}")
6655
if c_arg_is_pointer[i]:
67-
if c_arg_is_string[i]: # we assume c_arg is 'const char *'
68-
try: # if it's a 'char *' then user should be supplying a ctype pointer, not a Python string
69-
args[i] = arg.encode('utf-8') # in that case this conversion will fail
70-
except AttributeError: # but those functions are uncommon, so quicker on average to try the conversion
71-
pass # and ignore the exception
56+
if c_arg_is_string[i]: # we assume c_arg is 'const char *'
57+
try: # if it's a non-const 'char *' then user should be supplying a ctype pointer, not a Python
58+
# string
59+
args[i] = arg.encode('utf-8') # in that case this conversion will fail
60+
except AttributeError: # but those functions are uncommon, so quicker on average to try the
61+
# conversion
62+
pass # and ignore the exception
7263
# if user supplied a Python string but c_arg is a 'char *' not a 'const char *' then we ought to raise
7364
# exception because its an out
7465
# parameter and user should supply a ctype pointer, but we cant because cffi cant detect 'const'
@@ -82,13 +73,24 @@ def wrapped_func(*args):
8273
elif not is_cdata(arg):
8374
if c_args[i] == "<ctype '_Bool *'>":
8475
raise TypeError(
85-
"Argument must be a ctype bool, please create one with: pyray.ffi.new('bool *', True)")
76+
f"Argument {i} ({arg}) must be a ctype bool, please create one with: pyray.ffi.new('bool "
77+
f"*', True)")
8678
elif c_args[i] == "<ctype 'int *'>":
8779
raise TypeError(
88-
"Argument must be a ctype int, please create one with: pyray.ffi.new('int *', 1)")
80+
f"Argument {i} ({arg}) must be a ctype int, please create one with: pyray.ffi.new('int "
81+
f"*', 1)")
8982
elif c_args[i] == "<ctype 'float *'>":
9083
raise TypeError(
91-
"Argument must be a ctype float, please create one with: pyray.ffi.new('float *', 1.0)")
84+
f"Argument {i} ({arg}) must be a ctype float, please create one with: pyray.ffi.new("
85+
f"'float *', 1.0)")
86+
elif c_args[i] == "<ctype 'void *'>":
87+
# we could assume it's a string and try to convert it but we would have to be sure it's
88+
# const. that seems reasonable assumption for char* but i'm not confident it is for void*
89+
raise TypeError(
90+
f"Argument {i} ({arg}) must be a cdata pointer. Type is void so I don't know what type it "
91+
f"should be."
92+
"If it's a const string you can create it with pyray.ffi.new('char []', b\"whatever\") . "
93+
"If it's a float you can create it with pyray.ffi.new('float *', 1.0)")
9294

9395
result = original_func(*args)
9496
if result is None:
@@ -152,3 +154,7 @@ def func(*args):
152154

153155
# overwrite ffi enums with our own
154156
from raylib.enums import *
157+
158+
159+
def text_format(*args):
160+
raise RuntimeError("Use Python f-strings etc rather than calling text_format().")

0 commit comments

Comments
 (0)