summaryrefslogtreecommitdiffstats
path: root/Objects/object.c
diff options
context:
space:
mode:
authorJeroen Demeyer <jeroen.k.demeyer@gmail.com>2019-11-05 15:48:04 (GMT)
committerMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2019-11-05 15:48:04 (GMT)
commitbf17d41826a8bb4bc1e34ba6345da98aac779e41 (patch)
tree1c256d14c23ffdcbbb5ae54efa546cf718a8f892 /Objects/object.c
parentb3966639d28313809774ca3859a347b9007be8d2 (diff)
downloadcpython-bf17d41826a8bb4bc1e34ba6345da98aac779e41.zip
cpython-bf17d41826a8bb4bc1e34ba6345da98aac779e41.tar.gz
cpython-bf17d41826a8bb4bc1e34ba6345da98aac779e41.tar.bz2
bpo-37645: add new function _PyObject_FunctionStr() (GH-14890)
Additional note: the `method_check_args` function in `Objects/descrobject.c` is written in such a way that it applies to all kinds of descriptors. In particular, a future re-implementation of `wrapper_descriptor` could use that code. CC @vstinner @encukou https://bugs.python.org/issue37645 Automerge-Triggered-By: @encukou
Diffstat (limited to 'Objects/object.c')
-rw-r--r--Objects/object.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/Objects/object.c b/Objects/object.c
index 9536d46..3e61282 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -681,6 +681,64 @@ PyObject_Bytes(PyObject *v)
return PyBytes_FromObject(v);
}
+
+/*
+def _PyObject_FunctionStr(x):
+ try:
+ qualname = x.__qualname__
+ except AttributeError:
+ return str(x)
+ try:
+ mod = x.__module__
+ if mod is not None and mod != 'builtins':
+ return f"{x.__module__}.{qualname}()"
+ except AttributeError:
+ pass
+ return qualname
+*/
+PyObject *
+_PyObject_FunctionStr(PyObject *x)
+{
+ _Py_IDENTIFIER(__module__);
+ _Py_IDENTIFIER(__qualname__);
+ _Py_IDENTIFIER(builtins);
+ assert(!PyErr_Occurred());
+ PyObject *qualname;
+ int ret = _PyObject_LookupAttrId(x, &PyId___qualname__, &qualname);
+ if (qualname == NULL) {
+ if (ret < 0) {
+ return NULL;
+ }
+ return PyObject_Str(x);
+ }
+ PyObject *module;
+ PyObject *result = NULL;
+ ret = _PyObject_LookupAttrId(x, &PyId___module__, &module);
+ if (module != NULL && module != Py_None) {
+ PyObject *builtinsname = _PyUnicode_FromId(&PyId_builtins);
+ if (builtinsname == NULL) {
+ goto done;
+ }
+ ret = PyObject_RichCompareBool(module, builtinsname, Py_NE);
+ if (ret < 0) {
+ // error
+ goto done;
+ }
+ if (ret > 0) {
+ result = PyUnicode_FromFormat("%S.%S()", module, qualname);
+ goto done;
+ }
+ }
+ else if (ret < 0) {
+ goto done;
+ }
+ result = PyUnicode_FromFormat("%S()", qualname);
+done:
+ Py_DECREF(qualname);
+ Py_XDECREF(module);
+ return result;
+}
+
/* For Python 3.0.1 and later, the old three-way comparison has been
completely removed in favour of rich comparisons. PyObject_Compare() and
PyObject_Cmp() are gone, and the builtin cmp function no longer exists.