diff options
author | Hugo Lima <hugo.lima@openbossa.org> | 2010-01-28 17:19:33 -0200 |
---|---|---|
committer | Hugo Lima <hugo.lima@openbossa.org> | 2010-01-28 17:36:50 -0200 |
commit | 22eb430cecf6ddc10eed04dd67c81485aba84ab6 (patch) | |
tree | 4ebaa0d9d6c99cfe0419cf53859fde63713b852b /libshiboken | |
parent | 02cdcb7e0e49b22cd10a5146d5a85cec9bfca7bd (diff) |
Changed API for PySequence_to_argc_argv.
It was renamed to PySequenceToArgcArgv and a new argument was added, besides some documentation.
If the sequence is empty and defaultAppName (the new argument) was provided, argc will be 1 and
argv will have a copy of defaultAppName because some libraries, like Qt, need at least one element
in argv (the application name), otherwise it'll crash somewhere inside Qt.
Reviewed by Luciano Wolf <luciano.wolf@openbossa.org>
Diffstat (limited to 'libshiboken')
-rw-r--r-- | libshiboken/helper.cpp | 14 | ||||
-rw-r--r-- | libshiboken/helper.h | 10 |
2 files changed, 20 insertions, 4 deletions
diff --git a/libshiboken/helper.cpp b/libshiboken/helper.cpp index e1f23867d..4523a2059 100644 --- a/libshiboken/helper.cpp +++ b/libshiboken/helper.cpp @@ -38,19 +38,22 @@ namespace Shiboken { bool -PySequence_to_argc_argv(PyObject* argList, int* argc, char*** argv) +PySequenceToArgcArgv(PyObject* argList, int* argc, char*** argv, const char* defaultAppName) { if (!PySequence_Check(argList)) return false; + // Check all items int numArgs = PySequence_Size(argList); for (int i = 0; i < numArgs; ++i) if (!PyString_Check(PySequence_GetItem(argList, i))) return false; - *argc = (int) PySequence_Size(argList); + bool addAppName = !numArgs && defaultAppName; + *argc = addAppName ? 1 : numArgs; + *argv = new char*[*argc]; - for (int i = 0; i < *argc; ++i) { + for (int i = 0; i < numArgs; ++i) { PyObject* item = PySequence_GetItem(argList, i); char* string = PyString_AS_STRING(item); int size = strlen(string); @@ -58,6 +61,11 @@ PySequence_to_argc_argv(PyObject* argList, int* argc, char*** argv) (*argv)[i] = strcpy((*argv)[i], string); Py_DECREF(item); } + + if (addAppName) { + (*argv)[0] = new char[strlen(defaultAppName)+1]; + (*argv)[0] = strcpy((*argv)[0], defaultAppName); + } return true; } diff --git a/libshiboken/helper.h b/libshiboken/helper.h index 8c3e3a64d..2e625b3e9 100644 --- a/libshiboken/helper.h +++ b/libshiboken/helper.h @@ -61,7 +61,15 @@ inline PyObject* makeTuple(const A& a, const B& b, const C& c) return tuple; } -LIBSHIBOKEN_API bool PySequence_to_argc_argv(PyObject* argList, int* argc, char*** argv); +/** +* It transforms a python sequence into two C variables, argc and argv. +* If the sequence is empty and defaultAppName was provided, argc will be 1 and +* argv will have a copy of defaultAppName. +* +* \note argc and argv *should* be deleted by the user. +* \returns True on sucess, false otherwise. +*/ +LIBSHIBOKEN_API bool PySequenceToArgcArgv(PyObject* argList, int* argc, char*** argv, const char* defaultAppName = 0); } // namespace Shiboken |