From b5a7c9dcba5df2c55d7ee6b4122a8078c06ce775 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 18 Mar 2025 09:11:35 +0000 Subject: [PATCH] python: execute: Raise any output Signed-off-by: Michael Tremer --- src/python/pakfire.c | 25 +++++++++++-------------- tests/python/execute.py | 3 ++- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/python/pakfire.c b/src/python/pakfire.c index 3268ac56..9dbcfb7c 100644 --- a/src/python/pakfire.c +++ b/src/python/pakfire.c @@ -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; } diff --git a/tests/python/execute.py b/tests/python/execute.py index 3bed8e61..991c5adc 100755 --- a/tests/python/execute.py +++ b/tests/python/execute.py @@ -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"], -- 2.39.5