[Bf-committers] Python and Linked Lists

Campbell Barton cbarton at metavr.com
Mon Jul 9 18:13:57 CEST 2007


While trying to stop ANY python API crashes with the pyapi_devel branch 
I ran into this problem.

* Iterating on linked lists cannot guarantee that Blender wont remove 
the data python is looping on.

- For example, a script with a user interface could loop through 
objects, one step cycle per redraw - And its possible the user could 
remove the Object that the script is using at that moment. (freeing the 
Base). Crashing the script when it tries to access base->next
(I tested this in the console and it does crash 2.44 & trunk)

The simple solution is to store the current 'iter' as an index in the 
PyObject-
   "The 20th object of the scene", This is fast enough for arrays 
(verts, faces etc) But not so nice for linked lists since the previous 
items need looping on internally to get the 20th item, or 1000th item... 
however many objects there are. - which would make a simple loop through 
every item slow - especially with larger linkedLists


What I would like to do is to have Blender call a functon before 
removing an item from some linked lists (such as scene objects) to 
notify blender/python api that the Base is being removed and 
Blender/Python api can make sure no bad pointers happen as a result of this.

This would not be that slow, and in most cases scripts dont loop on 
objects at the same time they are using blender, but because its 
possible, Id like it to be accounted for.

the checks could work like this..

* call bpython listbase remove function when a base is removed from a scene.
** does this scene have a PyObject? (if yes...)
*** does this Scene PyObject have any iterators? - meaning are any 
scripts currently looping on objects. (if yes)
**** Are any of the scene.objects iterators currently on "Base to 
remove" (if yes...)
***** Set this iterator to an error value that will raise a python error 
  next cycle (rather then crashing)

Python API would deal with all this internally. I could add a generic 
function for linkedList removal with a LINKED_LIST and TYPE argument so 
as to check against the right Python Data.

-- 
Campbell J Barton (ideasman42)


More information about the Bf-committers mailing list