I want to get history count (and contents) from Python.
From command line it's possible with readline
module (installed via pip install pyreadline
):
import readline
x=readline.get_current_history_length()
x
Yields 2, as it should. However the same code using Python C API always returns zero, regardless of actual history:
#include <Python.h>
long GetHistoryCount() {
Py_Initialize();
PyRun_SimpleString("import readline");
PyRun_SimpleString("x=readline.get_current_history_length()");
PyObject *mainModule = PyImport_AddModule("__main__");
PyObject *var = PyObject_GetAttrString(mainModule, "x");
Py_Finalize();
return PyLong_AsLong(var);
}
int main(int argc, char **argv) {
long count = GetHistoryCount();
printf("Count: %ld", count);
return EXIT_SUCCESS;
}
Prints: Count: 0
How can I get history from Python C API?
Found readline module in this discussion