Skip to content

Commit 6b95bbe

Browse files
committed
Slight optimization in code size / string table size
Use tail call and string formatting for "can't delete attribute" exception
1 parent d2844d9 commit 6b95bbe

File tree

4 files changed

+23
-19
lines changed

4 files changed

+23
-19
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "scikit_build_core.build"
66

77
[project]
88
name = "panda3d-interrogate"
9-
version = "0.5.0"
9+
version = "0.5.1"
1010
description = "Binding generator designed for Panda3D"
1111
readme = "README.md"
1212
requires-python = ">=3.2"

src/interrogate/interfaceMakerPythonNative.cxx

+3-18
Original file line numberDiff line numberDiff line change
@@ -7355,12 +7355,7 @@ write_getset(ostream &out, Object *obj, Property *property) {
73557355
}
73567356
out << " return 0;\n";
73577357
} else {
7358-
out << "#ifdef NDEBUG\n"
7359-
" Dtool_Raise_TypeError(\"can't delete attribute\");\n"
7360-
"#else\n"
7361-
" Dtool_Raise_TypeError(\"can't delete " << ielem.get_name() << "[] attribute\");\n"
7362-
"#endif\n"
7363-
" return -1;\n";
7358+
out << " return Dtool_Raise_CantDeleteAttributeError(\"" << ielem.get_name() << "[]\");\n";
73647359
}
73657360
out << " }\n";
73667361

@@ -7525,12 +7520,7 @@ write_getset(ostream &out, Object *obj, Property *property) {
75257520
out << " return -1;\n";
75267521
}
75277522
} else {
7528-
out << "#ifdef NDEBUG\n"
7529-
" Dtool_Raise_TypeError(\"can't delete attribute\");\n"
7530-
"#else\n"
7531-
" Dtool_Raise_TypeError(\"can't delete " << ielem.get_name() << "[] attribute\");\n"
7532-
"#endif\n"
7533-
" return -1;\n";
7523+
out << " return Dtool_Raise_CantDeleteAttributeError(\"" << ielem.get_name() << "[]\");\n";
75347524
}
75357525
out << " }\n";
75367526

@@ -7763,12 +7753,7 @@ write_getset(ostream &out, Object *obj, Property *property) {
77637753
out << " " << cClassName << "::" << property->_deleter->_ifunc.get_name() << "();\n"
77647754
<< " return 0;\n";
77657755
} else {
7766-
out << "#ifdef NDEBUG\n"
7767-
" Dtool_Raise_TypeError(\"can't delete attribute\");\n"
7768-
"#else\n"
7769-
" Dtool_Raise_TypeError(\"can't delete " << ielem.get_name() << " attribute\");\n"
7770-
"#endif\n"
7771-
" return -1;\n";
7756+
out << " return Dtool_Raise_CantDeleteAttributeError(\"" << ielem.get_name() << "\");\n";
77727757
}
77737758
out << " }\n";
77747759

src/interrogatedb/py_panda.cxx

+18
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,24 @@ PyObject *Dtool_Raise_AttributeError(PyObject *obj, const char *attribute) {
209209
return nullptr;
210210
}
211211

212+
/**
213+
* Raises a TypeError of the form: can't delete attr attribute
214+
*
215+
* Always returns -1 so that it can be conveniently used as a return
216+
* expression for wrapper functions that return an int.
217+
*/
218+
int Dtool_Raise_CantDeleteAttributeError(const char *attribute) {
219+
#if PY_MAJOR_VERSION >= 3
220+
PyObject *message = PyUnicode_FromFormat(
221+
#else
222+
PyObject *message = PyString_FromFormat(
223+
#endif
224+
"can't delete %s attribute", attribute);
225+
226+
PyErr_SetObject(PyExc_TypeError, message);
227+
return -1;
228+
}
229+
212230
/**
213231
* Raises a TypeError of the form: Arguments must match: <list of overloads>
214232
*

src/interrogatedb/py_panda.h

+1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ EXPCL_PYPANDA PyObject *Dtool_Raise_AssertionError();
228228
EXPCL_PYPANDA PyObject *Dtool_Raise_TypeError(const char *message);
229229
EXPCL_PYPANDA PyObject *Dtool_Raise_ArgTypeError(PyObject *obj, int param, const char *function_name, const char *type_name);
230230
EXPCL_PYPANDA PyObject *Dtool_Raise_AttributeError(PyObject *obj, const char *attribute);
231+
EXPCL_PYPANDA int Dtool_Raise_CantDeleteAttributeError(const char *attribute);
231232

232233
EXPCL_PYPANDA PyObject *_Dtool_Raise_BadArgumentsError();
233234
EXPCL_PYPANDA PyObject *_Dtool_Raise_BadArgumentsError(const char *message);

0 commit comments

Comments
 (0)