Skip to content

Commit

Permalink
Add gd_string_name_from_unchecked_pystr and use it instead of doing s…
Browse files Browse the repository at this point in the history
…tring-to-string-name conversion
  • Loading branch information
touilleMan committed Jun 17, 2024
1 parent e3f95cd commit b21cacb
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
8 changes: 1 addition & 7 deletions src/godot/builtins_pyx/constructor.pyx.j2
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,7 @@ def __cinit__(self, s=None):
if s is None:
self._gd_data = {{ builtin.c_name_prefix }}_new()
elif isinstance(s, str):
gds = gd_string_from_unchecked_pystr(s)
try:
{% set constructor = builtin.get_constructor_from("String") %}
self._gd_data = {{ constructor.c_name }}(&gds)
return
finally:
gd_string_del(&gds)
self._gd_data = gd_string_name_from_unchecked_pystr(s)
else:
try:
{% set constructor = builtin.get_constructor_from("String") %}
Expand Down
4 changes: 1 addition & 3 deletions src/godot/classes.pyx.j2
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,7 @@ cdef object _object_call(GDExtensionObjectPtr obj, str meth, args):
variant_args_ptrs[i] = &variant_args[i]

# TODO: provide a helper for string name from Python str creation
cdef gd_string_t meth_gdstr = gd_string_from_unchecked_pystr(meth)
cdef gd_string_name_t meth_gdstrname = gd_string_name_new_from_string(&meth_gdstr)
gd_string_del(&meth_gdstr)
cdef gd_string_name_t meth_gdstrname = gd_string_name_from_unchecked_pystr(meth)
variant_args[0] = gd_string_name_into_variant(&meth_gdstrname)
# Into conversion steals the owneship, so no need to delete meth_gdstrname

Expand Down
6 changes: 6 additions & 0 deletions src/godot/hazmat/gdapi.pxd.j2
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ cdef inline gd_string_t gd_string_from_pybytes(bytes cstr):
return gdstr


cdef inline gd_string_name_t gd_string_name_from_unchecked_pystr(object pystr):
cdef ssize_t buffsize
cdef const char *buff = PyUnicode_AsUTF8AndSize(pystr, &buffsize)
return gd_string_name_from_utf8_and_len(buff, buffsize)


cdef inline gd_string_name_t gd_string_name_from_pybytes(bytes cstr):
cdef gd_string_t gdstr = gd_string_from_pybytes(cstr)
cdef gd_string_name_t gdstrname = gd_string_name_new_from_string(&gdstr)
Expand Down

0 comments on commit b21cacb

Please sign in to comment.