comparison src/if_python3.c @ 2955:f909f4f0f38c v7.3.250

updated for version 7.3.250 Problem: Python: Errors in Unicode characters not handled nicely. Solution: Add the surrogateescape error handler. (lilydjwg)
author Bram Moolenaar <bram@vim.org>
date Fri, 15 Jul 2011 15:54:44 +0200
parents c1733ef5b6e8
children e4f3fa1a474e
comparison
equal deleted inserted replaced
2954:12bd7c8b3ab7 2955:f909f4f0f38c
66 #undef main /* Defined in python.h - aargh */ 66 #undef main /* Defined in python.h - aargh */
67 #undef HAVE_FCNTL_H /* Clash with os_win32.h */ 67 #undef HAVE_FCNTL_H /* Clash with os_win32.h */
68 68
69 static void init_structs(void); 69 static void init_structs(void);
70 70
71 /* The "surrogateescape" error handler is new in Python 3.1 */
72 #if PY_VERSION_HEX >= 0x030100f0
73 # define CODEC_ERROR_HANDLER "surrogateescape"
74 #else
75 # define CODEC_ERROR_HANDLER NULL
76 #endif
77
71 #define PyInt Py_ssize_t 78 #define PyInt Py_ssize_t
72 #define PyString_Check(obj) PyUnicode_Check(obj) 79 #define PyString_Check(obj) PyUnicode_Check(obj)
73 #define PyString_AsBytes(obj) PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL); 80 #define PyString_AsBytes(obj) PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, CODEC_ERROR_HANDLER);
74 #define PyString_FreeBytes(obj) Py_XDECREF(bytes) 81 #define PyString_FreeBytes(obj) Py_XDECREF(bytes)
75 #define PyString_AsString(obj) PyBytes_AsString(obj) 82 #define PyString_AsString(obj) PyBytes_AsString(obj)
76 #define PyString_Size(obj) PyBytes_GET_SIZE(bytes) 83 #define PyString_Size(obj) PyBytes_GET_SIZE(bytes)
77 #define PyString_FromString(repr) PyUnicode_FromString(repr) 84 #define PyString_FromString(repr) PyUnicode_FromString(repr)
78 85
659 666
660 pygilstate = PyGILState_Ensure(); 667 pygilstate = PyGILState_Ensure();
661 668
662 /* PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause 669 /* PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
663 * SyntaxError (unicode error). */ 670 * SyntaxError (unicode error). */
664 cmdstr = PyUnicode_Decode(cmd, strlen(cmd), (char *)ENC_OPT, NULL); 671 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
665 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", NULL); 672 (char *)ENC_OPT, CODEC_ERROR_HANDLER);
673 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER);
666 Py_XDECREF(cmdstr); 674 Py_XDECREF(cmdstr);
667 PyRun_SimpleString(PyBytes_AsString(cmdbytes)); 675 PyRun_SimpleString(PyBytes_AsString(cmdbytes));
668 Py_XDECREF(cmdbytes); 676 Py_XDECREF(cmdbytes);
669 677
670 PyGILState_Release(pygilstate); 678 PyGILState_Release(pygilstate);
1461 ++p; 1469 ++p;
1462 ++str; 1470 ++str;
1463 } 1471 }
1464 *p = '\0'; 1472 *p = '\0';
1465 1473
1466 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, NULL); 1474 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, CODEC_ERROR_HANDLER);
1467 1475
1468 vim_free(tmp); 1476 vim_free(tmp);
1469 return result; 1477 return result;
1470 } 1478 }
1471 1479