[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [35119] trunk/blender/source/blender/ python/generic: revert part of own commit r35117 which modified mathutils initialization functions , found this could be done in a better way which doesnt have to deal with partly initialize instances being freed .

Campbell Barton ideasman42 at gmail.com
Thu Feb 24 06:46:58 CET 2011


Revision: 35119
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=35119
Author:   campbellbarton
Date:     2011-02-24 05:46:57 +0000 (Thu, 24 Feb 2011)
Log Message:
-----------
revert part of own commit r35117 which modified mathutils initialization functions, found this could be done in a better way which doesnt have to deal with partly initialize instances being freed.

Revision Links:
--------------
    http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=35117

Modified Paths:
--------------
    trunk/blender/source/blender/python/generic/mathutils.c
    trunk/blender/source/blender/python/generic/mathutils.h
    trunk/blender/source/blender/python/generic/mathutils_Color.c
    trunk/blender/source/blender/python/generic/mathutils_Euler.c
    trunk/blender/source/blender/python/generic/mathutils_Matrix.c
    trunk/blender/source/blender/python/generic/mathutils_Quaternion.c
    trunk/blender/source/blender/python/generic/mathutils_Vector.c
    trunk/blender/source/blender/python/generic/mathutils_Vector.h

Modified: trunk/blender/source/blender/python/generic/mathutils.c
===================================================================
--- trunk/blender/source/blender/python/generic/mathutils.c	2011-02-24 05:41:03 UTC (rev 35118)
+++ trunk/blender/source/blender/python/generic/mathutils.c	2011-02-24 05:46:57 UTC (rev 35119)
@@ -341,10 +341,7 @@
 {
 	/* only free non wrapped */
 	if(self->wrapped != Py_WRAP) {
-		/* the ONLY time this should be NULL is when the value failed to initialize */
-		if(self->data) {
-			PyMem_Free(self->data);
-		}
+		PyMem_Free(self->data);
 	}
 
 	BaseMathObject_clear(self);

Modified: trunk/blender/source/blender/python/generic/mathutils.h
===================================================================
--- trunk/blender/source/blender/python/generic/mathutils.h	2011-02-24 05:41:03 UTC (rev 35118)
+++ trunk/blender/source/blender/python/generic/mathutils.h	2011-02-24 05:46:57 UTC (rev 35119)
@@ -58,7 +58,6 @@
 PyObject *BaseMathObject_getOwner( BaseMathObject * self, void * );
 PyObject *BaseMathObject_getWrapped( BaseMathObject *self, void * );
 
-
 int BaseMathObject_traverse(BaseMathObject *self, visitproc visit, void *arg);
 int BaseMathObject_clear(BaseMathObject *self);
 void BaseMathObject_dealloc(BaseMathObject * self);

Modified: trunk/blender/source/blender/python/generic/mathutils_Color.c
===================================================================
--- trunk/blender/source/blender/python/generic/mathutils_Color.c	2011-02-24 05:41:03 UTC (rev 35118)
+++ trunk/blender/source/blender/python/generic/mathutils_Color.c	2011-02-24 05:46:57 UTC (rev 35119)
@@ -509,31 +509,6 @@
  (i.e. it was allocated elsewhere by MEM_mallocN())
   pass Py_NEW - if vector is not a WRAPPER and managed by PYTHON
  (i.e. it must be created here with PyMEM_malloc())*/
-static int newColorObject_init(ColorObject *self, float *col, int type)
-{
-	if(type == Py_WRAP) {
-		self->col = col;
-		self->wrapped = Py_WRAP;
-	}
-	else if (type == Py_NEW) {
-		self->col = PyMem_Malloc(COLOR_SIZE * sizeof(float));
-		if(col) {
-			copy_v3_v3(self->col, col);
-		}
-		else {
-			zero_v3(self->col);
-		}
-
-		self->wrapped = Py_NEW;
-	}
-	else {
-		return -1;
-	}
-
-	return 0;
-}
-
-
 PyObject *newColorObject(float *col, int type, PyTypeObject *base_type)
 {
 	ColorObject *self;
@@ -541,14 +516,28 @@
 	self= base_type ?	(ColorObject *)base_type->tp_alloc(base_type, 0) :
 						(ColorObject *)PyObject_GC_New(ColorObject, &color_Type);
 
-	/* init callbacks as NULL */
-	self->cb_user= NULL;
-	self->cb_type= self->cb_subtype= 0;
-	((BaseMathObject *)self)->data= NULL; /* incase of error */
+	if(self) {
+		/* init callbacks as NULL */
+		self->cb_user= NULL;
+		self->cb_type= self->cb_subtype= 0;
 
-	if(newColorObject_init(self, col, type) == -1) {
-		Py_DECREF(self);
-		return NULL;
+		if(type == Py_WRAP){
+			self->col = col;
+			self->wrapped = Py_WRAP;
+		}
+		else if (type == Py_NEW){
+			self->col = PyMem_Malloc(COLOR_SIZE * sizeof(float));
+			if(col)
+				copy_v3_v3(self->col, col);
+			else
+				zero_v3(self->col);
+
+			self->wrapped = Py_NEW;
+		}
+		else {
+			PyErr_SetString(PyExc_RuntimeError, "Color(): invalid type");
+			return NULL;
+		}
 	}
 
 	return (PyObject *)self;
@@ -556,19 +545,12 @@
 
 PyObject *newColorObject_cb(PyObject *cb_user, int cb_type, int cb_subtype)
 {
-	ColorObject *self;
-
-	self= (ColorObject *)PyObject_GC_New(ColorObject, &color_Type);
-
-	Py_INCREF(cb_user);
-	self->cb_user=			cb_user;
-	self->cb_type=			(unsigned char)cb_type;
-	self->cb_subtype=		(unsigned char)cb_subtype;
-	((BaseMathObject *)self)->data= NULL; /* incase of error */
-
-	if(newColorObject_init(self, NULL, Py_NEW) == -1) {
-		Py_DECREF(self);
-		return NULL;
+	ColorObject *self= (ColorObject *)newColorObject(NULL, Py_NEW, NULL);
+	if(self) {
+		Py_INCREF(cb_user);
+		self->cb_user=			cb_user;
+		self->cb_type=			(unsigned char)cb_type;
+		self->cb_subtype=		(unsigned char)cb_subtype;
 	}
 
 	return (PyObject *)self;

Modified: trunk/blender/source/blender/python/generic/mathutils_Euler.c
===================================================================
--- trunk/blender/source/blender/python/generic/mathutils_Euler.c	2011-02-24 05:41:03 UTC (rev 35118)
+++ trunk/blender/source/blender/python/generic/mathutils_Euler.c	2011-02-24 05:46:57 UTC (rev 35119)
@@ -642,32 +642,6 @@
  (i.e. it was allocated elsewhere by MEM_mallocN())
   pass Py_NEW - if vector is not a WRAPPER and managed by PYTHON
  (i.e. it must be created here with PyMEM_malloc())*/
-static int newEulerObject_init(EulerObject *self, float *eul, short order, int type)
-{
-	if(type == Py_WRAP) {
-		self->eul = eul;
-		self->wrapped = Py_WRAP;
-	}
-	else if (type == Py_NEW){
-		self->eul = PyMem_Malloc(EULER_SIZE * sizeof(float));
-		if(eul) {
-			copy_v3_v3(self->eul, eul);
-		}
-		else {
-			zero_v3(self->eul);
-		}
-		self->wrapped = Py_NEW;
-	}
-	else{
-		PyErr_SetString(PyExc_RuntimeError, "invalid type");
-		return -1;
-	}
-
-	self->order= order;
-
-	return 0;
-}
-
 PyObject *newEulerObject(float *eul, short order, int type, PyTypeObject *base_type)
 {
 	EulerObject *self;
@@ -675,14 +649,32 @@
 	self= base_type ?	(EulerObject *)base_type->tp_alloc(base_type, 0) :
 						(EulerObject *)PyObject_GC_New(EulerObject, &euler_Type);
 
-	/* init callbacks as NULL */
-	self->cb_user= NULL;
-	self->cb_type= self->cb_subtype= 0;
-	((BaseMathObject *)self)->data= NULL; /* incase of error */
+	if(self) {
+		/* init callbacks as NULL */
+		self->cb_user= NULL;
+		self->cb_type= self->cb_subtype= 0;
 
-	if(newEulerObject_init(self, eul, order, type) == -1) {
-		Py_DECREF(self);
-		return NULL;
+		if(type == Py_WRAP) {
+			self->eul = eul;
+			self->wrapped = Py_WRAP;
+		}
+		else if (type == Py_NEW) {
+			self->eul = PyMem_Malloc(EULER_SIZE * sizeof(float));
+			if(eul) {
+				copy_v3_v3(self->eul, eul);
+			}
+			else {
+				zero_v3(self->eul);
+			}
+
+			self->wrapped = Py_NEW;
+		}
+		else {
+			PyErr_SetString(PyExc_RuntimeError, "Euler(): invalid type");
+			return NULL;
+		}
+
+		self->order= order;
 	}
 
 	return (PyObject *)self;
@@ -690,19 +682,12 @@
 
 PyObject *newEulerObject_cb(PyObject *cb_user, short order, int cb_type, int cb_subtype)
 {
-	EulerObject *self;
-
-	self= (EulerObject *)PyObject_GC_New(VectorObject, &vector_Type);
-
-	Py_INCREF(cb_user);
-	self->cb_user=			cb_user;
-	self->cb_type=			(unsigned char)cb_type;
-	self->cb_subtype=		(unsigned char)cb_subtype;
-	((BaseMathObject *)self)->data= NULL; /* incase of error */
-
-	if(newEulerObject_init(self, NULL, order, Py_NEW) == -1) {
-		Py_DECREF(self);
-		return NULL;
+	EulerObject *self= (EulerObject *)newEulerObject(NULL, order, Py_NEW, NULL);
+	if(self) {
+		Py_INCREF(cb_user);
+		self->cb_user=			cb_user;
+		self->cb_type=			(unsigned char)cb_type;
+		self->cb_subtype=		(unsigned char)cb_subtype;
 	}
 
 	return (PyObject *)self;

Modified: trunk/blender/source/blender/python/generic/mathutils_Matrix.c
===================================================================
--- trunk/blender/source/blender/python/generic/mathutils_Matrix.c	2011-02-24 05:41:03 UTC (rev 35118)
+++ trunk/blender/source/blender/python/generic/mathutils_Matrix.c	2011-02-24 05:46:57 UTC (rev 35119)
@@ -1820,96 +1820,76 @@
  (i.e. it was allocated elsewhere by MEM_mallocN())
   pass Py_NEW - if vector is not a WRAPPER and managed by PYTHON
  (i.e. it must be created here with PyMEM_malloc())*/
-static int newMatrixObject_init(MatrixObject *self, float *mat, const unsigned short rowSize, const unsigned short colSize, int type)
+PyObject *newMatrixObject(float *mat, const unsigned short rowSize, const unsigned short colSize, int type, PyTypeObject *base_type)
 {
+	MatrixObject *self;
 	int x, row, col;
 
 	/*matrix objects can be any 2-4row x 2-4col matrix*/
 	if(rowSize < 2 || rowSize > 4 || colSize < 2 || colSize > 4) {
 		PyErr_SetString(PyExc_RuntimeError, "matrix(): row and column sizes must be between 2 and 4");
-		return -1;
+		return NULL;
 	}
 
-	self->row_size = rowSize;
-	self->col_size = colSize;
+	self= base_type ?	(MatrixObject *)base_type->tp_alloc(base_type, 0) :
+						(MatrixObject *)PyObject_GC_New(MatrixObject, &matrix_Type);
 
-	if(type == Py_WRAP){
-		self->contigPtr = mat;
-		/*pointer array points to contigous memory*/
-		for(x = 0; x < rowSize; x++) {
-			self->matrix[x] = self->contigPtr + (x * colSize);
+	if(self) {
+		self->row_size = rowSize;
+		self->col_size = colSize;
+
+		/* init callbacks as NULL */
+		self->cb_user= NULL;
+		self->cb_type= self->cb_subtype= 0;
+
+		if(type == Py_WRAP){
+			self->contigPtr = mat;
+			/*pointer array points to contigous memory*/
+			for(x = 0; x < rowSize; x++) {
+				self->matrix[x] = self->contigPtr + (x * colSize);
+			}
+			self->wrapped = Py_WRAP;
 		}
-		self->wrapped = Py_WRAP;
-	}
-	else if (type == Py_NEW){
-		self->contigPtr = PyMem_Malloc(rowSize * colSize * sizeof(float));
-		if(self->contigPtr == NULL) { /*allocation failure*/
-			PyErr_SetString(PyExc_MemoryError, "matrix(): problem allocating pointer space");
-			return -1;
-		}
-		/*pointer array points to contigous memory*/
-		for(x = 0; x < rowSize; x++) {
-			self->matrix[x] = self->contigPtr + (x * colSize);
-		}
-		/*parse*/
-		if(mat) {	/*if a float array passed*/
-			for(row = 0; row < rowSize; row++) {
-				for(col = 0; col < colSize; col++) {
-					self->matrix[row][col] = mat[(row * colSize) + col];
+		else if (type == Py_NEW){
+			self->contigPtr = PyMem_Malloc(rowSize * colSize * sizeof(float));
+			if(self->contigPtr == NULL) { /*allocation failure*/
+				PyErr_SetString(PyExc_MemoryError, "matrix(): problem allocating pointer space");
+				return NULL;
+			}
+			/*pointer array points to contigous memory*/
+			for(x = 0; x < rowSize; x++) {
+				self->matrix[x] = self->contigPtr + (x * colSize);
+			}
+			/*parse*/
+			if(mat) {	/*if a float array passed*/
+				for(row = 0; row < rowSize; row++) {
+					for(col = 0; col < colSize; col++) {
+						self->matrix[row][col] = mat[(row * colSize) + col];
+					}
 				}
 			}
+			else if (rowSize == colSize ) { /*or if no arguments are passed return identity matrix for square matrices */
+				PyObject *ret_dummy= Matrix_identity(self);
+				Py_DECREF(ret_dummy);
+			}
+			self->wrapped = Py_NEW;
 		}
-		else if (rowSize == colSize ) { /*or if no arguments are passed return identity matrix for square matrices */
-			PyObject *ret_dummy= Matrix_identity(self);
-			Py_DECREF(ret_dummy);
+		else {
+			PyErr_SetString(PyExc_RuntimeError, "Matrix(): invalid type");

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list