[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [38377] trunk/blender/source/blender/ python/generic/bgl.c: minor changes to bgl.Buffer py class

Campbell Barton ideasman42 at gmail.com
Thu Jul 14 01:24:21 CEST 2011


Revision: 38377
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=38377
Author:   campbellbarton
Date:     2011-07-13 23:24:21 +0000 (Wed, 13 Jul 2011)
Log Message:
-----------
minor changes to bgl.Buffer py class
- use getset rather then getattr
- remove verbose Buffer docstring, better use sphinx docs for this
- replace bge.Buffer with the Buffer class and add a __new__ function to the class so it can be called.
- improve error messages

Modified Paths:
--------------
    trunk/blender/source/blender/python/generic/bgl.c

Modified: trunk/blender/source/blender/python/generic/bgl.c
===================================================================
--- trunk/blender/source/blender/python/generic/bgl.c	2011-07-13 20:45:09 UTC (rev 38376)
+++ trunk/blender/source/blender/python/generic/bgl.c	2011-07-13 23:24:21 UTC (rev 38377)
@@ -44,26 +44,8 @@
 
 #include "BLI_utildefines.h"
 
+static PyObject *Buffer_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
 
-PyDoc_STRVAR(Method_Buffer_doc,
-	"(type, dimensions, [template]) - Create a new Buffer object\n\n\
-(type) - The format to store data in\n\
-(dimensions) - An int or sequence specifying the dimensions of the buffer\n\
-[template] - A sequence of matching dimensions to the buffer to be created\n\
-  which will be used to initialize the Buffer.\n\n\
-If a template is not passed in all fields will be initialized to 0.\n\n\
-The type should be one of GL_BYTE, GL_SHORT, GL_INT, GL_FLOAT, or GL_DOUBLE.\n\
-If the dimensions are specified as an int a linear buffer will be\n\
-created. If a sequence is passed for the dimensions the buffer\n\
-will have len(sequence) dimensions, where the size for each dimension\n\
-is determined by the value in the sequence at that index.\n\n\
-For example, passing [100, 100] will create a 2 dimensional\n\
-square buffer. Passing [16, 16, 32] will create a 3 dimensional\n\
-buffer which is twice as deep as it is wide or high."
-);
-
-static PyObject *Method_Buffer(PyObject *self, PyObject *args);
-
 /* Buffer sequence methods */
 
 static int Buffer_len(PyObject *self);
@@ -71,42 +53,98 @@
 static PyObject *Buffer_slice(PyObject *self, int begin, int end);
 static int Buffer_ass_item(PyObject *self, int i, PyObject *v);
 static int Buffer_ass_slice(PyObject *self, int begin, int end,
-				 PyObject *seq);
+                            PyObject *seq);
 
 static PySequenceMethods Buffer_SeqMethods = {
-	( lenfunc ) Buffer_len,						/*sq_length */
-	( binaryfunc ) NULL,						/*sq_concat */
-	( ssizeargfunc ) NULL,						/*sq_repeat */
-	( ssizeargfunc ) Buffer_item,				/*sq_item */
-	( ssizessizeargfunc ) Buffer_slice,			/*sq_slice, deprecated TODO, replace */
-	( ssizeobjargproc ) Buffer_ass_item,		/*sq_ass_item */
-	( ssizessizeobjargproc ) Buffer_ass_slice,	/*sq_ass_slice, deprecated TODO, replace */
+	(lenfunc) Buffer_len,						/*sq_length */
+	(binaryfunc) NULL,							/*sq_concat */
+	(ssizeargfunc) NULL,						/*sq_repeat */
+	(ssizeargfunc) Buffer_item,					/*sq_item */
+	(ssizessizeargfunc) Buffer_slice,			/*sq_slice, deprecated TODO, replace */
+	(ssizeobjargproc) Buffer_ass_item,			/*sq_ass_item */
+	(ssizessizeobjargproc) Buffer_ass_slice,	/*sq_ass_slice, deprecated TODO, replace */
 	(objobjproc) NULL,							/* sq_contains */
 	(binaryfunc) NULL,							/* sq_inplace_concat */
 	(ssizeargfunc) NULL,						/* sq_inplace_repeat */
 };
 
 static void Buffer_dealloc(PyObject *self);
-static PyObject *Buffer_tolist(PyObject *self);
-static PyObject *Buffer_dimensions(PyObject *self);
-static PyObject *Buffer_getattr(PyObject *self, char *name);
+static PyObject *Buffer_tolist(PyObject *self, void *arg);
+static PyObject *Buffer_dimensions(PyObject *self, void *arg);
 static PyObject *Buffer_repr(PyObject *self);
