Commit 2526c94e authored by Marius Wachtler's avatar Marius Wachtler

Merge pull request #1029 from Daetalus/pyerr_warnex

Implement PyErr_WarnEx
parents b3bacb8f d4e924b0
......@@ -114,6 +114,7 @@ file(GLOB_RECURSE STDPYTHON_SRCS Python
Python-ast.c
random.c
structmember.c
_warnings.c
)
# compile specified files in from_cpython/Python
......
......@@ -74,6 +74,7 @@
#include "classobject.h"
#include "cobject.h"
#include "fileobject.h"
#include "frameobject.h"
#include "pycapsule.h"
#include "traceback.h"
#include "sliceobject.h"
......
/* Frame object interface */
#ifndef Py_FRAMEOBJECT_H
#define Py_FRAMEOBJECT_H
#ifdef __cplusplus
extern "C" {
#endif
// Pyston changes: we don't use it for now.
#if 0
typedef struct {
int b_type; /* what kind of block this is */
int b_handler; /* where to jump to find handler */
int b_level; /* value stack level to pop to */
} PyTryBlock;
typedef struct _frame {
PyObject_VAR_HEAD
struct _frame *f_back; /* previous frame, or NULL */
PyCodeObject *f_code; /* code segment */
PyObject *f_builtins; /* builtin symbol table (PyDictObject) */
PyObject *f_globals; /* global symbol table (PyDictObject) */
PyObject *f_locals; /* local symbol table (any mapping) */
PyObject **f_valuestack; /* points after the last local */
/* Next free slot in f_valuestack. Frame creation sets to f_valuestack.
Frame evaluation usually NULLs it, but a frame that yields sets it
to the current stack top. */
PyObject **f_stacktop;
PyObject *f_trace; /* Trace function */
/* If an exception is raised in this frame, the next three are used to
* record the exception info (if any) originally in the thread state. See
* comments before set_exc_info() -- it's not obvious.
* Invariant: if _type is NULL, then so are _value and _traceback.
* Desired invariant: all three are NULL, or all three are non-NULL. That
* one isn't currently true, but "should be".
*/
PyObject *f_exc_type, *f_exc_value, *f_exc_traceback;
PyThreadState *f_tstate;
int f_lasti; /* Last instruction if called */
/* Call PyFrame_GetLineNumber() instead of reading this field
directly. As of 2.3 f_lineno is only valid when tracing is
active (i.e. when f_trace is set). At other times we use
PyCode_Addr2Line to calculate the line from the current
bytecode index. */
int f_lineno; /* Current line number */
int f_iblock; /* index in f_blockstack */
PyTryBlock f_blockstack[CO_MAXBLOCKS]; /* for try and loop blocks */
PyObject *f_localsplus[1]; /* locals+stack, dynamically sized */
} PyFrameObject;
/* Standard object interface */
PyAPI_DATA(PyTypeObject) PyFrame_Type;
#define PyFrame_Check(op) ((op)->ob_type == &PyFrame_Type)
#define PyFrame_IsRestricted(f) \
((f)->f_builtins != (f)->f_tstate->interp->builtins)
PyAPI_FUNC(PyFrameObject *) PyFrame_New(PyThreadState *, PyCodeObject *,
PyObject *, PyObject *);
/* The rest of the interface is specific for frame objects */
/* Block management functions */
PyAPI_FUNC(void) PyFrame_BlockSetup(PyFrameObject *, int, int, int);
PyAPI_FUNC(PyTryBlock *) PyFrame_BlockPop(PyFrameObject *);
/* Extend the value stack */
PyAPI_FUNC(PyObject **) PyFrame_ExtendStack(PyFrameObject *, int, int);
/* Conversions between "fast locals" and locals in dictionary */
PyAPI_FUNC(void) PyFrame_LocalsToFast(PyFrameObject *, int);
PyAPI_FUNC(void) PyFrame_FastToLocals(PyFrameObject *);
PyAPI_FUNC(int) PyFrame_ClearFreeList(void);
#endif
typedef struct _PyFrameObject PyFrameObject;
/* Return the line of code the frame is currently executing. */
PyAPI_FUNC(int) PyFrame_GetLineNumber(PyFrameObject *) PYSTON_NOEXCEPT;
// Pyston changes: add a function to get globals
PyAPI_FUNC(PyObject *) PyFrame_GetGlobals(PyFrameObject *) PYSTON_NOEXCEPT;
// Pyston changes: add a function to get frame object by level
PyAPI_FUNC(PyFrameObject *) PyFrame_ForStackLevel(int stack_level) PYSTON_NOEXCEPT;
#ifdef __cplusplus
}
#endif
#endif /* !Py_FRAMEOBJECT_H */
......@@ -25,6 +25,9 @@ PyAPI_FUNC(void) PySys_ResetWarnOptions(void) PYSTON_NOEXCEPT;
PyAPI_FUNC(void) PySys_AddWarnOption(char *) PYSTON_NOEXCEPT;
PyAPI_FUNC(int) PySys_HasWarnOptions(void) PYSTON_NOEXCEPT;
// Pyston change: add this API to get sys modules dict
PyAPI_FUNC(PyObject *) PySys_GetModulesDict(void) PYSTON_NOEXCEPT;
#ifdef __cplusplus
}
#endif
......
This diff is collapsed.
......@@ -41,6 +41,7 @@ BoxedDict* sys_modules_dict;
extern "C" {
// supposed to be exposed through sys.flags
int Py_BytesWarningFlag = 0;
int Py_DivisionWarningFlag = 0;
int Py_HashRandomizationFlag = 0;
}
......@@ -400,6 +401,10 @@ extern "C" const char* Py_GetPlatform() noexcept {
#endif
}
extern "C" PyObject* PySys_GetModulesDict() noexcept {
return getSysModulesDict();
}
static PyObject* sys_excepthook(PyObject* self, PyObject* args) noexcept {
PyObject* exc, *value, *tb;
if (!PyArg_UnpackTuple(args, "excepthook", 3, 3, &exc, &value, &tb))
......
......@@ -585,8 +585,11 @@ extern "C" int PyObject_IsTrue(PyObject* o) noexcept {
extern "C" int PyObject_Not(PyObject* o) noexcept {
fatalOrError(PyExc_NotImplementedError, "unimplemented");
return -1;
int res;
res = PyObject_IsTrue(o);
if (res < 0)
return res;
return res == 0;
}
extern "C" PyObject* PyObject_Call(PyObject* callable_object, PyObject* args, PyObject* kw) noexcept {
......@@ -1034,16 +1037,6 @@ extern "C" PyObject* PyErr_Occurred() noexcept {
return cur_thread_state.curexc_type;
}
extern "C" int PyErr_WarnEx(PyObject* category, const char* text, Py_ssize_t stacklevel) noexcept {
// These warnings are silenced by default:
// We should copy the real CPython code in here
if (category == PyExc_DeprecationWarning)
return 0;
fatalOrError(PyExc_NotImplementedError, "unimplemented");
return -1;
}
extern "C" void* PyObject_Malloc(size_t sz) noexcept {
return gc_compat_malloc(sz);
}
......@@ -1924,13 +1917,6 @@ Box* BoxedCApiFunction::tppCall(Box* _self, CallRewriteArgs* rewrite_args, ArgPa
return rtn;
}
/* Warning with explicit origin */
extern "C" int PyErr_WarnExplicit(PyObject* category, const char* text, const char* filename_str, int lineno,
const char* module_str, PyObject* registry) noexcept {
Py_FatalError("unimplemented");
}
/* extension modules might be compiled with GC support so these
functions must always be available */
......
......@@ -149,6 +149,22 @@ Box* getFrame(int depth) {
return BoxedFrame::boxFrame(std::move(it));
}
extern "C" int PyFrame_GetLineNumber(PyFrameObject* f) noexcept {
BoxedInt* lineno = (BoxedInt*)BoxedFrame::lineno((Box*)f, NULL);
return lineno->n;
}
extern "C" PyObject* PyFrame_GetGlobals(PyFrameObject* f) noexcept {
Box* globals = BoxedFrame::globals((Box*)f, NULL);
if (globals->cls == attrwrapper_cls)
return attrwrapperToDict(globals);
return globals;
}
extern "C" PyFrameObject* PyFrame_ForStackLevel(int stack_level) noexcept {
return (PyFrameObject*)getFrame(stack_level);
}
void setupFrame() {
frame_cls
= BoxedClass::create(type_cls, object_cls, &BoxedFrame::gchandler, 0, 0, sizeof(BoxedFrame), false, "frame");
......
......@@ -126,6 +126,10 @@ static Box* traceback_tb_next(Box* self, void*) {
return traceback->tb_next;
}
extern "C" int _Py_DisplaySourceLine(PyObject* f, const char* filename, int lineno, int indent) noexcept {
RELEASE_ASSERT(0, "Not implemented.");
}
void setupTraceback() {
traceback_cls = BoxedClass::create(type_cls, object_cls, BoxedTraceback::gcHandler, 0, 0, sizeof(BoxedTraceback),
false, "traceback");
......
......@@ -77,6 +77,7 @@ extern "C" void initzlib();
extern "C" void init_codecs();
extern "C" void init_socket();
extern "C" void _PyUnicode_Init();
extern "C" void _PyWarnings_Init() noexcept;
extern "C" void _string_init();
extern "C" void initunicodedata();
extern "C" void init_weakref();
......@@ -3937,6 +3938,7 @@ void setupRuntime() {
setupClassobj();
setupSuper();
_PyUnicode_Init();
_PyWarnings_Init();
_string_init();
setupDescr();
setupTraceback();
......
import warnings
import _warnings
# Specifying this as a DeprecationWarning is a hacky way of supressing the warning,
# since we don't output the exact same error message as CPython right now:
warnings.warn("hello world", DeprecationWarning)
warnings.filterwarnings('error')
try:
warnings.warn("hello world", Warning)
except Warning as w:
print(w.args[0])
try:
_warnings.warn("deperecated", Warning)
except Warning as w:
print(w.args[0])
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