Skip to content

Commit 3740848

Browse files
committed
Use C-level for builtins & classes instances in src/_pythonscript.pyx
1 parent 9f744dc commit 3740848

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

src/_pythonscript.pyx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ from godot.hazmat.gdapi cimport *
1212
from godot.hazmat.extension_class cimport *
1313
from godot.hazmat cimport gdptrs
1414
from godot.builtins cimport *
15-
from godot.classes cimport _load_class, _load_singleton, _cleanup_loaded_classes_and_singletons
15+
from godot.classes cimport _load_class, _load_singleton, _cleanup_loaded_classes_and_singletons, BaseGDObject
1616

1717
include "_pythonscript_editor.pxi"
1818
include "_pythonscript_extension_class_language.pxi"
@@ -147,12 +147,12 @@ cdef void _unregister_pythonscript_classes():
147147

148148
cdef void _customize_config():
149149
import sys
150-
ProjectSettings = _load_singleton("ProjectSettings")
151-
OS = _load_singleton("OS")
150+
cdef BaseGDObject ProjectSettings = <BaseGDObject>_load_singleton("ProjectSettings")
151+
cdef BaseGDObject OS = <BaseGDObject>_load_singleton("OS")
152152

153153
# Provide argv arguments
154154

155-
args = OS.get_cmdline_args()
155+
cdef PackedStringArray args = <PackedStringArray?>OS.get_cmdline_args()
156156
sys.argv = ["godot"]
157157
# TODO: iteration on `PackedStringArray` not supported yet !
158158
for i in range(args.size()):
@@ -178,12 +178,13 @@ cdef void _customize_config():
178178
cdef object _initialize_callback = None
179179
cdef object _initialize_callback_hook(int p_level):
180180
global _initialize_callback
181+
cdef GDString config
181182

182183
if _initialize_callback is None:
183-
config = _setup_config_entry("python/initialize_callback", "")
184-
185-
if not isinstance(config, GDString):
186-
raise ValueError("Invalid value for config `python/initialize_callback`: expected a string in format `<module>:<function>`")
184+
try:
185+
config = <GDString?>_setup_config_entry("python/initialize_callback", "")
186+
except TypeError as exc:
187+
raise ValueError("Invalid value for config `python/initialize_callback`: expected a string in format `<module>:<function>`") from exc
187188

188189
if config.is_empty():
189190
_initialize_callback = lambda _level: None # Dummy callback
@@ -213,12 +214,13 @@ cdef object _initialize_callback_hook(int p_level):
213214
cdef object _deinitialize_callback = None
214215
cdef object _deinitialize_callback_hook(int p_level):
215216
global _deinitialize_callback
217+
cdef GDString config
216218

217219
if _deinitialize_callback is None:
218-
config = _setup_config_entry("python/deinitialize_callback", "")
219-
220-
if not isinstance(config, GDString):
221-
raise ValueError("Invalid value for config `python/deinitialize_callback`: expected a string in format `<module>:<function>`")
220+
try:
221+
config = <GDString?>_setup_config_entry("python/deinitialize_callback", "")
222+
except TypeError as exc:
223+
raise ValueError("Invalid value for config `python/deinitialize_callback`: expected a string in format `<module>:<function>`") from exc
222224

223225
if config.is_empty():
224226
_deinitialize_callback = lambda _level: None # Dummy callback

src/godot/classes.pxd.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,6 @@ cdef class {{ cls.cy_type }}({{ cls.inherits.cy_type if cls.inherits else "" }})
105105

106106

107107
cdef object _load_class(str name)
108-
cpdef object _load_singleton(str name)
108+
cpdef BaseGDObject _load_singleton(str name)
109109
cdef void _cleanup_loaded_classes_and_singletons()
110110
cdef object _object_call(gd_object_t obj, str meth, list args)

src/godot/classes.pyx.j2

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ cdef void _cleanup_loaded_classes_and_singletons():
108108
_loaded_classes.clear()
109109

110110

111-
cpdef object _load_singleton(str name):
111+
cpdef BaseGDObject _load_singleton(str name):
112112
try:
113-
return _loaded_singletons[name]
113+
return <BaseGDObject>_loaded_singletons[name]
114114
except KeyError:
115115
pass
116116

@@ -123,7 +123,7 @@ cpdef object _load_singleton(str name):
123123
if gdobj == NULL:
124124
raise RuntimeError(f"Singleton `{name}` doesn't exist in Godot !")
125125

126-
cdef object singleton = cls._from_ptr(<size_t>gdobj)
126+
cdef BaseGDObject singleton = <BaseGDObject>cls._from_ptr(<size_t>gdobj)
127127

128128
_loaded_singletons[name] = singleton
129129
return singleton
@@ -134,16 +134,16 @@ cpdef object _load_singleton(str name):
134134
{% endfor %} #}
135135

136136

137-
cdef inline object _property_getter(object obj, object name):
138-
return _object_call((<BaseGDObject>obj)._gd_ptr, "get", [name])
137+
cdef inline object _property_getter(BaseGDObject obj, object name):
138+
return _object_call(obj._gd_ptr, "get", [name])
139139

140140

141-
cdef inline void _property_setter(object obj, object name, object value):
142-
_object_call((<BaseGDObject>obj)._gd_ptr, "set", [name, value])
141+
cdef inline void _property_setter(BaseGDObject obj, object name, object value):
142+
_object_call(obj._gd_ptr, "set", [name, value])
143143

144144

145-
cdef inline object _meth_call(object obj, object name, object args):
146-
return _object_call((<BaseGDObject>obj)._gd_ptr, "call", [name, *args])
145+
cdef inline object _meth_call(BaseGDObject obj, object name, object args):
146+
return _object_call(obj._gd_ptr, "call", [name, *args])
147147

148148

149149
cdef object _load_class(str name):

0 commit comments

Comments
 (0)