python string conversion
authorCampbell Barton <ideasman42@gmail.com>
Thu, 3 Nov 2011 14:09:18 +0000 (14:09 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Thu, 3 Nov 2011 14:09:18 +0000 (14:09 +0000)
- use _PyUnicode_AsStringAndSize where possible
- use %R for PyErr_Format(...) rather then running repr on the object explicitly
- use const char

14 files changed:
source/blender/imbuf/intern/moviecache.c
source/blender/python/generic/IDProp.c
source/blender/python/generic/IDProp.h
source/blender/python/generic/py_capi_utils.c
source/blender/python/intern/bpy_operator.c
source/blender/python/intern/bpy_props.c
source/gameengine/Converter/BL_ActionActuator.cpp
source/gameengine/Expressions/ListValue.cpp
source/gameengine/Expressions/PyObjectPlus.cpp
source/gameengine/Expressions/StringValue.h
source/gameengine/GameLogic/SCA_PythonController.cpp
source/gameengine/Ketsji/KX_Camera.cpp
source/gameengine/Ketsji/KX_PythonInit.cpp
source/gameengine/Ketsji/KX_PythonSeq.cpp

index 7492b6aec44be3d15b722b5899aea3018d6bf8be..1d752fe9c6d9b7f53b26c6b7fea291cc02706289 100644 (file)
@@ -230,7 +230,7 @@ void IMB_moviecache_put(MovieCache *cache, void *userkey, ImBuf *ibuf)
 
        key= BLI_mempool_alloc(cache->keys_pool);
        key->cache_owner= cache;
-       key->userkey= BLI_mempool_alloc(cache->userkeys_pool);;
+       key->userkey= BLI_mempool_alloc(cache->userkeys_pool);
        memcpy(key->userkey, userkey, cache->keysize);
 
        item= BLI_mempool_alloc(cache->items_pool);
index 076b4811d07c34422378abad77c0a0da7c2bd574..6d869a7eb1ff087d5dc112ca662037d96c6510ed 100644 (file)
@@ -195,19 +195,22 @@ static PyObject *BPy_IDGroup_GetName(BPy_IDProperty *self, void *UNUSED(closure)
 
 static int BPy_IDGroup_SetName(BPy_IDProperty *self, PyObject *value, void *UNUSED(closure))
 {
-       char *st;
+       const char *name;
+       Py_ssize_t name_size;
+
        if (!PyUnicode_Check(value)) {
                PyErr_SetString(PyExc_TypeError, "expected a string!");
                return -1;
        }
 
-       st = _PyUnicode_AsString(value);
-       if (BLI_strnlen(st, MAX_IDPROP_NAME) == MAX_IDPROP_NAME) {
+       name = _PyUnicode_AsStringAndSize(value, &name_size);
+
+       if (name_size > MAX_IDPROP_NAME) {
                PyErr_SetString(PyExc_TypeError, "string length cannot exceed 31 characters!");
                return -1;
        }
 
-       BLI_strncpy(self->prop->name, st, sizeof(self->prop->name));
+       memcpy(self->prop->name, name, name_size);
        return 0;
 }
 
@@ -236,7 +239,7 @@ static Py_ssize_t BPy_IDGroup_Map_Len(BPy_IDProperty *self)
 static PyObject *BPy_IDGroup_Map_GetItem(BPy_IDProperty *self, PyObject *item)
 {
        IDProperty *idprop;
-       char *name;
+       const char *name;
 
        if (self->prop->type  != IDP_GROUP) {
                PyErr_SetString(PyExc_TypeError, "unsubscriptable object");
@@ -301,14 +304,22 @@ static int idp_sequence_type(PyObject *seq)
        return type;
 }
 
-/* note: group can be a pointer array or a group */
-const char *BPy_IDProperty_Map_ValidateAndCreate(const char *name, IDProperty *group, PyObject *ob)
+/* note: group can be a pointer array or a group.
+ * assume we already checked key is a string. */
+const char *BPy_IDProperty_Map_ValidateAndCreate(PyObject *name_obj, IDProperty *group, PyObject *ob)
 {
        IDProperty *prop = NULL;
        IDPropertyTemplate val = {0};
 
-       if (strlen(name) >= sizeof(group->name))
-               return "the length of IDProperty names is limited to 31 characters";
+       const char *name= "";
+
+       if (name_obj) {
+               Py_ssize_t name_size;
+               name = _PyUnicode_AsStringAndSize(name_obj, &name_size);
+               if (name_size > MAX_IDPROP_NAME) {
+                       return "the length of IDProperty names is limited to 31 characters";
+               }
+       }
 
        if (PyFloat_Check(ob)) {
                val.d = PyFloat_AsDouble(ob);
@@ -364,7 +375,7 @@ const char *BPy_IDProperty_Map_ValidateAndCreate(const char *name, IDProperty *g
                        for (i=0; i<val.array.len; i++) {
                                const char *error;
                                item = PySequence_GetItem(ob, i);
-                               error= BPy_IDProperty_Map_ValidateAndCreate("", prop, item);
+                               error= BPy_IDProperty_Map_ValidateAndCreate(NULL, prop, item);
                                Py_DECREF(item);
 
                                if (error)
@@ -396,7 +407,7 @@ const char *BPy_IDProperty_Map_ValidateAndCreate(const char *name, IDProperty *g
                                Py_XDECREF(pval);
                                return "invalid element in subgroup dict template!";
                        }
-                       if (BPy_IDProperty_Map_ValidateAndCreate(_PyUnicode_AsString(key), prop, pval)) {
+                       if (BPy_IDProperty_Map_ValidateAndCreate(key, prop, pval)) {
                                IDP_FreeProperty(prop);
                                MEM_freeN(prop);
                                Py_XDECREF(keys);
@@ -453,7 +464,7 @@ int BPy_Wrap_SetMapItem(IDProperty *prop, PyObject *key, PyObject *val)
                        return -1;
                }
 
-               err = BPy_IDProperty_Map_ValidateAndCreate(_PyUnicode_AsString(key), prop, val);
+               err = BPy_IDProperty_Map_ValidateAndCreate(key, prop, val);
                if (err) {
                        PyErr_SetString(PyExc_KeyError, err );
                        return -1;
@@ -587,7 +598,7 @@ static PyObject *BPy_IDGroup_Pop(BPy_IDProperty *self, PyObject *value)
 {
        IDProperty *idprop;
        PyObject *pyform;
-       char *name = _PyUnicode_AsString(value);
+       const char *name = _PyUnicode_AsString(value);
 
        if (!name) {
                PyErr_SetString(PyExc_TypeError, "pop expected at least 1 argument, got 0");
@@ -724,7 +735,7 @@ static PyObject *BPy_IDGroup_GetItems(BPy_IDProperty *self)
 
 static int BPy_IDGroup_Contains(BPy_IDProperty *self, PyObject *value)
 {
-       char *name = _PyUnicode_AsString(value);
+       const char *name = _PyUnicode_AsString(value);
 
        if (!name) {
                PyErr_SetString(PyExc_TypeError, "expected a string");
index f71514f9df014ca37806823d4f4c191bbdef2a25..36cb4c76a5cb7fc0a4981421821a49e5c7bae76c 100644 (file)
@@ -61,7 +61,7 @@ int BPy_Wrap_SetMapItem(struct IDProperty *prop, PyObject *key, PyObject *val);
 
 
 PyObject *BPy_IDGroup_WrapData(struct ID *id, struct IDProperty *prop );
-const char *BPy_IDProperty_Map_ValidateAndCreate(const char *name, struct IDProperty *group, PyObject *ob);
+const char *BPy_IDProperty_Map_ValidateAndCreate(PyObject *key, struct IDProperty *group, PyObject *ob);
 
 void IDProp_Init_Types(void);
 
index c6fb5133e3354ff693475d2c019c958ba7402a8c..eb4ecf799419f53841c7dabd27ce88d20bdcb0c1 100644 (file)
@@ -363,7 +363,7 @@ error_cleanup:
 /* string conversion, escape non-unicode chars, coerce must be set to NULL */
 const char *PyC_UnicodeAsByte(PyObject *py_str, PyObject **coerce)
 {
-       char *result;
+       const char *result;
 
        result= _PyUnicode_AsString(py_str);
 
index 6f23c1e604f2a538d0a2c91ab8f6a5bb16ef253b..70a5d79e9ac22e662ef02c3bba65c9cdb3206178 100644 (file)
@@ -380,7 +380,7 @@ static PyObject *pyop_getrna(PyObject *UNUSED(self), PyObject *value)
 {
        wmOperatorType *ot;
        PointerRNA ptr;
-       char *opname= _PyUnicode_AsString(value);
+       const char *opname= _PyUnicode_AsString(value);
        BPy_StructRNA *pyrna= NULL;
        
        if (opname==NULL) {
@@ -413,7 +413,7 @@ static PyObject *pyop_getinstance(PyObject *UNUSED(self), PyObject *value)
        wmOperatorType *ot;
        wmOperator *op;
        PointerRNA ptr;
-       char *opname= _PyUnicode_AsString(value);
+       const char *opname= _PyUnicode_AsString(value);
        BPy_StructRNA *pyrna= NULL;
 
        if (opname==NULL) {
index 4b30b4705f46d31296c2d67ca8d981ef52f7ecc3..b10223207bf98f6a3e6cdee03d6c7a5c9169c75e 100644 (file)
@@ -1211,7 +1211,7 @@ static StructRNA *pointer_type_from_py(PyObject *value, const char *error_prefix
        if (!srna) {
                if (PyErr_Occurred()) {
                        PyObject *msg= PyC_ExceptionBuffer();
-                       char *msg_char= _PyUnicode_AsString(msg);
+                       const char *msg_char= _PyUnicode_AsString(msg);
                        PyErr_Format(PyExc_TypeError,
                                     "%.200s expected an RNA type derived from PropertyGroup, failed with: %s",
                                     error_prefix, msg_char);
index 6f2ca28c135b1a47062193dcbaf676e7911ccc4c..a6d02db8cea56b211d53ef5802792138d807785d 100644 (file)
@@ -344,8 +344,9 @@ bool BL_ActionActuator::Update(double curtime, bool frame)
 /* Python functions                                                          */
 /* ------------------------------------------------------------------------- */
 
-PyObject* BL_ActionActuator::PyGetChannel(PyObject* value) {
-       char *string= _PyUnicode_AsString(value);
+PyObject* BL_ActionActuator::PyGetChannel(PyObject* value)
+{
+       const char *string= _PyUnicode_AsString(value);
        
        if (!string) {
                PyErr_SetString(PyExc_TypeError, "expected a single string");
index ade54f6d924cc8ac9a5875ce9e1dba2e4400d410..cdd87235fd235211876b92f64044c8e0c8c09451 100644 (file)
@@ -339,10 +339,9 @@ PyObject* listvalue_mapping_subscript(PyObject* self, PyObject* pyindex)
                int index = PyLong_AsSsize_t(pyindex);
                return listvalue_buffer_item(self, index); /* wont add a ref */
        }
-       
-       PyObject *pyindex_str = PyObject_Repr(pyindex); /* new ref */
-       PyErr_Format(PyExc_KeyError, "CList[key]: '%s' key not in list", _PyUnicode_AsString(pyindex_str));
-       Py_DECREF(pyindex_str);
+
+       PyErr_Format(PyExc_KeyError,
+                    "CList[key]: '%R' key not in list", pyindex);
        return NULL;
 }
 
index 33aa6b8d177d3823323f44bdda6179c6e66d652f..b05b7be087759be62a2fd6eec7163e1faeb30aa7 100644 (file)
@@ -1012,8 +1012,8 @@ int PyObjectPlus::py_set_attrdef(PyObject *self_py, PyObject *value, const PyAtt
                        {
                                if (PyUnicode_Check(value)) 
                                {
-                                       Py_ssize_t val_len;
-                                       char *val = _PyUnicode_AsStringAndSize(value, &val_len);
+                                       Py_ssize_t val_size;
+                                       const char *val = _PyUnicode_AsStringAndSize(value, &val_size);
                                        strncpy(ptr, val, attrdef->m_size);
                                        ptr[attrdef->m_size-1] = 0;
                                }
@@ -1030,7 +1030,7 @@ int PyObjectPlus::py_set_attrdef(PyObject *self_py, PyObject *value, const PyAtt
                                if (PyUnicode_Check(value)) 
                                {
                                        Py_ssize_t val_len;
-                                       char *val = _PyUnicode_AsStringAndSize(value, &val_len);
+                                       const char *val = _PyUnicode_AsStringAndSize(value, &val_len); /* XXX, should be 'const' but we do a silly trick to have a shorter string */
                                        if (attrdef->m_clamp)
                                        {
                                                if (val_len < attrdef->m_imin)
@@ -1042,10 +1042,8 @@ int PyObjectPlus::py_set_attrdef(PyObject *self_py, PyObject *value, const PyAtt
                                                else if (val_len > attrdef->m_imax)
                                                {
                                                        // trim the string
-                                                       char c = val[attrdef->m_imax];
-                                                       val[attrdef->m_imax] = 0;
                                                        *var = val;
-                                                       val[attrdef->m_imax] = c;
+                                                       var->SetLength(attrdef->m_imax);
                                                        break;
                                                }
                                        } else if (val_len < attrdef->m_imin || val_len > attrdef->m_imax)
index d0f74a6eed8a68fc190de2f78f7aeb4e588e0897..e5a892ff82d9b19fa7392d639f463a0b7635d97a 100644 (file)
@@ -31,8 +31,7 @@ public:
        CStringValue();
        CStringValue (const char *txt, const char *name , AllocationTYPE alloctype = CValue::HEAPVALUE);
 
-       virtual ~CStringValue() {
-       };
+       virtual ~CStringValue() {}
        /// CValue implementation
        virtual bool            IsEqual(const STR_String & other);
        virtual const STR_String &      GetText();
@@ -40,7 +39,7 @@ public:
        
        virtual CValue*         Calc(VALUE_OPERATOR op, CValue *val);
        virtual CValue*         CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val);
-       virtual void            SetValue(CValue* newval) {      m_strString = newval->GetText(); SetModified(true);     };
+       virtual void            SetValue(CValue* newval) {      m_strString = newval->GetText(); SetModified(true);     }
        virtual CValue*         GetReplica();
 #ifdef WITH_PYTHON
        virtual PyObject*       ConvertValueToPython() {
index 303b3e9529e281e237368fb94d899219da7b8531..d69358928e6e91de51c72132fa9f25fe734b3ac7 100644 (file)
@@ -199,7 +199,7 @@ SCA_IActuator* SCA_PythonController::LinkedActuatorFromPy(PyObject *value)
        
        if (PyUnicode_Check(value)) {
                /* get the actuator from the name */
-               char *name= _PyUnicode_AsString(value);
+               const char *name= _PyUnicode_AsString(value);
                for(it = lacts.begin(); it!= lacts.end(); ++it) {
                        if( name == (*it)->GetName() ) {
                                return *it;
@@ -214,12 +214,11 @@ SCA_IActuator* SCA_PythonController::LinkedActuatorFromPy(PyObject *value)
                        }
                }
        }
-       
+
        /* set the exception */
-       PyObject *value_str = PyObject_Repr(value); /* new ref */
-       PyErr_Format(PyExc_ValueError, "'%s' not in this python controllers actuator list", _PyUnicode_AsString(value_str));
-       Py_DECREF(value_str);
-       
+       PyErr_Format(PyExc_ValueError,
+                    "%R not in this python controllers actuator list", value);
+
        return NULL;
 }
 
@@ -500,7 +499,7 @@ int SCA_PythonController::pyattr_set_script(void *self_v, const KX_PYATTRIBUTE_D
 {
        SCA_PythonController* self= static_cast<SCA_PythonController*>(self_v);
        
-       char *scriptArg = _PyUnicode_AsString(value);
+       const char *scriptArg = _PyUnicode_AsString(value);
        
        if (scriptArg==NULL) {
                PyErr_SetString(PyExc_TypeError, "controller.script = string: Python Controller, expected a string script text");
index 3b7770318319d042d6a7c0a135c7ab1175e159ab..77438b8f48a3688b50ee273491412a740abedb0b 100644 (file)
@@ -883,7 +883,9 @@ bool ConvertPythonToCamera(PyObject * value, KX_Camera **object, bool py_none_ok
                if (*object) {
                        return true;
                } else {
-                       PyErr_Format(PyExc_ValueError, "%s, requested name \"%s\" did not match any KX_Camera in this scene", error_prefix, _PyUnicode_AsString(value));
+                       PyErr_Format(PyExc_ValueError,
+                                    "%s, requested name \"%s\" did not match any KX_Camera in this scene",
+                                    error_prefix, _PyUnicode_AsString(value));
                        return false;
                }
        }
index 25145e87b78e60384939b733dd474121a0c42e19..c3a141e74057fcb7dc24c3f1e5582f7c2b961438 100644 (file)
@@ -705,7 +705,7 @@ static PyObject *gLibNew(PyObject*, PyObject* args)
        KX_Scene *kx_scene= gp_KetsjiScene;
        char *path;
        char *group;
-       char *name;
+       const char *name;
        PyObject *names;
        int idcode;
 
index 7d7dc39ba32dbfba3b783f039e245846f279f510..430dcdbd153abe24043a360a03179a34fe82c979 100644 (file)
@@ -189,7 +189,7 @@ static PyObject *KX_PythonSeq_getIndex(PyObject* self, int index)
        return NULL;
 }
 
-static PyObjectPlus * KX_PythonSeq_subscript__internal(PyObject *self, char *key)
+static PyObjectPlus * KX_PythonSeq_subscript__internal(PyObject *self, const char *key)
 {
        PyObjectPlus *self_plus= BGE_PROXY_REF(((KX_PythonSeq *)self)->base);
        
@@ -277,7 +277,7 @@ static PyObject * KX_PythonSeq_subscript(PyObject * self, PyObject *key)
                return KX_PythonSeq_getIndex(self, PyLong_AsSsize_t( key ));
        }
        else if ( PyUnicode_Check(key) ) {
-               char *name = _PyUnicode_AsString(key);
+               const char *name = _PyUnicode_AsString(key);
                PyObjectPlus *ret = KX_PythonSeq_subscript__internal(self, name);
                
                if(ret) {