python-c-api reference count -


my python code pass dictionary , lists c this:

if (!pyarg_parsetuple(args, "o!lo!o!o!o!o!", &pylist_type,&list,&los,&pydict_type,&entity_len,&pydict_type,&inlist_len,&pylist_type,&inindex,&pydict_type,&inlist,&pylist_type,&tokens))         return null;  

is ok items this?

int = pyint_as_long(pylist_getitem(pydict_getitem(inlist,pylist_getitem(tokens,pi)),current_index[pi])) 

i need call c function in python 10k times, , after running code 100 times, shows segment fault 11. how can fixed it?

i not suggest it, should error checking both pydict_getitem , pylist_getitem:

pyobject *obj = pylist_getitem(args) if(!obj){     return null } // same pydict_getitem difference // pydict won't set exception,  // need set yourself. 

both *_getitem functions return null when goes wrong. so, if chain these calls without proper error checking you'll run problems if 1 of these returns null.

additionally, both these functions return borrowed reference object:

the advantage of borrowing on owning reference you don’t need take care of disposing of reference on possible paths through code — in other words, borrowed reference don’t run risk of leaking when premature exit taken. the disadvantage of borrowing on owning there subtle situations in seemingly correct code borrowed reference can used after owner borrowed has in fact disposed of it.

(emphasis mine)

so shouldn't worry py_decrefing them. but, careful; if need use reference, store or pass elsewhere should increment it's reference , ownership of it.

finally, i'm not 100% sure effect when pass borrowed reference pydict; couldn't find in documentation. looking @ source pydict_getitem though, can't see decrefing key parameter think there shouldn't issues there.


Comments