Skip to content

Commit 49d1eb1

Browse files
committed
Improve PythonResourceFormatLoader._load
1 parent d255496 commit 49d1eb1

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/_pythonscript_extension_resource_format_loader.pxi

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,13 @@ cdef class PythonResourceFormatLoader:
115115
gd_string_del(&original_path)
116116
spy_log(f"CALLED PythonResourceFormatLoader::_load(path={py_path!r}, original_path={py_original_path!r}, use_sub_threads={use_sub_threads}, cache_mode={cache_mode})")
117117

118-
# Declare variables
119118
cdef PythonScript script
120119
cdef gd_string_t gd_source
121120
cdef gd_string_t gd_script_path
122121
cdef GDString source_code
123122

124-
# Create a new PythonScript instance
125-
script = PythonScript()
123+
# Load the source code from file
126124

127-
# Try to load the source code from file
128125
from godot.classes import FileAccess
129126
# TODO: use `path` directly !
130127
cdef object file = FileAccess.open(GDString(py_path), FileAccess.ModeFlags.READ.value)
@@ -135,15 +132,18 @@ cdef class PythonResourceFormatLoader:
135132
# TODO: what happen if the text is not UTF8 ?
136133
source_code = file.get_as_text()
137134

138-
# Set the source code on the script
139-
script._set_source_code(source_code._gd_data)
135+
# Create a new script instance from the source code
140136

141-
# Set the script path
142-
script._set_path(original_path)
137+
script = PythonScript()
138+
script._set_source_code(source_code.into_gd_data())
139+
# `into_gd_data()` steal the underlying Godot string, so `source_code`
140+
# ends up containing nothing and we'd rather destroy it early to avoid
141+
# confusions.
142+
del source_code
143143

144144
# Return the script as a variant
145-
ret = gd_object_into_variant(script._gd_ptr)
146145

146+
ret = gd_object_into_variant(script._gd_ptr)
147147
return ret
148148

149149
# Don't overload `_rename_dependencies()` to mimic GDScript

0 commit comments

Comments
 (0)