diff -r bff88c866886 Modules/_dbmmodule.c --- a/Modules/_dbmmodule.c Thu Apr 09 10:27:25 2015 +0200 +++ b/Modules/_dbmmodule.c Tue Apr 14 14:20:46 2015 -0400 @@ -272,7 +272,7 @@ self: dbmobject - key: str(length=True) + key: str(types={'str', 'robuffer'}, length=True) default: object = None / @@ -281,7 +281,7 @@ static PyObject * dbm_dbm_get_impl(dbmobject *dp, const char *key, Py_ssize_clean_t key_length, PyObject *default_value) -/*[clinic end generated code: output=c2bdccaa734ad349 input=aecf5efd2f2b1a3b]*/ +/*[clinic end generated code: output=c2bdccaa734ad349 input=f81478bc211895ef]*/ { datum dbm_key, val; diff -r bff88c866886 Modules/arraymodule.c --- a/Modules/arraymodule.c Thu Apr 09 10:27:25 2015 +0200 +++ b/Modules/arraymodule.c Tue Apr 14 14:20:46 2015 -0400 @@ -1600,7 +1600,7 @@ /*[clinic input] array.array.fromstring - buffer: Py_buffer(types='str bytes bytearray buffer') + buffer: Py_buffer(types={'str', 'buffer'}) / Appends items from the string, interpreting it as an array of machine values, as if it had been read from a file using the fromfile() method). @@ -1610,7 +1610,7 @@ static PyObject * array_array_fromstring_impl(arrayobject *self, Py_buffer *buffer) -/*[clinic end generated code: output=31c4baa779df84ce input=1302d94c97696b84]*/ +/*[clinic end generated code: output=31c4baa779df84ce input=fdde1a56cbe2b05b]*/ { if (PyErr_WarnEx(PyExc_DeprecationWarning, "fromstring() is deprecated. Use frombytes() instead.", 2) != 0) @@ -1928,7 +1928,7 @@ array._array_reconstructor arraytype: object(type="PyTypeObject *") - typecode: int(types='str') + typecode: int(types={'str'}) mformat_code: int(type="enum machine_format_code") items: object / @@ -1938,7 +1938,7 @@ static PyObject * array__array_reconstructor_impl(PyModuleDef *module, PyTypeObject *arraytype, int typecode, enum machine_format_code mformat_code, PyObject *items) -/*[clinic end generated code: output=c51081ec91caf7e9 input=f72492708c0a1d50]*/ +/*[clinic end generated code: output=c51081ec91caf7e9 input=a9ae223306d7b262]*/ { PyObject *converted_items; PyObject *result; diff -r bff88c866886 Tools/clinic/clinic.py --- a/Tools/clinic/clinic.py Thu Apr 09 10:27:25 2015 +0200 +++ b/Tools/clinic/clinic.py Tue Apr 14 14:20:46 2015 -0400 @@ -2117,6 +2117,9 @@ add_c_converter(cls) add_default_legacy_c_converter(cls) +def str_to_types(types): + return set(types.strip().split()) + class CConverter(metaclass=CConverterAutoRegister): """ For the init function, self, name, function, and default @@ -2415,7 +2418,6 @@ """ pass - class bool_converter(CConverter): type = 'int' default_type = bool @@ -2429,12 +2431,12 @@ class char_converter(CConverter): type = 'char' - default_type = str + default_type = (bytes, bytearray) format_unit = 'c' c_ignored_default = "'\0'" def converter_init(self): - if isinstance(self.default, str) and (len(self.default) != 1): + if isinstance(self.default, self.default_type) and (len(self.default) != 1): fail("char_converter: illegal default value " + repr(self.default)) @@ -2467,18 +2469,18 @@ if not bitwise: fail("Unsigned shorts must be bitwise (for now).") -@add_legacy_c_converter('C', types='str') +@add_legacy_c_converter('C', types={'str'}) class int_converter(CConverter): type = 'int' default_type = int format_unit = 'i' c_ignored_default = "0" - def converter_init(self, *, types='int', type=None): - if types == 'str': + def converter_init(self, *, types={'int'}, type=None): + if types == {'str'}: self.format_unit = 'C' - elif types != 'int': - fail("int_converter: illegal 'types' argument") + elif types != {'int'}: + fail("int_converter: illegal 'types' argument " + repr(types)) if type != None: self.type = type @@ -2569,63 +2571,68 @@ self.type = type -@add_legacy_c_converter('s#', length=True) -@add_legacy_c_converter('y', types="bytes") -@add_legacy_c_converter('y#', types="bytes", length=True) +# +# We define three string conventions for buffer types in the 'types' argument: +# 'buffer' : any object supporting the buffer interface +# 'rwbuffer': any object supporting the buffer interface, but must be writeable +# 'robuffer': any object supporting the buffer interface, but must not be writeable +# + +@add_legacy_c_converter('s#', types={"str", "robuffer"}, length=True) +@add_legacy_c_converter('y', types={"robuffer"}) +@add_legacy_c_converter('y#', types={"robuffer"}, length=True) @add_legacy_c_converter('z', nullable=True) -@add_legacy_c_converter('z#', nullable=True, length=True) +@add_legacy_c_converter('z#', types={"str", "robuffer"}, nullable=True, length=True) +# add_legacy_c_converter not supported for es, es#, et, et# +# because of their extra encoding argument class str_converter(CConverter): type = 'const char *' default_type = (str, Null, NoneType) format_unit = 's' - def converter_init(self, *, encoding=None, types="str", + def converter_init(self, *, encoding=None, types={"str"}, length=False, nullable=False, zeroes=False): - types = set(types.strip().split()) - bytes_type = {"bytes"} - str_type = {"str"} - all_3_type = {"bytearray"} | bytes_type | str_type - is_bytes = types == bytes_type - is_str = types == str_type - is_all_3 = types == all_3_type - self.length = bool(length) + + is_bytes_or_bytearray = types == {"bytes", "bytearray"} + is_str = types == {"str"} + is_robuffer = types == {"robuffer"} + is_str_robuffer = types == {"str", "robuffer"} + format_unit = None if encoding: self.encoding = encoding - if is_str and not (length or zeroes or nullable): + if (not length and not zeroes and not nullable and + is_str): format_unit = 'es' - elif is_all_3 and not (length or zeroes or nullable): + elif ( length and zeroes and not nullable and + is_str): + format_unit = 'es#' + elif (not length and not zeroes and not nullable and + is_bytes_or_bytearray): format_unit = 'et' - elif is_str and length and zeroes and not nullable: - format_unit = 'es#' - elif is_all_3 and length and not (nullable or zeroes): + elif ( length and zeroes and not nullable and + is_bytes_or_bytearray): format_unit = 'et#' - if format_unit.endswith('#'): - fail("Sorry: code using format unit ", repr(format_unit), "probably doesn't work properly yet.\nGive Larry your test case and he'll it.") - # TODO set pointer to NULL - # TODO add cleanup for buffer - pass - else: if zeroes: fail("str_converter: illegal combination of arguments (zeroes is only legal with an encoding)") - if is_bytes and not (nullable or length): + if is_robuffer and not length and not nullable: format_unit = 'y' - elif is_bytes and length and not nullable: + elif is_robuffer and length and not nullable: format_unit = 'y#' - elif is_str and not (nullable or length): + elif is_str and not length and not nullable: format_unit = 's' - elif is_str and length and not nullable: + elif is_str_robuffer and length and not nullable: format_unit = 's#' - elif is_str and nullable and not length: + elif is_str and not length and nullable: format_unit = 'z' - elif is_str and nullable and length: + elif is_str_robuffer and length and nullable : format_unit = 'z#' if not format_unit: @@ -2636,10 +2643,12 @@ class PyBytesObject_converter(CConverter): type = 'PyBytesObject *' format_unit = 'S' + # types = {'bytes'} class PyByteArrayObject_converter(CConverter): type = 'PyByteArrayObject *' format_unit = 'Y' + # types = {'bytearray'} class unicode_converter(CConverter): type = 'PyObject *' @@ -2661,43 +2670,33 @@ self.length = True self.format_unit = format_unit -# -# We define three string conventions for buffer types in the 'types' argument: -# 'buffer' : any object supporting the buffer interface -# 'rwbuffer': any object supporting the buffer interface, but must be writeable -# 'robuffer': any object supporting the buffer interface, but must not be writeable -# -@add_legacy_c_converter('s*', types='str bytes bytearray buffer') -@add_legacy_c_converter('z*', types='str bytes bytearray buffer', nullable=True) -@add_legacy_c_converter('w*', types='bytearray rwbuffer') +@add_legacy_c_converter('s*', types={'str', 'buffer'}) +@add_legacy_c_converter('z*', types={'str', 'buffer'}, nullable=True) +@add_legacy_c_converter('w*', types={'rwbuffer'}) class Py_buffer_converter(CConverter): type = 'Py_buffer' format_unit = 'y*' impl_by_reference = True c_ignored_default = "{NULL, NULL}" - def converter_init(self, *, types='bytes bytearray buffer', nullable=False): + def converter_init(self, *, types={'buffer'}, nullable=False): if self.default not in (unspecified, None): fail("The only legal default value for Py_buffer is None.") self.c_default = self.c_ignored_default - types = set(types.strip().split()) - bytes_type = {'bytes'} - bytearray_type = {'bytearray'} + buffer_type = {'buffer'} rwbuffer_type = {'rwbuffer'} - robuffer_type = {'robuffer'} str_type = {'str'} - bytes_bytearray_buffer_type = bytes_type | bytearray_type | buffer_type format_unit = None - if types == (str_type | bytes_bytearray_buffer_type): + if types == (str_type | buffer_type): format_unit = 's*' if not nullable else 'z*' else: if nullable: fail('Py_buffer_converter: illegal combination of arguments (nullable=True)') - elif types == (bytes_bytearray_buffer_type): + elif types == buffer_type: format_unit = 'y*' - elif types == (bytearray_type | rwbuffer_type): + elif types == rwbuffer_type: format_unit = 'w*' if not format_unit: fail("Py_buffer_converter: illegal combination of arguments")