digikar99/py4cl2-cffi

How are file Pointers passed

CL98K opened this issue · 2 comments

CL98K commented

`* (py4cl2-cffi:import-module "pickle" :as "pkl")
T

  • (with-open-file (stream "/mnt/d/Worker/Lisp/test/xx.plk" :element-type '(unsigned-byte 8))
    (py4cl2-cffi:pycall "pkl.load" (sb-sys:fd-stream-fd stream)))

debugger invoked on a PY4CL2-CFFI:PYERROR in thread
#<THREAD tid=53 "main thread" RUNNING {1001128093}>:
A python error occurred:
file must have 'read' and 'readline' attributes

Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.

restarts (invokable by number or by possibly-abbreviated name):
0: [CONTINUE-IGNORING-ERRORS]
1: [ABORT ] Exit debugger, returning to top level.

(PY4CL2-CFFI::PYTHON-MAY-BE-ERROR)
source: (ERROR 'PYERROR :FORMAT-CONTROL "A python error occurred:~% ~A"
:FORMAT-ARGUMENTS (LIST VALUE-STR))`

Currently, there is no native translation for file stream objects. To implement this, one would have to implement a py4cl2-cffi:pythonize method returning a pointer to an appropriate CPython object.

However, you can also write a python function that takes a lisp object, like string, and read the file in python itself. So, something like -

(py4cl2-cffi:raw-pyexec "
def read_python_object(filename):
  with open(filename, 'b') as f:
    return pkl.load(f)
")
CL98K commented

Okay, thank you!