# HG changeset patch
# User ZyX <[email protected]>
# Date 1346508509 -14400
# Node ID 0fa6134265ec58e8dba97cdb8e2a6730e05361df
# Parent 744a019963bcce46daa52f8dc61c79415c47bf45
Do not allow empty keys when creating vim dictionaries from python objects
Throw IndexError when dictionary item was not found, not vim.error
diff -r 744a019963bc -r 0fa6134265ec src/if_py_both.h
--- a/src/if_py_both.h Sat Sep 01 17:51:34 2012 +0400
+++ b/src/if_py_both.h Sat Sep 01 18:08:04 2012 +0400
@@ -612,6 +612,14 @@
static PyTypeObject DictionaryType;
+#define DICTKEY_GET_NOTEMPTY(err) \
+ DICTKEY_GET(err) \
+ if (*key == NUL) \
+ { \
+ PyErr_SetString(PyExc_ValueError, _("empty keys are not allowed")); \
+ return err; \
+ }
+
typedef struct
{
PyObject_HEAD
@@ -664,7 +672,7 @@
if (valObject == NULL)
return -1;
- DICTKEY_GET(-1)
+ DICTKEY_GET_NOTEMPTY(-1)
di = dictitem_alloc(key);
@@ -735,7 +743,7 @@
return -1;
}
- DICTKEY_GET(-1)
+ DICTKEY_GET_NOTEMPTY(-1)
valObject = PyTuple_GetItem(litem, 1);
if (valObject == NULL)
@@ -789,16 +797,22 @@
DictionaryItem(PyObject *self, PyObject *keyObject)
{
char_u *key;
- dictitem_T *val;
+ dictitem_T *di;
DICTKEY_DECL
- DICTKEY_GET(NULL)
-
- val = dict_find(((DictionaryObject *) (self))->dict, key, -1);
+ DICTKEY_GET_NOTEMPTY(NULL)
+
+ di = dict_find(((DictionaryObject *) (self))->dict, key, -1);
+
+ if (di == NULL)
+ {
+ PyErr_SetString(PyExc_IndexError, _("no such key in dictionary"));
+ return NULL;
+ }
DICTKEY_UNREF
- return ConvertToPyObject(&val->di_tv);
+ return ConvertToPyObject(&di->di_tv);
}
static PyInt
@@ -816,7 +830,7 @@
return -1;
}
- DICTKEY_GET(-1)
+ DICTKEY_GET_NOTEMPTY(-1)
di = dict_find(d, key, -1);
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php