summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Clayton <q3aiml@gmail.com>2020-03-25 19:46:23 -0500
committerMartin Michlmayr <tbm@cyrius.com>2020-03-26 11:07:46 +0800
commit6d20807d1ac95feef1dc24712e16f63007311c94 (patch)
treeeed96045b6c6aa4704c9ab6377852499fad8328f
parent2c77b8292329f00fd22e9542848c1a32888ca8f5 (diff)
downloadfork-ledger-6d20807d1ac95feef1dc24712e16f63007311c94.tar.gz
fork-ledger-6d20807d1ac95feef1dc24712e16f63007311c94.tar.bz2
fork-ledger-6d20807d1ac95feef1dc24712e16f63007311c94.zip
py3: ensure python output is not lost
Unbuffer python's stdio to avoid output getting stuck in buffer when stdout is not a TTY. Normally buffers are flushed by `Py_Finalize` but Boost has a long-standing issue preventing the proper shutdown of the interpreter with `Py_Finalize` when embedded [1]. This applies the same fix as 139beba but to any ledger usage rather than only the test suite. I removed `PYTHONUNBUFFERED=1` from tests as there is no expectation that users should need to have this set for ledger to function. For example without this fix piping ledger into cat usually loses any output (unless the output is large enough to cause the buffer to flush): $ ./ledger -f "test/baseline/feat-value_py3.test" reg <class 'bool'> True [...] $ ./ledger -f "test/baseline/feat-value_py3.test" reg | cat $ Interestingly `--verify` causes `python_interpreter_t` to be destroyed -- it doesn't appear to be otherwise -- which does call `Py_Finalize`. As expected this fixes the issue but can also crash due to the boost issue mentioned above: $ ./ledger -f "test/baseline/feat-value_py3.test" --verify reg <class 'bool'> True [...] Segmentation fault (core dumped) $ ./ledger -f "test/baseline/feat-value_py3.test" --verify reg | cat <class 'bool'> True [...] $ 1. https://www.boost.org/doc/libs/1_62_0/libs/python/doc/html/tutorial/tutorial/embedding.html
-rw-r--r--src/pyinterp.cc5
-rw-r--r--test/CMakeLists.txt6
-rw-r--r--test/unit/CMakeLists.txt2
3 files changed, 9 insertions, 4 deletions
diff --git a/src/pyinterp.cc b/src/pyinterp.cc
index 5381758e..72995eb6 100644
--- a/src/pyinterp.cc
+++ b/src/pyinterp.cc
@@ -151,6 +151,11 @@ void python_interpreter_t::initialize()
DEBUG("python.interp", "Initializing Python");
#if PY_MAJOR_VERSION >= 3
+ // Unbuffer stdio to avoid python output getting stuck in buffer when
+ // stdout is not a TTY. Normally buffers are flushed by Py_Finalize but
+ // Boost has a long-standing issue preventing proper shutdown of the
+ // interpreter with Py_Finalize when embedded.
+ Py_UnbufferedStdioFlag = 1;
// PyImport_AppendInittab docs: "This should be called before Py_Initialize()".
PyImport_AppendInittab((char*)"ledger", PyInit_ledger);
#endif
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index a0905004..c1dde710 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -34,7 +34,7 @@ macro(add_ledger_harness_tests _class)
$<TARGET_FILE:ledger> ${PROJECT_SOURCE_DIR}
${TestFile} ${TEST_PYTHON_FLAGS})
set_tests_properties(${_class}Test_${TestFile_Name}
- PROPERTIES ENVIRONMENT "PYTHONUNBUFFERED=1;TZ=${Ledger_TEST_TIMEZONE}")
+ PROPERTIES ENVIRONMENT "TZ=${Ledger_TEST_TIMEZONE}")
endif()
endforeach()
endif()
@@ -53,7 +53,7 @@ if (Python_EXECUTABLE)
COMMAND ${Python_EXECUTABLE} ${PROJECT_SOURCE_DIR}/test/${_class}.py
--ledger $<TARGET_FILE:ledger> --file ${TestFile})
set_tests_properties(${_class}Test_${TestFile_Name}
- PROPERTIES ENVIRONMENT "PYTHONUNBUFFERED=1;TZ=${Ledger_TEST_TIMEZONE}")
+ PROPERTIES ENVIRONMENT "TZ=${Ledger_TEST_TIMEZONE}")
endforeach()
# CheckManpage and CheckTexinfo are disabled, since they do not work
@@ -64,7 +64,7 @@ if (Python_EXECUTABLE)
COMMAND ${Python_EXECUTABLE} ${PROJECT_SOURCE_DIR}/test/${_class}.py
--ledger $<TARGET_FILE:ledger> --source ${PROJECT_SOURCE_DIR})
set_tests_properties(${_class}
- PROPERTIES ENVIRONMENT "PYTHONUNBUFFERED=1;TZ=${Ledger_TEST_TIMEZONE}")
+ PROPERTIES ENVIRONMENT "TZ=${Ledger_TEST_TIMEZONE}")
endforeach()
endif()
diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt
index 4a9911c5..1bd5e4b3 100644
--- a/test/unit/CMakeLists.txt
+++ b/test/unit/CMakeLists.txt
@@ -2,7 +2,7 @@ macro(add_ledger_test _name)
target_link_libraries(${_name} libledger)
add_test(Ledger${_name} ${PROJECT_BINARY_DIR}/${_name})
set_tests_properties(Ledger${_name}
- PROPERTIES ENVIRONMENT "PYTHONUNBUFFERED=1;TZ=${Ledger_TEST_TIMEZONE}")
+ PROPERTIES ENVIRONMENT "TZ=${Ledger_TEST_TIMEZONE}")
endmacro(add_ledger_test _name)
include_directories(${PROJECT_SOURCE_DIR}/src)