1313 * Common code for if_python.c and if_python3.c.
1414 */
1515
16+ static char_u e_py_systemexit [] = "E880: Can't handle SystemExit of %s exception in vim" ;
17+
1618#if PY_VERSION_HEX < 0x02050000
1719typedef int Py_ssize_t ; /* Python 2.4 and earlier don't have this type. */
1820#endif
@@ -275,7 +277,7 @@ ObjectDir(PyObject *self, char **attributes)
275277
276278 if (self )
277279 for (method = self -> ob_type -> tp_methods ; method -> ml_name != NULL ; ++ method )
278- if (add_string (ret , (char * ) method -> ml_name ))
280+ if (add_string (ret , (char * )method -> ml_name ))
279281 {
280282 Py_DECREF (ret );
281283 return NULL ;
@@ -549,8 +551,9 @@ VimTryStart(void)
549551VimTryEnd (void )
550552{
551553 -- trylevel ;
552- /* Without this it stops processing all subsequent VimL commands and
553- * generates strange error messages if I e.g. try calling Test() in a cycle */
554+ /* Without this it stops processing all subsequent VimL commands and
555+ * generates strange error messages if I e.g. try calling Test() in a
556+ * cycle */
554557 did_emsg = FALSE;
555558 /* Keyboard interrupt should be preferred over anything else */
556559 if (got_int )
@@ -570,7 +573,7 @@ VimTryEnd(void)
570573 /* Finally transform VimL exception to python one */
571574 else
572575 {
573- PyErr_SetVim ((char * ) current_exception -> value );
576+ PyErr_SetVim ((char * )current_exception -> value );
574577 discard_current_exception ();
575578 return -1 ;
576579 }
@@ -667,15 +670,15 @@ VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
667670
668671 /* For backwards compatibility numbers are stored as strings. */
669672 sprintf (buf , "%ld" , (long )our_tv -> vval .v_number );
670- ret = PyString_FromString ((char * ) buf );
673+ ret = PyString_FromString ((char * )buf );
671674 }
672675# ifdef FEAT_FLOAT
673676 else if (our_tv -> v_type == VAR_FLOAT )
674677 {
675678 char buf [NUMBUFLEN ];
676679
677680 sprintf (buf , "%f" , our_tv -> vval .v_float );
678- ret = PyString_FromString ((char * ) buf );
681+ ret = PyString_FromString ((char * )buf );
679682 }
680683# endif
681684 else if (our_tv -> v_type == VAR_LIST )
@@ -955,7 +958,7 @@ map_rtp_callback(char_u *path, void *_data)
955958 PyObject * pathObject ;
956959 map_rtp_data * mr_data = * ((map_rtp_data * * ) data );
957960
958- if (!(pathObject = PyString_FromString ((char * ) path )))
961+ if (!(pathObject = PyString_FromString ((char * )path )))
959962 {
960963 * data = NULL ;
961964 return ;
@@ -1124,7 +1127,7 @@ find_module(char *fullname, char *tail, PyObject *new_path)
11241127 PyObject * module ;
11251128 char * dot ;
11261129
1127- if ((dot = (char * ) vim_strchr ((char_u * ) tail , '.' )))
1130+ if ((dot = (char * )vim_strchr ((char_u * ) tail , '.' )))
11281131 {
11291132 /*
11301133 * There is a dot in the name: call find_module recursively without the
@@ -1658,7 +1661,7 @@ DictionaryIterNext(dictiterinfo_T **dii)
16581661
16591662 -- ((* dii )-> todo );
16601663
1661- if (!(ret = PyBytes_FromString ((char * ) (* dii )-> hi -> hi_key )))
1664+ if (!(ret = PyBytes_FromString ((char * )(* dii )-> hi -> hi_key )))
16621665 return NULL ;
16631666
16641667 return ret ;
@@ -2680,12 +2683,12 @@ FunctionCall(FunctionObject *self, PyObject *argsObject, PyObject *kwargs)
26802683FunctionRepr (FunctionObject * self )
26812684{
26822685#ifdef Py_TRACE_REFS
2683- /* For unknown reason self->name may be NULL after calling
2686+ /* For unknown reason self->name may be NULL after calling
26842687 * Finalize */
26852688 return PyString_FromFormat ("<vim.Function '%s'>" ,
2686- (self -> name == NULL ? "<NULL>" : (char * ) self -> name ));
2689+ (self -> name == NULL ? "<NULL>" : (char * )self -> name ));
26872690#else
2688- return PyString_FromFormat ("<vim.Function '%s'>" , (char * ) self -> name );
2691+ return PyString_FromFormat ("<vim.Function '%s'>" , (char * )self -> name );
26892692#endif
26902693}
26912694
@@ -2809,7 +2812,7 @@ OptionsItem(OptionsObject *self, PyObject *keyObject)
28092812 {
28102813 if (stringval )
28112814 {
2812- PyObject * ret = PyBytes_FromString ((char * ) stringval );
2815+ PyObject * ret = PyBytes_FromString ((char * )stringval );
28132816 vim_free (stringval );
28142817 return ret ;
28152818 }
@@ -4525,7 +4528,7 @@ BufferAttr(BufferObject *self, char *name)
45254528{
45264529 if (strcmp (name , "name" ) == 0 )
45274530 return PyString_FromString ((self -> buf -> b_ffname == NULL
4528- ? "" : (char * ) self -> buf -> b_ffname ));
4531+ ? "" : (char * )self -> buf -> b_ffname ));
45294532 else if (strcmp (name , "number" ) == 0 )
45304533 return Py_BuildValue (Py_ssize_t_fmt , self -> buf -> b_fnum );
45314534 else if (strcmp (name , "vars" ) == 0 )
@@ -4961,7 +4964,19 @@ run_cmd(const char *cmd, void *arg UNUSED
49614964#endif
49624965 )
49634966{
4964- PyRun_SimpleString ((char * ) cmd );
4967+ PyObject * run_ret ;
4968+ run_ret = PyRun_String ((char * )cmd , Py_file_input , globals , globals );
4969+ if (run_ret != NULL )
4970+ {
4971+ Py_DECREF (run_ret );
4972+ }
4973+ else if (PyErr_Occurred () && PyErr_ExceptionMatches (PyExc_SystemExit ))
4974+ {
4975+ EMSG2 (_ (e_py_systemexit ), "python" );
4976+ PyErr_Clear ();
4977+ }
4978+ else
4979+ PyErr_PrintEx (1 );
49654980}
49664981
49674982static const char * code_hdr = "def " DOPY_FUNC "(line, linenr):\n " ;
@@ -4979,6 +4994,7 @@ run_do(const char *cmd, void *arg UNUSED
49794994 char * code ;
49804995 int status ;
49814996 PyObject * pyfunc , * pymain ;
4997+ PyObject * run_ret ;
49824998
49834999 if (u_save ((linenr_T )RangeStart - 1 , (linenr_T )RangeEnd + 1 ) != OK )
49845000 {
@@ -4990,7 +5006,23 @@ run_do(const char *cmd, void *arg UNUSED
49905006 code = PyMem_New (char , len + 1 );
49915007 memcpy (code , code_hdr , code_hdr_len );
49925008 STRCPY (code + code_hdr_len , cmd );
4993- status = PyRun_SimpleString (code );
5009+ run_ret = PyRun_String (code , Py_file_input , globals , globals );
5010+ status = -1 ;
5011+ if (run_ret != NULL )
5012+ {
5013+ status = 0 ;
5014+ Py_DECREF (run_ret );
5015+ }
5016+ else if (PyErr_Occurred () && PyErr_ExceptionMatches (PyExc_SystemExit ))
5017+ {
5018+ PyMem_Free (code );
5019+ EMSG2 (_ (e_py_systemexit ), "python" );
5020+ PyErr_Clear ();
5021+ return ;
5022+ }
5023+ else
5024+ PyErr_PrintEx (1 );
5025+
49945026 PyMem_Free (code );
49955027
49965028 if (status )
@@ -5068,9 +5100,14 @@ run_eval(const char *cmd, typval_T *rettv
50685100{
50695101 PyObject * run_ret ;
50705102
5071- run_ret = PyRun_String ((char * ) cmd , Py_eval_input , globals , globals );
5103+ run_ret = PyRun_String ((char * )cmd , Py_eval_input , globals , globals );
50725104 if (run_ret == NULL )
50735105 {
5106+ if (PyErr_ExceptionMatches (PyExc_SystemExit ))
5107+ {
5108+ EMSG2 (_ (e_py_systemexit ), "python" );
5109+ PyErr_Clear ();
5110+ }
50745111 if (PyErr_Occurred () && !msg_silent )
50755112 PyErr_PrintEx (0 );
50765113 EMSG (_ ("E858: Eval did not return a valid python object" ));
0 commit comments