Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't convert from PyPtr to related type #1043

Open
hiiroo opened this issue Jul 1, 2023 · 2 comments
Open

Can't convert from PyPtr to related type #1043

hiiroo opened this issue Jul 1, 2023 · 2 comments

Comments

@hiiroo
Copy link

hiiroo commented Jul 1, 2023

Hello everyone and thanks for the great library. I was trying to use rdflib of Python in PyCall.jl. I'm using macos, Julia 1.9.1 and using ENV["Python"]="" with Conda.jl. With this code,

using PyCall

# Create an empty graph
graph = pyimport("rdflib").Graph()

# Define the namespace
ex = "http://example.org/"

# Define the triples
subject = pyimport("rdflib").URIRef(ex * "subject")
predicate = pyimport("rdflib").URIRef(ex * "predicate")
object = pyimport("rdflib").Literal("object")

# Add the triples to the graph
graph.add((subject, predicate, object))

I got the following output

ERROR: PyError ($(Expr(:escape, :(ccall(#= /Users/hiiroo/.julia/packages/PyCall/ilqDX/src/pyfncall.jl:43 =# @pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, pyargsptr, kw))))) <class 'AssertionError'>
AssertionError('Subject http://example.org/subject must be an rdflib term')
  File "/Users/hiiroo/.julia/conda/3/lib/python3.7/site-packages/rdflib/graph.py", line 532, in add
    assert isinstance(s, Node), "Subject %s must be an rdflib term" % (s,)
@hiiroo
Copy link
Author

hiiroo commented Jul 1, 2023

Additional details;

When the following code run it returns a string "http://example.org/subject1"

s1 = rdflib.URIRef("http://example.org/subject1")

However, when it run in python it returns rdflib.term.URIRef('http://example.org/subject1')

subject = rdflib.URIRef(ex + "subject1")

@stevengj
Copy link
Member

stevengj commented Jul 3, 2023

You can suppress PyCall's automatic conversion with:

pycall(rdflib.URIRef, PyObject, "http://example.org/subject1")

which tells it to return a raw PyObject rather than trying to convert to a native Julia object (which I guess happens here because rdflib.term.URIRef is a subtype of string).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants