fix for long standing problem with blender 2.5x py api.

Removing data then accessing would allow invalid memory access and often crash.

Example:
  import bpy
  image = bpy.data.images.new(name="a", width=5, height=5)
  bpy.data.images.remove(image)
  print(image.name)

Now access to the removed data raises an error:
  ReferenceError: StructRNA of type Image has been removed

This is the same level of error checking that was done in blender 2.4x but was made difficult by RNA functions not having access to the PyObject's.
This commit is contained in:
2012-11-01 15:56:42 +00:00
parent 818e9ff88d
commit f4e5404e4a
5 changed files with 57 additions and 17 deletions

View File

@@ -121,8 +121,7 @@ int pyrna_prop_validity_check(BPy_PropertyRNA *self)
void pyrna_invalidate(BPy_DummyPointerRNA *self)
{
self->ptr.type = NULL; /* this is checked for validity */
self->ptr.id.data = NULL; /* should not be needed but prevent bad pointer access, just in case */
RNA_POINTER_INVALIDATE(&self->ptr);
}
#ifdef USE_PYRNA_INVALIDATE_GC
@@ -1776,10 +1775,21 @@ static int pyrna_py_to_prop(PointerRNA *ptr, PropertyRNA *prop, void *data, PyOb
if (data) {
if (flag & PROP_RNAPTR) {
if (value == Py_None)
memset(data, 0, sizeof(PointerRNA));
else
*((PointerRNA *)data) = param->ptr;
if (flag & PROP_THICK_WRAP) {
if (value == Py_None)
memset(data, 0, sizeof(PointerRNA));
else
*((PointerRNA *)data) = param->ptr;
}
else {
/* for function calls, we sometimes want to pass the 'ptr' directly,
* watch out that it remains valid!, possibly we could support this later if needed */
BLI_assert(value_new == NULL);
if (value == Py_None)
*((void **)data) = NULL;
else
*((PointerRNA **)data) = &param->ptr;
}
}
else if (value == Py_None) {
*((void **)data) = NULL;