diff --git a/Include/object.h b/Include/object.h --- a/Include/object.h +++ b/Include/object.h @@ -492,6 +492,9 @@ PyAPI_FUNC(unsigned int) PyType_ClearCache(void); PyAPI_FUNC(void) PyType_Modified(PyTypeObject *); +PyAPI_FUNC(PyObject *) _PyType_GetDocFromInternalDoc(const char *name, const char *internal_doc); +PyAPI_FUNC(PyObject *) _PyType_GetTextSignatureFromInternalDoc(const char *name, const char *internal_doc); + /* Generic operations on objects */ struct _Py_Identifier; #ifndef Py_LIMITED_API diff --git a/Lib/inspect.py b/Lib/inspect.py --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1419,9 +1419,11 @@ _WrapperDescriptor = type(type.__call__) _MethodWrapper = type(all.__call__) +_ClassMethodWrapper = type(int.__dict__['from_bytes']) _NonUserDefinedCallables = (_WrapperDescriptor, _MethodWrapper, + _ClassMethodWrapper, types.BuiltinFunctionType) @@ -1460,12 +1462,13 @@ if sig is not None: return sig - if isinstance(obj, types.FunctionType): return Signature.from_function(obj) - if isinstance(obj, types.BuiltinFunctionType): - return Signature.from_builtin(obj) + if isinstance(obj, _NonUserDefinedCallables) or ismethoddescriptor(obj): + sig = Signature.from_builtin(obj) + if sig: + return sig if isinstance(obj, functools.partial): sig = signature(obj.func) @@ -2066,6 +2069,15 @@ kind = Parameter.VAR_KEYWORD p(f.args.kwarg, empty) + # strip off self if it's already been bound + if parameters and parameters[0].name == 'self': + if isbuiltin(func) and getattr(func, '__self__', None): + parameters.pop(0) + else: + # for builtins, self parameter is always positional-only! + p = parameters[0].replace(kind=Parameter.POSITIONAL_ONLY) + parameters[0] = p + return cls(parameters, return_annotation=cls.empty) diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -1595,7 +1595,8 @@ "Signature information for builtins requires docstrings") def test_signature_on_builtins(self): # min doesn't have a signature (yet) - self.assertEqual(inspect.signature(min), None) + with self.assertRaises(ValueError): + inspect.signature(min) signature = inspect.signature(_testcapi.docstring_with_signature_with_defaults) self.assertTrue(isinstance(signature, inspect.Signature)) diff --git a/Modules/_cryptmodule.c b/Modules/_cryptmodule.c --- a/Modules/_cryptmodule.c +++ b/Modules/_cryptmodule.c @@ -30,7 +30,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(crypt_crypt__doc__, -"crypt(word, salt)\n" +"crypt(module, word, salt)\n" "Hash a *word* with the given *salt* and return the hashed password.\n" "\n" "*word* will usually be a user\'s password. *salt* (either a random 2 or 16\n" @@ -63,7 +63,7 @@ static PyObject * crypt_crypt_impl(PyModuleDef *module, const char *word, const char *salt) -/*[clinic end generated code: checksum=a137540bf6862f9935fc112b8bb1d62d6dd1ad02]*/ +/*[clinic end generated code: checksum=dbfe26a21eb335abefe6a0bbd0a682ea22b9adc0]*/ { /* On some platforms (AtheOS) crypt returns NULL for an invalid salt. Return None in that case. XXX Maybe raise an exception? */ diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -584,7 +584,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(curses_window_addch__doc__, -"addch([x, y,] ch, [attr])\n" +"addch(self, [x, y,] ch, [attr])\n" "Paint character ch at (y, x) with attributes attr.\n" "\n" " x\n" @@ -650,7 +650,7 @@ static PyObject * curses_window_addch_impl(PyObject *self, int group_left_1, int x, int y, PyObject *ch, int group_right_1, long attr) -/*[clinic end generated code: checksum=b073327add8197b6ba7fb96c87062422c8312954]*/ +/*[clinic end generated code: checksum=d36b1e845f8c5b424db59df9f2b7ec8e1cbc40e0]*/ { PyCursesWindowObject *cwself = (PyCursesWindowObject *)self; int coordinates_group = group_left_1; diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -4159,7 +4159,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(datetime_datetime_now__doc__, -"now(tz=None)\n" +"now(cls, tz=None)\n" "Returns new datetime object representing current time local to tz.\n" "\n" " tz\n" @@ -4192,7 +4192,7 @@ static PyObject * datetime_datetime_now_impl(PyTypeObject *cls, PyObject *tz) -/*[clinic end generated code: checksum=ca3d26a423b3f633b260c7622e303f0915a96f7c]*/ +/*[clinic end generated code: checksum=5db324b86b467dbf967a0a22f2057538843d11fb]*/ { PyObject *self; diff --git a/Modules/_dbmmodule.c b/Modules/_dbmmodule.c --- a/Modules/_dbmmodule.c +++ b/Modules/_dbmmodule.c @@ -279,7 +279,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(dbm_dbm_get__doc__, -"get(key, [default])\n" +"get(self, key, [default])\n" "Return the value for key if present, otherwise default."); #define DBM_DBM_GET_METHODDEF \ @@ -289,7 +289,7 @@ dbm_dbm_get_impl(dbmobject *dp, const char *key, Py_ssize_clean_t key_length, int group_right_1, PyObject *default_value); static PyObject * -dbm_dbm_get(PyObject *self, PyObject *args) +dbm_dbm_get(dbmobject *dp, PyObject *args) { PyObject *return_value = NULL; const char *key; @@ -311,14 +311,14 @@ PyErr_SetString(PyExc_TypeError, "dbm.dbm.get requires 1 to 2 arguments"); return NULL; } - return_value = dbm_dbm_get_impl((dbmobject *)self, key, key_length, group_right_1, default_value); + return_value = dbm_dbm_get_impl(dp, key, key_length, group_right_1, default_value); return return_value; } static PyObject * dbm_dbm_get_impl(dbmobject *dp, const char *key, Py_ssize_clean_t key_length, int group_right_1, PyObject *default_value) -/*[clinic end generated code: checksum=2c3209571267017f1b9abbd19e1b521849fd5d4a]*/ +/*[clinic end generated code: checksum=f19f39e772897721ab7f7525283adf046797faa9]*/ { datum dbm_key, val; @@ -461,7 +461,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(dbmopen__doc__, -"open(filename, flags=\'r\', mode=0o666)\n" +"open(module, filename, flags=\'r\', mode=0o666)\n" "Return a database object.\n" "\n" " filename\n" @@ -498,7 +498,7 @@ static PyObject * dbmopen_impl(PyModuleDef *module, const char *filename, const char *flags, int mode) -/*[clinic end generated code: checksum=fb265f75641553ccd963f84c143b35c11f9121fc]*/ +/*[clinic end generated code: checksum=9efae7d3c3b67a365011bf4e463e918901ba6c79]*/ { int iflags; diff --git a/Modules/_opcode.c b/Modules/_opcode.c --- a/Modules/_opcode.c +++ b/Modules/_opcode.c @@ -21,7 +21,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_opcode_stack_effect__doc__, -"stack_effect(opcode, [oparg])\n" +"stack_effect(module, opcode, [oparg])\n" "Compute the stack effect of the opcode."); #define _OPCODE_STACK_EFFECT_METHODDEF \ @@ -40,17 +40,17 @@ int _return_value; switch (PyTuple_GET_SIZE(args)) { - case 1: - if (!PyArg_ParseTuple(args, "i:stack_effect", &opcode)) + case 2: + if (!PyArg_ParseTuple(args, "i:stack_effect", &module, &opcode)) return NULL; break; - case 2: - if (!PyArg_ParseTuple(args, "ii:stack_effect", &opcode, &oparg)) + case 3: + if (!PyArg_ParseTuple(args, "ii:stack_effect", &module, &opcode, &oparg)) return NULL; group_right_1 = 1; break; default: - PyErr_SetString(PyExc_TypeError, "_opcode.stack_effect requires 1 to 2 arguments"); + PyErr_SetString(PyExc_TypeError, "_opcode.stack_effect requires 2 to 3 arguments"); return NULL; } _return_value = _opcode_stack_effect_impl(module, opcode, group_right_1, oparg); @@ -64,7 +64,7 @@ static int _opcode_stack_effect_impl(PyModuleDef *module, int opcode, int group_right_1, int oparg) -/*[clinic end generated code: checksum=47e76ec27523da4ab192713642d32482cd743aa4]*/ +/*[clinic end generated code: checksum=5e86663fffa112bf17ffe4f8abc0927827261063]*/ { int effect; if (HAS_ARG(opcode)) { diff --git a/Modules/_pickle.c b/Modules/_pickle.c --- a/Modules/_pickle.c +++ b/Modules/_pickle.c @@ -3889,7 +3889,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_pickle_Pickler_clear_memo__doc__, -"clear_memo()\n" +"clear_memo(self)\n" "Clears the pickler\'s \"memo\".\n" "\n" "The memo is the data structure that remembers which objects the\n" @@ -3904,18 +3904,18 @@ _pickle_Pickler_clear_memo_impl(PicklerObject *self); static PyObject * -_pickle_Pickler_clear_memo(PyObject *self, PyObject *Py_UNUSED(ignored)) +_pickle_Pickler_clear_memo(PicklerObject *self, PyObject *Py_UNUSED(ignored)) { PyObject *return_value = NULL; - return_value = _pickle_Pickler_clear_memo_impl((PicklerObject *)self); + return_value = _pickle_Pickler_clear_memo_impl(self); return return_value; } static PyObject * _pickle_Pickler_clear_memo_impl(PicklerObject *self) -/*[clinic end generated code: checksum=0574593b102fffb8e781d7bb9b536ceffc525ac1]*/ +/*[clinic end generated code: checksum=853ef8d6514b207a8aa1f4e5a794a1f54a55f576]*/ { if (self->memo) PyMemoTable_Clear(self->memo); @@ -3935,7 +3935,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_pickle_Pickler_dump__doc__, -"dump(obj)\n" +"dump(self, obj)\n" "Write a pickled representation of the given object to the open file."); #define _PICKLE_PICKLER_DUMP_METHODDEF \ @@ -3943,7 +3943,7 @@ static PyObject * _pickle_Pickler_dump(PicklerObject *self, PyObject *obj) -/*[clinic end generated code: checksum=b72a69ec98737fabf66dae7c5a3210178bdbd3e6]*/ +/*[clinic end generated code: checksum=36db7f67c8bc05ca6f17b8ab57c54d64bfd0539e]*/ { /* Check whether the Pickler was initialized correctly (issue3664). Developers often forget to call __init__() in their subclasses, which @@ -4048,7 +4048,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_pickle_Pickler___init____doc__, -"__init__(file, protocol=None, fix_imports=True)\n" +"__init__(self, file, protocol=None, fix_imports=True)\n" "This takes a binary file for writing a pickle data stream.\n" "\n" "The optional *protocol* argument tells the pickler to use the given\n" @@ -4072,7 +4072,7 @@ _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, PyObject *protocol, int fix_imports); static PyObject * -_pickle_Pickler___init__(PyObject *self, PyObject *args, PyObject *kwargs) +_pickle_Pickler___init__(PicklerObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; static char *_keywords[] = {"file", "protocol", "fix_imports", NULL}; @@ -4084,7 +4084,7 @@ "O|Op:__init__", _keywords, &file, &protocol, &fix_imports)) goto exit; - return_value = _pickle_Pickler___init___impl((PicklerObject *)self, file, protocol, fix_imports); + return_value = _pickle_Pickler___init___impl(self, file, protocol, fix_imports); exit: return return_value; @@ -4092,7 +4092,7 @@ static PyObject * _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, PyObject *protocol, int fix_imports) -/*[clinic end generated code: checksum=defa3d9e9f8b51fb257d4fdfca99db503db0e6df]*/ +/*[clinic end generated code: checksum=332b47d3de135ab1577b9a4bb5e133bc907258dc]*/ { _Py_IDENTIFIER(persistent_id); _Py_IDENTIFIER(dispatch_table); @@ -4147,7 +4147,7 @@ static int Pickler_init(PyObject *self, PyObject *args, PyObject *kwargs) { - PyObject *result = _pickle_Pickler___init__(self, args, kwargs); + PyObject *result = _pickle_Pickler___init__((PicklerObject *)self, args, kwargs); if (result == NULL) { return -1; } @@ -4179,7 +4179,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_pickle_PicklerMemoProxy_clear__doc__, -"clear()\n" +"clear(self)\n" "Remove all items from memo."); #define _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF \ @@ -4189,18 +4189,18 @@ _pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self); static PyObject * -_pickle_PicklerMemoProxy_clear(PyObject *self, PyObject *Py_UNUSED(ignored)) +_pickle_PicklerMemoProxy_clear(PicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored)) { PyObject *return_value = NULL; - return_value = _pickle_PicklerMemoProxy_clear_impl((PicklerMemoProxyObject *)self); + return_value = _pickle_PicklerMemoProxy_clear_impl(self); return return_value; } static PyObject * _pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self) -/*[clinic end generated code: checksum=c6ca252530ccb3ea2f4b33507b51b183f23b24c7]*/ +/*[clinic end generated code: checksum=93534cc095870012705a9561293b2ce16e44f86b]*/ { if (self->pickler->memo) PyMemoTable_Clear(self->pickler->memo); @@ -4216,7 +4216,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_pickle_PicklerMemoProxy_copy__doc__, -"copy()\n" +"copy(self)\n" "Copy the memo to a new object."); #define _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF \ @@ -4226,18 +4226,18 @@ _pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self); static PyObject * -_pickle_PicklerMemoProxy_copy(PyObject *self, PyObject *Py_UNUSED(ignored)) +_pickle_PicklerMemoProxy_copy(PicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored)) { PyObject *return_value = NULL; - return_value = _pickle_PicklerMemoProxy_copy_impl((PicklerMemoProxyObject *)self); + return_value = _pickle_PicklerMemoProxy_copy_impl(self); return return_value; } static PyObject * _pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self) -/*[clinic end generated code: checksum=808c4d5a37359ed5fb2efe81dbe5ff480719f470]*/ +/*[clinic end generated code: checksum=d6f0ab8a76f90f7e781cab29cf38f8bf80f9b834]*/ { Py_ssize_t i; PyMemoTable *memo; @@ -4283,7 +4283,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_pickle_PicklerMemoProxy___reduce____doc__, -"__reduce__()\n" +"__reduce__(self)\n" "Implement pickle support."); #define _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF \ @@ -4293,18 +4293,18 @@ _pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self); static PyObject * -_pickle_PicklerMemoProxy___reduce__(PyObject *self, PyObject *Py_UNUSED(ignored)) +_pickle_PicklerMemoProxy___reduce__(PicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored)) { PyObject *return_value = NULL; - return_value = _pickle_PicklerMemoProxy___reduce___impl((PicklerMemoProxyObject *)self); + return_value = _pickle_PicklerMemoProxy___reduce___impl(self); return return_value; } static PyObject * _pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self) -/*[clinic end generated code: checksum=2293152bdf53951a012d430767b608f5fb4213b5]*/ +/*[clinic end generated code: checksum=3909a9a3c25e8ed27cb2f1aeee9bb89886ebf8e3]*/ { PyObject *reduce_value, *dict_args; PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self); @@ -6326,7 +6326,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_pickle_Unpickler_load__doc__, -"load()\n" +"load(self)\n" "Load a pickle.\n" "\n" "Read a pickled object representation from the open file object given\n" @@ -6351,7 +6351,7 @@ static PyObject * _pickle_Unpickler_load_impl(PyObject *self) -/*[clinic end generated code: checksum=55f35fcaf034817e75c355ec50b7878577355899]*/ +/*[clinic end generated code: checksum=cdaa74dbbd7eaf8b0cb7332465084e1fef01dfc3]*/ { UnpicklerObject *unpickler = (UnpicklerObject*)self; @@ -6394,7 +6394,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_pickle_Unpickler_find_class__doc__, -"find_class(module_name, global_name)\n" +"find_class(self, module_name, global_name)\n" "Return an object from a specified module.\n" "\n" "If necessary, the module will be imported. Subclasses may override\n" @@ -6411,7 +6411,7 @@ _pickle_Unpickler_find_class_impl(UnpicklerObject *self, PyObject *module_name, PyObject *global_name); static PyObject * -_pickle_Unpickler_find_class(PyObject *self, PyObject *args) +_pickle_Unpickler_find_class(UnpicklerObject *self, PyObject *args) { PyObject *return_value = NULL; PyObject *module_name; @@ -6421,7 +6421,7 @@ "OO:find_class", &module_name, &global_name)) goto exit; - return_value = _pickle_Unpickler_find_class_impl((UnpicklerObject *)self, module_name, global_name); + return_value = _pickle_Unpickler_find_class_impl(self, module_name, global_name); exit: return return_value; @@ -6429,7 +6429,7 @@ static PyObject * _pickle_Unpickler_find_class_impl(UnpicklerObject *self, PyObject *module_name, PyObject *global_name) -/*[clinic end generated code: checksum=1f353d13a32c9d94feb1466b3c2d0529a7e5650e]*/ +/*[clinic end generated code: checksum=efa38f10372144ad65c951703fe0ba497f1b5da8]*/ { PyObject *global; PyObject *modules_dict; @@ -6612,7 +6612,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_pickle_Unpickler___init____doc__, -"__init__(file, *, fix_imports=True, encoding=\'ASCII\', errors=\'strict\')\n" +"__init__(self, file, *, fix_imports=True, encoding=\'ASCII\', errors=\'strict\')\n" "This takes a binary file for reading a pickle data stream.\n" "\n" "The protocol version of the pickle is detected automatically, so no\n" @@ -6638,7 +6638,7 @@ _pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, int fix_imports, const char *encoding, const char *errors); static PyObject * -_pickle_Unpickler___init__(PyObject *self, PyObject *args, PyObject *kwargs) +_pickle_Unpickler___init__(UnpicklerObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; static char *_keywords[] = {"file", "fix_imports", "encoding", "errors", NULL}; @@ -6651,7 +6651,7 @@ "O|$pss:__init__", _keywords, &file, &fix_imports, &encoding, &errors)) goto exit; - return_value = _pickle_Unpickler___init___impl((UnpicklerObject *)self, file, fix_imports, encoding, errors); + return_value = _pickle_Unpickler___init___impl(self, file, fix_imports, encoding, errors); exit: return return_value; @@ -6659,7 +6659,7 @@ static PyObject * _pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, int fix_imports, const char *encoding, const char *errors) -/*[clinic end generated code: checksum=26c1d4a06841a8e51d29a0c244ba7f4607ff358a]*/ +/*[clinic end generated code: checksum=5fa5a5ee6789d54d847967b299622fe9bcc3ce6e]*/ { _Py_IDENTIFIER(persistent_load); @@ -6705,7 +6705,7 @@ static int Unpickler_init(PyObject *self, PyObject *args, PyObject *kwargs) { - PyObject *result = _pickle_Unpickler___init__(self, args, kwargs); + PyObject *result = _pickle_Unpickler___init__((UnpicklerObject *)self, args, kwargs); if (result == NULL) { return -1; } @@ -6740,7 +6740,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_pickle_UnpicklerMemoProxy_clear__doc__, -"clear()\n" +"clear(self)\n" "Remove all items from memo."); #define _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF \ @@ -6750,18 +6750,18 @@ _pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self); static PyObject * -_pickle_UnpicklerMemoProxy_clear(PyObject *self, PyObject *Py_UNUSED(ignored)) +_pickle_UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored)) { PyObject *return_value = NULL; - return_value = _pickle_UnpicklerMemoProxy_clear_impl((UnpicklerMemoProxyObject *)self); + return_value = _pickle_UnpicklerMemoProxy_clear_impl(self); return return_value; } static PyObject * _pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self) -/*[clinic end generated code: checksum=e0f99c26d48444a3f58f598bec3190c66595fce7]*/ +/*[clinic end generated code: checksum=a5cf5f428ecd908bc85c757f5161a822f3edb8d5]*/ { _Unpickler_MemoCleanup(self->unpickler); self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size); @@ -6779,7 +6779,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_pickle_UnpicklerMemoProxy_copy__doc__, -"copy()\n" +"copy(self)\n" "Copy the memo to a new object."); #define _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF \ @@ -6789,18 +6789,18 @@ _pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self); static PyObject * -_pickle_UnpicklerMemoProxy_copy(PyObject *self, PyObject *Py_UNUSED(ignored)) +_pickle_UnpicklerMemoProxy_copy(UnpicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored)) { PyObject *return_value = NULL; - return_value = _pickle_UnpicklerMemoProxy_copy_impl((UnpicklerMemoProxyObject *)self); + return_value = _pickle_UnpicklerMemoProxy_copy_impl(self); return return_value; } static PyObject * _pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self) -/*[clinic end generated code: checksum=8c0ab91c0b694ea71a1774650898a760d1ab4765]*/ +/*[clinic end generated code: checksum=ed7e2bb366c9d91e92ffffe4da276d38b230b99e]*/ { Py_ssize_t i; PyObject *new_memo = PyDict_New(); @@ -6839,7 +6839,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_pickle_UnpicklerMemoProxy___reduce____doc__, -"__reduce__()\n" +"__reduce__(self)\n" "Implement pickling support."); #define _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF \ @@ -6849,18 +6849,18 @@ _pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self); static PyObject * -_pickle_UnpicklerMemoProxy___reduce__(PyObject *self, PyObject *Py_UNUSED(ignored)) +_pickle_UnpicklerMemoProxy___reduce__(UnpicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored)) { PyObject *return_value = NULL; - return_value = _pickle_UnpicklerMemoProxy___reduce___impl((UnpicklerMemoProxyObject *)self); + return_value = _pickle_UnpicklerMemoProxy___reduce___impl(self); return return_value; } static PyObject * _pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self) -/*[clinic end generated code: checksum=4ee76a65511291f0de2e9e63db395d2e5d6d8df6]*/ +/*[clinic end generated code: checksum=7d2971a9623664a1ade7d894ed6a8d04e59dedfb]*/ { PyObject *reduce_value; PyObject *constructor_args; @@ -7169,7 +7169,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_pickle_dump__doc__, -"dump(obj, file, protocol=None, *, fix_imports=True)\n" +"dump(module, obj, file, protocol=None, *, fix_imports=True)\n" "Write a pickled representation of obj to the open file object file.\n" "\n" "This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may\n" @@ -7220,7 +7220,7 @@ static PyObject * _pickle_dump_impl(PyModuleDef *module, PyObject *obj, PyObject *file, PyObject *protocol, int fix_imports) -/*[clinic end generated code: checksum=eb5c23e64da34477178230b704d2cc9c6b6650ea]*/ +/*[clinic end generated code: checksum=1d4ff873e13eb840ff275d716d8d4c5554af087c]*/ { PicklerObject *pickler = _Pickler_New(); @@ -7272,7 +7272,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_pickle_dumps__doc__, -"dumps(obj, protocol=None, *, fix_imports=True)\n" +"dumps(module, obj, protocol=None, *, fix_imports=True)\n" "Return the pickled representation of the object as a bytes object.\n" "\n" "The optional *protocol* argument tells the pickler to use the given\n" @@ -7314,7 +7314,7 @@ static PyObject * _pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol, int fix_imports) -/*[clinic end generated code: checksum=e9b915d61202a9692cb6c6718db74fe54fc9c4d1]*/ +/*[clinic end generated code: checksum=9c6c0291ef2d2b0856b7d4caecdcb7bad13a23b3]*/ { PyObject *result; PicklerObject *pickler = _Pickler_New(); @@ -7373,7 +7373,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_pickle_load__doc__, -"load(file, *, fix_imports=True, encoding=\'ASCII\', errors=\'strict\')\n" +"load(module, file, *, fix_imports=True, encoding=\'ASCII\', errors=\'strict\')\n" "Read and return an object from the pickle data stored in a file.\n" "\n" "This is equivalent to ``Unpickler(file).load()``, but may be more\n" @@ -7426,7 +7426,7 @@ static PyObject * _pickle_load_impl(PyModuleDef *module, PyObject *file, int fix_imports, const char *encoding, const char *errors) -/*[clinic end generated code: checksum=b41f06970e57acf2fd602e4b7f88e3f3e1e53087]*/ +/*[clinic end generated code: checksum=2b5b7e5e3a836cf1c53377ce9274a84a8bceef67]*/ { PyObject *result; UnpicklerObject *unpickler = _Unpickler_New(); @@ -7478,7 +7478,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_pickle_loads__doc__, -"loads(data, *, fix_imports=True, encoding=\'ASCII\', errors=\'strict\')\n" +"loads(module, data, *, fix_imports=True, encoding=\'ASCII\', errors=\'strict\')\n" "Read and return an object from the given pickle data.\n" "\n" "The protocol version of the pickle is detected automatically, so no\n" @@ -7522,7 +7522,7 @@ static PyObject * _pickle_loads_impl(PyModuleDef *module, PyObject *data, int fix_imports, const char *encoding, const char *errors) -/*[clinic end generated code: checksum=0663de43aca6c21508a777e29d98c9c3a6e7f72d]*/ +/*[clinic end generated code: checksum=7b21a75997c8f6636e4bf48c663b28f2bfd4eb6a]*/ { PyObject *result; UnpicklerObject *unpickler = _Unpickler_New(); diff --git a/Modules/_sre.c b/Modules/_sre.c --- a/Modules/_sre.c +++ b/Modules/_sre.c @@ -541,7 +541,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(pattern_match__doc__, -"match(pattern, pos=0, endpos=sys.maxsize)\n" +"match(self, pattern, pos=0, endpos=sys.maxsize)\n" "Matches zero or more characters at the beginning of the string."); #define PATTERN_MATCH_METHODDEF \ @@ -551,7 +551,7 @@ pattern_match_impl(PatternObject *self, PyObject *pattern, Py_ssize_t pos, Py_ssize_t endpos); static PyObject * -pattern_match(PyObject *self, PyObject *args, PyObject *kwargs) +pattern_match(PatternObject *self, PyObject *args, PyObject *kwargs) { PyObject *return_value = NULL; static char *_keywords[] = {"pattern", "pos", "endpos", NULL}; @@ -563,7 +563,7 @@ "O|nn:match", _keywords, &pattern, &pos, &endpos)) goto exit; - return_value = pattern_match_impl((PatternObject *)self, pattern, pos, endpos); + return_value = pattern_match_impl(self, pattern, pos, endpos); exit: return return_value; @@ -571,7 +571,7 @@ static PyObject * pattern_match_impl(PatternObject *self, PyObject *pattern, Py_ssize_t pos, Py_ssize_t endpos) -/*[clinic end generated code: checksum=63e59c5f3019efe6c1f3acdec42b2d3595e14a09]*/ +/*[clinic end generated code: checksum=4a3865d13638cb7c13dcae1fe58c1a9c35071998]*/ { SRE_STATE state; Py_ssize_t status; diff --git a/Modules/_weakref.c b/Modules/_weakref.c --- a/Modules/_weakref.c +++ b/Modules/_weakref.c @@ -20,7 +20,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_weakref_getweakrefcount__doc__, -"getweakrefcount(object)\n" +"getweakrefcount(module, object)\n" "Return the number of weak references to \'object\'."); #define _WEAKREF_GETWEAKREFCOUNT_METHODDEF \ @@ -45,7 +45,7 @@ static Py_ssize_t _weakref_getweakrefcount_impl(PyModuleDef *module, PyObject *object) -/*[clinic end generated code: checksum=436e8fbe0297434375f039d8c2d9fc3a9bbe773c]*/ +/*[clinic end generated code: checksum=9041f91e714796266bf54c3e3c938f11114f7bfb]*/ { PyWeakReference **list; diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -2430,7 +2430,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(os_stat__doc__, -"stat(path, *, dir_fd=None, follow_symlinks=True)\n" +"stat(module, path, *, dir_fd=None, follow_symlinks=True)\n" "Perform a stat system call on the given path.\n" "\n" " path\n" @@ -2481,7 +2481,7 @@ static PyObject * os_stat_impl(PyModuleDef *module, path_t *path, int dir_fd, int follow_symlinks) -/*[clinic end generated code: checksum=85a71ad602e89f8e280118da976f70cd2f9abdf1]*/ +/*[clinic end generated code: checksum=09cc91b4947f9e3b9335c8be998bb7c56f7f8b40]*/ { return posix_do_stat("stat", path, dir_fd, follow_symlinks); } @@ -2562,7 +2562,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(os_access__doc__, -"access(path, mode, *, dir_fd=None, effective_ids=False, follow_symlinks=True)\n" +"access(module, path, mode, *, dir_fd=None, effective_ids=False, follow_symlinks=True)\n" "Use the real uid/gid to test for access to a path.\n" "\n" " path\n" @@ -2622,7 +2622,7 @@ static PyObject * os_access_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd, int effective_ids, int follow_symlinks) -/*[clinic end generated code: checksum=636e835c36562a2fc11acab75314634127fdf769]*/ +/*[clinic end generated code: checksum=6483a51e1fee83da4f8e41cbc8054a701cfed1c5]*/ { PyObject *return_value = NULL; @@ -2718,7 +2718,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(os_ttyname__doc__, -"ttyname(fd)\n" +"ttyname(module, fd)\n" "Return the name of the terminal device connected to \'fd\'.\n" "\n" " fd\n" @@ -2752,7 +2752,7 @@ static char * os_ttyname_impl(PyModuleDef *module, int fd) -/*[clinic end generated code: checksum=0f368134dc0a7f21f25185e2e6bacf7675fb473a]*/ +/*[clinic end generated code: checksum=11bbb8b7969155f54bb8a1ec35ac1ebdfd4b0fec]*/ { char *ret; diff --git a/Modules/unicodedata.c b/Modules/unicodedata.c --- a/Modules/unicodedata.c +++ b/Modules/unicodedata.c @@ -129,7 +129,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(unicodedata_UCD_decimal__doc__, -"decimal(unichr, default=None)\n" +"decimal(self, unichr, default=None)\n" "Converts a Unicode character into its equivalent decimal value.\n" "\n" "Returns the decimal value assigned to the Unicode character unichr\n" @@ -161,7 +161,7 @@ static PyObject * unicodedata_UCD_decimal_impl(PyObject *self, PyUnicodeObject *unichr, PyObject *default_value) -/*[clinic end generated code: checksum=73edde0e9cd5913ea174c4fa81504369761b7426]*/ +/*[clinic end generated code: checksum=01826b179d497d8fd3842c56679ecbd4faddaa95]*/ { PyUnicodeObject *v = (PyUnicodeObject *)unichr; int have_old = 0; diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c --- a/Modules/zlibmodule.c +++ b/Modules/zlibmodule.c @@ -180,7 +180,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(zlib_compress__doc__, -"compress(bytes, [level])\n" +"compress(module, bytes, [level])\n" "Returns compressed string.\n" "\n" " bytes\n" @@ -203,17 +203,17 @@ int level = 0; switch (PyTuple_GET_SIZE(args)) { - case 1: - if (!PyArg_ParseTuple(args, "y*:compress", &bytes)) + case 2: + if (!PyArg_ParseTuple(args, "y*:compress", &module, &bytes)) return NULL; break; - case 2: - if (!PyArg_ParseTuple(args, "y*i:compress", &bytes, &level)) + case 3: + if (!PyArg_ParseTuple(args, "y*i:compress", &module, &bytes, &level)) return NULL; group_right_1 = 1; break; default: - PyErr_SetString(PyExc_TypeError, "zlib.compress requires 1 to 2 arguments"); + PyErr_SetString(PyExc_TypeError, "zlib.compress requires 2 to 3 arguments"); return NULL; } return_value = zlib_compress_impl(module, &bytes, group_right_1, level); @@ -227,7 +227,7 @@ static PyObject * zlib_compress_impl(PyModuleDef *module, Py_buffer *bytes, int group_right_1, int level) -/*[clinic end generated code: checksum=66c4d16d0b8b9dd423648d9ef00d6a89d3363665]*/ +/*[clinic end generated code: checksum=b0da42799f18332ad0c02fcb73ff564714fd3046]*/ { PyObject *ReturnVal = NULL; Byte *input, *output = NULL; @@ -765,7 +765,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(zlib_Decompress_decompress__doc__, -"decompress(data, max_length=0)\n" +"decompress(self, data, max_length=0)\n" "Return a string containing the decompressed version of the data.\n" "\n" " data\n" @@ -786,7 +786,7 @@ zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data, unsigned int max_length); static PyObject * -zlib_Decompress_decompress(PyObject *self, PyObject *args) +zlib_Decompress_decompress(compobject *self, PyObject *args) { PyObject *return_value = NULL; Py_buffer data = {NULL, NULL}; @@ -796,7 +796,7 @@ "y*|O&:decompress", &data, uint_converter, &max_length)) goto exit; - return_value = zlib_Decompress_decompress_impl((compobject *)self, &data, max_length); + return_value = zlib_Decompress_decompress_impl(self, &data, max_length); exit: /* Cleanup for data */ @@ -808,7 +808,7 @@ static PyObject * zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data, unsigned int max_length) -/*[clinic end generated code: checksum=e0058024c4a97b411d2e2197791b89fde175f76f]*/ +/*[clinic end generated code: checksum=b7fd2e3b23430f57f5a84817189575bc46464901]*/ { int err; unsigned int old_length, length = DEFAULTALLOC; @@ -1035,7 +1035,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(zlib_Compress_copy__doc__, -"copy()\n" +"copy(self)\n" "Return a copy of the compression object."); #define ZLIB_COMPRESS_COPY_METHODDEF \ @@ -1045,18 +1045,18 @@ zlib_Compress_copy_impl(compobject *self); static PyObject * -zlib_Compress_copy(PyObject *self, PyObject *Py_UNUSED(ignored)) +zlib_Compress_copy(compobject *self, PyObject *Py_UNUSED(ignored)) { PyObject *return_value = NULL; - return_value = zlib_Compress_copy_impl((compobject *)self); + return_value = zlib_Compress_copy_impl(self); return return_value; } static PyObject * zlib_Compress_copy_impl(compobject *self) -/*[clinic end generated code: checksum=2f454ee15be3bc53cfb4e845c3f891f68be4c8e4]*/ +/*[clinic end generated code: checksum=82046ecc689a3b78ce3cb144035518bf7a86d405]*/ { compobject *retval = NULL; int err; diff --git a/Objects/descrobject.c b/Objects/descrobject.c --- a/Objects/descrobject.c +++ b/Objects/descrobject.c @@ -350,14 +350,21 @@ return result; } + static PyObject * method_get_doc(PyMethodDescrObject *descr, void *closure) { - if (descr->d_method->ml_doc == NULL) { - Py_INCREF(Py_None); - return Py_None; - } - return PyUnicode_FromString(descr->d_method->ml_doc); + const char *name = descr->d_method->ml_name; + const char *doc = descr->d_method->ml_doc; + return _PyType_GetDocFromInternalDoc(name, doc); +} + +static PyObject * +method_get_text_signature(PyMethodDescrObject *descr, void *closure) +{ + const char *name = descr->d_method->ml_name; + const char *doc = descr->d_method->ml_doc; + return _PyType_GetTextSignatureFromInternalDoc(name, doc); } static PyObject * @@ -425,22 +432,30 @@ static PyGetSetDef method_getset[] = { {"__doc__", (getter)method_get_doc}, {"__qualname__", (getter)descr_get_qualname}, + {"__text_signature__", (getter)method_get_text_signature}, {0} }; static PyObject * member_get_doc(PyMemberDescrObject *descr, void *closure) { - if (descr->d_member->doc == NULL) { - Py_INCREF(Py_None); - return Py_None; - } - return PyUnicode_FromString(descr->d_member->doc); + const char *name = descr->d_member->name; + const char *doc = descr->d_member->doc; + return _PyType_GetDocFromInternalDoc(name, doc); +} + +static PyObject * +member_get_text_signature(PyMemberDescrObject *descr, void *closure) +{ + const char *name = descr->d_member->name; + const char *doc = descr->d_member->doc; + return _PyType_GetTextSignatureFromInternalDoc(name, doc); } static PyGetSetDef member_getset[] = { {"__doc__", (getter)member_get_doc}, {"__qualname__", (getter)descr_get_qualname}, + {"__text_signature__", (getter)member_get_text_signature}, {0} }; @@ -463,16 +478,23 @@ static PyObject * wrapperdescr_get_doc(PyWrapperDescrObject *descr, void *closure) { - if (descr->d_base->doc == NULL) { - Py_INCREF(Py_None); - return Py_None; - } - return PyUnicode_FromString(descr->d_base->doc); + const char *name = descr->d_base->name; + const char *doc = descr->d_base->doc; + return _PyType_GetDocFromInternalDoc(name, doc); +} + +static PyObject * +wrapperdescr_get_text_signature(PyWrapperDescrObject *descr, void *closure) +{ + const char *name = descr->d_base->name; + const char *doc = descr->d_base->doc; + return _PyType_GetTextSignatureFromInternalDoc(name, doc); } static PyGetSetDef wrapperdescr_getset[] = { {"__doc__", (getter)wrapperdescr_get_doc}, {"__qualname__", (getter)descr_get_qualname}, + {"__text_signature__", (getter)wrapperdescr_get_text_signature}, {0} }; @@ -1143,19 +1165,23 @@ } static PyObject * -wrapper_doc(wrapperobject *wp) +wrapper_doc(wrapperobject *wp, void *closure) { - const char *s = wp->descr->d_base->doc; + const char *name = wp->descr->d_base->name; + const char *doc = wp->descr->d_base->doc; + return _PyType_GetDocFromInternalDoc(name, doc); +} - if (s == NULL) { - Py_INCREF(Py_None); - return Py_None; - } - else { - return PyUnicode_FromString(s); - } +static PyObject * +wrapper_text_signature(wrapperobject *wp, void *closure) +{ + const char *name = wp->descr->d_base->name; + const char *doc = wp->descr->d_base->doc; + return _PyType_GetTextSignatureFromInternalDoc(name, doc); } + + static PyObject * wrapper_qualname(wrapperobject *wp) { @@ -1167,6 +1193,7 @@ {"__name__", (getter)wrapper_name}, {"__qualname__", (getter)wrapper_qualname}, {"__doc__", (getter)wrapper_doc}, + {"__text_signature__", (getter)wrapper_text_signature}, {0} }; diff --git a/Objects/dictobject.c b/Objects/dictobject.c --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -2176,7 +2176,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(dict___contains____doc__, -"__contains__(key)\n" +"__contains__(self, key)\n" "True if D has a key k, else False."); #define DICT___CONTAINS___METHODDEF \ @@ -2184,7 +2184,7 @@ static PyObject * dict___contains__(PyObject *self, PyObject *key) -/*[clinic end generated code: checksum=402ddb624ba1e4db764bfdfbbee6c1c59d1a11fa]*/ +/*[clinic end generated code: checksum=c4f85a39baac4776c4275ad5f072f7732c5f0806]*/ { register PyDictObject *mp = (PyDictObject *)self; Py_hash_t hash; diff --git a/Objects/methodobject.c b/Objects/methodobject.c --- a/Objects/methodobject.c +++ b/Objects/methodobject.c @@ -179,75 +179,20 @@ {NULL, NULL} }; -/* - * finds the docstring's introspection signature. - * if present, returns a pointer pointing to the first '('. - * otherwise returns NULL. - */ -static const char *find_signature(PyCFunctionObject *m) -{ - const char *trace = m->m_ml->ml_doc; - const char *name = m->m_ml->ml_name; - size_t length; - if (!trace || !name) - return NULL; - length = strlen(name); - if (strncmp(trace, name, length)) - return NULL; - trace += length; - if (*trace != '(') - return NULL; - return trace; -} - -/* - * skips to the end of the docstring's instrospection signature. - */ -static const char *skip_signature(const char *trace) -{ - while (*trace && *trace != '\n') - trace++; - return trace; -} - -static const char *skip_eols(const char *trace) -{ - while (*trace == '\n') - trace++; - return trace; -} - static PyObject * meth_get__text_signature__(PyCFunctionObject *m, void *closure) { - const char *start = find_signature(m); - const char *trace; - - if (!start) { - Py_INCREF(Py_None); - return Py_None; - } - - trace = skip_signature(start); - return PyUnicode_FromStringAndSize(start, trace - start); + const char *name = m->m_ml->ml_name; + const char *doc = m->m_ml->ml_doc; + return _PyType_GetTextSignatureFromInternalDoc(name, doc); } static PyObject * meth_get__doc__(PyCFunctionObject *m, void *closure) { - const char *doc = find_signature(m); - - if (doc) - doc = skip_eols(skip_signature(doc)); - else - doc = m->m_ml->ml_doc; - - if (!doc) { - Py_INCREF(Py_None); - return Py_None; - } - - return PyUnicode_FromString(doc); + const char *name = m->m_ml->ml_name; + const char *doc = m->m_ml->ml_doc; + return _PyType_GetDocFromInternalDoc(name, doc); } static PyObject * diff --git a/Objects/typeobject.c b/Objects/typeobject.c --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -54,6 +54,80 @@ static PyObject * slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds); + +/* + * finds the docstring's introspection signature. + * if present, returns a pointer pointing to the first '('. + * otherwise returns NULL. + */ +static const char * +find_signature(const char *name, const char *doc) +{ + size_t length; + if (!doc || !name) + return NULL; + length = strlen(name); + if (strncmp(doc, name, length)) + return NULL; + doc += length; + if (*doc != '(') + return NULL; + return doc; +} + +/* + * skips to the end of the docstring's instrospection signature. + */ +static const char * +skip_signature(const char *doc) +{ + while (*doc && *doc != '\n') + doc++; + return doc; +} + +static const char * +skip_eols(const char *trace) +{ + while (*trace == '\n') + trace++; + return trace; +} + +PyObject * +_PyType_GetDocFromInternalDoc(const char *name, const char *internal_doc) +{ + const char *signature = find_signature(name, internal_doc); + const char *doc; + + if (signature) + doc = skip_eols(skip_signature(signature)); + else + doc = internal_doc; + + if (!doc) { + Py_INCREF(Py_None); + return Py_None; + } + + return PyUnicode_FromString(doc); +} + +PyObject * +_PyType_GetTextSignatureFromInternalDoc(const char *name, const char *internal_doc) +{ + const char *signature = find_signature(name, internal_doc); + const char *doc; + + if (!signature) { + Py_INCREF(Py_None); + return Py_None; + } + + doc = skip_signature(signature); + return PyUnicode_FromStringAndSize(signature, doc - signature); +} + unsigned int PyType_ClearCache(void) { @@ -3480,7 +3554,7 @@ { PyObject **dict; dict = _PyObject_GetDictPtr(obj); - /* It is possible that the object's dict is not initialized + /* It is possible that the object's dict is not initialized yet. In this case, we will return None for the state. We also return None if the dict is empty to make the behavior consistent regardless whether the dict was initialized or not. @@ -3788,7 +3862,7 @@ Py_DECREF(state); Py_DECREF(listitems); Py_DECREF(dictitems); - return result; + return result; } static PyObject * @@ -3813,7 +3887,7 @@ } else if (kwargs != NULL) { if (PyDict_Size(kwargs) > 0) { - PyErr_SetString(PyExc_ValueError, + PyErr_SetString(PyExc_ValueError, "must use protocol 4 or greater to copy this " "object; since __getnewargs_ex__ returned " "keyword arguments."); diff --git a/Python/import.c b/Python/import.c --- a/Python/import.c +++ b/Python/import.c @@ -232,7 +232,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_imp_lock_held__doc__, -"lock_held()\n" +"lock_held(module)\n" "Return True if the import lock is currently held, else False.\n" "\n" "On platforms without threads, return False."); @@ -255,7 +255,7 @@ static PyObject * _imp_lock_held_impl(PyModuleDef *module) -/*[clinic end generated code: checksum=c5858b257881f94dee95526229a8d1a57ccff158]*/ +/*[clinic end generated code: checksum=5f0fda2bbaf1d5f0990624e1aa3c2dfd3443ce42]*/ { #ifdef WITH_THREAD return PyBool_FromLong(import_lock_thread != -1); @@ -274,7 +274,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_imp_acquire_lock__doc__, -"acquire_lock()\n" +"acquire_lock(module)\n" "Acquires the interpreter\'s import lock for the current thread.\n" "\n" "This lock should be used by import hooks to ensure thread-safety when importing\n" @@ -298,7 +298,7 @@ static PyObject * _imp_acquire_lock_impl(PyModuleDef *module) -/*[clinic end generated code: checksum=badb56ed0079a6b902c9616fe068d572765b1863]*/ +/*[clinic end generated code: checksum=a0871f98761c265775ed64582c619b56dbe62e10]*/ { #ifdef WITH_THREAD _PyImport_AcquireLock(); @@ -316,7 +316,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_imp_release_lock__doc__, -"release_lock()\n" +"release_lock(module)\n" "Release the interpreter\'s import lock.\n" "\n" "On platforms without threads, this function does nothing."); @@ -339,7 +339,7 @@ static PyObject * _imp_release_lock_impl(PyModuleDef *module) -/*[clinic end generated code: checksum=f1c2a75e3136a113184e0af2a676d5f0b5b685b4]*/ +/*[clinic end generated code: checksum=c3aae437d1986abd6ae6d51f7d4ddefbdab50f7f]*/ { #ifdef WITH_THREAD if (_PyImport_ReleaseLock() < 0) { @@ -939,7 +939,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_imp__fix_co_filename__doc__, -"_fix_co_filename(code, path)\n" +"_fix_co_filename(module, code, path)\n" "Changes code.co_filename to specify the passed-in file path.\n" "\n" " code\n" @@ -972,7 +972,7 @@ static PyObject * _imp__fix_co_filename_impl(PyModuleDef *module, PyCodeObject *code, PyObject *path) -/*[clinic end generated code: checksum=4f55bad308072b30ad1921068fc4ce85bd2b39bf]*/ +/*[clinic end generated code: checksum=d32cf2b2e0480c714f909921cc9e55d763b39dd5]*/ { update_compiled_module(code, path); @@ -1835,7 +1835,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_imp_extension_suffixes__doc__, -"extension_suffixes()\n" +"extension_suffixes(module)\n" "Returns the list of file suffixes used to identify extension modules."); #define _IMP_EXTENSION_SUFFIXES_METHODDEF \ @@ -1856,7 +1856,7 @@ static PyObject * _imp_extension_suffixes_impl(PyModuleDef *module) -/*[clinic end generated code: checksum=835921e67fd698e22e101eea64839d1ad62b6451]*/ +/*[clinic end generated code: checksum=12b12b8510ccf266d3ee6274be3e5cf203cd841b]*/ { PyObject *list; const char *suffix; @@ -1894,7 +1894,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_imp_init_builtin__doc__, -"init_builtin(name)\n" +"init_builtin(module, name)\n" "Initializes a built-in module."); #define _IMP_INIT_BUILTIN_METHODDEF \ @@ -1921,7 +1921,7 @@ static PyObject * _imp_init_builtin_impl(PyModuleDef *module, PyObject *name) -/*[clinic end generated code: checksum=59239206e5b2fb59358066e72fd0e72e55a7baf5]*/ +/*[clinic end generated code: checksum=a4e4805a523757cd3ddfeec6e5b16740678fed6a]*/ { int ret; PyObject *m; @@ -1948,7 +1948,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_imp_init_frozen__doc__, -"init_frozen(name)\n" +"init_frozen(module, name)\n" "Initializes a frozen module."); #define _IMP_INIT_FROZEN_METHODDEF \ @@ -1975,7 +1975,7 @@ static PyObject * _imp_init_frozen_impl(PyModuleDef *module, PyObject *name) -/*[clinic end generated code: checksum=503fcc3de9961263e4d9484259af357a7d287a0b]*/ +/*[clinic end generated code: checksum=2a58c119dd3e121cf5a9924f936cfd7b40253c12]*/ { int ret; PyObject *m; @@ -2002,7 +2002,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_imp_get_frozen_object__doc__, -"get_frozen_object(name)\n" +"get_frozen_object(module, name)\n" "Create a code object for a frozen module."); #define _IMP_GET_FROZEN_OBJECT_METHODDEF \ @@ -2029,7 +2029,7 @@ static PyObject * _imp_get_frozen_object_impl(PyModuleDef *module, PyObject *name) -/*[clinic end generated code: checksum=7a6423a4daf139496b9a394ff3ac6130089d1cba]*/ +/*[clinic end generated code: checksum=94c9108b58dda80d187fef21275a009bd0f91e96]*/ { return get_frozen_object(name); } @@ -2044,7 +2044,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_imp_is_frozen_package__doc__, -"is_frozen_package(name)\n" +"is_frozen_package(module, name)\n" "Returns True if the module name is of a frozen package."); #define _IMP_IS_FROZEN_PACKAGE_METHODDEF \ @@ -2071,7 +2071,7 @@ static PyObject * _imp_is_frozen_package_impl(PyModuleDef *module, PyObject *name) -/*[clinic end generated code: checksum=dc7e361ea30b6945b8bbe7266d7b9a5ea433b510]*/ +/*[clinic end generated code: checksum=17a342b94dbe859cdfc361bc8a6bc1b3cb163364]*/ { return is_frozen_package(name); } @@ -2086,7 +2086,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_imp_is_builtin__doc__, -"is_builtin(name)\n" +"is_builtin(module, name)\n" "Returns True if the module name corresponds to a built-in module."); #define _IMP_IS_BUILTIN_METHODDEF \ @@ -2113,7 +2113,7 @@ static PyObject * _imp_is_builtin_impl(PyModuleDef *module, PyObject *name) -/*[clinic end generated code: checksum=353938c1d55210a1e3850d3ccba7539d02165cac]*/ +/*[clinic end generated code: checksum=51c6139dcfd9bee1f40980ea68b7797f8489d69a]*/ { return PyLong_FromLong(is_builtin(name)); } @@ -2128,7 +2128,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_imp_is_frozen__doc__, -"is_frozen(name)\n" +"is_frozen(module, name)\n" "Returns True if the module name corresponds to a frozen module."); #define _IMP_IS_FROZEN_METHODDEF \ @@ -2155,7 +2155,7 @@ static PyObject * _imp_is_frozen_impl(PyModuleDef *module, PyObject *name) -/*[clinic end generated code: checksum=978b547ddcb76fa6c4a181ad53569c9acf382c7b]*/ +/*[clinic end generated code: checksum=4b079fb45a495835056ea5604735d552d222be5c]*/ { const struct _frozen *p; @@ -2177,7 +2177,7 @@ [clinic start generated code]*/ PyDoc_STRVAR(_imp_load_dynamic__doc__, -"load_dynamic(name, path, file=None)\n" +"load_dynamic(module, name, path, file=None)\n" "Loads an extension module."); #define _IMP_LOAD_DYNAMIC_METHODDEF \ @@ -2206,7 +2206,7 @@ static PyObject * _imp_load_dynamic_impl(PyModuleDef *module, PyObject *name, PyObject *path, PyObject *file) -/*[clinic end generated code: checksum=6795f65d9ce003ccaf08e4e8eef484dc52e262d0]*/ +/*[clinic end generated code: checksum=63e051fd0d0350c785bf185be41b0892f9920622]*/ { PyObject *mod; FILE *fp; diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -122,13 +122,11 @@ # though it's called c_keywords, really it's a list of parameter names # that are okay in Python but aren't a good idea in C. so if they're used # Argument Clinic will add "_value" to the end of the name in C. -# (We added "args", "type", "module", "self", "cls", and "null" -# just to be safe, even though they're not C keywords.) c_keywords = set(""" -args asm auto break case char cls const continue default do double -else enum extern float for goto if inline int long module null -register return self short signed sizeof static struct switch -type typedef typeof union unsigned void volatile while +asm auto break case char const continue default do double +else enum extern float for goto if inline int long +register return short signed sizeof static struct switch +typedef typeof union unsigned void volatile while """.strip().split()) def ensure_legal_c_identifier(s): @@ -578,7 +576,7 @@ # five arguments would map to B+C, not C+D. add, output = text_accumulator() - parameters = list(f.parameters.values()) + parameters = list(f.selfless.values()) groups = [] group = None @@ -683,39 +681,41 @@ positional = has_option_groups = False - if parameters: - last_group = 0 - - for p in parameters: - c = p.converter - - # insert group variable - group = p.group - if last_group != group: - last_group = group - if group: - group_name = self.group_to_variable_name(group) - data.impl_arguments.append(group_name) - data.declarations.append("int " + group_name + " = 0;") - data.impl_parameters.append("int " + group_name) - has_option_groups = True - c.render(p, data) - - positional = parameters[-1].kind == inspect.Parameter.POSITIONAL_ONLY - if has_option_groups and (not positional): - fail("You cannot use optional groups ('[' and ']')\nunless all parameters are positional-only ('/').") - - # now insert our "self" (or whatever) parameters - # (we deliberately don't call render on self converters) - stock_self = self_converter('self', f) - template_dict['self_name'] = stock_self.name - template_dict['self_type'] = stock_self.type - data.impl_parameters.insert(0, f.self_converter.type + ("" if f.self_converter.type.endswith('*') else " ") + f.self_converter.name) - if f.self_converter.type != stock_self.type: - self_cast = '(' + f.self_converter.type + ')' - else: - self_cast = '' - data.impl_arguments.insert(0, self_cast + stock_self.name) + assert parameters + + last_group = 0 + + for p in parameters: + c = p.converter + + # insert group variable + group = p.group + if last_group != group: + last_group = group + if group: + group_name = self.group_to_variable_name(group) + data.impl_arguments.append(group_name) + data.declarations.append("int " + group_name + " = 0;") + data.impl_parameters.append("int " + group_name) + has_option_groups = True + c.render(p, data) + + positional = parameters[-1].kind == inspect.Parameter.POSITIONAL_ONLY + if has_option_groups and (not positional): + fail("You cannot use optional groups ('[' and ']')\nunless all parameters are positional-only ('/').") + + values = list(f.parameters.values()) + p_self = values[0] + assert isinstance(p_self.converter, self_converter), "No self parameter in function!" + template_dict['self_name'] = p_self.converter.name + template_dict['self_type'] = p_self.converter.type + + # data.impl_parameters.insert(0, f.self_converter.type + ("" if f.self_converter.type.endswith('*') else " ") + f.self_converter.name) + # if f.self_converter.type != stock_self.type: + # self_cast = '(' + f.self_converter.type + ')' + # else: + # self_cast = '' + # data.impl_arguments.insert(0, self_cast + stock_self.name) f.return_converter.render(f, data) template_dict['impl_return_type'] = f.return_converter.type @@ -736,6 +736,10 @@ default_return_converter = (not f.return_converter or f.return_converter.type == 'PyObject *') + if parameters and isinstance(parameters[0].converter, self_converter): + parameters.pop(0) + converters.pop(0) + if not parameters: template = self.meth_noargs_template(f.methoddef_flags) elif (len(parameters) == 1 and @@ -1296,6 +1300,7 @@ return_converter, return_annotation=_empty, docstring=None, kind=CALLABLE, coexist=False): self.parameters = parameters or collections.OrderedDict() + self.selfless = None self.return_annotation = return_annotation self.name = name self.full_name = full_name @@ -1470,6 +1475,10 @@ # Only used by format units ending with '#'. length = False + # Should we show this parameter in the generated + # __text_signature__? This is *almost* always True. + show_in_signature = True + def __init__(self, name, function, default=unspecified, *, c_default=None, py_default=None, annotation=unspecified, **kwargs): self.function = function self.name = name @@ -1497,30 +1506,36 @@ def is_optional(self): return (self.default is not unspecified) - def render(self, parameter, data): - """ - parameter is a clinic.Parameter instance. - data is a CRenderData instance. - """ + def _render_self(self, parameter, data): self.parameter = parameter original_name = self.name name = ensure_legal_c_identifier(original_name) - # declarations - d = self.declaration() - data.declarations.append(d) - - # initializers - initializers = self.initialize() - if initializers: - data.initializers.append('/* initializers for ' + name + ' */\n' + initializers.rstrip()) - # impl_arguments s = ("&" if self.impl_by_reference else "") + name data.impl_arguments.append(s) if self.length: data.impl_arguments.append(self.length_name()) + # impl_parameters + data.impl_parameters.append(self.simple_declaration(by_reference=self.impl_by_reference)) + if self.length: + data.impl_parameters.append("Py_ssize_clean_t " + self.length_name()) + + def _render_non_self(self, parameter, data): + self.parameter = parameter + original_name = self.name + name = ensure_legal_c_identifier(original_name) + + # declarations + d = self.declaration() + data.declarations.append(d) + + # initializers + initializers = self.initialize() + if initializers: + data.initializers.append('/* initializers for ' + name + ' */\n' + initializers.rstrip()) + # keywords data.keywords.append(original_name) @@ -1534,16 +1549,20 @@ # parse_arguments self.parse_argument(data.parse_arguments) - # impl_parameters - data.impl_parameters.append(self.simple_declaration(by_reference=self.impl_by_reference)) - if self.length: - data.impl_parameters.append("Py_ssize_clean_t " + self.length_name()) - # cleanup cleanup = self.cleanup() if cleanup: data.cleanup.append('/* Cleanup for ' + name + ' */\n' + cleanup.rstrip() + "\n") + + def render(self, parameter, data): + """ + parameter is a clinic.Parameter instance. + data is a CRenderData instance. + """ + self._render_self(parameter, data) + self._render_non_self(parameter, data) + def length_name(self): """Computes the name of the associated "length" variable.""" if not self.length: @@ -1924,6 +1943,8 @@ this is the default converter used for "self". """ type = "PyObject *" + format_unit = '' + def converter_init(self, *, type=None): f = self.function if f.kind in (CALLABLE, METHOD_INIT): @@ -1935,6 +1956,7 @@ elif f.kind == STATIC_METHOD: self.name = "null" self.type = "void *" + self.show_in_signature = False elif f.kind == CLASS_METHOD: self.name = "cls" self.type = "PyTypeObject *" @@ -1945,10 +1967,15 @@ if type: self.type = type + # def render(self, parameter, data): + # fail("render() should never be called on self_converter instances") + def render(self, parameter, data): - fail("render() should never be called on self_converter instances") - - + """ + parameter is a clinic.Parameter instance. + data is a CRenderData instance. + """ + self._render_self(parameter, data) def add_c_return_converter(f, name=None): if not name: @@ -2409,6 +2436,12 @@ self.function = Function(name=function_name, full_name=full_name, module=module, cls=cls, c_basename=c_basename, return_converter=return_converter, kind=self.kind, coexist=self.coexist) self.block.signatures.append(self.function) + + # insert a self converter automatically + sc = self.function.self_converter = self_converter("self", self.function) + p_self = Parameter(sc.name, inspect.Parameter.POSITIONAL_ONLY, function=self.function, converter=sc) + self.function.parameters[sc.name] = p_self + (cls or module).functions.append(self.function) self.next(self.state_parameters_start) @@ -2667,18 +2700,22 @@ fail('{} is not a valid {}converter'.format(name, legacy_str)) converter = dict[name](parameter_name, self.function, value, **kwargs) - # special case: if it's the self converter, - # don't actually add it to the parameter list + kind = inspect.Parameter.KEYWORD_ONLY if self.keyword_only else inspect.Parameter.POSITIONAL_OR_KEYWORD + if isinstance(converter, self_converter): - if self.function.parameters or (self.parameter_state != self.ps_required): + if len(self.function.parameters) == 1: + if (self.parameter_state != self.ps_required): + fail("The 'self' parameter cannot be marked optional.") + if value is not unspecified: + fail("The 'self' parameter cannot have a default value.") + if self.group: + fail("The 'self' parameter cannot be in an optional group.") + kind = inspect.Parameter.POSITIONAL_ONLY + self.parameter_state = self.ps_start + self.function.parameters.clear() + else: fail("The 'self' parameter, if specified, must be the very first thing in the parameter block.") - if self.function.self_converter: - fail("You can't specify the 'self' parameter more than once.") - self.function.self_converter = converter - self.parameter_state = self.ps_start - return - - kind = inspect.Parameter.KEYWORD_ONLY if self.keyword_only else inspect.Parameter.POSITIONAL_OR_KEYWORD + p = Parameter(parameter_name, kind, function=self.function, converter=converter, default=value, group=self.group) self.function.parameters[parameter_name] = p @@ -2734,7 +2771,7 @@ self.parameter_state = self.ps_seen_slash # fixup preceeding parameters for p in self.function.parameters.values(): - if p.kind != inspect.Parameter.POSITIONAL_OR_KEYWORD: + if (p.kind != inspect.Parameter.POSITIONAL_OR_KEYWORD and not isinstance(p.converter, self_converter)): fail("Function " + self.function.name + " mixes keyword-only and positional-only parameters, which is unsupported.") p.kind = inspect.Parameter.POSITIONAL_ONLY @@ -2806,17 +2843,24 @@ add('(') # populate "right_bracket_count" field for every parameter - if parameters: + assert parameters, "We should always have a self parameter. " + repr(f) + assert isinstance(parameters[0].converter, self_converter) + parameters[0].right_bracket_count = 0 + parameters_after_self = parameters[1:] + if parameters_after_self: # for now, the only way Clinic supports positional-only parameters - # is if all of them are positional-only. - positional_only_parameters = [p.kind == inspect.Parameter.POSITIONAL_ONLY for p in parameters] - if parameters[0].kind == inspect.Parameter.POSITIONAL_ONLY: + # is if all of them are positional-only... + # + # ... except for self! self is always positional-only. + + positional_only_parameters = [p.kind == inspect.Parameter.POSITIONAL_ONLY for p in parameters_after_self] + if parameters_after_self[0].kind == inspect.Parameter.POSITIONAL_ONLY: assert all(positional_only_parameters) for p in parameters: p.right_bracket_count = abs(p.group) else: # don't put any right brackets around non-positional-only parameters, ever. - for p in parameters: + for p in parameters_after_self: p.right_bracket_count = 0 right_bracket_count = 0 @@ -2836,6 +2880,9 @@ add_comma = False for p in parameters: + if not p.converter.show_in_signature: + continue + assert p.name if p.is_keyword_only() and not added_star: @@ -2843,6 +2890,7 @@ if add_comma: add(', ') add('*') + add_comma = True a = [p.name] if p.converter.is_optional(): @@ -2955,9 +3003,6 @@ if not self.function: return - if not self.function.self_converter: - self.function.self_converter = self_converter("self", self.function) - if self.keyword_only: values = self.function.parameters.values() if not values: @@ -2976,6 +3021,10 @@ self.function.docstring = self.format_docstring() + self.function.selfless = self.function.parameters.copy() + if 'self' in self.function.selfless: + del self.function.selfless['self'] + # maps strings to callables. # the callable should return an object @@ -3002,6 +3051,7 @@ cmdline = argparse.ArgumentParser() cmdline.add_argument("-f", "--force", action='store_true') cmdline.add_argument("-o", "--output", type=str) + cmdline.add_argument("-v", "--verbose", action='store_true') cmdline.add_argument("--converters", action='store_true') cmdline.add_argument("--make", action='store_true') cmdline.add_argument("filename", type=str, nargs="*") @@ -3089,13 +3139,15 @@ cmdline.print_usage() sys.exit(-1) for root, dirs, files in os.walk('.'): - for rcs_dir in ('.svn', '.git', '.hg'): + for rcs_dir in ('.svn', '.git', '.hg', 'build'): if rcs_dir in dirs: dirs.remove(rcs_dir) for filename in files: - if not filename.endswith('.c'): + if not (filename.endswith('.c') or filename.endswith('.h')): continue path = os.path.join(root, filename) + if ns.verbose: + print(path) parse_file(path, verify=not ns.force) return @@ -3110,6 +3162,8 @@ sys.exit(-1) for filename in ns.filename: + if ns.verbose: + print(filename) parse_file(filename, output=ns.output, verify=not ns.force)