factor/extra/python/errors/errors.factor

31 lines
714 B
Factor
Raw Normal View History

USING: alien.c-types alien.data kernel python.ffi ;
IN: python.errors
ERROR: python-error type message ;
<PRIVATE
: get-error ( -- ptype pvalue )
{ void* void* void* } [ PyErr_Fetch ] with-out-parameters drop ;
: throw-error ( ptype pvalue -- )
[ "__name__" PyObject_GetAttrString ] [ PyObject_Str ] bi*
[ &Py_DecRef PyString_AsString ] bi@ python-error ;
PRIVATE>
2014-03-04 14:06:55 -05:00
: (check-ref) ( ref -- ref )
[ get-error throw-error f ] unless* ;
2014-03-04 14:06:55 -05:00
: check-new-ref ( ref -- ref )
&Py_DecRef (check-ref) ;
2014-03-04 14:06:55 -05:00
: check-borrowed-ref ( ref -- ref )
dup Py_IncRef &Py_DecRef (check-ref) ;
: check-zero ( code -- )
0 = [ get-error throw-error ] unless ;
2014-03-04 14:06:55 -05:00
: unsteal-ref ( ref -- ref )
dup Py_IncRef ;