cosmopolitan/third_party/python/Python/flushstdfiles.c
Justine Tunney b5f743cdc3 Begin incorporating Python unit tests into build
We now build a separate APE binary for each test so they can run in
parallel. We've got 148 tests running fast and stable so far.
2021-09-12 21:04:44 -07:00

60 lines
2.1 KiB
C

/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8 -*-│
│vi: set net ft=c ts=4 sts=4 sw=4 fenc=utf-8 :vi│
╞══════════════════════════════════════════════════════════════════════════════╡
│ Python 3 │
│ https://docs.python.org/3/license.html │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "third_party/python/Include/abstract.h"
#include "third_party/python/Include/object.h"
#include "third_party/python/Include/pyerrors.h"
#include "third_party/python/Include/pylifecycle.h"
#include "third_party/python/Include/sysmodule.h"
/* clang-format off */
_Py_IDENTIFIER(flush);
_Py_IDENTIFIER(stdout);
_Py_IDENTIFIER(stderr);
static int
file_is_closed(PyObject *fobj)
{
int r;
PyObject *tmp = PyObject_GetAttrString(fobj, "closed");
if (tmp == NULL) {
PyErr_Clear();
return 0;
}
r = PyObject_IsTrue(tmp);
Py_DECREF(tmp);
if (r < 0)
PyErr_Clear();
return r > 0;
}
int
_Py_FlushStdFiles(void)
{
PyObject *fout = _PySys_GetObjectId(&PyId_stdout);
PyObject *ferr = _PySys_GetObjectId(&PyId_stderr);
PyObject *tmp;
int status = 0;
if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
tmp = _PyObject_CallMethodId(fout, &PyId_flush, NULL);
if (tmp == NULL) {
PyErr_WriteUnraisable(fout);
status = -1;
}
else
Py_DECREF(tmp);
}
if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
tmp = _PyObject_CallMethodId(ferr, &PyId_flush, NULL);
if (tmp == NULL) {
PyErr_Clear();
status = -1;
}
else
Py_DECREF(tmp);
}
return status;
}