diff -r 35a9d60145cd Modules/_dbmmodule.c --- a/Modules/_dbmmodule.c Fri Apr 17 04:58:45 2015 +0300 +++ b/Modules/_dbmmodule.c Mon Apr 20 11:02:05 2015 -0700 @@ -272,7 +272,7 @@ self: dbmobject - key: str(types={'str', 'robuffer'}, length=True) + key: str(accept={str, robuffer}, length=True) default: object = None / @@ -282,7 +282,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=4f5c0e523eaf1251 input=f81478bc211895ef]*/ +/*[clinic end generated code: output=4f5c0e523eaf1251 input=531cfd0811ff9360]*/ { datum dbm_key, val; diff -r 35a9d60145cd Modules/_io/_iomodule.c --- a/Modules/_io/_iomodule.c Fri Apr 17 04:58:45 2015 +0300 +++ b/Modules/_io/_iomodule.c Mon Apr 20 11:02:05 2015 -0700 @@ -100,9 +100,9 @@ file: object mode: str = "r" buffering: int = -1 - encoding: str(nullable=True) = NULL - errors: str(nullable=True) = NULL - newline: str(nullable=True) = NULL + encoding: str(accept={str, NoneType}) = NULL + errors: str(accept={str, NoneType}) = NULL + newline: str(accept={str, NoneType}) = NULL closefd: int(c_default="1") = True opener: object = None @@ -230,7 +230,7 @@ _io_open_impl(PyModuleDef *module, PyObject *file, const char *mode, int buffering, const char *encoding, const char *errors, const char *newline, int closefd, PyObject *opener) -/*[clinic end generated code: output=7615d0d746eb14d2 input=0541ce15691a82f2]*/ +/*[clinic end generated code: output=7615d0d746eb14d2 input=f4e1ca75223987bc]*/ { unsigned i; diff -r 35a9d60145cd Modules/_io/bufferedio.c --- a/Modules/_io/bufferedio.c Fri Apr 17 04:58:45 2015 +0300 +++ b/Modules/_io/bufferedio.c Mon Apr 20 11:02:05 2015 -0700 @@ -101,26 +101,26 @@ /*[clinic input] _io._BufferedIOBase.readinto - buffer: Py_buffer(types={'rwbuffer'}) + buffer: Py_buffer(accept={rwbuffer}) / [clinic start generated code]*/ static PyObject * _io__BufferedIOBase_readinto_impl(PyObject *self, Py_buffer *buffer) -/*[clinic end generated code: output=8c8cda6684af8038 input=f8242a06c21763a0]*/ +/*[clinic end generated code: output=8c8cda6684af8038 input=00a6b9a38f29830a]*/ { return _bufferediobase_readinto_generic(self, buffer, 0); } /*[clinic input] _io._BufferedIOBase.readinto1 - buffer: Py_buffer(types={'rwbuffer'}) + buffer: Py_buffer(accept={rwbuffer}) / [clinic start generated code]*/ static PyObject * _io__BufferedIOBase_readinto1_impl(PyObject *self, Py_buffer *buffer) -/*[clinic end generated code: output=358623e4fd2b69d3 input=2003e706c730bd21]*/ +/*[clinic end generated code: output=358623e4fd2b69d3 input=ebad75b4aadfb9be]*/ { return _bufferediobase_readinto_generic(self, buffer, 1); } @@ -1065,26 +1065,26 @@ /*[clinic input] _io._Buffered.readinto - buffer: Py_buffer(types={'rwbuffer'}) + buffer: Py_buffer(accept={rwbuffer}) / [clinic start generated code]*/ static PyObject * _io__Buffered_readinto_impl(buffered *self, Py_buffer *buffer) -/*[clinic end generated code: output=bcb376580b1d8170 input=962b7496eac38b3a]*/ +/*[clinic end generated code: output=bcb376580b1d8170 input=ed6b98b7a20a3008]*/ { return _buffered_readinto_generic(self, buffer, 0); } /*[clinic input] _io._Buffered.readinto1 - buffer: Py_buffer(types={'rwbuffer'}) + buffer: Py_buffer(accept={rwbuffer}) / [clinic start generated code]*/ static PyObject * _io__Buffered_readinto1_impl(buffered *self, Py_buffer *buffer) -/*[clinic end generated code: output=6e5c6ac5868205d6 input=834614d93f0adeb5]*/ +/*[clinic end generated code: output=6e5c6ac5868205d6 input=4455c5d55fdf1687]*/ { return _buffered_readinto_generic(self, buffer, 1); } diff -r 35a9d60145cd Modules/_io/bytesio.c --- a/Modules/_io/bytesio.c Fri Apr 17 04:58:45 2015 +0300 +++ b/Modules/_io/bytesio.c Mon Apr 20 11:02:05 2015 -0700 @@ -541,7 +541,7 @@ /*[clinic input] _io.BytesIO.readinto - buffer: Py_buffer(types={'rwbuffer'}) + buffer: Py_buffer(accept={rwbuffer}) / Read up to len(buffer) bytes into buffer. @@ -552,7 +552,7 @@ static PyObject * _io_BytesIO_readinto_impl(bytesio *self, Py_buffer *buffer) -/*[clinic end generated code: output=a5d407217dcf0639 input=d289da851c7c4159]*/ +/*[clinic end generated code: output=a5d407217dcf0639 input=71581f32635c3a31]*/ { Py_ssize_t len, n; diff -r 35a9d60145cd Modules/_io/fileio.c --- a/Modules/_io/fileio.c Fri Apr 17 04:58:45 2015 +0300 +++ b/Modules/_io/fileio.c Mon Apr 20 11:02:05 2015 -0700 @@ -604,7 +604,7 @@ /*[clinic input] _io.FileIO.readinto - buffer: Py_buffer(types={'rwbuffer'}) + buffer: Py_buffer(accept={rwbuffer}) / Same as RawIOBase.readinto(). @@ -612,7 +612,7 @@ static PyObject * _io_FileIO_readinto_impl(fileio *self, Py_buffer *buffer) -/*[clinic end generated code: output=b01a5a22c8415cb4 input=5edd8327498d468c]*/ +/*[clinic end generated code: output=b01a5a22c8415cb4 input=4721d7b68b154eaf]*/ { Py_ssize_t n; int err; diff -r 35a9d60145cd Modules/_io/textio.c --- a/Modules/_io/textio.c Fri Apr 17 04:58:45 2015 +0300 +++ b/Modules/_io/textio.c Mon Apr 20 11:02:05 2015 -0700 @@ -789,9 +789,9 @@ /*[clinic input] _io.TextIOWrapper.__init__ buffer: object - encoding: str(nullable=True) = NULL - errors: str(nullable=True) = NULL - newline: str(nullable=True) = NULL + encoding: str(accept={str, NoneType}) = NULL + errors: str(accept={str, NoneType}) = NULL + newline: str(accept={str, NoneType}) = NULL line_buffering: int(c_default="0") = False write_through: int(c_default="0") = False @@ -830,7 +830,7 @@ const char *encoding, const char *errors, const char *newline, int line_buffering, int write_through) -/*[clinic end generated code: output=56a83402ce2a8381 input=1f20decb8d54a4ec]*/ +/*[clinic end generated code: output=56a83402ce2a8381 input=3126cb3101a2c99b]*/ { PyObject *raw, *codec_info = NULL; _PyIO_State *state = NULL; diff -r 35a9d60145cd Modules/arraymodule.c --- a/Modules/arraymodule.c Fri Apr 17 04:58:45 2015 +0300 +++ b/Modules/arraymodule.c Mon Apr 20 11:02:05 2015 -0700 @@ -1600,7 +1600,7 @@ /*[clinic input] array.array.fromstring - buffer: Py_buffer(types={'str', 'buffer'}) + buffer: Py_buffer(accept={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=fdde1a56cbe2b05b]*/ +/*[clinic end generated code: output=31c4baa779df84ce input=a3341a512e11d773]*/ { if (PyErr_WarnEx(PyExc_DeprecationWarning, "fromstring() is deprecated. Use frombytes() instead.", 2) != 0) @@ -1929,7 +1929,7 @@ array._array_reconstructor arraytype: object(type="PyTypeObject *") - typecode: int(types={'str'}) + typecode: int(accept={str}) mformat_code: int(type="enum machine_format_code") items: object / @@ -1942,7 +1942,7 @@ int typecode, enum machine_format_code mformat_code, PyObject *items) -/*[clinic end generated code: output=6ecbf0e8e4d92ab9 input=a9ae223306d7b262]*/ +/*[clinic end generated code: output=6ecbf0e8e4d92ab9 input=2464dc8f4c7736b5]*/ { PyObject *converted_items; PyObject *result; diff -r 35a9d60145cd Modules/cjkcodecs/multibytecodec.c --- a/Modules/cjkcodecs/multibytecodec.c Fri Apr 17 04:58:45 2015 +0300 +++ b/Modules/cjkcodecs/multibytecodec.c Mon Apr 20 11:02:05 2015 -0700 @@ -544,7 +544,7 @@ _multibytecodec.MultibyteCodec.encode input: object - errors: str(nullable=True) = NULL + errors: str(accept={str, NoneType}) = NULL Return an encoded string version of `input'. @@ -558,7 +558,7 @@ _multibytecodec_MultibyteCodec_encode_impl(MultibyteCodecObject *self, PyObject *input, const char *errors) -/*[clinic end generated code: output=7b26652045ba56a9 input=252e7ee695867b2d]*/ +/*[clinic end generated code: output=7b26652045ba56a9 input=05f6ced3c8dd0582]*/ { MultibyteCodec_State state; PyObject *errorcb, *r, *ucvt; @@ -613,7 +613,7 @@ _multibytecodec.MultibyteCodec.decode input: Py_buffer - errors: str(nullable=True) = NULL + errors: str(accept={str, NoneType}) = NULL Decodes 'input'. @@ -627,7 +627,7 @@ _multibytecodec_MultibyteCodec_decode_impl(MultibyteCodecObject *self, Py_buffer *input, const char *errors) -/*[clinic end generated code: output=ff419f65bad6cc77 input=37e1d9236e3ce8f3]*/ +/*[clinic end generated code: output=ff419f65bad6cc77 input=a7d45f87f75e5e02]*/ { MultibyteCodec_State state; MultibyteDecodeBuffer buf; diff -r 35a9d60145cd Modules/pyexpat.c --- a/Modules/pyexpat.c Fri Apr 17 04:58:45 2015 +0300 +++ b/Modules/pyexpat.c Mon Apr 20 11:02:05 2015 -0700 @@ -911,7 +911,7 @@ /*[clinic input] pyexpat.xmlparser.ExternalEntityParserCreate - context: str(nullable=True) + context: str(accept={str, NoneType}) encoding: str = NULL / @@ -922,7 +922,7 @@ pyexpat_xmlparser_ExternalEntityParserCreate_impl(xmlparseobject *self, const char *context, const char *encoding) -/*[clinic end generated code: output=535cda9d7a0fbcd6 input=283206575d960272]*/ +/*[clinic end generated code: output=535cda9d7a0fbcd6 input=b906714cc122c322]*/ { xmlparseobject *new_parser; int i; @@ -1546,8 +1546,8 @@ /*[clinic input] pyexpat.ParserCreate - encoding: str(nullable=True) = NULL - namespace_separator: str(nullable=True) = NULL + encoding: str(accept={str, NoneType}) = NULL + namespace_separator: str(accept={str, NoneType}) = NULL intern: object = NULL Return a new XML parser object. @@ -1556,7 +1556,7 @@ static PyObject * pyexpat_ParserCreate_impl(PyModuleDef *module, const char *encoding, const char *namespace_separator, PyObject *intern) -/*[clinic end generated code: output=81fccd233e1743a8 input=71b9f471aa6f8f86]*/ +/*[clinic end generated code: output=81fccd233e1743a8 input=23d29704acad385d]*/ { PyObject *result; int intern_decref = 0; diff -r 35a9d60145cd Tools/clinic/clinic.py --- a/Tools/clinic/clinic.py Fri Apr 17 04:58:45 2015 +0300 +++ b/Tools/clinic/clinic.py Mon Apr 20 11:02:05 2015 -0700 @@ -26,8 +26,12 @@ import tempfile import textwrap import traceback +import types import uuid +from types import * +NoneType = type(None) + # TODO: # # soon: @@ -2531,18 +2535,18 @@ if not bitwise: fail("Unsigned shorts must be bitwise (for now).") -@add_legacy_c_converter('C', types={'str'}) +@add_legacy_c_converter('C', accept={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, *, accept={int}, type=None): + if accept == {str}: self.format_unit = 'C' - elif types != {'int'}: - fail("int_converter: illegal 'types' argument " + repr(types)) + elif accept != {int}: + fail("int_converter: illegal 'accept' argument " + repr(accept)) if type != None: self.type = type @@ -2634,17 +2638,22 @@ # -# 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 +# We define three conventions for buffer types in the 'accept' argument: # - -@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#', types={"str", "robuffer"}, nullable=True, length=True) +# 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 +# + +class buffer: pass +class rwbuffer: pass +class robuffer: pass + +@add_legacy_c_converter('s#', accept={str, robuffer}, length=True) +@add_legacy_c_converter('y', accept={robuffer}) +@add_legacy_c_converter('y#', accept={robuffer}, length=True) +@add_legacy_c_converter('z', accept={str, NoneType}) +@add_legacy_c_converter('z#', accept={str, NoneType}, length=True) # add_legacy_c_converter not supported for es, es#, et, et# # because of their extra encoding argument class str_converter(CConverter): @@ -2652,45 +2661,47 @@ default_type = (str, Null, NoneType) format_unit = 's' - def converter_init(self, *, encoding=None, types={"str"}, - length=False, nullable=False, zeroes=False): + def converter_init(self, *, encoding=None, accept={str}, length=False, zeroes=False): self.length = bool(length) - is_b_or_ba = types == {"bytes", "bytearray"} - is_str = types == {"str"} - is_robuffer = types == {"robuffer"} - is_str_or_robuffer = types == {"str", "robuffer"} + is_b_or_ba = accept == {bytes, bytearray} + is_b_or_ba_or_none = accept == {bytes, bytearray, NoneType} + is_str = accept == {str} + is_str_or_none = accept == {str, NoneType} + is_robuffer = accept == {robuffer} + is_str_or_robuffer = accept == {str, robuffer} + is_str_or_robuffer_or_none = accept == {str, robuffer, NoneType} format_unit = None if encoding: self.encoding = encoding - if is_str and not length and not zeroes and not nullable: + if is_str and not length and not zeroes: format_unit = 'es' - elif is_str and length and zeroes and nullable: + elif is_str_or_none and length and zeroes: format_unit = 'es#' - elif is_b_or_ba and not length and not zeroes and not nullable: + elif is_b_or_ba and not length and not zeroes: format_unit = 'et' - elif is_b_or_ba and length and zeroes and nullable: + elif is_b_or_ba_or_none and length and zeroes: format_unit = 'et#' else: if zeroes: fail("str_converter: illegal combination of arguments (zeroes is only legal with an encoding)") - if is_str and not length and not nullable: + if is_str and not length: format_unit = 's' - elif is_str and not length and nullable: + elif is_str_or_none and not length: format_unit = 'z' - elif is_robuffer and not length and not nullable: + elif is_robuffer and not length: format_unit = 'y' - elif is_robuffer and length and not nullable: + elif is_robuffer and length: format_unit = 'y#' - elif is_str_or_robuffer and length and not nullable: + elif is_str_or_robuffer and length: format_unit = 's#' - elif is_str_or_robuffer and length and nullable: + elif is_str_or_robuffer_or_none and length: format_unit = 'z#' if not format_unit: @@ -2701,12 +2712,12 @@ class PyBytesObject_converter(CConverter): type = 'PyBytesObject *' format_unit = 'S' - # types = {'bytes'} + # accept = {bytes} class PyByteArrayObject_converter(CConverter): type = 'PyByteArrayObject *' format_unit = 'Y' - # types = {'bytearray'} + # accept = {bytearray} class unicode_converter(CConverter): type = 'PyObject *' @@ -2714,45 +2725,44 @@ format_unit = 'U' @add_legacy_c_converter('u#', length=True) -@add_legacy_c_converter('Z', nullable=True) -@add_legacy_c_converter('Z#', nullable=True, length=True) +@add_legacy_c_converter('Z', accept={str, NoneType}) +@add_legacy_c_converter('Z#', accept={str, NoneType}, length=True) class Py_UNICODE_converter(CConverter): type = 'Py_UNICODE *' default_type = (str, Null, NoneType) format_unit = 'u' - def converter_init(self, *, nullable=False, length=False): - format_unit = 'Z' if nullable else 'u' + def converter_init(self, *, accept={str}, length=False): + format_unit = 'Z' if accept=={str, NoneType} else 'u' if length: format_unit += '#' self.length = True self.format_unit = format_unit -@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'}) +@add_legacy_c_converter('s*', accept={str, buffer}) +@add_legacy_c_converter('z*', accept={str, buffer, NoneType}) +@add_legacy_c_converter('w*', accept={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={'buffer'}, nullable=False): + def converter_init(self, *, accept={buffer}): 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 - format_unit = None - if types == {'str', 'buffer'}: - format_unit = 's*' if not nullable else 'z*' + if accept == {str, buffer, NoneType}: + format_unit = 'z*' + elif accept == {str, buffer}: + format_unit = 's*' + elif accept == {buffer}: + format_unit = 'y*' + elif accept == {rwbuffer}: + format_unit = 'w*' else: - if nullable: - fail('Py_buffer_converter: illegal combination of arguments (nullable=True)') - elif types == {'buffer'}: - format_unit = 'y*' - elif types == {'rwbuffer'}: - format_unit = 'w*' - if not format_unit: fail("Py_buffer_converter: illegal combination of arguments") self.format_unit = format_unit @@ -3035,6 +3045,24 @@ 'return_value = PyUnicode_DecodeFSDefault(_return_value);\n') +def eval_ast_expr(node, globals, *, filename='-'): + """ + Takes an ast.Expr node. Compiles and evaluates it. + Returns the result of the expression. + + globals represents the globals dict the expression + should see. (There's no equivalent for "locals" here.) + """ + + if isinstance(node, ast.Expr): + node = node.value + + node = ast.Expression(node) + co = compile(node, filename, 'eval') + fn = types.FunctionType(co, globals) + return fn() + + class IndentStack: def __init__(self): self.indents = [] @@ -3622,7 +3650,7 @@ except SyntaxError: try: # the last = was probably inside a function call, like - # i: int(nullable=True) + # c: int(accept={str}) # so assume there was no actual default value. default = None ast_input = "def x({}): pass".format(line) @@ -3795,7 +3823,9 @@ fail("Annotations must be either a name, a function call, or a string.") name = annotation.func.id - kwargs = {node.arg: ast.literal_eval(node.value) for node in annotation.keywords} + symbols = globals() + + kwargs = {node.arg: eval_ast_expr(node.value, symbols) for node in annotation.keywords} return name, False, kwargs def parse_special_symbol(self, symbol):