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

[BUG] casting PyObject * is broken in pure python mode #4243

Closed
matusvalo opened this issue Jun 23, 2021 · 5 comments · Fixed by #4248
Closed

[BUG] casting PyObject * is broken in pure python mode #4243

matusvalo opened this issue Jun 23, 2021 · 5 comments · Fixed by #4248

Comments

@matusvalo
Copy link
Contributor

Describe the bug
For some cases casting PyObject * is failing during compilation with error: Cannot convert 'PyObject *' to Python object

To Reproduce
Following code is compiling with no error:

from cpython.ref cimport PyObject


foo = {"abc": 123}

cdef PyObject *bar = <PyObject *> foo

But in pure python version is failing:

import cython
from cython.cimports.cpython.ref import PyObject

foo = {"abc": 123}

bar: cython.pointer(PyObject) = cython.cast(cython.pointer(PyObject), foo)

Expected behavior

Pure python mode should reflect cython language.

Environment (please complete the following information):

  • OS: Linux
  • Python version: 3.9.2
  • Cython version: master branch
@scoder
Copy link
Contributor

scoder commented Jun 23, 2021 via email

@matusvalo
Copy link
Contributor Author

Hmm, yes it seems working. I did mistake when I was generelizing bug but it seems that following code snippet fails:

import cython
from cython.cimports.cpython.ref import PyObject

@cython.cfunc
def foo(obj: cython.pointer(PyObject)):
    pass

def main():
    python_dict = {"abc": 123}
    foo(cython.cast(cython.pointer(PyObject), python_dict))

compiling fails with following error:

$ cython -3a test.py

Error compiling Cython file:
------------------------------------------------------------
...
def foo(obj: cython.pointer(PyObject)):
    pass

def main():
    python_dict = {"abc": 123}
    foo(cython.cast(cython.pointer(PyObject), python_dict))
             ^
------------------------------------------------------------

test.py:12:14: Cannot convert 'PyObject *' to Python object

@matusvalo
Copy link
Contributor Author

@scoder can you confirm the case? If yes I will close this issue and create new one with new snippet.

@da-woods
Copy link
Contributor

The module globals issue is: #3944 (the issue that the the annotations on globals are ignored).

Your function-based version looks like a genuine bug (I think). I don't think it's necessary to create a new issue for it though

da-woods added a commit to da-woods/cython that referenced this issue Jun 24, 2021
Fixes cython#4243 (and probably a bunch more annotation bugs)

AnnotationNode doesn't have any child_attrs/subexprs because
it mostly doesn't want to be analyzed (it's just transformed to
an evaluable string). However this means that InterpretCompilerDirectives
wasn't visiting its children to set is_cython_module and so the
typing was failing. I've special-cased InterpretCompilerDirectives
to visit AnnotationNode fully.
da-woods added a commit to da-woods/cython that referenced this issue Jun 24, 2021
Fixes cython#4243 (and probably a bunch more annotation bugs)

AnnotationNode doesn't have any child_attrs/subexprs because
it mostly doesn't want to be analyzed (it's just transformed to
an evaluable string). However this means that InterpretCompilerDirectives
wasn't visiting its children to set is_cython_module and so the
typing was failing. I've special-cased InterpretCompilerDirectives
to visit AnnotationNode fully.
@matusvalo
Copy link
Contributor Author

matusvalo commented Jun 24, 2021

Ok closing this bug. Thank you @da-woods !

scoder pushed a commit that referenced this issue Jun 28, 2021
Fixes #4243
(and probably a bunch more annotation bugs)

AnnotationNode doesn't have any child_attrs/subexprs because it mostly doesn't want to be analyzed (it's just transformed to an evaluable string). However this means that InterpretCompilerDirectives wasn't visiting its children to set is_cython_module and so the typing was failing. I've special-cased InterpretCompilerDirectives to visit AnnotationNode fully.
@scoder scoder added this to the 3.0 milestone Jun 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants