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

cppclass: `del this` -> "local variable 'this' referenced before assignment" #3233

Open
navytux opened this issue Nov 13, 2019 · 0 comments
Open

Comments

@navytux
Copy link
Contributor

@navytux navytux commented Nov 13, 2019

Hello up there. Please consider the following minimal example:

# cython: language_level=2
# distutils: language=c++

cdef cppclass PyValue nogil:

    void decref():
        #cdef PyValue *x = this
        #del x
        del this

When I try to compile it - it complains:

Error compiling Cython file:
------------------------------------------------------------
...
cdef cppclass PyValue nogil:

    void decref():
        #cdef PyValue *x = this
        #del x
        del this
           ^
------------------------------------------------------------

delthis.pyx:9:12: local variable 'this' referenced before assignment

Error compiling Cython file:
------------------------------------------------------------
...
# cython: language_level=2
# distutils: language=c++

cdef cppclass PyValue nogil:

    void decref():
   ^
------------------------------------------------------------

delthis.pyx:6:4: Function declared nogil has Python locals or temporaries

Error compiling Cython file:
------------------------------------------------------------
...
cdef cppclass PyValue nogil:

    void decref():
        #cdef PyValue *x = this
        #del x
        del this
       ^
------------------------------------------------------------

delthis.pyx:9:8: Deleting Python object not allowed without gil

However if I change del this to cdef PyValue *x = this + del x - it compiles just fine and produces expected C++ code:

/* "delthis.pyx":6
 * cdef cppclass PyValue nogil:
 * 
 *     void decref():             # <<<<<<<<<<<<<<
 *         cdef PyValue *x = this
 *         del x
 */

void __pyx_t_7delthis_PyValue::decref(void) {
  __pyx_t_7delthis_PyValue *__pyx_v_x;

  /* "delthis.pyx":7
 * 
 *     void decref():
 *         cdef PyValue *x = this             # <<<<<<<<<<<<<<
 *         del x
 *         #del this
 */
  __pyx_v_x = this;

  /* "delthis.pyx":8
 *     void decref():
 *         cdef PyValue *x = this
 *         del x             # <<<<<<<<<<<<<<
 *         #del this
 */
  delete __pyx_v_x;

  /* "delthis.pyx":6
 * cdef cppclass PyValue nogil:
 * 
 *     void decref():             # <<<<<<<<<<<<<<
 *         cdef PyValue *x = this
 *         del x
 */

  /* function exit code */
}

Cython version: 0.29.14-591-gaf997e97e (today's master)

Thanks beforehand,
Kirill

/cc @robertwb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.