[cython/cython] [BUG] Seg-fault calling class method accessing object member on class instance (Issue #6207)

0 views
Skip to first unread message

Oleksandr Pavlyk

unread,
May 21, 2024, 7:33:35 AM5/21/24
to cython/cython, Subscribed

Describe the bug

In [1]: import a

In [2]: inst = a.A("instance")

In [3]: inst.__value__()
Out[3]: 'instance'

In [4]: type(inst).__value__(type(inst))
Segmentation fault (core dumped)
Backtrace:
>>> import a
>>> inst = a.A('instance')
>>> type(inst).__value__(type(inst))

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7ba9ce5 in _Py_INCREF (op=0x0) at /localdisk/work/opavlyk/miniconda3/envs/dev_dpctl/include/python3.10/object.h:472
472         op->ob_refcnt++;
(gdb) bt
#0  0x00007ffff7ba9ce5 in _Py_INCREF (op=0x0) at /localdisk/work/opavlyk/miniconda3/envs/dev_dpctl/include/python3.10/object.h:472
#1  0x00007ffff7baa8cf in __pyx_pf_1a_1A_2__value__ (__pyx_v_self=0x7ffff7bb9460 <__pyx_type_1a_A>) at a.cpp:2817
#2  0x00007ffff7baa771 in __pyx_pw_1a_1A_3__value__ (__pyx_v_self=0x7ffff7bb9460 <__pyx_type_1a_A>, __pyx_args=0x7ffff75be0b8, __pyx_nargs=0, __pyx_kwds=0x0) at a.cpp:2792
#3  0x00007ffff7bb23df in __Pyx_CyFunction_Vectorcall_FASTCALL_KEYWORDS (func=0x7ffff7573d30, args=0x7ffff75be0b8, nargsf=9223372036854775809, kwnames=0x0) at a.cpp:6511

Code to reproduce the behaviour:

# a.pxd
cdef class A:
    cdef object v
# a.pyx
# distutils: language = c++
# cython: language_level=3
# cython: linetrace=True

cdef class A:
    """Dummpy class to reproduce crash"""
    def __cinit__(self, val=None):
        self.v = val

    def __value__(self):
        "Get the value"
        return self.v

Compiling:

cython --cplus -3 a.pyx
gcc --shared -fPIC a.cpp $(python3-config --includes) -o a.so

Expected behaviour

Ideally, the behavior should be consistent with that of C-Python:

In [6]: type(inst).value()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[6], line 1
----> 1 type(inst).value()

TypeError: A.value() missing 1 required positional argument: 'self'

OS

Linux

Python version

3.10, 3.12

Cython version

3.0.10

Additional context

No response


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <cython/cython/issues/6207@github.com>

da-woods

unread,
May 21, 2024, 12:38:53 PM5/21/24
to cython/cython, Subscribed

Closed #6207 as completed.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <cython/cython/issue/6207/issue_event/12882762929@github.com>

da-woods

unread,
May 21, 2024, 12:38:54 PM5/21/24
to cython/cython, Subscribed

I think this is a duplicate of #6127 - essentially you're passing it a type when it expects an A.

We historically didn't check the type of the self argument because Python did it for us. Since we've moved to using our own function class (for better introspection) we no longer get that check. So we probably should add it back.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <cython/cython/issues/6207/2123023042@github.com>

Reply all
Reply to author
Forward
0 new messages