Commit cfb1045a authored by Kevin Modzelewski's avatar Kevin Modzelewski

Add more CPython code

parent c7c10250
...@@ -219,10 +219,9 @@ PyAPI_DATA(PyTypeObject *) VMSError; ...@@ -219,10 +219,9 @@ PyAPI_DATA(PyTypeObject *) VMSError;
#define PyExc_BufferError ((PyObject*)BufferError) #define PyExc_BufferError ((PyObject*)BufferError)
PyAPI_DATA(PyTypeObject *) BufferError; PyAPI_DATA(PyTypeObject *) BufferError;
#define PyExc_MemoryErrorInst ((PyObject*)MemoryErrorInst) PyAPI_DATA(PyObject *) PyExc_MemoryErrorInst;
PyAPI_DATA(PyTypeObject *) MemoryErrorInst; PyAPI_DATA(PyObject *) PyExc_RecursionErrorInst;
#define PyExc_RecursionErrorInst ((PyObject*)RecursionErrorInst)
PyAPI_DATA(PyTypeObject *) RecursionErrorInst;
/* Predefined warning categories */ /* Predefined warning categories */
#define PyExc_Warning ((PyObject*)Warning) #define PyExc_Warning ((PyObject*)Warning)
......
...@@ -181,6 +181,7 @@ extern "C" void PyErr_WriteUnraisable(PyObject* obj) { ...@@ -181,6 +181,7 @@ extern "C" void PyErr_WriteUnraisable(PyObject* obj) {
Py_XDECREF(v); Py_XDECREF(v);
Py_XDECREF(tb); Py_XDECREF(tb);
} }
extern "C" void PyErr_Display(PyObject* exception, PyObject* value, PyObject* tb) { extern "C" void PyErr_Display(PyObject* exception, PyObject* value, PyObject* tb) {
Py_FatalError("unimplemented"); Py_FatalError("unimplemented");
} }
......
...@@ -521,6 +521,9 @@ BoxedClass* BaseException, *Exception, *StandardError, *AssertionError, *Attribu ...@@ -521,6 +521,9 @@ BoxedClass* BaseException, *Exception, *StandardError, *AssertionError, *Attribu
*RuntimeError, *ImportError, *StopIteration, *Warning, *SyntaxError, *OverflowError, *DeprecationWarning, *RuntimeError, *ImportError, *StopIteration, *Warning, *SyntaxError, *OverflowError, *DeprecationWarning,
*MemoryError, *LookupError, *EnvironmentError, *ArithmeticError, *BufferError, *KeyboardInterrupt, *SystemExit, *MemoryError, *LookupError, *EnvironmentError, *ArithmeticError, *BufferError, *KeyboardInterrupt, *SystemExit,
*SystemError, *NotImplementedError, *PendingDeprecationWarning; *SystemError, *NotImplementedError, *PendingDeprecationWarning;
Box* PyExc_RecursionErrorInst;
Box* PyExc_MemoryErrorInst;
} }
Box* exceptionNew1(BoxedClass* cls) { Box* exceptionNew1(BoxedClass* cls) {
...@@ -1086,5 +1089,11 @@ void setupBuiltins() { ...@@ -1086,5 +1089,11 @@ void setupBuiltins() {
builtins_module->giveAttr("property", property_cls); builtins_module->giveAttr("property", property_cls);
builtins_module->giveAttr("staticmethod", staticmethod_cls); builtins_module->giveAttr("staticmethod", staticmethod_cls);
builtins_module->giveAttr("classmethod", classmethod_cls); builtins_module->giveAttr("classmethod", classmethod_cls);
PyExc_RecursionErrorInst = new BoxedException(RuntimeError);
gc::registerPermanentRoot(PyExc_RecursionErrorInst);
PyExc_MemoryErrorInst = new BoxedException(MemoryError);
gc::registerPermanentRoot(PyExc_MemoryErrorInst);
} }
} }
...@@ -76,11 +76,20 @@ Box* getSysStdout() { ...@@ -76,11 +76,20 @@ Box* getSysStdout() {
} }
extern "C" int PySys_SetObject(const char* name, PyObject* v) { extern "C" int PySys_SetObject(const char* name, PyObject* v) {
Py_FatalError("unimplemented"); try {
if (!v) {
if (sys_module->getattr(name))
sys_module->delattr(name, NULL);
} else
sys_module->setattr(name, v, NULL);
} catch (Box* b) {
abort();
}
return 0;
} }
extern "C" PyObject* PySys_GetObject(const char* name) { extern "C" PyObject* PySys_GetObject(const char* name) {
Py_FatalError("unimplemented"); return sys_module->getattr(name);
} }
static void mywrite(const char* name, FILE* fp, const char* format, va_list va) noexcept { static void mywrite(const char* name, FILE* fp, const char* format, va_list va) noexcept {
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "capi/types.h" #include "capi/types.h"
#include "core/threading.h" #include "core/threading.h"
#include "core/types.h" #include "core/types.h"
#include "runtime/classobj.h"
#include "runtime/import.h" #include "runtime/import.h"
#include "runtime/objmodel.h" #include "runtime/objmodel.h"
#include "runtime/types.h" #include "runtime/types.h"
...@@ -643,7 +644,101 @@ extern "C" int Py_FlushLine(void) { ...@@ -643,7 +644,101 @@ extern "C" int Py_FlushLine(void) {
} }
extern "C" void PyErr_NormalizeException(PyObject** exc, PyObject** val, PyObject** tb) { extern "C" void PyErr_NormalizeException(PyObject** exc, PyObject** val, PyObject** tb) {
Py_FatalError("unimplemented"); PyObject* type = *exc;
PyObject* value = *val;
PyObject* inclass = NULL;
PyObject* initial_tb = NULL;
PyThreadState* tstate = NULL;
if (type == NULL) {
/* There was no exception, so nothing to do. */
return;
}
/* If PyErr_SetNone() was used, the value will have been actually
set to NULL.
*/
if (!value) {
value = Py_None;
Py_INCREF(value);
}
if (PyExceptionInstance_Check(value))
inclass = PyExceptionInstance_Class(value);
/* Normalize the exception so that if the type is a class, the
value will be an instance.
*/
if (PyExceptionClass_Check(type)) {
/* if the value was not an instance, or is not an instance
whose class is (or is derived from) type, then use the
value as an argument to instantiation of the type
class.
*/
if (!inclass || !PyObject_IsSubclass(inclass, type)) {
PyObject* args, *res;
if (value == Py_None)
args = PyTuple_New(0);
else if (PyTuple_Check(value)) {
Py_INCREF(value);
args = value;
} else
args = PyTuple_Pack(1, value);
if (args == NULL)
goto finally;
res = PyEval_CallObject(type, args);
Py_DECREF(args);
if (res == NULL)
goto finally;
Py_DECREF(value);
value = res;
}
/* if the class of the instance doesn't exactly match the
class of the type, believe the instance
*/
else if (inclass != type) {
Py_DECREF(type);
type = inclass;
Py_INCREF(type);
}
}
*exc = type;
*val = value;
return;
finally:
Py_DECREF(type);
Py_DECREF(value);
/* If the new exception doesn't set a traceback and the old
exception had a traceback, use the old traceback for the
new exception. It's better than nothing.
*/
initial_tb = *tb;
PyErr_Fetch(exc, val, tb);
if (initial_tb != NULL) {
if (*tb == NULL)
*tb = initial_tb;
else
Py_DECREF(initial_tb);
}
/* normalize recursively */
tstate = PyThreadState_GET();
if (++tstate->recursion_depth > Py_GetRecursionLimit()) {
--tstate->recursion_depth;
/* throw away the old exception... */
Py_DECREF(*exc);
Py_DECREF(*val);
/* ... and use the recursion error instead */
*exc = PyExc_RuntimeError;
*val = PyExc_RecursionErrorInst;
Py_INCREF(*exc);
Py_INCREF(*val);
/* just keeping the old traceback */
return;
}
PyErr_NormalizeException(exc, val, tb);
--tstate->recursion_depth;
} }
void checkAndThrowCAPIException() { void checkAndThrowCAPIException() {
...@@ -705,11 +800,11 @@ extern "C" int PyErr_CheckSignals() { ...@@ -705,11 +800,11 @@ extern "C" int PyErr_CheckSignals() {
} }
extern "C" int PyExceptionClass_Check(PyObject* o) { extern "C" int PyExceptionClass_Check(PyObject* o) {
Py_FatalError("unimplemented"); return PyClass_Check(o) || (PyType_Check(o) && isSubclass(static_cast<BoxedClass*>(o), BaseException));
} }
extern "C" int PyExceptionInstance_Check(PyObject* o) { extern "C" int PyExceptionInstance_Check(PyObject* o) {
Py_FatalError("unimplemented"); return PyInstance_Check(o) || isSubclass(o->cls, BaseException);
} }
extern "C" const char* PyExceptionClass_Name(PyObject* o) { extern "C" const char* PyExceptionClass_Name(PyObject* o) {
...@@ -717,6 +812,10 @@ extern "C" const char* PyExceptionClass_Name(PyObject* o) { ...@@ -717,6 +812,10 @@ extern "C" const char* PyExceptionClass_Name(PyObject* o) {
} }
extern "C" PyObject* PyExceptionInstance_Class(PyObject* o) { extern "C" PyObject* PyExceptionInstance_Class(PyObject* o) {
return PyInstance_Check(o) ? (Box*)static_cast<BoxedInstance*>(o)->inst_cls : o->cls;
}
extern "C" int PyTraceBack_Print(PyObject* v, PyObject* f) {
Py_FatalError("unimplemented"); Py_FatalError("unimplemented");
} }
......
...@@ -192,7 +192,10 @@ extern "C" PyObject* PyFile_FromFile(FILE* fp, char* name, char* mode, int (*clo ...@@ -192,7 +192,10 @@ extern "C" PyObject* PyFile_FromFile(FILE* fp, char* name, char* mode, int (*clo
} }
extern "C" FILE* PyFile_AsFile(PyObject* f) { extern "C" FILE* PyFile_AsFile(PyObject* f) {
Py_FatalError("unimplemented"); if (!f || !PyFile_Check(f))
return NULL;
return static_cast<BoxedFile*>(f)->f;
} }
extern "C" int PyFile_WriteObject(PyObject* v, PyObject* f, int flags) { extern "C" int PyFile_WriteObject(PyObject* v, PyObject* f, int flags) {
......
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