From f7417717c9c00c83f73d850a6328dc2b23961299 Mon Sep 17 00:00:00 2001 From: digikar Date: Fri, 26 Jul 2024 23:56:34 +0530 Subject: [PATCH] [add] simple-pyerror containing the python error type --- README.md | 17 +++++++++++++++++ src/package.lisp | 1 + src/python-process.lisp | 15 ++++++++++++--- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 14ec0f6..50e37a2 100644 --- a/README.md +++ b/README.md @@ -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) @@ -910,6 +911,7 @@ python callable, which is then retrieved using PYVALUE* Condition ``` +A lisp error to indicate all kinds of python error. ### pyeval @@ -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. + +**Direct Slots** + +**type** +```lisp +Initargs: :TYPE +``` + ### with-lispifiers ```lisp diff --git a/src/package.lisp b/src/package.lisp index 4e343a2..e9a1309 100644 --- a/src/package.lisp +++ b/src/package.lisp @@ -23,6 +23,7 @@ #:raw-pyexec #:pyexec #:pyerror + #:simple-pyerror #:pyvalue #:pythonize diff --git a/src/python-process.lisp b/src/python-process.lisp index d188a84..9216579 100644 --- a/src/python-process.lisp +++ b/src/python-process.lisp @@ -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." @@ -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" @@ -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 @@ -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