raylib 5.5.0.0.dev3__pp39-pypy39_pp73-macosx_10_13_x86_64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of raylib might be problematic. Click here for more details.

pyray/__init__.py ADDED
@@ -0,0 +1,160 @@
1
+ # Copyright (c) 2021 Richard Smith and others
2
+ #
3
+ # This program and the accompanying materials are made available under the
4
+ # terms of the Eclipse Public License 2.0 which is available at
5
+ # http://www.eclipse.org/legal/epl-2.0.
6
+ #
7
+ # This Source Code may also be made available under the following Secondary
8
+ # licenses when the conditions for such availability set forth in the Eclipse
9
+ # Public License, v. 2.0 are satisfied: GNU General Public License, version 2
10
+ # with the GNU Classpath Exception which is
11
+ # available at https://www.gnu.org/software/classpath/license.html.
12
+ #
13
+ # SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
14
+ import re
15
+ import weakref
16
+ from array import array
17
+
18
+ from raylib import rl, ffi
19
+ from raylib.colors import *
20
+
21
+ try:
22
+ from raylib.defines import *
23
+ except AttributeError:
24
+ print("sorry deprecated enums dont work on dynamic version")
25
+
26
+ from inspect import getmembers, isbuiltin
27
+
28
+ current_module = __import__(__name__)
29
+
30
+
31
+ def _underscore(word: str) -> str:
32
+ """
33
+ from inflection
34
+ """
35
+ word = re.sub(r"([A-Z]+)([A-Z][a-z])", r'\1_\2', word)
36
+ word = re.sub(r"([a-z\d])([A-Z])", r'\1_\2', word)
37
+ word = word.replace("-", "_")
38
+ return word.lower()
39
+
40
+
41
+ def _wrap_function(original_func):
42
+ c_args = [str(x) for x in ffi.typeof(original_func).args]
43
+ number_of_args = len(c_args)
44
+ c_arg_is_pointer = [x.kind == 'pointer' for x in ffi.typeof(original_func).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
+
48
+ def wrapped_func(*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):
51
+ try:
52
+ arg = args[i]
53
+ except IndexError:
54
+ raise RuntimeError(f"function requires {number_of_args} arguments but you supplied {len(args)}")
55
+ if c_arg_is_pointer[i]:
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
63
+ # if user supplied a Python string but c_arg is a 'char *' not a 'const char *' then we ought to raise
64
+ # exception because its an out
65
+ # parameter and user should supply a ctype pointer, but we cant because cffi cant detect 'const'
66
+ # so we would have to get the info from raylib.json
67
+ elif c_args[i] == "<ctype 'char * *'>" and type(arg) is list:
68
+ args[i] = [ffi.new("char[]", x.encode('utf-8')) for x in arg]
69
+ elif is_cdata(arg) and "*" not in str(arg):
70
+ args[i] = ffi.addressof(arg)
71
+ elif arg is None:
72
+ args[i] = ffi.NULL
73
+ elif not is_cdata(arg):
74
+ if c_args[i] == "<ctype '_Bool *'>":
75
+ raise TypeError(
76
+ f"Argument {i} ({arg}) must be a ctype bool, please create one with: pyray.ffi.new('bool "
77
+ f"*', True)")
78
+ elif c_args[i] == "<ctype 'int *'>":
79
+ raise TypeError(
80
+ f"Argument {i} ({arg}) must be a ctype int, please create one with: pyray.ffi.new('int "
81
+ f"*', 1)")
82
+ elif c_args[i] == "<ctype 'float *'>":
83
+ raise TypeError(
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)")
94
+
95
+ result = original_func(*args)
96
+ if result is None:
97
+ return
98
+ elif is_cdata(result) and str(result).startswith("<cdata 'char *'"):
99
+ if str(result) == "<cdata 'char *' NULL>":
100
+ return ""
101
+ else:
102
+ return ffi.string(result).decode('utf-8')
103
+ else:
104
+ return result
105
+
106
+ # apparently pypy and cpython produce different types so check for both
107
+ def is_cdata(arg):
108
+ return str(type(arg)) == "<class '_cffi_backend.__CDataOwn'>" or str(
109
+ type(arg)) == "<class '_cffi_backend._CDataBase'>"
110
+
111
+ return wrapped_func
112
+
113
+
114
+ global_weakkeydict = weakref.WeakKeyDictionary()
115
+
116
+
117
+ def _make_struct_constructor_function(struct):
118
+ def func(*args):
119
+ # print(struct, args)
120
+ modified_args = []
121
+ for (field, arg) in zip(ffi.typeof(struct).fields, args):
122
+ # print("arg:", str(arg), "field:", field[1], "field type:", field[1].type, "type(arg):", str(type(arg)))
123
+ if arg is None:
124
+ arg = ffi.NULL
125
+ elif (field[1].type.kind == 'pointer'
126
+ and (str(type(arg)) == "<class 'numpy.ndarray'>"
127
+ or isinstance(arg, (array, bytes, bytearray, memoryview)))):
128
+ arg = ffi.from_buffer(field[1].type, arg)
129
+ modified_args.append(arg)
130
+ s = ffi.new(f"struct {struct} *", modified_args)[0]
131
+ global_weakkeydict[s] = modified_args
132
+ return s
133
+
134
+ return func
135
+
136
+
137
+ for name, attr in getmembers(rl):
138
+ # print(name, attr)
139
+ uname = _underscore(name).replace('3_d', '_3d').replace('2_d', '_2d')
140
+ if isbuiltin(attr) or str(type(attr)) == "<class '_cffi_backend.__FFIFunctionWrapper'>" or str(
141
+ type(attr)) == "<class '_cffi_backend._CDataBase'>":
142
+ # print(attr.__call__)
143
+ # print(attr.__doc__)
144
+ # print(dir(attr))
145
+ # print(dir(attr.__repr__))
146
+ f = _wrap_function(attr)
147
+ setattr(current_module, uname, f)
148
+ else:
149
+ setattr(current_module, name, attr)
150
+
151
+ for struct in ffi.list_types()[0]:
152
+ f = _make_struct_constructor_function(struct)
153
+ setattr(current_module, struct, f)
154
+
155
+ # overwrite ffi enums with our own
156
+ from raylib.enums import *
157
+
158
+
159
+ def text_format(*args):
160
+ raise RuntimeError("Use Python f-strings etc rather than calling text_format().")