Add validation to keypressScene() for non-callable arguments

Added PyCallable_Check validation to ensure keypressScene() only
accepts callable objects. Now properly raises TypeError with a
clear error message when passed non-callable arguments like
strings, numbers, None, or dicts.
This commit is contained in:
John McCardle 2025-07-03 20:41:03 -04:00
commit 6134869371
2 changed files with 101 additions and 0 deletions

View file

@ -603,6 +603,13 @@ PyObject* McRFPy_API::_createScene(PyObject* self, PyObject* args) {
PyObject* McRFPy_API::_keypressScene(PyObject* self, PyObject* args) {
PyObject* callable;
if (!PyArg_ParseTuple(args, "O", &callable)) return NULL;
// Validate that the argument is callable
if (!PyCallable_Check(callable)) {
PyErr_SetString(PyExc_TypeError, "keypressScene() argument must be callable");
return NULL;
}
/*
if (game->currentScene()->key_callable != NULL and game->currentScene()->key_callable != Py_None)
{
@ -613,6 +620,7 @@ PyObject* McRFPy_API::_keypressScene(PyObject* self, PyObject* args) {
Py_INCREF(Py_None);
*/
game->currentScene()->key_callable = std::make_unique<PyKeyCallable>(callable);
Py_INCREF(Py_None);
return Py_None;
}