[vte] Bug 583078 – [python] allow passing of environment as a dictionary



commit 56727a20b71cd5f960e468444ef544296ddc5edf
Author: Frédéric Péters <fpeters 0d be>
Date:   Mon May 18 17:36:46 2009 +0200

    Bug 583078 â?? [python] allow passing of environment as a dictionary
---
 python/vte.override |   92 ++++++++++++++++++++++++++++++++------------------
 1 files changed, 59 insertions(+), 33 deletions(-)

diff --git a/python/vte.override b/python/vte.override
index 114d204..277bc3f 100644
--- a/python/vte.override
+++ b/python/vte.override
@@ -106,6 +106,55 @@ _wrap_vte_terminal_feed_child_binary(PyGObject *self, PyObject *args, PyObject *
 %%
 override vte_terminal_fork_command kwargs
 
+static int
+_build_envv(PyObject *py_envv, char ***envv)
+{
+	int i, pos, n_envs;
+	PyObject *py_dict_k, *py_dict_v;
+
+	if (py_envv == NULL || py_envv != Py_None) {
+		return 0;
+	}
+
+	if (PyDict_Check(py_envv)) {
+		n_envs = PyMapping_Length(py_envv);
+		*envv = g_new(gchar *, n_envs + 1);
+		pos = 0;
+		i = 0;
+		while (PyDict_Next(py_envv, &pos, &py_dict_k, &py_dict_v)) {
+			*envv[i++] = g_strdup_printf("%s=%s",
+				PyString_AsString(py_dict_k),
+				PyString_AsString(py_dict_v));
+		}
+		*envv[n_envs] = NULL;
+	} else if (PySequence_Check(py_envv)) {
+		n_envs = PySequence_Length(py_envv);
+		*envv = g_new(gchar *, n_envs + 1);
+		for (i = 0; i < n_envs; i++) {
+			PyObject *item = PySequence_GetItem(py_envv, i);
+			Py_DECREF(item);  /* PySequence_GetItem INCREF's */
+			*envv[i] = PyString_AsString(item);
+		}
+		*envv[n_envs] = NULL;
+	} else {
+		PyErr_SetString(PyExc_TypeError,
+				"envv must be a sequence or a dictionary");
+		return -1;
+	}
+
+	return n_envs;
+}
+
+static void
+_free_envv(PyObject *py_envv, char **envv)
+{
+	if (PyDict_Check(py_envv)) {
+		g_strfreev(envv);
+	} else {
+		g_free(envv);
+	}
+}
+
 static PyObject *
 _wrap_vte_terminal_fork_command(PyGObject * self, PyObject * args,
 				PyObject * kwargs)
@@ -145,21 +194,10 @@ _wrap_vte_terminal_fork_command(PyGObject * self, PyObject * args,
 		argv[n_args] = NULL;
 	}
 
-	if (py_envv != NULL && py_envv != Py_None) {
-		if (!PySequence_Check(py_envv)) {
-			PyErr_SetString(PyExc_TypeError,
-					"envv must be a sequence");
-			return NULL;
-		}
-
-		n_envs = PySequence_Length(py_envv);
-		envv = g_new(gchar *, n_envs + 1);
-		for (i = 0; i < n_envs; i++) {
-			PyObject *item = PySequence_GetItem(py_envv, i);
-			Py_DECREF(item);  /* PySequence_GetItem INCREF's */
-			envv[i] = PyString_AsString(item);
-		}
-		envv[n_envs] = NULL;
+	n_envs = _build_envv(py_envv, &envv);
+	if (n_envs == -1) {
+		g_free(argv);
+		return NULL;
 	}
 
 	pid = vte_terminal_fork_command(VTE_TERMINAL(self->obj),
@@ -172,7 +210,7 @@ _wrap_vte_terminal_fork_command(PyGObject * self, PyObject * args,
 					PyObject_IsTrue(logwtmp));
 
 	if (envv) {
-		g_free(envv);
+		_free_envv(py_envv, envv);
 	}
 
 	if (argv) {
@@ -193,7 +231,7 @@ _wrap_vte_terminal_forkpty(PyGObject * self, PyObject * args, PyObject * kwargs)
 				  NULL };
 	PyObject *py_envv = NULL,
 		 *loglastlog = NULL, *logutmp = NULL, *logwtmp = NULL;
-	int i, n_args, n_envs;
+	int n_envs;
 	pid_t pid;
 
 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OsOOO:forkpty",
@@ -202,21 +240,9 @@ _wrap_vte_terminal_forkpty(PyGObject * self, PyObject * args, PyObject * kwargs)
 		return NULL;
 	}
 
-	if (py_envv != NULL && py_envv != Py_None) {
-		if (!PySequence_Check(py_envv)) {
-			PyErr_SetString(PyExc_TypeError,
-					"envv must be a sequence");
-			return NULL;
-		}
-
-		n_envs = PySequence_Length(py_envv);
-		envv = g_new(gchar *, n_envs + 1);
-		for (i = 0; i < n_envs; i++) {
-			PyObject *item = PySequence_GetItem(py_envv, i);
-			Py_DECREF(item);  /* PySequence_GetItem INCREF's */
-			envv[i] = PyString_AsString(item);
-		}
-		envv[n_envs] = NULL;
+	n_envs = _build_envv(py_envv, &envv);
+	if (n_envs == -1) {
+		return NULL;
 	}
 
 	pid = vte_terminal_forkpty(VTE_TERMINAL(self->obj),
@@ -229,7 +255,7 @@ _wrap_vte_terminal_forkpty(PyGObject * self, PyObject * args, PyObject * kwargs)
 				   PyObject_IsTrue(logwtmp));
 
 	if (envv) {
-		g_free(envv);
+		_free_envv(py_envv, envv);
 	}
 
 	return PyInt_FromLong(pid);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]