Skip to content

Commit

Permalink
[add] simple-pyerror containing the python error type
Browse files Browse the repository at this point in the history
  • Loading branch information
digikar99 committed Jul 26, 2024
1 parent 8795e11 commit f741771
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ See [this publication](https://zenodo.org/records/10997435) for the broad design
- [pyversion-info](#pyversion-info)
- [raw-pyeval](#raw-pyeval)
- [raw-pyexec](#raw-pyexec)
- [simple-pyerror](#simple-pyerror)
- [with-lispifiers](#with-lispifiers)
- [with-pygc](#with-pygc)
- [with-python-error-output](#with-python-error-output)
Expand Down Expand Up @@ -910,6 +911,7 @@ python callable, which is then retrieved using PYVALUE*
Condition
```

A lisp error to indicate all kinds of python error.

### pyeval

Expand Down Expand Up @@ -1119,6 +1121,21 @@ Instead, use [pycall](#pycall), [pyvalue](#pyvalue), (SETF [pyvalue](#pyvalue)),

RAW-PY, [raw-pyeval](#raw-pyeval), `raw-pyexec` are only provided for backward compatibility.

### simple-pyerror

```lisp
Condition
```

A specialization of PYERROR to hold the python error type.

<u>**Direct Slots**</u>

**type**
```lisp
Initargs: :TYPE
```

### with-lispifiers

```lisp
Expand Down
1 change: 1 addition & 0 deletions src/package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#:raw-pyexec
#:pyexec
#:pyerror
#:simple-pyerror
#:pyvalue

#:pythonize
Expand Down
15 changes: 12 additions & 3 deletions src/python-process.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,12 @@ py4cl_utils = ctypes.pydll.LoadLibrary(\"~A\")
(:report (lambda (condition stream)
(with-slots (format-control format-arguments)
condition
(apply #'format stream format-control format-arguments)))))
(apply #'format stream format-control format-arguments))))
(:documentation "A lisp error to indicate all kinds of python error."))

(define-condition simple-pyerror (pyerror)
((type :initarg :type))
(:documentation "A specialization of PYERROR to hold the python error type."))

(defun python-error-fetch ()
"Fetches the python error, already assuming that it has occurred."
Expand All @@ -324,6 +329,8 @@ py4cl_utils = ctypes.pydll.LoadLibrary(\"~A\")
(let* ((type (mem-aref ptype :pointer))
(value (mem-aref pvalue :pointer))
(traceback (mem-aref ptraceback :pointer))
(type-str (foreign-string-to-lisp
(pytypeobject-name type)))
(value-str
(foreign-string-to-lisp
(pyforeign-funcall "PyUnicode_AsUTF8"
Expand All @@ -341,7 +348,8 @@ py4cl_utils = ctypes.pydll.LoadLibrary(\"~A\")
(pycall "traceback.format_exception" type value traceback))))))
(with-simple-restart (continue-ignoring-errors "")
(cond ((string= "" value-str)
(error 'pyerror
(error 'simple-pyerror
:type type-str
:format-control "A python error occurred:~% ~A"
:format-arguments
(list (etypecase traceback-str
Expand All @@ -350,7 +358,8 @@ py4cl_utils = ctypes.pydll.LoadLibrary(\"~A\")
(coerce traceback-str 'list)))
(list (apply #'uiop:strcat traceback-str))))))
(t
(error 'pyerror
(error 'simple-pyerror
:type type-str
:format-control "A python error occurred:~% ~A~%~%~A"
:format-arguments
(list value-str
Expand Down

0 comments on commit f741771

Please sign in to comment.