diff --git a/P4API.cpp b/P4API.cpp index 657fd09..a75462c 100644 --- a/P4API.cpp +++ b/P4API.cpp @@ -223,35 +223,66 @@ static PyObject * P4Adapter_set_var(P4Adapter * self, PyObject *args) static PyObject * P4Adapter_run(P4Adapter * self, PyObject * args) { - PyObject * cmd = PyTuple_GetItem(args, 0); - if (cmd == NULL) { - return NULL; - } - + // assume the args are flattened already vector argv; - for (Py_ssize_t i = 1; i < PyTuple_Size(args); ++i) { - PyObject * item = PyTuple_GET_ITEM(args, i); + vector converted; + for (Py_ssize_t i = 0; i < PyTuple_Size(args); ++i) { + PyObject * item = PyTuple_GET_ITEM(args, i); + bool did_convert = false; #if PY_MAJOR_VERSION >= 3 - // check encoding here? if( ! PyUnicode_Check(item) && ! PyBytes_Check(item) ) { // preserve plain Unicode and Byte strings item = PyObject_Str(item); + did_convert = true; } #else if( ! PyBytes_Check(item) ) { item = PyObject_Str(item); + did_convert = true; } #endif - argv.push_back(GetPythonString(item)); + if (item == NULL) { + for (PyObject * obj : converted) { + Py_DECREF(obj); + } + return NULL; + } + if (did_convert) { + converted.push_back(item); + } + const char * cstr = GetPythonString(item); + if (!cstr) { + for (PyObject * obj : converted) { + Py_DECREF(obj); + } + return NULL; + } + argv.push_back(cstr); } + + if (argv.empty()) { + PyErr_SetString(PyExc_RuntimeError, "No command provided"); + return NULL; + } + const char * cmd = argv[0]; // this is a bit of a hack: it assumes the storage layout of the vector is continuous // the other hack is that the API expects (char * const *), but this cannot be stored // a std::vector<>, because it cannot exchange pointers - - return self->clientAPI->Run(GetPythonString(cmd), (int)argv.size(), - (argv.size() > 0) ? (char * const *) &argv[0] : NULL ); + + size_t nargs = argv.size()-1; + PyObject *result = NULL; + if (nargs > INT_MAX) { + PyErr_SetString(PyExc_OverflowError, "too many arguments"); + } else { + result = self->clientAPI->Run(cmd, (int)nargs, + (argv.size() > 1) ? (char * const *) &argv[1] : NULL ); + } + for (PyObject * obj : converted) { + Py_DECREF(obj); + } + return result; } static PyObject * P4API_identify(PyObject * self) @@ -1525,4 +1556,3 @@ initP4API(void) return module; #endif } - diff --git a/SpecMgr.cpp b/SpecMgr.cpp index 19dcf7f..bd07514 100644 --- a/SpecMgr.cpp +++ b/SpecMgr.cpp @@ -769,7 +769,7 @@ const char * GetPythonString(PyObject *obj) { return PyUnicode_AsUTF8(obj); } else - return PyBytes_AS_STRING(obj); + return PyBytes_AsString(obj); } #endif diff --git a/p4test.py b/p4test.py index 16acaf1..da27766 100644 --- a/p4test.py +++ b/p4test.py @@ -2787,6 +2787,43 @@ def testEncodingModes(self): changes = self.p4.run('changes', b'-m1') self.assertIsInstance(changes, list, "bytes argument under utf-8 encoding was not handled") + info_bytes = self.p4.run(b'info') + self.assertIsInstance(info_bytes, list, + "bytes command under utf-8 encoding was not handled") + + def testRunArgumentConversionErrors(self): + """Check that run() propagates conversion errors and releases converted args.""" + class Stringifiable: + def __str__(self): + return shared_string + + class RaisesOnString: + def __str__(self): + raise ValueError("argument conversion failed") + + shared_string = "converted argument" + original_refcount = sys.getrefcount(shared_string) + + with self.assertRaises(ValueError) as context: + self.p4.run('info', Stringifiable(), RaisesOnString()) + self.assertEqual(str(context.exception), "argument conversion failed") + self.assertEqual(sys.getrefcount(shared_string), original_refcount, + "Converted argument was not released after a later conversion failed") + + with self.assertRaises(UnicodeEncodeError): + self.p4.run('info', Stringifiable(), '\ud800') + self.assertEqual(sys.getrefcount(shared_string), original_refcount, + "Converted argument was not released after UTF-8 conversion failed") + + # The Python wrapper must also surface errors while encoding str arguments. + self.p4.encoding = 'ascii' + with self.assertRaises(UnicodeEncodeError): + self.p4.run('info', '\u2603') + + # An empty argument list must fail cleanly before attempting to execute a command. + with self.assertRaises(RuntimeError) as context: + self.p4.run() + self.assertEqual(str(context.exception), "No command provided") def testLogMessages(self): """Cover the run() logger integration: info logging on success and the