summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorLarry Hastings <larry@hastings.org>2013-11-23 23:37:55 (GMT)
committerLarry Hastings <larry@hastings.org>2013-11-23 23:37:55 (GMT)
commit44e2eaab5491881120aab43e2838da8afe7ab70e (patch)
tree92847876fa89736ab40d027431ff27e4973409c9 /Objects
parent7fa6e1aeea111e7d954b753fb483afc18f21add0 (diff)
downloadcpython-44e2eaab5491881120aab43e2838da8afe7ab70e.zip
cpython-44e2eaab5491881120aab43e2838da8afe7ab70e.tar.gz
cpython-44e2eaab5491881120aab43e2838da8afe7ab70e.tar.bz2
Issue #19674: inspect.signature() now produces a correct signature
for some builtins.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/dictobject.c13
-rw-r--r--Objects/methodobject.c71
-rw-r--r--Objects/unicodeobject.c11
3 files changed, 81 insertions, 14 deletions
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index 3c1b3bb..bfc730b 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -69,6 +69,11 @@ to the combined-table form.
#include "Python.h"
#include "stringlib/eq.h"
+/*[clinic]
+class dict
+[clinic]*/
+/*[clinic checksum: da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
+
typedef struct {
/* Cached hash code of me_key. */
Py_hash_t me_hash;
@@ -2160,7 +2165,6 @@ dict_richcompare(PyObject *v, PyObject *w, int op)
}
/*[clinic]
-class dict
@coexist
dict.__contains__
@@ -2172,16 +2176,15 @@ True if D has a key k, else False"
[clinic]*/
PyDoc_STRVAR(dict___contains____doc__,
-"True if D has a key k, else False\"\n"
-"\n"
-"dict.__contains__(key)");
+"__contains__(key)\n"
+"True if D has a key k, else False\"");
#define DICT___CONTAINS___METHODDEF \
{"__contains__", (PyCFunction)dict___contains__, METH_O|METH_COEXIST, dict___contains____doc__},
static PyObject *
dict___contains__(PyObject *self, PyObject *key)
-/*[clinic checksum: 61c5c802ea1d35699a1a754f1f3538ea9b259cf4]*/
+/*[clinic checksum: 3bbac5ce898ae630d9668fa1c8b3afb645ff22e8]*/
{
register PyDictObject *mp = (PyDictObject *)self;
Py_hash_t hash;
diff --git a/Objects/methodobject.c b/Objects/methodobject.c
index 11c8b6e..ca21a68 100644
--- a/Objects/methodobject.c
+++ b/Objects/methodobject.c
@@ -159,15 +159,75 @@ meth_dealloc(PyCFunctionObject *m)
}
}
+/*
+ * 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);
+}
+
static PyObject *
meth_get__doc__(PyCFunctionObject *m, void *closure)
{
- const char *doc = m->m_ml->ml_doc;
+ 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;
+ }
- if (doc != NULL)
- return PyUnicode_FromString(doc);
- Py_INCREF(Py_None);
- return Py_None;
+ return PyUnicode_FromString(doc);
}
static PyObject *
@@ -236,6 +296,7 @@ static PyGetSetDef meth_getsets [] = {
{"__name__", (getter)meth_get__name__, NULL, NULL},
{"__qualname__", (getter)meth_get__qualname__, NULL, NULL},
{"__self__", (getter)meth_get__self__, NULL, NULL},
+ {"__text_signature__", (getter)meth_get__text_signature__, NULL, NULL},
{0}
};
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 1f3164c..34d51e4 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -47,6 +47,11 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <windows.h>
#endif
+/*[clinic]
+class str
+[clinic]*/
+/*[clinic checksum: da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
+
/* --- Globals ------------------------------------------------------------
NOTE: In the interpreter's initialization phase, some globals are currently
@@ -12883,7 +12888,6 @@ unicode_swapcase(PyObject *self)
}
/*[clinic]
-class str
@staticmethod
str.maketrans as unicode_maketrans
@@ -12908,10 +12912,9 @@ must be a string, whose characters will be mapped to None in the result.
[clinic]*/
PyDoc_STRVAR(unicode_maketrans__doc__,
+"maketrans(x, y=None, z=None)\n"
"Return a translation table usable for str.translate().\n"
"\n"
-"str.maketrans(x, y=None, z=None)\n"
-"\n"
"If there is only one argument, it must be a dictionary mapping Unicode\n"
"ordinals (integers) or characters to Unicode ordinals, strings or None.\n"
"Character keys will be then converted to ordinals.\n"
@@ -12946,7 +12949,7 @@ exit:
static PyObject *
unicode_maketrans_impl(void *null, PyObject *x, PyObject *y, PyObject *z)
-/*[clinic checksum: 6d522e3aea2f2e123da3c5d367132a99d803f9b9]*/
+/*[clinic checksum: 7f76f414a0dfd0c614e0d4717872eeb520516da7]*/
{
PyObject *new = NULL, *key, *value;
Py_ssize_t i = 0;