Again, “explicit is better then implicit”. It is easier to find bugs if code
reports the error in place of silently doing nothing.
# HG changeset patch
# User ZyX <[email protected]>
# Date 1368994282 -14400
# Branch python-extended-2
# Node ID 717340f097ae3d9d9e56ccf0e2833fcc924e7530
# Parent a7dc5bbc1f98a715d046ec1163b13fca17a195f0
Make set_option_value_for throw errors in case trying to use incorrect value
diff -r a7dc5bbc1f98 -r 717340f097ae src/if_py_both.h
--- a/src/if_py_both.h Sun May 19 23:43:21 2013 +0400
+++ b/src/if_py_both.h Mon May 20 00:11:22 2013 +0400
@@ -1521,6 +1521,24 @@
}
static int
+set_option_value_err(key, numval, stringval, opt_flags)
+ char_u *key;
+ int numval;
+ char_u *stringval;
+ int opt_flags;
+{
+ char_u *errmsg;
+ if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
+ {
+ if (VimTryEnd())
+ return -1;
+ PyErr_SetVim(errmsg);
+ return -1;
+ }
+ return 0;
+}
+
+ static int
set_option_value_for(key, numval, stringval, opt_flags, opt_type, from)
char_u *key;
int numval;
@@ -1533,6 +1551,7 @@
tabpage_T *save_curtab = NULL;
buf_T *save_curbuf = NULL;
int r = 0;
+ char_u *errmsg;
VimTryStart();
switch (opt_type)
@@ -1546,16 +1565,22 @@
PyErr_SetVim("Problem while switching windows.");
return -1;
}
- set_option_value(key, numval, stringval, opt_flags);
+ r = set_option_value_err(key, numval, stringval, opt_flags);
restore_win(save_curwin, save_curtab);
+ if (r)
+ return;
break;
case SREQ_BUF:
switch_buffer(&save_curbuf, (buf_T *)from);
- set_option_value(key, numval, stringval, opt_flags);
+ r = set_option_value_err(key, numval, stringval, opt_flags);
restore_buffer(save_curbuf);
+ if (r)
+ return;
break;
case SREQ_GLOBAL:
- set_option_value(key, numval, stringval, opt_flags);
+ r = set_option_value_err(key, numval, stringval, opt_flags);
+ if (r)
+ return;
break;
}
return VimTryEnd();
diff -r a7dc5bbc1f98 -r 717340f097ae src/option.c
--- a/src/option.c Sun May 19 23:43:21 2013 +0400
+++ b/src/option.c Mon May 20 00:11:22 2013 +0400
@@ -3015,7 +3015,7 @@
# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
#endif
static void set_string_option_global __ARGS((int opt_idx, char_u **varp));
-static void set_string_option __ARGS((int opt_idx, char_u *value, int
opt_flags));
+static char_u *set_string_option __ARGS((int opt_idx, char_u *value, int
opt_flags));
static char_u *did_set_string_option __ARGS((int opt_idx, char_u **varp, int
new_value_alloced, char_u *oldval, char_u *errbuf, int opt_flags));
static char_u *set_chars_option __ARGS((char_u **varp));
#ifdef FEAT_SYN_HL
@@ -5597,8 +5597,10 @@
/*
* Set a string option to a new value, and handle the effects.
- */
- static void
+ *
+ * Returns NULL on success or error message on error.
+ */
+ static char_u *
set_string_option(opt_idx, value, opt_flags)
int opt_idx;
char_u *value;
@@ -5607,9 +5609,10 @@
char_u *s;
char_u **varp;
char_u *oldval;
+ char_u *r = NULL;
if (options[opt_idx].var == NULL) /* don't set hidden option */
- return;
+ return NULL;
s = vim_strsave(value);
if (s != NULL)
@@ -5621,10 +5624,11 @@
: opt_flags);
oldval = *varp;
*varp = s;
- if (did_set_string_option(opt_idx, varp, TRUE, oldval, NULL,
- opt_flags) == NULL)
+ if ((r = did_set_string_option(opt_idx, varp, TRUE, oldval, NULL,
+ opt_flags)) == NULL)
did_set_option(opt_idx, opt_flags, TRUE);
}
+ return r;
}
/*
@@ -8961,8 +8965,10 @@
/*
* Set the value of option "name".
* Use "string" for string options, use "number" for other options.
- */
- void
+ *
+ * Returns NULL on success or error message on error.
+ */
+ char_u *
set_option_value(name, number, string, opt_flags)
char_u *name;
long number;
@@ -8984,11 +8990,11 @@
if (sandbox > 0 && (flags & P_SECURE))
{
EMSG(_(e_sandbox));
- return;
+ return NULL;
}
#endif
if (flags & P_STRING)
- set_string_option(opt_idx, string, opt_flags);
+ return set_string_option(opt_idx, string, opt_flags);
else
{
varp = get_varp_scope(&(options[opt_idx]), opt_flags);
@@ -9014,10 +9020,10 @@
}
}
if (flags & P_NUM)
- (void)set_num_option(opt_idx, varp, number,
+ return set_num_option(opt_idx, varp, number,
NULL, 0, opt_flags);
else
- (void)set_bool_option(opt_idx, varp, (int)number,
+ return set_bool_option(opt_idx, varp, (int)number,
opt_flags);
}
}
diff -r a7dc5bbc1f98 -r 717340f097ae src/proto/option.pro
--- a/src/proto/option.pro Sun May 19 23:43:21 2013 +0400
+++ b/src/proto/option.pro Mon May 20 00:11:22 2013 +0400
@@ -23,7 +23,7 @@
char_u *check_stl_option __ARGS((char_u *s));
int get_option_value __ARGS((char_u *name, long *numval, char_u **stringval,
int opt_flags));
int get_option_value_strict __ARGS((char_u *name, long *numval, char_u
**stringval, int opt_type, void *from));
-void set_option_value __ARGS((char_u *name, long number, char_u *string, int
opt_flags));
+char_u *set_option_value __ARGS((char_u *name, long number, char_u *string,
int opt_flags));
char_u *get_term_code __ARGS((char_u *tname));
char_u *get_highlight_default __ARGS((void));
char_u *get_encoding_default __ARGS((void));
diff -r a7dc5bbc1f98 -r 717340f097ae src/testdir/test86.ok
--- a/src/testdir/test86.ok Sun May 19 23:43:21 2013 +0400
+++ b/src/testdir/test86.ok Mon May 20 00:11:22 2013 +0400
@@ -166,6 +166,7 @@
inv: -100! KeyError
gopts1! KeyError
p/wopts1: 8
+ inv: -100! error
p/bopts1! KeyError
inv: -100! KeyError
bopts1! KeyError
@@ -184,6 +185,7 @@
inv: 'abc'! KeyError
gopts1! KeyError
p/wopts1: ''
+ inv: 'abc'! error
p/bopts1! KeyError
inv: 'abc'! KeyError
bopts1! KeyError
diff -r a7dc5bbc1f98 -r 717340f097ae src/testdir/test87.ok
--- a/src/testdir/test87.ok Sun May 19 23:43:21 2013 +0400
+++ b/src/testdir/test87.ok Mon May 20 00:11:22 2013 +0400
@@ -155,6 +155,7 @@
inv: -100! KeyError
gopts1! KeyError
p/wopts1: 8
+ inv: -100! error
p/bopts1! KeyError
inv: -100! KeyError
bopts1! KeyError
@@ -173,6 +174,7 @@
inv: 'abc'! KeyError
gopts1! KeyError
p/wopts1: b''
+ inv: 'abc'! error
p/bopts1! KeyError
inv: 'abc'! KeyError
bopts1! KeyError
--
--
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
---
You received this message because you are subscribed to the Google Groups
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.
diff -cr vim.a7dc5bbc1f98/src/if_py_both.h vim.717340f097ae/src/if_py_both.h
*** vim.a7dc5bbc1f98/src/if_py_both.h 2013-05-20 00:44:03.781083385 +0400
--- vim.717340f097ae/src/if_py_both.h 2013-05-20 00:44:03.793083266 +0400
***************
*** 1521,1526 ****
--- 1521,1544 ----
}
static int
+ set_option_value_err(key, numval, stringval, opt_flags)
+ char_u *key;
+ int numval;
+ char_u *stringval;
+ int opt_flags;
+ {
+ char_u *errmsg;
+ if ((errmsg = set_option_value(key, numval, stringval, opt_flags)))
+ {
+ if (VimTryEnd())
+ return -1;
+ PyErr_SetVim(errmsg);
+ return -1;
+ }
+ return 0;
+ }
+
+ static int
set_option_value_for(key, numval, stringval, opt_flags, opt_type, from)
char_u *key;
int numval;
***************
*** 1533,1538 ****
--- 1551,1557 ----
tabpage_T *save_curtab = NULL;
buf_T *save_curbuf = NULL;
int r = 0;
+ char_u *errmsg;
VimTryStart();
switch (opt_type)
***************
*** 1546,1561 ****
PyErr_SetVim("Problem while switching windows.");
return -1;
}
! set_option_value(key, numval, stringval, opt_flags);
restore_win(save_curwin, save_curtab);
break;
case SREQ_BUF:
switch_buffer(&save_curbuf, (buf_T *)from);
! set_option_value(key, numval, stringval, opt_flags);
restore_buffer(save_curbuf);
break;
case SREQ_GLOBAL:
! set_option_value(key, numval, stringval, opt_flags);
break;
}
return VimTryEnd();
--- 1565,1586 ----
PyErr_SetVim("Problem while switching windows.");
return -1;
}
! r = set_option_value_err(key, numval, stringval, opt_flags);
restore_win(save_curwin, save_curtab);
+ if (r)
+ return;
break;
case SREQ_BUF:
switch_buffer(&save_curbuf, (buf_T *)from);
! r = set_option_value_err(key, numval, stringval, opt_flags);
restore_buffer(save_curbuf);
+ if (r)
+ return;
break;
case SREQ_GLOBAL:
! r = set_option_value_err(key, numval, stringval, opt_flags);
! if (r)
! return;
break;
}
return VimTryEnd();
diff -cr vim.a7dc5bbc1f98/src/option.c vim.717340f097ae/src/option.c
*** vim.a7dc5bbc1f98/src/option.c 2013-05-20 00:44:03.786083336 +0400
--- vim.717340f097ae/src/option.c 2013-05-20 00:44:03.798083217 +0400
***************
*** 3015,3021 ****
# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
#endif
static void set_string_option_global __ARGS((int opt_idx, char_u **varp));
! static void set_string_option __ARGS((int opt_idx, char_u *value, int opt_flags));
static char_u *did_set_string_option __ARGS((int opt_idx, char_u **varp, int new_value_alloced, char_u *oldval, char_u *errbuf, int opt_flags));
static char_u *set_chars_option __ARGS((char_u **varp));
#ifdef FEAT_SYN_HL
--- 3015,3021 ----
# define insecure_flag(opt_idx, opt_flags) (&options[opt_idx].flags)
#endif
static void set_string_option_global __ARGS((int opt_idx, char_u **varp));
! static char_u *set_string_option __ARGS((int opt_idx, char_u *value, int opt_flags));
static char_u *did_set_string_option __ARGS((int opt_idx, char_u **varp, int new_value_alloced, char_u *oldval, char_u *errbuf, int opt_flags));
static char_u *set_chars_option __ARGS((char_u **varp));
#ifdef FEAT_SYN_HL
***************
*** 5597,5604 ****
/*
* Set a string option to a new value, and handle the effects.
*/
! static void
set_string_option(opt_idx, value, opt_flags)
int opt_idx;
char_u *value;
--- 5597,5606 ----
/*
* Set a string option to a new value, and handle the effects.
+ *
+ * Returns NULL on success or error message on error.
*/
! static char_u *
set_string_option(opt_idx, value, opt_flags)
int opt_idx;
char_u *value;
***************
*** 5607,5615 ****
char_u *s;
char_u **varp;
char_u *oldval;
if (options[opt_idx].var == NULL) /* don't set hidden option */
! return;
s = vim_strsave(value);
if (s != NULL)
--- 5609,5618 ----
char_u *s;
char_u **varp;
char_u *oldval;
+ char_u *r = NULL;
if (options[opt_idx].var == NULL) /* don't set hidden option */
! return NULL;
s = vim_strsave(value);
if (s != NULL)
***************
*** 5621,5630 ****
: opt_flags);
oldval = *varp;
*varp = s;
! if (did_set_string_option(opt_idx, varp, TRUE, oldval, NULL,
! opt_flags) == NULL)
did_set_option(opt_idx, opt_flags, TRUE);
}
}
/*
--- 5624,5634 ----
: opt_flags);
oldval = *varp;
*varp = s;
! if ((r = did_set_string_option(opt_idx, varp, TRUE, oldval, NULL,
! opt_flags)) == NULL)
did_set_option(opt_idx, opt_flags, TRUE);
}
+ return r;
}
/*
***************
*** 8961,8968 ****
/*
* Set the value of option "name".
* Use "string" for string options, use "number" for other options.
*/
! void
set_option_value(name, number, string, opt_flags)
char_u *name;
long number;
--- 8965,8974 ----
/*
* Set the value of option "name".
* Use "string" for string options, use "number" for other options.
+ *
+ * Returns NULL on success or error message on error.
*/
! char_u *
set_option_value(name, number, string, opt_flags)
char_u *name;
long number;
***************
*** 8984,8994 ****
if (sandbox > 0 && (flags & P_SECURE))
{
EMSG(_(e_sandbox));
! return;
}
#endif
if (flags & P_STRING)
! set_string_option(opt_idx, string, opt_flags);
else
{
varp = get_varp_scope(&(options[opt_idx]), opt_flags);
--- 8990,9000 ----
if (sandbox > 0 && (flags & P_SECURE))
{
EMSG(_(e_sandbox));
! return NULL;
}
#endif
if (flags & P_STRING)
! return set_string_option(opt_idx, string, opt_flags);
else
{
varp = get_varp_scope(&(options[opt_idx]), opt_flags);
***************
*** 9014,9023 ****
}
}
if (flags & P_NUM)
! (void)set_num_option(opt_idx, varp, number,
NULL, 0, opt_flags);
else
! (void)set_bool_option(opt_idx, varp, (int)number,
opt_flags);
}
}
--- 9020,9029 ----
}
}
if (flags & P_NUM)
! return set_num_option(opt_idx, varp, number,
NULL, 0, opt_flags);
else
! return set_bool_option(opt_idx, varp, (int)number,
opt_flags);
}
}
diff -cr vim.a7dc5bbc1f98/src/proto/option.pro vim.717340f097ae/src/proto/option.pro
*** vim.a7dc5bbc1f98/src/proto/option.pro 2013-05-20 00:44:03.777083425 +0400
--- vim.717340f097ae/src/proto/option.pro 2013-05-20 00:44:03.790083296 +0400
***************
*** 23,29 ****
char_u *check_stl_option __ARGS((char_u *s));
int get_option_value __ARGS((char_u *name, long *numval, char_u **stringval, int opt_flags));
int get_option_value_strict __ARGS((char_u *name, long *numval, char_u **stringval, int opt_type, void *from));
! void set_option_value __ARGS((char_u *name, long number, char_u *string, int opt_flags));
char_u *get_term_code __ARGS((char_u *tname));
char_u *get_highlight_default __ARGS((void));
char_u *get_encoding_default __ARGS((void));
--- 23,29 ----
char_u *check_stl_option __ARGS((char_u *s));
int get_option_value __ARGS((char_u *name, long *numval, char_u **stringval, int opt_flags));
int get_option_value_strict __ARGS((char_u *name, long *numval, char_u **stringval, int opt_type, void *from));
! char_u *set_option_value __ARGS((char_u *name, long number, char_u *string, int opt_flags));
char_u *get_term_code __ARGS((char_u *tname));
char_u *get_highlight_default __ARGS((void));
char_u *get_encoding_default __ARGS((void));
diff -cr vim.a7dc5bbc1f98/src/testdir/test86.ok vim.717340f097ae/src/testdir/test86.ok
*** vim.a7dc5bbc1f98/src/testdir/test86.ok 2013-05-20 00:44:03.776083435 +0400
--- vim.717340f097ae/src/testdir/test86.ok 2013-05-20 00:44:03.789083306 +0400
***************
*** 166,171 ****
--- 166,172 ----
inv: -100! KeyError
gopts1! KeyError
p/wopts1: 8
+ inv: -100! error
p/bopts1! KeyError
inv: -100! KeyError
bopts1! KeyError
***************
*** 184,189 ****
--- 185,191 ----
inv: 'abc'! KeyError
gopts1! KeyError
p/wopts1: ''
+ inv: 'abc'! error
p/bopts1! KeyError
inv: 'abc'! KeyError
bopts1! KeyError
diff -cr vim.a7dc5bbc1f98/src/testdir/test87.ok vim.717340f097ae/src/testdir/test87.ok
*** vim.a7dc5bbc1f98/src/testdir/test87.ok 2013-05-20 00:44:03.777083425 +0400
--- vim.717340f097ae/src/testdir/test87.ok 2013-05-20 00:44:03.789083306 +0400
***************
*** 155,160 ****
--- 155,161 ----
inv: -100! KeyError
gopts1! KeyError
p/wopts1: 8
+ inv: -100! error
p/bopts1! KeyError
inv: -100! KeyError
bopts1! KeyError
***************
*** 173,178 ****
--- 174,180 ----
inv: 'abc'! KeyError
gopts1! KeyError
p/wopts1: b''
+ inv: 'abc'! error
p/bopts1! KeyError
inv: 'abc'! KeyError
bopts1! KeyError