Skip to content

Commit

Permalink
[int][change] remove type slot from pyobject-wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
digikar99 committed Apr 13, 2024
1 parent 48f1cc9 commit 00b8796
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 48 deletions.
12 changes: 12 additions & 0 deletions src/lisp-utils.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,15 @@

(defun mkfifo (path)
(cffi:foreign-funcall "mkfifo" :string path :int #o0664))

(defmacro ensure-non-null-pointer (pointer
&key (format-control nil format-control-p)
format-arguments)
(once-only (pointer)
`(if (null-pointer-p ,pointer)
,(if format-control-p
`(error 'pyerror
:format-control ,format-control
:format-arguments ,format-arguments)
`(error 'pyerror))
,pointer)))
6 changes: 1 addition & 5 deletions src/lispifiers.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,7 @@
(zerop (pyforeign-funcall "PyTuple_Size" :pointer pyobject :int)))))
(pyuntrack pyobject)
(pyuntrack pyobject-type)
(make-tracked-pyobject-wrapper
:pointer pyobject
:type (lispify (pyforeign-funcall "PyObject_Str"
:pointer pyobject-type
:pointer))))
(make-tracked-pyobject-wrapper pyobject))
(t
(funcall lispifier pyobject))))))

Expand Down
12 changes: 0 additions & 12 deletions src/python-process.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -355,18 +355,6 @@ from inside PYTHON-MAY-BE-ERROR does not lead to an infinite recursion.")
(python-may-be-error)
(locally ,@body))))

(defmacro ensure-non-null-pointer (pointer
&key (format-control nil format-control-p)
format-arguments)
(once-only (pointer)
`(if (null-pointer-p ,pointer)
,(if format-control-p
`(error 'pyerror
:format-control ,format-control
:format-arguments ,format-arguments)
`(error 'pyerror))
,pointer)))

(defun raw-py (cmd-char &rest code-strings)
"CMD-CHAR should be #\e for eval and #\x for exec.
Expand Down
63 changes: 32 additions & 31 deletions src/pythonizers.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

(defstruct pyobject-wrapper
"A wrapper around a pointer to a python object.
TYPE slot is the python type string
POINTER slot points to the object"
type
LOAD-FORM is used if the pyobject-wrapper is dumped into a compiled lisp file."
pointer
load-form)

(defun make-tracked-pyobject-wrapper (&key type pointer)
(let* ((pyobject-wrapper (make-pyobject-wrapper :type type :pointer pointer)))
(defun make-tracked-pyobject-wrapper (pointer)
(let* ((pyobject-wrapper (make-pyobject-wrapper :pointer pointer)))
(unless (null-pointer-p pointer)
(tg:finalize pyobject-wrapper (finalization-lambda (pointer-address pointer))))
pyobject-wrapper))
Expand Down Expand Up @@ -60,32 +58,35 @@ the same lisp objects which are EQ to each other. Returns NIL in all other cases
(pyobject-wrapper-pointer o2)))

(defmethod print-object ((o pyobject-wrapper) s)
(if *print-pyobject-wrapper-identity*
(print-unreadable-object (o s :type t :identity t)
(with-pygc
(with-slots (type pointer) o
(if *print-pyobject-wrapper*
(progn
(format s ":type ~A~%" type)
(pprint-logical-block (s nil :per-line-prefix " ")
(write-string (lispify (pyforeign-funcall "PyObject_Str"
:pointer pointer
:pointer))
s))
(terpri s))
(format s ":POINTER ~A :TYPE ~A" pointer
(lispify (pyforeign-funcall "PyObject_Str"
:pointer type :pointer)))))))
(with-pygc
(with-slots (type pointer) o
(if *print-pyobject-wrapper*
(write-string (lispify (pyforeign-funcall "PyObject_Str"
:pointer pointer
:pointer))
s)
(format s ":POINTER ~A :TYPE ~A" pointer
(lispify (pyforeign-funcall "PyObject_Str"
:pointer type :pointer))))))))
(with-pygc
(let* ((pointer (pyobject-wrapper-pointer o)))
(flet ((type ()
(let ((may-be-type (pyforeign-funcall "PyObject_Type"
:pointer pointer
:pointer)))
(ensure-non-null-pointer may-be-type)
(lispify
(pyforeign-funcall "PyObject_Str"
:pointer may-be-type
:pointer)))))
(if *print-pyobject-wrapper-identity*
(print-unreadable-object (o s :type t :identity t)
(if *print-pyobject*
(progn
(format s ":type ~A~%" (type))
(pprint-logical-block (s nil :per-line-prefix " ")
(write-string (lispify (pyforeign-funcall "PyObject_Str"
:pointer pointer
:pointer))
s))
(terpri s))
(format s ":POINTER ~A :TYPE ~A" pointer (type))))
(if *print-pyobject*
(write-string (lispify (pyforeign-funcall "PyObject_Str"
:pointer pointer
:pointer))
s)
(format s ":POINTER ~A :TYPE ~A" pointer (type))))))))

(defmethod make-load-form ((o pyobject-wrapper) &optional env)
(with-slots (pointer load-form) o
Expand Down

0 comments on commit 00b8796

Please sign in to comment.