Index: Modules/_ctypes/callproc.c =================================================================== --- Modules/_ctypes/callproc.c (révision 81870) +++ Modules/_ctypes/callproc.c (copie de travail) @@ -1606,38 +1606,7 @@ return arg; } -#ifdef CTYPES_UNICODE - -static char set_conversion_mode_doc[] = -"set_conversion_mode(encoding, errors) -> (previous-encoding, previous-errors)\n\ -\n\ -Set the encoding and error handling ctypes uses when converting\n\ -between unicode and strings. Returns the previous values.\n"; - static PyObject * -set_conversion_mode(PyObject *self, PyObject *args) -{ - char *coding, *mode; - PyObject *result; - - if (!PyArg_ParseTuple(args, "zs:set_conversion_mode", &coding, &mode)) - return NULL; - result = Py_BuildValue("(zz)", _ctypes_conversion_encoding, _ctypes_conversion_errors); - if (coding) { - PyMem_Free(_ctypes_conversion_encoding); - _ctypes_conversion_encoding = PyMem_Malloc(strlen(coding) + 1); - strcpy(_ctypes_conversion_encoding, coding); - } else { - _ctypes_conversion_encoding = NULL; - } - PyMem_Free(_ctypes_conversion_errors); - _ctypes_conversion_errors = PyMem_Malloc(strlen(mode) + 1); - strcpy(_ctypes_conversion_errors, mode); - return result; -} -#endif - -static PyObject * resize(PyObject *self, PyObject *args) { CDataObject *obj; @@ -1817,9 +1786,6 @@ {"_unpickle", unpickle, METH_VARARGS }, {"buffer_info", buffer_info, METH_O, "Return buffer interface information"}, {"resize", resize, METH_VARARGS, "Resize the memory buffer of a ctypes instance"}, -#ifdef CTYPES_UNICODE - {"set_conversion_mode", set_conversion_mode, METH_VARARGS, set_conversion_mode_doc}, -#endif #ifdef MS_WIN32 {"get_last_error", get_last_error, METH_NOARGS}, {"set_last_error", set_last_error, METH_VARARGS}, Index: Modules/_ctypes/_ctypes.c =================================================================== --- Modules/_ctypes/_ctypes.c (révision 81870) +++ Modules/_ctypes/_ctypes.c (copie de travail) @@ -132,8 +132,6 @@ /* a callable object used for unpickling */ static PyObject *_unpickle; -char *_ctypes_conversion_encoding = NULL; -char *_ctypes_conversion_errors = NULL; /****************************************************************/ @@ -1090,13 +1088,7 @@ return -1; } - if (PyUnicode_Check(value)) { - value = PyUnicode_AsEncodedString(value, - _ctypes_conversion_encoding, - _ctypes_conversion_errors); - if (!value) - return -1; - } else if (!PyBytes_Check(value)) { + if (!PyBytes_Check(value)) { PyErr_Format(PyExc_TypeError, "str/bytes expected instead of %s instance", Py_TYPE(value)->tp_name); @@ -1150,13 +1142,7 @@ "can't delete attribute"); return -1; } - if (PyBytes_Check(value)) { - value = PyUnicode_FromEncodedObject(value, - _ctypes_conversion_encoding, - _ctypes_conversion_errors); - if (!value) - return -1; - } else if (!PyUnicode_Check(value)) { + if (!PyUnicode_Check(value)) { PyErr_Format(PyExc_TypeError, "unicode string expected instead of %s instance", Py_TYPE(value)->tp_name); @@ -1510,7 +1496,7 @@ Py_INCREF(Py_None); return Py_None; } - if (PyBytes_Check(value) || PyUnicode_Check(value)) { + if (PyBytes_Check(value)) { PyCArgObject *parg; struct fielddesc *fd = _ctypes_get_fielddesc("z"); Index: Modules/_ctypes/cfield.c =================================================================== --- Modules/_ctypes/cfield.c (révision 81870) +++ Modules/_ctypes/cfield.c (copie de travail) @@ -1168,20 +1168,6 @@ static PyObject * c_set(void *ptr, PyObject *value, Py_ssize_t size) { - if (PyUnicode_Check(value)) { - value = PyUnicode_AsEncodedString(value, - _ctypes_conversion_encoding, - _ctypes_conversion_errors); - if (value == NULL) - return NULL; - if (PyBytes_GET_SIZE(value) != 1) { - Py_DECREF(value); - goto error; - } - *(char *)ptr = PyBytes_AS_STRING(value)[0]; - Py_DECREF(value); - _RET(value); - } if (PyBytes_Check(value) && PyBytes_GET_SIZE(value) == 1) { *(char *)ptr = PyBytes_AS_STRING(value)[0]; _RET(value); @@ -1217,13 +1203,7 @@ u_set(void *ptr, PyObject *value, Py_ssize_t size) { Py_ssize_t len; - if (PyBytes_Check(value)) { - value = PyUnicode_FromEncodedObject(value, - _ctypes_conversion_encoding, - _ctypes_conversion_errors); - if (!value) - return NULL; - } else if (!PyUnicode_Check(value)) { + if (!PyUnicode_Check(value)) { PyErr_Format(PyExc_TypeError, "unicode string expected instead of %s instance", value->ob_type->tp_name); @@ -1292,13 +1272,7 @@ /* It's easier to calculate in characters than in bytes */ length /= sizeof(wchar_t); - if (PyBytes_Check(value)) { - value = PyUnicode_FromEncodedObject(value, - _ctypes_conversion_encoding, - _ctypes_conversion_errors); - if (!value) - return NULL; - } else if (!PyUnicode_Check(value)) { + if (!PyUnicode_Check(value)) { PyErr_Format(PyExc_TypeError, "unicode string expected instead of %s instance", value->ob_type->tp_name); @@ -1342,14 +1316,7 @@ char *data; Py_ssize_t size; - if (PyUnicode_Check(value)) { - value = PyUnicode_AsEncodedString(value, - _ctypes_conversion_encoding, - _ctypes_conversion_errors); - if (value == NULL) - return NULL; - assert(PyBytes_Check(value)); - } else if(PyBytes_Check(value)) { + if(PyBytes_Check(value)) { Py_INCREF(value); } else { PyErr_Format(PyExc_TypeError, @@ -1393,14 +1360,6 @@ *(char **)ptr = PyBytes_AsString(value); Py_INCREF(value); return value; - } else if (PyUnicode_Check(value)) { - PyObject *str = PyUnicode_AsEncodedString(value, - _ctypes_conversion_encoding, - _ctypes_conversion_errors); - if (str == NULL) - return NULL; - *(char **)ptr = PyBytes_AS_STRING(str); - return str; } else if (PyLong_Check(value)) { #if SIZEOF_VOID_P == SIZEOF_LONG_LONG *(char **)ptr = (char *)PyLong_AsUnsignedLongLongMask(value); @@ -1454,13 +1413,7 @@ Py_INCREF(Py_None); return Py_None; } - if (PyBytes_Check(value)) { - value = PyUnicode_FromEncodedObject(value, - _ctypes_conversion_encoding, - _ctypes_conversion_errors); - if (!value) - return NULL; - } else if (!PyUnicode_Check(value)) { + if (!PyUnicode_Check(value)) { PyErr_Format(PyExc_TypeError, "unicode string or integer address expected instead of %s instance", value->ob_type->tp_name); @@ -1540,12 +1493,6 @@ /* convert value into a PyUnicodeObject or NULL */ if (Py_None == value) { value = NULL; - } else if (PyBytes_Check(value)) { - value = PyUnicode_FromEncodedObject(value, - _ctypes_conversion_encoding, - _ctypes_conversion_errors); - if (!value) - return NULL; } else if (PyUnicode_Check(value)) { Py_INCREF(value); /* for the descref below */ } else { Index: Lib/ctypes/__init__.py =================================================================== --- Lib/ctypes/__init__.py (révision 81870) +++ Lib/ctypes/__init__.py (copie de travail) @@ -259,42 +259,32 @@ from _ctypes import POINTER, pointer, _pointer_type_cache -try: - from _ctypes import set_conversion_mode -except ImportError: - pass -else: - if _os.name in ("nt", "ce"): - set_conversion_mode("mbcs", "ignore") - else: - set_conversion_mode("ascii", "strict") +class c_wchar_p(_SimpleCData): + _type_ = "Z" - class c_wchar_p(_SimpleCData): - _type_ = "Z" +class c_wchar(_SimpleCData): + _type_ = "u" - class c_wchar(_SimpleCData): - _type_ = "u" +POINTER(c_wchar).from_param = c_wchar_p.from_param #_SimpleCData.c_wchar_p_from_param - POINTER(c_wchar).from_param = c_wchar_p.from_param #_SimpleCData.c_wchar_p_from_param +def create_unicode_buffer(init, size=None): + """create_unicode_buffer(aString) -> character array + create_unicode_buffer(anInteger) -> character array + create_unicode_buffer(aString, anInteger) -> character array + """ + if isinstance(init, (str, bytes)): + if size is None: + size = len(init)+1 + buftype = c_wchar * size + buf = buftype() + buf.value = init + return buf + elif isinstance(init, int): + buftype = c_wchar * init + buf = buftype() + return buf + raise TypeError(init) - def create_unicode_buffer(init, size=None): - """create_unicode_buffer(aString) -> character array - create_unicode_buffer(anInteger) -> character array - create_unicode_buffer(aString, anInteger) -> character array - """ - if isinstance(init, (str, bytes)): - if size is None: - size = len(init)+1 - buftype = c_wchar * size - buf = buftype() - buf.value = init - return buf - elif isinstance(init, int): - buftype = c_wchar * init - buf = buftype() - return buf - raise TypeError(init) - POINTER(c_char).from_param = c_char_p.from_param #_SimpleCData.c_char_p_from_param # XXX Deprecated