summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHugo Parente Lima <hugo.pl@gmail.com>2010-10-29 15:25:07 -0200
committerHugo Parente Lima <hugo.pl@gmail.com>2010-10-29 15:38:58 -0200
commit5c9f0f811e5d8eac4465b21a2255613aa9f543a7 (patch)
treeccf6d501871362b82e0a856541165f09566086e9
parent5348af9f0700715fb4013886178d0915dd3fae80 (diff)
downloadshiboken-5c9f0f811e5d8eac4465b21a2255613aa9f543a7.tar.gz
shiboken-5c9f0f811e5d8eac4465b21a2255613aa9f543a7.tar.xz
shiboken-5c9f0f811e5d8eac4465b21a2255613aa9f543a7.zip
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>
-rw-r--r--libshiboken/helper.cpp45
-rw-r--r--libshiboken/helper.h14
2 files changed, 40 insertions, 19 deletions
diff --git a/libshiboken/helper.cpp b/libshiboken/helper.cpp
index f61fc144..c37c5432 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;
}
diff --git a/libshiboken/helper.h b/libshiboken/helper.h
index 36340907..a0630412 100644
--- a/libshiboken/helper.h
+++ b/libshiboken/helper.h
@@ -67,12 +67,20 @@ inline PyObject* makeTuple(const A& a, const B& b, const C& c, const D& d, const
/**
* 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.
+* This function tries to find the application (script) name and put it into argv[0], if
+* the application name can't be guessed, defaultAppName will be used.
*
-* \note argc and argv *should* be deleted by the user.
+* No memory is allocated is an error occur.
+*
+* \note argc must be a valid address.
+* \note The argv array is allocated using new operator and each item is allocated using malloc.
* \returns True on sucess, false otherwise.
*/
+LIBSHIBOKEN_API bool sequenceToArgcArgv(PyObject* argList, int* argc, char*** argv, const char* defaultAppName = 0);
+
+/**
+ * \deprecated Use sequenceToArgcArgv instead.
+ */
LIBSHIBOKEN_API bool PySequenceToArgcArgv(PyObject* argList, int* argc, char*** argv, const char* defaultAppName = 0);
/**