JuliaPy / PyCall.jl

Package to call Python functions from the Julia language
MIT License
1.47k stars 187 forks source link

Misalignment of first line in multiline show of PyObject #888

Open olof3 opened 3 years ago

olof3 commented 3 years ago

PyObjects whose pystring method returns a multiline string have their first row misaligned when shown. For example

using PyCall
qiskit = pyimport("qiskit")
qc = qiskit.QuantumCircuit(1)
qc.x(0)
qc.draw()

gives

PyObject      ┌───┐
q_0: ┤ X ├
     └───┘

A fix would be to replace

function show(io::IO, o::PyObject)
    print(io, "PyObject $(pystring(o))")
end

with

function show(io::IO, o::PyObject)
    str = pystring(o)
    sep = contains(str, "\n") ? "\n" : " " # Avoid misaligning the first line for multiline prints
    print(io, "PyObject$sep$str")
end

which gives the following print instead.

PyObject
     ┌───┐
q_0: ┤ X ├
     └───┘
stevengj commented 3 years ago

Can you give an example?

olof3 commented 3 years ago

There was some kind of example in the original post, but I've updated it to make it more clear.