]> gitweb.factorcode.org Git - factor.git/commitdiff
python.throwing: new module to support capturing python tracebacks
authorBjörn Lindqvist <bjourne@gmail.com>
Fri, 26 Sep 2014 18:04:33 +0000 (20:04 +0200)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 2 Oct 2014 15:32:39 +0000 (08:32 -0700)
extra/python/errors/errors.factor
extra/python/throwing/throwing.factor [new file with mode: 0644]

index c60b155ccb639d0cedfade983a62fcb66cad02d1..6325929f6f6d83cdd0a8f1309cd8f9dc06420a18 100644 (file)
@@ -1,16 +1,14 @@
-USING: alien.c-types alien.data kernel python.ffi ;
+USING: alien.c-types alien.data kernel python.ffi vocabs.loader words ;
 IN: python.errors
 
-ERROR: python-error type message ;
-
 <PRIVATE
 
-: get-error ( -- ptype pvalue )
-    { void* void* void* } [ PyErr_Fetch ] with-out-parameters drop ;
+: get-error ( -- ptype pvalue ptraceback )
+    { void* void* void* } [ PyErr_Fetch ] with-out-parameters ;
 
-: throw-error ( ptype pvalue -- )
-    [ "__name__" PyObject_GetAttrString ] [ PyObject_Str ] bi*
-    [ &Py_DecRef PyString_AsString ] bi@ python-error ;
+! Breaking out of a circular dependency.
+: throw-error ( ptype pvalue ptraceback -- )
+    "throw-error" "python.throwing" lookup-word execute( a b c -- ) ;
 
 PRIVATE>
 
diff --git a/extra/python/throwing/throwing.factor b/extra/python/throwing/throwing.factor
new file mode 100644 (file)
index 0000000..f18aedf
--- /dev/null
@@ -0,0 +1,14 @@
+USING: arrays kernel python python.syntax sequences ;
+IN: python.throwing
+
+ERROR: python-error type message traceback ;
+
+PY-FROM: traceback => format_tb ( tb -- seq ) ;
+
+PY-METHODS: obj =>
+    __name__ ( o -- str )
+    __str__ ( o -- str ) ;
+
+: throw-error ( ptype pvalue ptraceback -- )
+    [ $__name__ py> ] [ __str__ py> ] [ [ format_tb py> ] [ f ] if* ] tri*
+    python-error ;