#define PyErr_SetVim(str) PyErr_SetString(VimError, str)
+#define RAISE_NO_EMPTY_KEYS PyErr_SetString(PyExc_ValueError, \
+ _("empty keys are not allowed"))
+
#define INVALID_BUFFER_VALUE ((buf_T *)(-1))
#define INVALID_WINDOW_VALUE ((win_T *)(-1))
#define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
-#define DICTKEY_DECL \
- PyObject *dictkey_todecref = NULL;
-#define DICTKEY_GET(err, decref) \
- if (!(key = StringToChars(keyObject, &dictkey_todecref))) \
- { \
- if (decref) \
- { \
- Py_DECREF(keyObject); \
- } \
- return err; \
- } \
- if (decref && !dictkey_todecref) \
- dictkey_todecref = keyObject; \
- if (*key == NUL) \
- { \
- PyErr_SetString(PyExc_ValueError, _("empty keys are not allowed")); \
- return err; \
- }
-#define DICTKEY_UNREF \
- Py_XDECREF(dictkey_todecref);
-
typedef void (*rangeinitializer)(void *);
typedef void (*runner)(const char *, void *
#ifdef PY_CAN_RECURSE
dictitem_T *di;
dict_T *dict = self->dict;
hashitem_T *hi;
-
- DICTKEY_DECL
+ PyObject *todecref;
if (flags & DICT_FLAG_HAS_DEFAULT)
{
if (flags & DICT_FLAG_RETURN_BOOL)
defObject = Py_False;
- DICTKEY_GET(NULL, 0)
+ if (!(key = StringToChars(keyObject, &todecref)))
+ return NULL;
+
+ if (*key == NUL)
+ {
+ RAISE_NO_EMPTY_KEYS;
+ return NULL;
+ }
hi = hash_find(&dict->dv_hashtab, key);
- DICTKEY_UNREF
+ Py_XDECREF(todecref);
if (HASHITEM_EMPTY(hi))
{
typval_T tv;
dict_T *dict = self->dict;
dictitem_T *di;
- DICTKEY_DECL
+ PyObject *todecref;
if (dict->dv_lock)
{
return -1;
}
- DICTKEY_GET(-1, 0)
+ if (!(key = StringToChars(keyObject, &todecref)))
+ return -1;
+ if (*key == NUL)
+ {
+ RAISE_NO_EMPTY_KEYS;
+ return -1;
+ }
di = dict_find(dict, key, -1);
if (di == NULL)
{
- DICTKEY_UNREF
+ Py_XDECREF(todecref);
PyErr_SetObject(PyExc_KeyError, keyObject);
return -1;
}
{
if (!(di = dictitem_alloc(key)))
{
+ Py_XDECREF(todecref);
PyErr_NoMemory();
return -1;
}
if (dict_add(dict, di) == FAIL)
{
- DICTKEY_UNREF
+ Py_XDECREF(todecref);
vim_free(di);
dictitem_free(di);
PyErr_SetVim(_("failed to add key to dictionary"));
else
clear_tv(&di->di_tv);
- DICTKEY_UNREF
+ Py_XDECREF(todecref);
copy_tv(&tv, &di->di_tv);
clear_tv(&tv);
int flags;
long numval;
char_u *stringval;
- DICTKEY_DECL
+ PyObject *todecref;
if (self->Check(self->from))
return NULL;
- DICTKEY_GET(NULL, 0)
+ if (!(key = StringToChars(keyObject, &todecref)))
+ return NULL;
+ if (*key == NUL)
+ {
+ RAISE_NO_EMPTY_KEYS;
+ return NULL;
+ }
flags = get_option_value_strict(key, &numval, &stringval,
self->opt_type, self->from);
- DICTKEY_UNREF
+ Py_XDECREF(todecref);
if (flags == 0)
{
int flags;
int opt_flags;
int r = 0;
- DICTKEY_DECL
+ PyObject *todecref;
if (self->Check(self->from))
return -1;
- DICTKEY_GET(-1, 0)
+ if (!(key = StringToChars(keyObject, &todecref)))
+ return -1;
+ if (*key == NUL)
+ {
+ RAISE_NO_EMPTY_KEYS;
+ return -1;
+ }
flags = get_option_value_strict(key, NULL, NULL,
self->opt_type, self->from);
if (flags == 0)
{
PyErr_SetObject(PyExc_KeyError, keyObject);
- DICTKEY_UNREF
+ Py_XDECREF(todecref);
return -1;
}
{
PyErr_SetString(PyExc_ValueError,
_("unable to unset global option"));
- DICTKEY_UNREF
+ Py_XDECREF(todecref);
return -1;
}
else if (!(flags & SOPT_GLOBAL))
{
PyErr_SetString(PyExc_ValueError, _("unable to unset option "
"without global value"));
- DICTKEY_UNREF
+ Py_XDECREF(todecref);
return -1;
}
else
{
unset_global_local_option(key, self->from);
- DICTKEY_UNREF
+ Py_XDECREF(todecref);
return 0;
}
}
else
{
PyErr_SetString(PyExc_TypeError, _("object must be integer"));
- DICTKEY_UNREF
+ Py_XDECREF(todecref);
return -1;
}
r = -1;
}
- DICTKEY_UNREF
+ Py_XDECREF(todecref);
return r;
}
PyObject *valObject;
Py_ssize_t iter = 0;
- if (!(dict = dict_alloc()))
+ if (!(dict = py_dict_alloc()))
return -1;
tv->v_type = VAR_DICT;
{
dict_unref(dict);
Py_XDECREF(todecref);
+ RAISE_NO_EMPTY_KEYS;
return -1;
}
PyObject *keyObject;
PyObject *valObject;
- if (!(dict = dict_alloc()))
+ if (!(dict = py_dict_alloc()))
return -1;
tv->v_type = VAR_DICT;
Py_DECREF(iterator);
Py_XDECREF(todecref);
dict_unref(dict);
+ RAISE_NO_EMPTY_KEYS;
return -1;
}