python - Cython Extension Type inheriting from int cause a MemoryError -
i trying make extension type inheriting int or cython.int. necessary me need able use type index lists/arrays.
here code reproduce bug on python 2.7.9 win32 (i'm running windows 7) cython v0.22 on anaconda:
import cython cimport cython import sys cdef class extendedint(int): #cdef object __weakref__ # explicitly enable weakref extension type, self-destruct when no longer referenced. def __add__(a, b): return a+b # simple test case def main(): total_it = 1000 in xrange(total_it): j in xrange(10000000): extendedint(j) sys.stdout.write("\rgenerating lists of extendedint : %i/%i" % (i, total_it))
what happens if try create lots of extendedint, @ moment python interpreter crash memoryerror. code snippet above, on machine 4 gb memory crashes @ 11th iteration, vary depending on machine specs. tried enable weakref doesn't fix issue.
however, if replace "cdef class extendedint(int):" "class extendedint(int)" (so it's no longer extension type simple python class), issue not happen, there's no memoryerror crash.
thus seems extension types inheriting integers not freed correctly, if enable weakref. bug should fill on tracker or missing something?
is bug of current cython version? not find reference on bugtracker... or missing fix issue?
stefan behnel discovered bug in python 2.7 inside intobject.c:
your extension type automatically inherits "int_free" slot function base type, "else" case in "int_dealloc()" in fact call "int_free()" , append object free list despite not being of type pyint. then, when creating new extendedint instance, python's int-subtype instantiation code ignores free list , instead creates new object.
thus, free list keeps growing until fills memory , process dies. created cpython ticket.
here's link cpython ticket.
here's answer guido van rossum:
the intended solution require int subclasses override tp_free.
note of code written pretty assuming subclasses created using class statements (in regular python module, not cython) take care of these details. doesn't mean cython wrong try this, mean there isn't lot of documentation, , means don't think thing reported qualifies bug in cpython.
so i'm not sure if fixed or not or if cython implement workaround. anyway, facing same issue did, can workaround problem inheriting object , reimplement magic methods integers, int, index, add, etc explained a concrete example here.
Comments
Post a Comment