]> git.ipfire.org Git - pakfire.git/commitdiff
python: execute: Raise any output
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 18 Mar 2025 09:11:35 +0000 (09:11 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 18 Mar 2025 09:11:35 +0000 (09:11 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/python/pakfire.c
tests/python/execute.py

index 3268ac56127ba979b4050f29424f874ef5b61c79..9dbcfb7cd3725558eb301704426dd182172ee73f 100644 (file)
@@ -646,16 +646,10 @@ static PyObject* Pakfire_execute(PakfireObject* self, PyObject* args, PyObject*
                }
        }
 
-       // Capture and return the output?
-       if (return_output) {
-               // Allocate a new buffer
-               output = PyBytes_FromString("");
-               if (!output)
-                       goto ERROR;
-
-               // Register a function that captures the output
-               stdout_callback = Pakfire_execute_stdout_callback;
-       }
+       // Allocate a new output buffer
+       output = PyBytes_FromString("");
+       if (!output)
+               goto ERROR;
 
        // Create a new jail
        r = pakfire_jail_create(&jail, self->pakfire);
@@ -678,7 +672,8 @@ static PyObject* Pakfire_execute(PakfireObject* self, PyObject* args, PyObject*
        Py_BEGIN_ALLOW_THREADS
 
        // Execute command
-       r = pakfire_jail_communicate(jail, argv, env, 0, NULL, NULL, stdout_callback, &output);
+       r = pakfire_jail_communicate(jail, argv, env, 0,
+                       NULL, NULL, Pakfire_execute_stdout_callback, &output);
 
        Py_END_ALLOW_THREADS
 
@@ -690,11 +685,13 @@ static PyObject* Pakfire_execute(PakfireObject* self, PyObject* args, PyObject*
 
        // Otherwise the executed command returned some error code
        } else if (r > 0) {
-               PyObject* code = PyLong_FromLong(r);
+               PyObject* error = Py_BuildValue("iO", r, output);
+               if (!error)
+                       goto ERROR;
 
                // Raise CommandExecutionError
-               PyErr_SetObject(PyExc_CommandExecutionError, code);
-               Py_DECREF(code);
+               PyErr_SetObject(PyExc_CommandExecutionError, error);
+               Py_DECREF(error);
 
                goto ERROR;
        }
index 3bed8e61792dea7dc5b6425cd7b32d690df9688f..991c5adc0d9889bbc469db1c1f95b72a1a95057b 100755 (executable)
@@ -42,9 +42,10 @@ class ExecuteTests(tests.TestCase):
                        self.pakfire.execute(["/command", "exit-with-code", "123"])
 
                # Extract return code
-               code, = e.exception.args
+               code, output = e.exception.args
 
                self.assertTrue(code == 123)
+               self.assertIsInstance(output, bytes)
 
        def test_environ(self):
                r = self.pakfire.execute(["/command", "echo-environ", "VAR1"],