Passing bytes instead strings in PyCall.jl


#1

With Python 3 as a backend, what’s the solution to this error?

julia> writer("a")
ERROR: PyError (ccall(@pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, arg, C_NULL)) <class 'TypeError'>
TypeError('expected bytes, str found',)

I also tried

julia> writer(Vector{UInt8}("a"))
ERROR: PyError (ccall(@pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, arg, C_NULL)) <class 'TypeError'>
TypeError('expected bytes, bytearray found',)

#2

Try defining:

pybytes(b) = PyObject(PyCall.@pycheckn 
    ccall(PyCall.@pysym(PyCall.PyString_FromStringAndSize),
                                             PyPtr, (Ptr{UInt8}, Int),
                                             b, sizeof(b)))

and then calling writer(pybytes("a")) to force it to create a bytes object.

In PyCall master, this function is available as PyCall.pybytes


#3

Thanks!