diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2010-07-28 00:15:03 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2010-07-28 00:15:03 (GMT) |
commit | cf448832ebca7ed34809168660fa96c3c61f8abb (patch) | |
tree | 2f288bff5d59676d902953a12b16a22de0d1045f /Modules | |
parent | 42746df17bd1f147f1bea90216ed9848efc730c8 (diff) | |
download | cpython-cf448832ebca7ed34809168660fa96c3c61f8abb.zip cpython-cf448832ebca7ed34809168660fa96c3c61f8abb.tar.gz cpython-cf448832ebca7ed34809168660fa96c3c61f8abb.tar.bz2 |
Issue #8966: ctypes: Remove implicit bytes-unicode conversion
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_ctypes/_ctypes.c | 20 | ||||
-rw-r--r-- | Modules/_ctypes/callproc.c | 34 | ||||
-rw-r--r-- | Modules/_ctypes/cfield.c | 61 |
3 files changed, 7 insertions, 108 deletions
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index e84bb8a..09e202e 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -132,8 +132,6 @@ static PyTypeObject Simple_Type; /* a callable object used for unpickling */ static PyObject *_unpickle; -char *_ctypes_conversion_encoding = NULL; -char *_ctypes_conversion_errors = NULL; /****************************************************************/ @@ -1090,13 +1088,7 @@ CharArray_set_value(CDataObject *self, PyObject *value) 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 @@ WCharArray_set_value(CDataObject *self, PyObject *value) "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 @@ c_char_p_from_param(PyObject *type, PyObject *value) 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"); diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c index bec9eb3..44b2d11 100644 --- a/Modules/_ctypes/callproc.c +++ b/Modules/_ctypes/callproc.c @@ -1641,37 +1641,6 @@ My_Py_DECREF(PyObject *self, PyObject *arg) 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) { @@ -1852,9 +1821,6 @@ PyMethodDef _ctypes_module_methods[] = { {"_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}, diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c index 876e2c0..b83447b 100644 --- a/Modules/_ctypes/cfield.c +++ b/Modules/_ctypes/cfield.c @@ -1168,20 +1168,6 @@ O_set(void *ptr, PyObject *value, Py_ssize_t size) 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 @@ static PyObject * 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 @@ U_set(void *ptr, PyObject *value, Py_ssize_t length) /* 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 @@ s_set(void *ptr, PyObject *value, Py_ssize_t length) 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 @@ z_set(void *ptr, PyObject *value, Py_ssize_t size) *(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 @@ Z_set(void *ptr, PyObject *value, Py_ssize_t size) 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 @@ BSTR_set(void *ptr, PyObject *value, Py_ssize_t size) /* 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 { |