Skip to content

Commit 3744686

Browse files
throw helpful errors when params supplied are not ctype pointers (#122)
1 parent 33dce4e commit 3744686

File tree

3 files changed

+77
-19
lines changed

3 files changed

+77
-19
lines changed

create_stub_pyray.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ def ctype_to_python_type(t):
4141
return "int"
4242
elif t == "double":
4343
return "float"
44+
elif "char * *" in t:
45+
return "list[str]"
4446
elif "char *" in t:
4547
return "str"
4648
elif "char" in t:

pyray/__init__.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,43 @@ def func(*args):
4747
for (c_arg, arg) in zip(ffi.typeof(a).args, args):
4848
# print("arg:",str(arg), "c_arg.kind:", c_arg.kind, "c_arg:", c_arg, "type(arg):",str(type(arg)))
4949
if c_arg.kind == 'pointer':
50-
if type(arg) == str:
50+
if type(arg) is str:
5151
arg = arg.encode('utf-8')
52-
elif type(arg) is bool:
53-
arg = ffi.new("bool *", arg)
54-
elif type(arg) is int:
55-
arg = ffi.new("int *", arg)
56-
elif type(arg) is float:
57-
arg = ffi.new("float *", arg)
52+
# if c_arg is a 'char *' not a 'const char *' then we ought to raise here because its an out
53+
# parameter and user should supply a ctype pointer, but cffi cant detect const
54+
# so we would have to get the info from raylib.json
5855
elif type(arg) is list and str(c_arg) == "<ctype 'char * *'>":
5956
arg = [ffi.new("char[]", x.encode('utf-8')) for x in arg]
60-
elif str(type(arg)) == "<class '_cffi_backend.__CDataOwn'>" and "*" not in str(arg): # CPython
61-
arg = ffi.addressof(arg)
62-
elif str(type(arg)) == "<class '_cffi_backend._CDataBase'>" and "*" not in str(arg): # Pypy
57+
elif is_cdata(arg) and "*" not in str(arg):
6358
arg = ffi.addressof(arg)
6459
elif arg is None:
6560
arg = ffi.NULL
61+
elif not is_cdata(arg):
62+
if str(c_arg) == "<ctype '_Bool *'>":
63+
raise TypeError(
64+
"Argument must be a ctype bool, please create one with: pyray.ffi.new('bool *', True)")
65+
elif str(c_arg) == "<ctype 'int *'>":
66+
raise TypeError(
67+
"Argument must be a ctype int, please create one with: pyray.ffi.new('int *', 1)")
68+
elif str(c_arg) == "<ctype 'float *'>":
69+
raise TypeError(
70+
"Argument must be a ctype float, please create one with: pyray.ffi.new('float *', 1.0)")
6671
modified_args.append(arg)
6772
result = a(*modified_args)
6873
if result is None:
6974
return
70-
if str(type(result)) == "<class '_cffi_backend._CDataBase'>" and str(result).startswith("<cdata 'char *'"):
75+
elif is_cdata(result) and str(result).startswith("<cdata 'char *'"):
7176
if str(result) == "<cdata 'char *' NULL>":
72-
result = ""
77+
return ""
7378
else:
74-
result = ffi.string(result).decode('utf-8')
75-
return result
79+
return ffi.string(result).decode('utf-8')
80+
else:
81+
return result
82+
83+
# apparently pypy and cpython produce different types so check for both
84+
def is_cdata(arg):
85+
return str(type(arg)) == "<class '_cffi_backend.__CDataOwn'>" or str(
86+
type(arg)) == "<class '_cffi_backend._CDataBase'>"
7687

7788
return func
7889

@@ -99,20 +110,16 @@ def func(*args):
99110

100111

101112
for name, attr in getmembers(rl):
102-
# print(name, attr)
113+
#print(name, dir(attr))
103114
uname = inflection.underscore(name).replace('3_d', '_3d').replace('2_d', '_2d')
104115
if isbuiltin(attr) or str(type(attr)) == "<class '_cffi_backend.__FFIFunctionWrapper'>" or str(
105116
type(attr)) == "<class '_cffi_backend._CDataBase'>":
106117
# print(attr.__call__)
107118
# print(attr.__doc__)
108-
# print(attr.__text_signature__)
109119
# print(dir(attr))
110120
# print(dir(attr.__repr__))
111121
f = makefunc(attr)
112122
setattr(current_module, uname, f)
113-
# def wrap(*args):
114-
# print("call to ",attr)
115-
# setattr(PyRay, uname, lambda *args: print("call to ",attr))
116123
else:
117124
setattr(current_module, name, attr)
118125

tests/xtest_raygui2.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import pyray
2+
from pyray import *
3+
import pytest
4+
5+
SCREEN_WIDTH = 640
6+
SCREEN_HEIGHT = 480
7+
8+
9+
def main():
10+
init_window(SCREEN_WIDTH, SCREEN_HEIGHT, "raygui")
11+
set_exit_key(KeyboardKey.KEY_ESCAPE)
12+
set_target_fps(30)
13+
14+
_bool_pointer = pyray.ffi.new("bool *", True)
15+
_bool = True
16+
_float = 5.5
17+
_float_pointer = pyray.ffi.new("float *", 5.2)
18+
color = RED
19+
color_pointer = Color()
20+
_int=0
21+
_int_pointer = pyray.ffi.new("int *", 0)
22+
_string ="cant edit this string"
23+
_string_pointer = pyray.ffi.new("char []", b"0123456789")
24+
25+
while not window_should_close():
26+
begin_drawing()
27+
clear_background(WHITE)
28+
29+
gui_check_box((40, 40, 30, 30), "checkbox", _bool_pointer)
30+
with pytest.raises(TypeError):
31+
gui_check_box(Rectangle(40, 40, 100, 100), "checkbox", _bool)
32+
33+
gui_color_bar_alpha((40, 80, 100, 20), "bar", _float_pointer)
34+
with pytest.raises(TypeError):
35+
gui_color_bar_alpha((40, 80, 100, 20), "bar", _float)
36+
37+
gui_color_picker((40,120, 100, 100), "color", color_pointer)
38+
with pytest.raises(TypeError):
39+
gui_color_picker((40,120, 100, 100), "color", color)
40+
41+
gui_text_box((40,230, 100, 30), _string_pointer, 10, True)
42+
gui_text_box((200,230, 100, 30), _string, 10, True)
43+
44+
gui_tab_bar((40,300, 100, 30), ["foo", "boo"], 2, _int_pointer)
45+
with pytest.raises(TypeError):
46+
gui_tab_bar((40,300, 100, 30), ["foo", "boo"], 2, _int)
47+
48+
end_drawing()
49+
main()

0 commit comments

Comments
 (0)