C-API proper initialization and deallocation of subclasses

P

ptb

Hello all,

I decided to play around with the C-API and have gotten stuck. I went through the Shoddy example (https://docs.python.org/3/extending/newtypes.html#subclassing-other-types) in the docs and tried to extend it by adding a method which creates and returns a shoddy instance. I dug around to find ways to allocate and initialize my shoddy instance and that seems to work well. However, I get segfaults when I try to delete my instance. The code is inthe gist: https://gist.github.com/pbrady/f2daf50761e458bbe44a

The magic happens in the make_a_shoddy function.

Here's a sample session (Python 3.4.1)
tup build
shd allocated
list style allocation successful
Py_SIZE(list) : 5
Py_SIZE(shoddy) : 5
type(shd)
shd[:] [1, 2, 3, 4, 5]
shd.increment() 1
shd.increment() 2
del shd
Segmentation fault (core dumped)

This happens even if I don't set the destructor. Any ideas on what I am doing wrong?

Thanks,
Peter.
 
P

ptb

While there doesn't appear to be too much interest in this question I thought I would post the solution. I had to modify shoddy by adding the proper flags and clear/traverse methods such to ensure that cyclic garbage collection was properly handled. I'm not quite sure why I had to do this since myshoddy instance did not have any cyclic references but I'm wondering if itwas necessary since it's a subclass of list which has all the cyclic GC machinery in place. I suspect I won't understand the answer but if someone with more knowledge can clear things up, that would great.

Hello all,



I decided to play around with the C-API and have gotten stuck. I went through the Shoddy example (https://docs.python.org/3/extending/newtypes.html#subclassing-other-types) in the docs and tried to extend it by adding a method which creates and returns a shoddy instance. I dug around to find ways to allocate and initialize my shoddy instance and that seems to work well.. However, I get segfaults when I try to delete my instance. The code is in the gist: https://gist.github.com/pbrady/f2daf50761e458bbe44a



The magic happens in the make_a_shoddy function.



Here's a sample session (Python 3.4.1)



tup build

shd allocated

list style allocation successful

Py_SIZE(list) : 5

Py_SIZE(shoddy) : 5

[1, 2, 3, 4, 5]

Segmentation fault (core dumped)



This happens even if I don't set the destructor. Any ideas on what I am doing wrong?



Thanks,

Peter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,056
Messages
2,570,443
Members
47,091
Latest member
IsaacLuna

Latest Threads

Top