diff options
Diffstat (limited to 'sources/shiboken6/shibokenmodule/files.dir/shibokensupport/signature/errorhandler.py')
-rw-r--r-- | sources/shiboken6/shibokenmodule/files.dir/shibokensupport/signature/errorhandler.py | 87 |
1 files changed, 35 insertions, 52 deletions
diff --git a/sources/shiboken6/shibokenmodule/files.dir/shibokensupport/signature/errorhandler.py b/sources/shiboken6/shibokenmodule/files.dir/shibokensupport/signature/errorhandler.py index 71f2ffbab..c2a19efef 100644 --- a/sources/shiboken6/shibokenmodule/files.dir/shibokensupport/signature/errorhandler.py +++ b/sources/shiboken6/shibokenmodule/files.dir/shibokensupport/signature/errorhandler.py @@ -1,41 +1,7 @@ -############################################################################# -## -## Copyright (C) 2020 The Qt Company Ltd. -## Contact: https://www.qt.io/licensing/ -## -## This file is part of Qt for Python. -## -## $QT_BEGIN_LICENSE:LGPL$ -## Commercial License Usage -## Licensees holding valid commercial Qt licenses may use this file in -## accordance with the commercial license agreement provided with the -## Software or, alternatively, in accordance with the terms contained in -## a written agreement between you and The Qt Company. For licensing terms -## and conditions see https://www.qt.io/terms-conditions. For further -## information use the contact form at https://www.qt.io/contact-us. -## -## GNU Lesser General Public License Usage -## Alternatively, this file may be used under the terms of the GNU Lesser -## General Public License version 3 as published by the Free Software -## Foundation and appearing in the file LICENSE.LGPL3 included in the -## packaging of this file. Please review the following information to -## ensure the GNU Lesser General Public License version 3 requirements -## will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -## -## GNU General Public License Usage -## Alternatively, this file may be used under the terms of the GNU -## General Public License version 2.0 or (at your option) the GNU General -## Public license version 3 or any later version approved by the KDE Free -## Qt Foundation. The licenses are as published by the Free Software -## Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -## included in the packaging of this file. Please review the following -## information to ensure the GNU General Public License requirements will -## be met: https://www.gnu.org/licenses/gpl-2.0.html and -## https://www.gnu.org/licenses/gpl-3.0.html. -## -## $QT_END_LICENSE$ -## -############################################################################# +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +# flake8: noqa E:721 """ errorhandler.py @@ -54,21 +20,33 @@ enough to produce a useful ValueError. This matter will be improved in a later version. """ -import inspect -import sys +import collections.abc +import typing from shibokensupport.signature import get_signature -from shibokensupport.signature.mapping import update_mapping, namespace +from shibokensupport.signature.mapping import namespace from textwrap import dedent def qt_isinstance(inst, the_type): if the_type == float: - return isinstance(inst, int) or isinstance(int, float) + # Qt thinks differently about int and float - simply keep it. + return isinstance(inst, int) or isinstance(inst, float) + if the_type.__module__ == "typing": + if the_type is typing.Any: + return True + if the_type.__origin__ is typing.Union: + return any(qt_isinstance(inst, _) for _ in the_type.__args__) + if the_type.__origin__ in (collections.abc.Sequence, + collections.abc.Iterable): + try: + return all(qt_isinstance(_, the_type.__args__[0]) for _ in inst) + except TypeError: + return False try: return isinstance(inst, the_type) except TypeError as e: - print("FIXME", e) + print(f"FIXME qt_isinstance({inst}, {the_type}):", e) return False @@ -82,13 +60,7 @@ def matched_type(args, sigs): if params[k].default is params[k].empty: # this is a necessary parameter, so it fails. continue - ok = True - for arg, param in zip(args, params): - ann = param.annotation - if qt_isinstance(arg, ann): - continue - ok = False - if ok: + if all(qt_isinstance(arg, param.annotation) for arg, param in zip(args, params)): return sig return None @@ -98,12 +70,15 @@ def seterror_argument(args, func_name, info): try: func = eval(func_name, namespace) except Exception as e: - msg = f"Internal error evaluating {func_name}: " + str(e) - return TypeError, msg + msg = f"Error evaluating `{func_name}`: {e}" + return type(e), msg if info and type(info) is str: err = TypeError if info == "<": msg = f"{func_name}(): not enough arguments" + elif info == "0": + msg = (f"{func_name}(): not enough arguments. " + "Note: keyword arguments are only supported for optional parameters.") elif info == ">": msg = f"{func_name}(): too many arguments" elif info.isalnum(): @@ -112,6 +87,12 @@ def seterror_argument(args, func_name, info): msg = f"{func_name}(): {info}" err = AttributeError return err, msg + if isinstance(info, Exception): + # PYSIDE-2230: Python 3.12 seems to always do normalization. + err = type(info) + info = info.args[0] + msg = f"{func_name}(): {info}" + return err, msg if info and type(info) is dict: msg = f"{func_name}(): unsupported keyword '{tuple(info)[0]}'" return AttributeError, msg @@ -151,6 +132,8 @@ def check_string_type(s): def make_helptext(func): existing_doc = func.__doc__ + if existing_doc is None and hasattr(func, "__dict__"): + existing_doc = func.__dict__.get("__doc__") sigs = get_signature(func) if not sigs: return existing_doc |