+static PyGetSetDef Buffer_getseters[];
 
 PyTypeObject BGL_bufferType = {
 	PyVarObject_HEAD_INIT(NULL, 0)
-	"buffer",		/*tp_name */
-	sizeof( Buffer ),	/*tp_basicsize */
-	0,			/*tp_itemsize */
-	( destructor ) Buffer_dealloc,	/*tp_dealloc */
-	( printfunc ) 0,	/*tp_print */
-	( getattrfunc ) Buffer_getattr,	/*tp_getattr */
-	( setattrfunc ) 0,	/*tp_setattr */
+	"bgl.Buffer",               /*tp_name */
+	sizeof(Buffer),             /*tp_basicsize */
+	0,                          /*tp_itemsize */
+	(destructor)Buffer_dealloc, /*tp_dealloc */
+	(printfunc)NULL,            /*tp_print */
+	NULL,                       /*tp_getattr */
+	NULL,                       /*tp_setattr */
 	NULL,		/*tp_compare */
-	( reprfunc ) Buffer_repr,	/*tp_repr */
+	(reprfunc) Buffer_repr,	/*tp_repr */
 	NULL,			/*tp_as_number */
 	&Buffer_SeqMethods,	/*tp_as_sequence */
+	NULL,		/* PyMappingMethods *tp_as_mapping; */
+
+	/* More standard operations (here for binary compatibility) */
+
+	NULL, /* hashfunc tp_hash; */
+	NULL,                       /* ternaryfunc tp_call; */
+	NULL,                       /* reprfunc tp_str; */
+	NULL,                       /* getattrofunc tp_getattro; */
+	NULL,                       /* setattrofunc tp_setattro; */
+
+	/* Functions to access object as input/output buffer */
+	NULL,                       /* PyBufferProcs *tp_as_buffer; */
+
+	/*** Flags to define presence of optional/expanded features ***/
+	Py_TPFLAGS_DEFAULT,         /* long tp_flags; */
+
+	NULL,                       /*  char *tp_doc;  Documentation string */
+	/*** Assigned meaning in release 2.0 ***/
+	/* call function for all accessible objects */
+	NULL,                       /* traverseproc tp_traverse; */
+
+	/* delete references to contained objects */
+	NULL,                       /* inquiry tp_clear; */
+
+	/***  Assigned meaning in release 2.1 ***/
+	/*** rich comparisons ***/
+	NULL,                       /* richcmpfunc tp_richcompare; */
+
+	/***  weak reference enabler ***/
+	0,                          /* long tp_weaklistoffset; */
+
+	/*** Added in release 2.2 ***/
+	/*   Iterators */
+	NULL, /* getiterfunc tp_iter; */
+	NULL,                       /* iternextfunc tp_iternext; */
+	/*** Attribute descriptor and subclassing stuff ***/
+	NULL,        /* struct PyMethodDef *tp_methods; */
+	NULL,                       /* struct PyMemberDef *tp_members; */
+	Buffer_getseters,           /* struct PyGetSetDef *tp_getset; */
+	NULL,						/*tp_base*/
+	NULL,						/*tp_dict*/
+	NULL,						/*tp_descr_get*/
+	NULL,						/*tp_descr_set*/
+	0,							/*tp_dictoffset*/
+	NULL,						/*tp_init*/
+	NULL,						/*tp_alloc*/
+	Buffer_new,					/*tp_new*/
+	NULL,						/*tp_free*/
+	NULL,						/*tp_is_gc*/
+	NULL,						/*tp_bases*/
+	NULL,						/*tp_mro*/
+	NULL,						/*tp_cache*/
+	NULL,						/*tp_subclasses*/
+	NULL,						/*tp_weaklist*/
+	NULL						/*tp_del*/
 };
 
+
 /* #ifndef __APPLE__ */
 
 #define BGL_Wrap(nargs, funcname, ret, arg_list) \
@@ -174,26 +212,13 @@
 	}
 	else {
 		memset(buffer->buf.asvoid, 0, length*size);
-		/*
-		for (i= 0; i<length; i++) {
-			if (type==GL_BYTE) 
-				buffer->buf.asbyte[i]= 0;
-			else if (type==GL_SHORT) 
-				buffer->buf.asshort[i]= 0;
-			else if (type==GL_INT) 
-				buffer->buf.asint[i]= 0;
-			else if (type==GL_FLOAT) 
-				buffer->buf.asfloat[i]= 0.0f;
-			else if (type==GL_DOUBLE)
-				buffer->buf.asdouble[i]= 0.0;
-		}
-		*/
 	}
 	return buffer;
 }
 
+
 #define MAX_DIMENSIONS	256
