Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 43 additions & 13 deletions P4API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char *> argv;
for (Py_ssize_t i = 1; i < PyTuple_Size(args); ++i) {
PyObject * item = PyTuple_GET_ITEM(args, i);
vector<PyObject *> 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)
Expand Down Expand Up @@ -1525,4 +1556,3 @@ initP4API(void)
return module;
#endif
}

2 changes: 1 addition & 1 deletion SpecMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ const char * GetPythonString(PyObject *obj) {
return PyUnicode_AsUTF8(obj);
}
else
return PyBytes_AS_STRING(obj);
return PyBytes_AsString(obj);
}

#endif
37 changes: 37 additions & 0 deletions p4test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down