diff options
author | Hugo Parente Lima <hugo.pl@gmail.com> | 2010-10-29 15:25:07 -0200 |
---|---|---|
committer | Hugo Parente Lima <hugo.pl@gmail.com> | 2012-03-08 16:08:54 -0300 |
commit | 0e0331dd62afb53005a08f2f5e22a631204110e6 (patch) | |
tree | 8c6a0b55c8911692be8fc53721d28c1df8abac57 /libshiboken/helper.cpp | |
parent | 2cc7283b09b77b61bda1f7bc5fa75069975d50ff (diff) |
Fix function PySequenceToArgcArgv to support unicode strings.
Also fix some reference leaks.
Reviewer: Luciano Wolf <luciano.wolf@openbossa.org>
Lauro Moura <lauro.neto@openbossa.org>
Diffstat (limited to 'libshiboken/helper.cpp')
-rw-r--r-- | libshiboken/helper.cpp | 45 |
1 files changed, 29 insertions, 16 deletions
diff --git a/libshiboken/helper.cpp b/libshiboken/helper.cpp index f61fc1447..c37c54328 100644 --- a/libshiboken/helper.cpp +++ b/libshiboken/helper.cpp @@ -28,32 +28,45 @@ namespace Shiboken bool PySequenceToArgcArgv(PyObject* argList, int* argc, char*** argv, const char* defaultAppName) { + return sequenceToArgcArgv(argList, argc, argv, defaultAppName); +} + +bool +sequenceToArgcArgv(PyObject* argList, int* argc, char*** argv, const char* defaultAppName) +{ if (!PySequence_Check(argList)) return false; + if (!defaultAppName) + defaultAppName = "PySideApplication"; + // Check all items - int numArgs = PySequence_Size(argList); - for (int i = 0; i < numArgs; ++i) - if (!PyString_Check(PySequence_GetItem(argList, i))) + Shiboken::AutoDecRef args(PySequence_Fast(argList, 0)); + int numArgs = PySequence_Fast_GET_SIZE(argList); + for (int i = 0; i < numArgs; ++i) { + PyObject* item = PySequence_Fast_GET_ITEM(args.object(), i); + if (!PyString_Check(item) && !PyUnicode_Check(item)) return false; + } - bool addAppName = !numArgs && defaultAppName; - *argc = addAppName ? 1 : numArgs; - + *argc = numArgs + 1; *argv = new char*[*argc]; for (int i = 0; i < numArgs; ++i) { - PyObject* item = PySequence_GetItem(argList, i); - char* string = PyString_AS_STRING(item); - int size = strlen(string); - (*argv)[i] = new char[size+1]; - (*argv)[i] = strcpy((*argv)[i], string); - Py_DECREF(item); + PyObject* item = PySequence_Fast_GET_ITEM(args.object(), i); + char* string; + if (PyUnicode_Check(item)) { + Shiboken::AutoDecRef utf8(PyUnicode_AsUTF8String(item)); + string = strdup(PyString_AS_STRING(utf8.object())); + } else { + string = strdup(PyString_AS_STRING(item)); + } + (*argv)[i+1] = string; } - if (addAppName) { - (*argv)[0] = new char[strlen(defaultAppName)+1]; - (*argv)[0] = strcpy((*argv)[0], defaultAppName); - } + // Try to get the script name + PyObject* globals = PyEval_GetGlobals(); + PyObject* appName = PyDict_GetItemString(globals, "__file__"); + (*argv)[0] = strdup(appName ? PyString_AS_STRING(appName) : defaultAppName); return true; } |