[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [50895] trunk/blender/source/gameengine/ Expressions/ListValue.cpp: add back game engine python api slicing, ( was missing / regression, since move to py3x)

Campbell Barton ideasman42 at gmail.com
Wed Sep 26 01:28:18 CEST 2012


Revision: 50895
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=50895
Author:   campbellbarton
Date:     2012-09-25 23:28:15 +0000 (Tue, 25 Sep 2012)
Log Message:
-----------
add back game engine python api slicing, (was missing / regression, since move to py3x)

not many people must have used it since it would crash with non-zero start slice values.

Modified Paths:
--------------
    trunk/blender/source/gameengine/Expressions/ListValue.cpp

Modified: trunk/blender/source/gameengine/Expressions/ListValue.cpp
===================================================================
--- trunk/blender/source/gameengine/Expressions/ListValue.cpp	2012-09-25 22:05:40 UTC (rev 50894)
+++ trunk/blender/source/gameengine/Expressions/ListValue.cpp	2012-09-25 23:28:15 UTC (rev 50895)
@@ -316,17 +316,46 @@
 		return cval->GetProxy();
 }
 
-static PyObject *listvalue_mapping_subscript(PyObject *self, PyObject *pyindex)
+
+/* just slice it into a python list... */
+static PyObject *listvalue_buffer_slice(CListValue *list, Py_ssize_t start, Py_ssize_t stop)
 {
+	PyObject *newlist;
+	Py_ssize_t i, j;
+
+	/* caller needs to validate negative index */
+#if 0
+	Py_ssize_t len = list->GetCount();
+
+	if (start > len) start = len;
+	if (stop  > len) stop  = len;
+#endif
+
+	newlist = PyList_New(stop - start);
+	if (!newlist)
+		return NULL;
+
+	for (i = start, j = 0; i < stop; i++, j++) {
+		PyObject *pyobj = list->GetValue(i)->ConvertValueToPython();
+		if (!pyobj) {
+			pyobj = list->GetValue(i)->GetProxy();
+		}
+		PyList_SET_ITEM(newlist, j, pyobj);
+	}
+	return newlist;
+}
+
+
+static PyObject *listvalue_mapping_subscript(PyObject *self, PyObject *key)
+{
 	CListValue *list= static_cast<CListValue *>(BGE_PROXY_REF(self));
 	if (list==NULL) {
 		PyErr_SetString(PyExc_SystemError, "value = CList[i], "BGE_PROXY_ERROR_MSG);
 		return NULL;
 	}
 	
-	if (PyUnicode_Check(pyindex))
-	{
-		CValue *item = ((CListValue*) list)->FindValue(_PyUnicode_AsString(pyindex));
+	if (PyUnicode_Check(key)) {
+		CValue *item = ((CListValue*) list)->FindValue(_PyUnicode_AsString(key));
 		if (item) {
 			PyObject *pyobj = item->ConvertValueToPython();
 			if (pyobj)
@@ -335,54 +364,33 @@
 				return item->GetProxy();
 		}
 	}
-	else if (PyLong_Check(pyindex))
-	{
-		int index = PyLong_AsSsize_t(pyindex);
+	else if (PyIndex_Check(key)) {
+		int index = PyLong_AsSsize_t(key);
 		return listvalue_buffer_item(self, index); /* wont add a ref */
 	}
+	else if (PySlice_Check(key)) {
+		Py_ssize_t start, stop, step, slicelength;
 
-	PyErr_Format(PyExc_KeyError,
-	             "CList[key]: '%R' key not in list", pyindex);
-	return NULL;
-}
+		if (PySlice_GetIndicesEx(key, list->GetCount(), &start, &stop, &step, &slicelength) < 0)
+			return NULL;
 
-
-/* just slice it into a python list... */
-static PyObject *listvalue_buffer_slice(PyObject *self,Py_ssize_t ilow, Py_ssize_t ihigh)
-{
-	CListValue *list= static_cast<CListValue *>(BGE_PROXY_REF(self));
-	if (list==NULL) {
-		PyErr_SetString(PyExc_SystemError, "val = CList[i:j], "BGE_PROXY_ERROR_MSG);
-		return NULL;
+		if (slicelength <= 0) {
+			return PyList_New(0);
+		}
+		else if (step == 1) {
+			return listvalue_buffer_slice(list, start, stop);
+		}
+		else {
+			PyErr_SetString(PyExc_TypeError, "CList[slice]: slice steps not supported");
+			return NULL;
+		}
 	}
-	
-	int i, j;
-	PyObject *newlist;
 
-	if (ilow < 0) ilow = 0;
-
-	int n = ((CListValue*) list)->GetCount();
-
-	if (ihigh >= n)
-		ihigh = n;
-	if (ihigh < ilow)
-		ihigh = ilow;
-
-	newlist = PyList_New(ihigh - ilow);
-	if (!newlist)
-		return NULL;
-
-	for (i = ilow, j = 0; i < ihigh; i++, j++)
-	{
-		PyObject *pyobj = list->GetValue(i)->ConvertValueToPython();
-		if (!pyobj)
-			pyobj = list->GetValue(i)->GetProxy();
-		PyList_SET_ITEM(newlist, i, pyobj);
-	}
-	return newlist;
+	PyErr_Format(PyExc_KeyError,
+	             "CList[key]: '%R' key not in list", key);
+	return NULL;
 }
 
-
 /* clist + list, return a list that python owns */
 static PyObject *listvalue_buffer_concat(PyObject *self, PyObject *other)
 {




More information about the Bf-blender-cvs mailing list