Commit 9b3c6d65 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Adding PyErr_Print and its dependencies

parent b6ba1a27
......@@ -79,6 +79,7 @@
#include "modsupport.h"
#include "pythonrun.h"
#include "ceval.h"
#include "sysmodule.h"
#include "intrcheck.h"
#include "import.h"
......
// This file is originally from CPython 2.7, with modifications for Pyston
/* System module interface */
#ifndef Py_SYSMODULE_H
#define Py_SYSMODULE_H
#ifdef __cplusplus
extern "C" {
#endif
// Pyston change: changed most of these to const char*
PyAPI_FUNC(PyObject *) PySys_GetObject(const char *);
PyAPI_FUNC(int) PySys_SetObject(const char *, PyObject *);
PyAPI_FUNC(FILE *) PySys_GetFile(char *, FILE *);
PyAPI_FUNC(void) PySys_SetArgv(int, char **);
PyAPI_FUNC(void) PySys_SetArgvEx(int, char **, int);
PyAPI_FUNC(void) PySys_SetPath(char *);
PyAPI_FUNC(void) PySys_WriteStdout(const char *format, ...)
Py_GCC_ATTRIBUTE((format(printf, 1, 2)));
PyAPI_FUNC(void) PySys_WriteStderr(const char *format, ...)
Py_GCC_ATTRIBUTE((format(printf, 1, 2)));
PyAPI_FUNC(void) PySys_ResetWarnOptions(void);
PyAPI_FUNC(void) PySys_AddWarnOption(char *);
PyAPI_FUNC(int) PySys_HasWarnOptions(void);
#ifdef __cplusplus
}
#endif
#endif /* !Py_SYSMODULE_H */
......@@ -117,11 +117,94 @@ extern "C" PyObject* PyErr_SetFromErrnoWithUnicodeFilename(PyObject* exc, const
}
#endif /* MS_WINDOWS */
extern "C" void PyErr_Fetch(PyObject** p_type, PyObject** p_value, PyObject** p_traceback) {
auto* tstate = &threading::cur_thread_state;
*p_type = tstate->exc_type;
*p_value = tstate->exc_value;
*p_traceback = tstate->exc_traceback;
tstate->exc_type = NULL;
tstate->exc_value = NULL;
tstate->exc_traceback = NULL;
}
extern "C" PyObject* PyErr_SetFromErrno(PyObject* exc) {
return PyErr_SetFromErrnoWithFilenameObject(exc, NULL);
}
extern "C" void PyErr_Print() {
extern "C" void PyErr_Display(PyObject* exception, PyObject* value, PyObject* tb) {
Py_FatalError("unimplemented");
}
static void handle_system_exit(void) noexcept {
Py_FatalError("unimplemented");
}
extern "C" void PyErr_PrintEx(int set_sys_last_vars) {
PyObject* exception, *v, *tb, *hook;
if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
handle_system_exit();
}
PyErr_Fetch(&exception, &v, &tb);
if (exception == NULL)
return;
PyErr_NormalizeException(&exception, &v, &tb);
if (exception == NULL)
return;
/* Now we know v != NULL too */
if (set_sys_last_vars) {
PySys_SetObject("last_type", exception);
PySys_SetObject("last_value", v);
PySys_SetObject("last_traceback", tb);
}
hook = PySys_GetObject("excepthook");
if (hook && hook != Py_None) {
PyObject* args = PyTuple_Pack(3, exception, v, tb ? tb : Py_None);
PyObject* result = PyEval_CallObject(hook, args);
if (result == NULL) {
PyObject* exception2, *v2, *tb2;
if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
handle_system_exit();
}
PyErr_Fetch(&exception2, &v2, &tb2);
PyErr_NormalizeException(&exception2, &v2, &tb2);
/* It should not be possible for exception2 or v2
to be NULL. However PyErr_Display() can't
tolerate NULLs, so just be safe. */
if (exception2 == NULL) {
exception2 = Py_None;
Py_INCREF(exception2);
}
if (v2 == NULL) {
v2 = Py_None;
Py_INCREF(v2);
}
if (Py_FlushLine())
PyErr_Clear();
fflush(stdout);
PySys_WriteStderr("Error in sys.excepthook:\n");
PyErr_Display(exception2, v2, tb2);
PySys_WriteStderr("\nOriginal exception was:\n");
PyErr_Display(exception, v, tb);
Py_DECREF(exception2);
Py_DECREF(v2);
Py_XDECREF(tb2);
}
Py_XDECREF(result);
Py_XDECREF(args);
} else {
PySys_WriteStderr("sys.excepthook is missing\n");
PyErr_Display(exception, v, tb);
}
Py_XDECREF(exception);
Py_XDECREF(v);
Py_XDECREF(tb);
}
extern "C" void PyErr_Print() {
PyErr_PrintEx(1);
}
}
......@@ -76,6 +76,56 @@ Box* getSysStdout() {
return sys_stdout;
}
extern "C" int PySys_SetObject(const char* name, PyObject* v) {
Py_FatalError("unimplemented");
}
extern "C" PyObject* PySys_GetObject(const char* name) {
Py_FatalError("unimplemented");
}
static void mywrite(const char* name, FILE* fp, const char* format, va_list va) noexcept {
PyObject* file;
PyObject* error_type, *error_value, *error_traceback;
PyErr_Fetch(&error_type, &error_value, &error_traceback);
file = PySys_GetObject(name);
if (file == NULL || PyFile_AsFile(file) == fp)
vfprintf(fp, format, va);
else {
char buffer[1001];
const int written = PyOS_vsnprintf(buffer, sizeof(buffer), format, va);
if (PyFile_WriteString(buffer, file) != 0) {
PyErr_Clear();
fputs(buffer, fp);
}
if (written < 0 || (size_t)written >= sizeof(buffer)) {
const char* truncated = "... truncated";
if (PyFile_WriteString(truncated, file) != 0) {
PyErr_Clear();
fputs(truncated, fp);
}
}
}
PyErr_Restore(error_type, error_value, error_traceback);
}
extern "C" void PySys_WriteStdout(const char* format, ...) {
va_list va;
va_start(va, format);
mywrite("stdout", stdout, format, va);
va_end(va);
}
extern "C" void PySys_WriteStderr(const char* format, ...) {
va_list va;
va_start(va, format);
mywrite("stderr", stderr, format, va);
va_end(va);
}
void addToSysArgv(const char* str) {
Box* sys_argv = sys_module->getattr("argv");
assert(sys_argv);
......
......@@ -630,6 +630,19 @@ extern "C" int PyCallable_Check(PyObject* x) {
return typeLookup(x->cls, call_attr, NULL) != NULL;
}
extern "C" int Py_FlushLine(void) {
PyObject* f = PySys_GetObject("stdout");
if (f == NULL)
return 0;
if (!PyFile_SoftSpace(f, 0))
return 0;
return PyFile_WriteString("\n", f);
}
extern "C" void PyErr_NormalizeException(PyObject** exc, PyObject** val, PyObject** tb) {
Py_FatalError("unimplemented");
}
void checkAndThrowCAPIException() {
Box* value = threading::cur_thread_state.exc_value;
if (value) {
......
......@@ -191,6 +191,14 @@ extern "C" PyObject* PyFile_FromFile(FILE* fp, char* name, char* mode, int (*clo
Py_FatalError("unimplemented");
}
extern "C" FILE* PyFile_AsFile(PyObject* f) {
Py_FatalError("unimplemented");
}
extern "C" int PyFile_WriteString(const char* s, PyObject* f) {
Py_FatalError("unimplemented");
}
extern "C" void PyFile_SetBufSize(PyObject* f, int bufsize) {
Py_FatalError("unimplemented");
}
......@@ -203,6 +211,10 @@ extern "C" int PyObject_AsFileDescriptor(PyObject* o) {
Py_FatalError("unimplemented");
}
extern "C" int PyFile_SoftSpace(PyObject* f, int newflag) {
Py_FatalError("unimplemented");
}
void setupFile() {
file_cls->giveAttr("__name__", boxStrConstant("file"));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment