Browse Source

interrogate: Fix some pointless code generation case

Some cases of eg. inplace ternary operator would needlessly construct and then immediately destruct a tuple
rdb 1 year ago
parent
commit
7926a3a929
1 changed files with 16 additions and 8 deletions
  1. 16 8
      dtool/src/interrogate/interfaceMakerPythonNative.cxx

+ 16 - 8
dtool/src/interrogate/interfaceMakerPythonNative.cxx

@@ -2192,12 +2192,16 @@ write_module_class(ostream &out, Object *obj) {
           string expected_params;
           bool always_returns = true;
           out << "  if (arg2 != nullptr) { // __setitem__\n";
-          out << "    PyObject *args = PyTuple_Pack(2, arg, arg2);\n";
-          if (!write_function_forset(out, setitem_remaps, 2, 2, expected_params, 4,
-                                     true, true, AT_varargs, RF_int | RF_decref_args, false)) {
+          if (setitem_remaps.empty()) {
             always_returns = false;
+          } else {
+            out << "    PyObject *args = PyTuple_Pack(2, arg, arg2);\n";
+            if (!write_function_forset(out, setitem_remaps, 2, 2, expected_params, 4,
+                                       true, true, AT_varargs, RF_int | RF_decref_args, false)) {
+              always_returns = false;
+            }
+            out << "    Py_DECREF(args);\n";
           }
-          out << "    Py_DECREF(args);\n";
           out << "  } else { // __delitem__\n";
           if (!write_function_forset(out, delitem_remaps, 1, 1, expected_params, 4,
                                      true, true, AT_single_arg, RF_int, false)) {
@@ -2434,12 +2438,16 @@ write_module_class(ostream &out, Object *obj) {
           string expected_params;
           bool always_returns = true;
           out << "  if (arg2 != nullptr && arg2 != Py_None) {\n";
-          out << "    PyObject *args = PyTuple_Pack(2, arg, arg2);\n";
-          if (!write_function_forset(out, two_param_remaps, 2, 2, expected_params, 4,
-                                     true, true, AT_varargs, return_flags | RF_decref_args, true)) {
+          if (two_param_remaps.empty()) {
             always_returns = false;
+          } else {
+            out << "    PyObject *args = PyTuple_Pack(2, arg, arg2);\n";
+            if (!write_function_forset(out, two_param_remaps, 2, 2, expected_params, 4,
+                                       true, true, AT_varargs, return_flags | RF_decref_args, true)) {
+              always_returns = false;
+            }
+            out << "    Py_DECREF(args);\n";
           }
-          out << "    Py_DECREF(args);\n";
           out << "  } else {\n";
           if (!write_function_forset(out, one_param_remaps, 1, 1, expected_params, 4,
                                      true, true, AT_single_arg, return_flags, true)) {