Accept negative indices's for ListValues

scene.getObjectList()[-1] works like a python sequence.

removed some STR_String creation that was only used to do comparisons, in a simple expressions benchmark this made logic use 4% less overall.
This commit is contained in:
Campbell Barton
2009-02-19 07:01:49 +00:00
parent 7d2582de09
commit 92d4ef0939
4 changed files with 23 additions and 20 deletions

View File

@@ -272,32 +272,29 @@ void CParser::NextSym()
|| ((ch >= 'A') && (ch <= 'Z'))) || ((ch >= 'A') && (ch <= 'Z')))
{ // reserved word? { // reserved word?
int start; int start;
STR_String funstr;
start = chcount; start = chcount;
CharRep(); CharRep();
GrabString(start); GrabString(start);
funstr = const_as_string; if (!strcasecmp(const_as_string, "SUM")) {
funstr.Upper();
if (funstr == STR_String("SUM")) {
sym = sumsym; sym = sumsym;
} }
else if (funstr == STR_String("NOT")) { else if (!strcasecmp(const_as_string, "NOT")) {
sym = opsym; sym = opsym;
opkind = OPnot; opkind = OPnot;
} }
else if (funstr == STR_String("AND")) { else if (!strcasecmp(const_as_string, "AND")) {
sym = opsym; opkind = OPand; sym = opsym; opkind = OPand;
} }
else if (funstr == STR_String("OR")) { else if (!strcasecmp(const_as_string, "OR")) {
sym = opsym; opkind = OPor; sym = opsym; opkind = OPor;
} }
else if (funstr == STR_String("IF")) { else if (!strcasecmp(const_as_string, "IF"))
sym = ifsym; sym = ifsym;
} else if (funstr == STR_String("WHOMADE")) { else if (!strcasecmp(const_as_string, "WHOMADE"))
sym = whocodedsym; sym = whocodedsym;
} else if (funstr == STR_String("FALSE")) { else if (!strcasecmp(const_as_string, "FALSE")) {
sym = constsym; constkind = booltype; boolvalue = false; sym = constsym; constkind = booltype; boolvalue = false;
} else if (funstr == STR_String("TRUE")) { } else if (!strcasecmp(const_as_string, "TRUE")) {
sym = constsym; constkind = booltype; boolvalue = true; sym = constsym; constkind = booltype; boolvalue = true;
} else { } else {
sym = idsym; sym = idsym;

View File

@@ -34,7 +34,12 @@ Py_ssize_t listvalue_bufferlen(PyObject* list)
PyObject* listvalue_buffer_item(PyObject* list,Py_ssize_t index) PyObject* listvalue_buffer_item(PyObject* list,Py_ssize_t index)
{ {
if (index >= 0 && index < ((CListValue*) list)->GetCount()) int count = ((CListValue*) list)->GetCount();
if (index < 0)
index = count+index;
if (index >= 0 && index < count)
{ {
PyObject* pyobj = ((CListValue*) list)->GetValue(index)->ConvertValueToPython(); PyObject* pyobj = ((CListValue*) list)->GetValue(index)->ConvertValueToPython();
if (pyobj) if (pyobj)
@@ -64,8 +69,7 @@ PyObject* listvalue_mapping_subscript(PyObject* list,PyObject* pyindex)
} }
PyObject *pyindex_str = PyObject_Repr(pyindex); /* new ref */ PyObject *pyindex_str = PyObject_Repr(pyindex); /* new ref */
STR_String index_str(PyString_AsString(pyindex_str)); PyErr_Format(PyExc_KeyError, "'%s' not in list", PyString_AsString(pyindex_str));
PyErr_Format(PyExc_KeyError, "'%s' not in list", index_str.Ptr());
Py_DECREF(pyindex_str); Py_DECREF(pyindex_str);
return NULL; return NULL;
} }

View File

@@ -94,7 +94,7 @@ PyObjectPlus::PyObjectPlus(PyTypeObject *T) // constructor
* PyObjectPlus Methods -- Every class, even the abstract one should have a Methods * PyObjectPlus Methods -- Every class, even the abstract one should have a Methods
------------------------------*/ ------------------------------*/
PyMethodDef PyObjectPlus::Methods[] = { PyMethodDef PyObjectPlus::Methods[] = {
{"isA", (PyCFunction) sPy_isA, METH_VARARGS}, {"isA", (PyCFunction) sPy_isA, METH_O},
{NULL, NULL} /* Sentinel */ {NULL, NULL} /* Sentinel */
}; };
@@ -688,19 +688,21 @@ bool PyObjectPlus::isA(const char *mytypename) // check typename of each parent
for (P = Ps[i=0]; P != NULL; P = Ps[i++]) for (P = Ps[i=0]; P != NULL; P = Ps[i++])
{ {
if (STR_String(P->tp_name) == STR_String(mytypename) ) if (strcmp(P->tp_name, mytypename)==0)
return true; return true;
} }
return false; return false;
} }
PyObject *PyObjectPlus::Py_isA(PyObject *args) // Python wrapper for isA PyObject *PyObjectPlus::Py_isA(PyObject *value) // Python wrapper for isA
{ {
char *mytypename; char *mytypename;
if (!PyArg_ParseTuple(args, "s", &mytypename)) if (!PyString_Check(value)) {
PyErr_SetString(PyExc_TypeError, "expected a string");
return NULL; return NULL;
if(isA(mytypename)) }
if(isA(PyString_AsString(value)))
Py_RETURN_TRUE; Py_RETURN_TRUE;
else else
Py_RETURN_FALSE; Py_RETURN_FALSE;

View File

@@ -199,7 +199,7 @@ bool KX_TouchSensor::NewHandleCollision(void*object1,void*object2,const PHY_Coll
{ {
if (client_info->m_auxilary_info) if (client_info->m_auxilary_info)
{ {
found = (m_touchedpropname == STR_String((char*)client_info->m_auxilary_info)); found = (!strcmp(m_touchedpropname.Ptr(), (char*)client_info->m_auxilary_info));
} }
} else } else
{ {