-static PyObject *Method_Buffer (PyObject *UNUSED(self), PyObject *args)
+static PyObject *Buffer_new(PyTypeObject *UNUSED(type), PyObject *args, PyObject *kwds)
 {
 	PyObject *length_ob= NULL, *init= NULL;
 	Buffer *buffer;
@@ -201,31 +226,40 @@
 	
 	int i, type;
 	int ndimensions = 0;
-	
-	if (!PyArg_ParseTuple(args, "iO|O", &type, &length_ob, &init)) {
-		PyErr_SetString(PyExc_AttributeError, "expected an int and one or two PyObjects");
+
+	if(kwds && PyDict_Size(kwds)) {
+		PyErr_SetString(PyExc_TypeError, "bgl.Buffer(): takes no keyword args");
 		return NULL;
 	}
+
+	if (!PyArg_ParseTuple(args, "iO|O: bgl.Buffer", &type, &length_ob, &init)) {
+		return NULL;
+	}
 	if (!ELEM5(type, GL_BYTE, GL_SHORT, GL_INT, GL_FLOAT, GL_DOUBLE)) {
-		PyErr_SetString(PyExc_AttributeError, "invalid first argument type, should be one of GL_BYTE, GL_SHORT, GL_INT, GL_FLOAT or GL_DOUBLE");
+		PyErr_SetString(PyExc_AttributeError,
+		                "invalid first argument type, should be one of "
+		                "GL_BYTE, GL_SHORT, GL_INT, GL_FLOAT or GL_DOUBLE");
 		return NULL;
 	}
 
 	if (PyLong_Check(length_ob)) {
 		ndimensions= 1;
 		if(((dimensions[0]= PyLong_AsLong(length_ob)) < 1)) {
-			PyErr_SetString(PyExc_AttributeError, "dimensions must be between 1 and "STRINGIFY(MAX_DIMENSIONS));
+			PyErr_SetString(PyExc_AttributeError,
+			                "dimensions must be between 1 and "STRINGIFY(MAX_DIMENSIONS));
 			return NULL;
 		}
 	}
 	else if (PySequence_Check(length_ob)) {
 		ndimensions= PySequence_Size(length_ob);
 		if (ndimensions > MAX_DIMENSIONS) {
-			PyErr_SetString(PyExc_AttributeError, "too many dimensions, max is "STRINGIFY(MAX_DIMENSIONS));
+			PyErr_SetString(PyExc_AttributeError,
+			                "too many dimensions, max is "STRINGIFY(MAX_DIMENSIONS));
 			return NULL;
 		}
 		else if (ndimensions < 1) {
-			PyErr_SetString(PyExc_AttributeError, "sequence must have at least one dimension");
+			PyErr_SetString(PyExc_AttributeError,
+			                "sequence must have at least one dimension");
 			return NULL;
 		}
 		for (i=0; i<ndimensions; i++) {
@@ -236,13 +270,16 @@
 			Py_DECREF(ob);
 
 			if(dimensions[i] < 1) {
-				PyErr_SetString(PyExc_AttributeError, "dimensions must be between 1 and "STRINGIFY(MAX_DIMENSIONS));
+				PyErr_SetString(PyExc_AttributeError,
+				                "dimensions must be between 1 and "STRINGIFY(MAX_DIMENSIONS));
 				return NULL;
 			}
 		}
 	}
 	else {
-		PyErr_Format(PyExc_TypeError, "invalid second argument argument expected a sequence or an int, not a %.200s", Py_TYPE(length_ob)->tp_name);
+		PyErr_Format(PyExc_TypeError,
+		             "invalid second argument argument expected a sequence "
+		             "or an int, not a %.200s", Py_TYPE(length_ob)->tp_name);
 		return NULL;
 	}
 	
@@ -336,42 +373,38 @@
 	Buffer *buf= (Buffer *) self;
 	
 	if (i >= buf->dimensions[0]) {
-		PyErr_SetString(PyExc_IndexError, "array assignment index out of range");
+		PyErr_SetString(PyExc_IndexError,
+		                "array assignment index out of range");
 		return -1;
 	}
-	
+
 	if (buf->ndimensions!=1) {
 		PyObject *row= Buffer_item(self, i);
-		int ret;
 
-		if (!row) return -1;
-		ret= Buffer_ass_slice(row, 0, buf->dimensions[1], v);
-		Py_DECREF(row);
-		return ret;
+		if (row) {
+			int ret= Buffer_ass_slice(row, 0, buf->dimensions[1], v);
+			Py_DECREF(row);
+			return ret;
+		}
+		else {
+			return -1;
+		}
 	}
 
-	if (buf->type==GL_BYTE) {
-		if (!PyArg_Parse(v, "b:Coordinates must be ints", &buf->buf.asbyte[i]))
-		return -1;
+	switch(buf->type) {
+	case GL_BYTE:
+		return PyArg_Parse(v, "b:Expected ints", &buf->buf.asbyte[i]) ? 0:-1;
+	case GL_SHORT:
+		return PyArg_Parse(v, "h:Expected ints", &buf->buf.asshort[i]) ? 0:-1;
+	case GL_INT:

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list