summaryrefslogtreecommitdiff
path: root/Modules
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2019-02-18 03:05:52 -0800
committerGitHub <noreply@github.com>2019-02-18 03:05:52 -0800
commita01065a3588d3f0d0c57ea35107aa97e722fe2b2 (patch)
treefaaaf96e8a818b47e6bca83e8947603001717b95 /Modules
parenta7f929db605326da452fbdeebfe341afa9316d25 (diff)
downloadcpython-git-a01065a3588d3f0d0c57ea35107aa97e722fe2b2.tar.gz
bpo-35942: Improve the error message if __fspath__ returns invalid types in path_converter (GH-11831)
The error message emitted when returning invalid types from __fspath__ in interfaces that allow passing PathLike objects has been improved and now it does explain the origin of the error. (cherry picked from commit 09fbcd6085e18b534fd4161844ff39f77eb4a082) Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
Diffstat (limited to 'Modules')
-rw-r--r--Modules/posixmodule.c23
1 files changed, 15 insertions, 8 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index e587ed23c2..e7a1f987de 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -979,28 +979,35 @@ path_converter(PyObject *o, void *p)
if (!is_index && !is_buffer && !is_unicode && !is_bytes) {
/* Inline PyOS_FSPath() for better error messages. */
_Py_IDENTIFIER(__fspath__);
- PyObject *func = NULL;
+ PyObject *func, *res;
func = _PyObject_LookupSpecial(o, &PyId___fspath__);
if (NULL == func) {
goto error_format;
}
- /* still owns a reference to the original object */
- Py_DECREF(o);
- o = _PyObject_CallNoArg(func);
+ res = _PyObject_CallNoArg(func);
Py_DECREF(func);
- if (NULL == o) {
+ if (NULL == res) {
goto error_exit;
}
- else if (PyUnicode_Check(o)) {
+ else if (PyUnicode_Check(res)) {
is_unicode = 1;
}
- else if (PyBytes_Check(o)) {
+ else if (PyBytes_Check(res)) {
is_bytes = 1;
}
else {
- goto error_format;
+ PyErr_Format(PyExc_TypeError,
+ "expected %.200s.__fspath__() to return str or bytes, "
+ "not %.200s", Py_TYPE(o)->tp_name,
+ Py_TYPE(res)->tp_name);
+ Py_DECREF(res);
+ goto error_exit;
}
+
+ /* still owns a reference to the original object */
+ Py_DECREF(o);
+ o = res;
}
if (is_unicode) {