call function force by python api

42 views
Skip to first unread message

piaopiao Ran

unread,
Jan 12, 2023, 9:17:22 AM1/12/23
to cython-users
when one funciton defined by "cpdef" in pxd file,  and this function will be called by function pointer.
how to force cython call this function by __Pyx_PyObject_GetAttrStr and __Pyx_PyObject_CallOneArg?

D Woods

unread,
Jan 16, 2023, 1:55:20 PM1/16/23
to cython-users
I'm not quite sure why you'd want to do this, but if you want to force Cython as a Python function you'd do:

py_function = <object>cpdef_function
py_function()

when you cast it to object Cython loses all information about the type. You can't specifically pick __Pyx_PyObject_GetAttrStr or __Pyx_PyObject_CallOneArg though.

piaopiao Ran

unread,
Jan 17, 2023, 1:37:33 AM1/17/23
to cython-users
thanks for your help,the following demo is the example resault in cython coredump.(that's the reason why i want cython to call function by  __Pyx_PyObject_GetAttrStr)

-------------------------------------file test_module.py--------------------------------------------------------------------
import test_class_a
class B(object):
    def do_B(self):
        setattr(B, "A_method_2", test_class_a.A.A_method_2.im_func)
        test_class_a.A.A_method_1.im_func(self)  # cython will coredump in this line
b=B()
b.do_B()

--------------------------------------------------------------------------------------------------------------------------------------------

----------------------------------- file test_class_a.py---------------------------------------------------------------------------
class A(object):
    def A_method_1(self):
        self.A_method_2()
    def A_method_2(self):
        pass

------------------------------------------------------------------------------------------------------------------------------------------

---------------------------------- file test_class_a.pxd-------------------------------------------------------------------------------
cdef class A(object):
    cpdef A_method_2(self)

---------------------------------------------------------------------------------------------------------------------------------------------------

why demo is so obscure ? in our  project, we have too many moudles like class Ahowever , we want them become part of class B(because we dont want class B  moudle's code too long) by method like do_B, so we did like demo.
my purpose is to speed up class A and not to croedump

thanks for your help again

D Woods

unread,
Jan 17, 2023, 3:40:41 AM1/17/23
to cython-users
> test_class_a.A.A_method_1.im_func(self)  # cython will coredump in this line

I don't know know what `im_func` is here. However you look to be trying to call a method of `A` with an instance of `B` so it's not surprising that it crashes.
Reply all
Reply to author
Forward
0 new messages