diff options
Diffstat (limited to 'sources/shiboken2/shibokenmodule/files.dir')
9 files changed, 118 insertions, 115 deletions
diff --git a/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/backport_inspect.py b/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/backport_inspect.py index c690493b6..0f9598c64 100644 --- a/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/backport_inspect.py +++ b/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/backport_inspect.py @@ -109,15 +109,22 @@ CO_NOFREE = 0x0040 ############################################################################### + +# PYSIDE-1286: We now use the added __qualname__ for classes. +def _get_class_name(cls): + return getattr(cls, "__qualname__", cls.__name__) + # This function was changed: 'builtins' and 'qualname' don't exist. -# We use '__builtin__' and '__name__' instead. +# We use '__builtin__' and '__(qual)?name__' instead. def formatannotation(annotation, base_module=None): if getattr(annotation, '__module__', None) == 'typing': - return repr(annotation).replace('typing.', '') + # The replace must not be done on Python 2.7 because it + # already happens somewhere else. + return repr(annotation) ##.replace('typing.', '') if isinstance(annotation, type): if annotation.__module__ in ('__builtin__', base_module): - return annotation.__name__ - return annotation.__module__+'.'+annotation.__name__ + return _get_class_name(annotation) + return annotation.__module__ + '.' + _get_class_name(annotation) return repr(annotation) @@ -391,7 +398,7 @@ class Parameter(object): return formatted def __repr__(self): - return '<{} "{}">'.format(self.__class__.__name__, self) + return '<{} "{}">'.format(_get_class_name(self.__class__), self) def __hash__(self): return hash((self.name, self.kind, self.annotation, self.default)) @@ -534,7 +541,7 @@ class BoundArguments(object): args = [] for arg, value in self.arguments.items(): args.append('{}={!r}'.format(arg, value)) - return '<{} ({})>'.format(self.__class__.__name__, ', '.join(args)) + return '<{} ({})>'.format(_get_class_name(self.__class__), ', '.join(args)) class Signature(object): @@ -840,7 +847,7 @@ class Signature(object): self._return_annotation = state['_return_annotation'] def __repr__(self): - return '<{} {}>'.format(self.__class__.__name__, self) + return '<{} {}>'.format(_get_class_name(self.__class__), self) def __str__(self): result = [] diff --git a/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/errorhandler.py b/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/errorhandler.py index cb148830f..4dbed077d 100644 --- a/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/errorhandler.py +++ b/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/errorhandler.py @@ -1,6 +1,6 @@ ############################################################################# ## -## Copyright (C) 2019 The Qt Company Ltd. +## Copyright (C) 2020 The Qt Company Ltd. ## Contact: https://www.qt.io/licensing/ ## ## This file is part of Qt for Python. @@ -56,6 +56,8 @@ enough to produce a useful ValueError. This matter will be improved in a later version. """ +import sys + from shibokensupport.signature import inspect from shibokensupport.signature import get_signature from shibokensupport.signature.mapping import update_mapping, namespace @@ -122,6 +124,11 @@ def seterror_argument(args, func_name): # We don't raise the error here, to avoid the loader in the traceback. return TypeError, msg +def check_string_type(s): + if sys.version_info[0] == 3: + return isinstance(s, str) + else: + return isinstance(s, (str, unicode)) def make_helptext(func): existing_doc = func.__doc__ @@ -135,7 +142,7 @@ def make_helptext(func): except AttribureError: func_name = func.__func__.__name__ sigtext = "\n".join(func_name + str(sig) for sig in sigs) - msg = sigtext + "\n\n" + existing_doc if existing_doc else sigtext + msg = sigtext + "\n\n" + existing_doc if check_string_type(existing_doc) else sigtext return msg # end of file diff --git a/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/importhandler.py b/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/importhandler.py index 0417f132a..7af43bea0 100644 --- a/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/importhandler.py +++ b/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/importhandler.py @@ -70,7 +70,7 @@ def finish_import(module): if func: func(module) except Exception as e: - name = e.__class__.__name__ + name = e.__class__.__qualname__ print(72 * "*") print("Error in deprecated.py, ignored:") print(" {name}: {e}".format(**locals())) diff --git a/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/lib/enum_sig.py b/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/lib/enum_sig.py index b026a5d20..fa4d5e77c 100644 --- a/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/lib/enum_sig.py +++ b/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/lib/enum_sig.py @@ -1,6 +1,6 @@ ############################################################################# ## -## Copyright (C) 2018 The Qt Company Ltd. +## Copyright (C) 2020 The Qt Company Ltd. ## Contact: https://www.qt.io/licensing/ ## ## This file is part of Qt for Python. @@ -52,11 +52,6 @@ by producing a lot of clarity. import sys from shibokensupport.signature import inspect from shibokensupport.signature import get_signature -try: - from PySide2.QtCore import Qt - EnumType = type(Qt.Key) -except ImportError: - EnumType = None class ExactEnumerator(object): @@ -69,6 +64,14 @@ class ExactEnumerator(object): """ def __init__(self, formatter, result_type=dict): + global EnumType + try: + # Lazy import + from PySide2.QtCore import Qt + EnumType = type(Qt.Key) + except ImportError: + EnumType = None + self.fmt = formatter self.result_type = result_type self.fmt.level = 0 @@ -81,6 +84,7 @@ class ExactEnumerator(object): def module(self, mod_name): __import__(mod_name) + self.fmt.mod_name = mod_name with self.fmt.module(mod_name): module = sys.modules[mod_name] members = inspect.getmembers(module, inspect.isclass) @@ -90,47 +94,65 @@ class ExactEnumerator(object): for class_name, klass in members: ret.update(self.klass(class_name, klass)) if isinstance(klass, EnumType): - self.enum(klass) + raise SystemError("implement enum instances at module level") for func_name, func in functions: ret.update(self.function(func_name, func)) return ret def klass(self, class_name, klass): - modname = klass.__module__ - if not (modname.startswith("PySide2") or modname.startswith("shiboken2")): - # don't look into any foreign classes! - ret = self.result_type() + ret = self.result_type() + if "<" in class_name: + # This is happening in QtQuick for some reason: + ## class QSharedPointer<QQuickItemGrabResult >: + # We simply skip over this class. return ret bases_list = [] for base in klass.__bases__: name = base.__name__ - if name in ("object", "type"): - pass - else: - modname = base.__module__ - name = modname + "." + base.__name__ + if name not in ("object", "type"): + name = base.__module__ + "." + name bases_list.append(name) class_str = "{}({})".format(class_name, ", ".join(bases_list)) - with self.fmt.klass(class_name, class_str): - ret = self.result_type() - # class_members = inspect.getmembers(klass) - # gives us also the inherited things. - class_members = sorted(list(klass.__dict__.items())) - subclasses = [] - functions = [] - for thing_name, thing in class_members: - if inspect.isclass(thing): - subclass_name = ".".join((class_name, thing_name)) - subclasses.append((subclass_name, thing)) - elif inspect.isroutine(thing): - func_name = thing_name.split(".")[0] # remove ".overload" + # class_members = inspect.getmembers(klass) + # gives us also the inherited things. + class_members = sorted(list(klass.__dict__.items())) + subclasses = [] + functions = [] + enums = [] + + for thing_name, thing in class_members: + if inspect.isclass(thing): + subclass_name = ".".join((class_name, thing_name)) + subclasses.append((subclass_name, thing)) + elif inspect.isroutine(thing): + func_name = thing_name.split(".")[0] # remove ".overload" + signature = getattr(thing, "__signature__", None) + if signature is not None: functions.append((func_name, thing)) + elif type(type(thing)) is EnumType: + enums.append((thing_name, thing)) + init_signature = getattr(klass, "__signature__", None) + enums.sort(key=lambda tup: tup[1]) # sort by enum value + self.fmt.have_body = bool(subclasses or functions or enums or init_signature) + + with self.fmt.klass(class_name, class_str): self.fmt.level += 1 + self.fmt.class_name = class_name + if hasattr(self.fmt, "enum"): + # this is an optional feature + for enum_name, value in enums: + with self.fmt.enum(class_name, enum_name, int(value)): + pass for subclass_name, subclass in subclasses: + if klass == subclass: + # this is a side effect of the typing module for Python 2.7 + # via the "._gorg" property, which we can safely ignore. + print("Warning: {class_name} points to itself via {subclass_name}, skipped!" + .format(**locals())) + continue ret.update(self.klass(subclass_name, subclass)) - if isinstance(subclass, EnumType): - self.enum(subclass) - ret = self.function("__init__", klass) + self.fmt.class_name = class_name + ret.update(self.function("__init__", klass)) for func_name, func in functions: func_kind = get_signature(func, "__func_kind__") modifier = func_kind if func_kind in ( @@ -142,24 +164,13 @@ class ExactEnumerator(object): def function(self, func_name, func, modifier=None): self.fmt.level += 1 ret = self.result_type() - signature = getattr(func, '__signature__', None) + signature = func.__signature__ if signature is not None: with self.fmt.function(func_name, signature, modifier) as key: ret[key] = signature self.fmt.level -= 1 return ret - def enum(self, subclass): - if not hasattr(self.fmt, "enum"): - # this is an optional feature - return - class_name = subclass.__name__ - for enum_name, value in subclass.__dict__.items(): - if type(type(value)) is EnumType: - with self.fmt.enum(class_name, enum_name, int(value)): - pass - self._after_enum = True - def stringify(signature): if isinstance(signature, list): diff --git a/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/lib/tool.py b/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/lib/tool.py index 3b0825049..24e75e42c 100644 --- a/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/lib/tool.py +++ b/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/lib/tool.py @@ -116,7 +116,7 @@ def build_brace_pattern(level, separators=""): | {so} {replacer} {sc} | {co} {replacer} {cc} | {ao} {replacer} {ac} - )* + )+ ) """) no_braces_q = "[^{all}{qu}{bs}]*".format(**locals()) diff --git a/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/loader.py b/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/loader.py index a0367883a..1efc6fde5 100644 --- a/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/loader.py +++ b/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/loader.py @@ -75,14 +75,18 @@ try: except NameError: ModuleNotFoundError = ImportError +def _qualname(x): + return getattr(x, "__qualname__", x.__name__) + # patching inspect's formatting to keep the word "typing": def formatannotation(annotation, base_module=None): # if getattr(annotation, '__module__', None) == 'typing': # return repr(annotation).replace('typing.', '') if isinstance(annotation, type): + name = _qualname(annotation) if annotation.__module__ in ('builtins', base_module): - return annotation.__qualname__ - return annotation.__module__ + '.' + annotation.__qualname__ + return name + return annotation.__module__ + '.' + name return repr(annotation) # Note also that during the tests we have a different encoding that would @@ -152,10 +156,12 @@ if sys.version_info >= (3,): import inspect inspect.formatannotation = formatannotation else: - if "typing" not in sys.modules: + tp_name = "typing" + if tp_name not in sys.modules: orig_typing = False from shibokensupport import typing27 as typing - sys.modules["typing"] = typing + sys.modules[tp_name] = typing + typing.__name__ = tp_name else: import typing import inspect diff --git a/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/mapping.py b/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/mapping.py index d77d34f97..0571b11bd 100644 --- a/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/mapping.py +++ b/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/mapping.py @@ -73,45 +73,6 @@ NoneType = type(None) _S = TypeVar("_S") -# Building our own Char type, which is much nicer than -# Char = typing.Union[str, int] # how do I model the limitation to 1 char? - -class _CharMeta(type): - def __repr__(self): - return '%s.%s' % (self.__module__, self.__name__) - - -class Char(with_metaclass(_CharMeta)): - """ - From http://doc.qt.io/qt-5/qchar.html : - - In Qt, Unicode characters are 16-bit entities without any markup or - structure. This class represents such an entity. It is lightweight, - so it can be used everywhere. Most compilers treat it like an - unsigned short. - - Here, we provide a simple implementation just to avoid long aliases. - """ - __module__ = "typing" - - def __init__(self, code): - if isinstance(code, int): - self.code = code & 0xffff - else: - self.code = ord(code) - - def __add__(self, other): - return chr(self.code) + other - - def __radd__(self, other): - return other + chr(self.code) - - def __repr__(self): - return "typing.Char({})".format(self.code) - -typing.Char = Char - - MultiMap = typing.DefaultDict[str, typing.List[str]] # ulong_max is only 32 bit on windows. @@ -197,7 +158,7 @@ class _Parameterized(object): class ResultVariable(_Parameterized): pass -# Mark the primitive variables to become Sequence, Iterator or List +# Mark the primitive variables to become Sequence, Iterable or List # (decided in the parser). class ArrayLikeVariable(_Parameterized): pass @@ -264,8 +225,9 @@ type_map = {} namespace = globals() # our module's __dict__ type_map.update({ + "...": ellipsis, "bool": bool, - "char": Char, + "char": int, "char*": str, "char*const": str, "double": float, @@ -277,7 +239,7 @@ type_map.update({ "PyObject": object, "PySequence": typing.Iterable, # important for numpy "PyTypeObject": type, - "QChar": Char, + "QChar": str, "QHash": typing.Dict, "qint16": int, "qint32": int, @@ -304,7 +266,7 @@ type_map.update({ "QVector": typing.List, "real": float, "short": int, - "signed char": Char, + "signed char": int, "signed long": int, "std.list": typing.List, "std.map": typing.Dict, @@ -313,12 +275,12 @@ type_map.update({ "str": str, "true": True, "Tuple": typing.Tuple, - "uchar": Char, + "uchar": int, "uchar*": str, "uint": int, "ulong": int, "ULONG_MAX": ulong_max, - "unsigned char": Char, # 5.9 + "unsigned char": int, # 5.9 "unsigned char*": str, "unsigned int": int, "unsigned long int": int, # 5.6, RHEL 6.6 @@ -326,6 +288,7 @@ type_map.update({ "unsigned long": int, "unsigned short int": int, # 5.6, RHEL 6.6 "unsigned short": int, + "Unspecified": None, "ushort": int, "void": int, # be more specific? "WId": WId, @@ -346,6 +309,7 @@ type_map.update({ "array GLuint*" : ArrayLikeVariable(int), "array int*" : ArrayLikeVariable(int), "array long long*" : ArrayLikeVariable(int), + "array long*" : ArrayLikeVariable(int), "array short*" : ArrayLikeVariable(int), "array signed char*" : bytes, "array unsigned char*" : bytes, @@ -402,7 +366,7 @@ def init_minimal(): def init_sample(): import datetime type_map.update({ - "char": Char, + "char": int, "char**": typing.List[str], "Complex": complex, "double": float, @@ -415,7 +379,7 @@ def init_sample(): "PStr": str, "PyDate": datetime.date, "sample.bool": bool, - "sample.char": Char, + "sample.char": int, "sample.double": float, "sample.int": int, "sample.ObjectType": object, @@ -424,7 +388,7 @@ def init_sample(): "sample.Photon.TemplateBase[Photon.IdentityType]": sample.Photon.ValueIdentity, "sample.Point": Point, "sample.PStr": str, - "sample.unsigned char": Char, + "sample.unsigned char": int, "std.size_t": int, "std.string": str, "ZeroIn": 0, @@ -486,6 +450,10 @@ def init_PySide2_QtCore(): PySide2.QtCore.QCborStringResultByteArray, "PySide2.QtCore.QCborStreamReader.StringResult[QString]": PySide2.QtCore.QCborStringResultString, + "PySide2.QtCore.QCborStreamReader.QCborStringResultByteArray": + PySide2.QtCore.QCborStringResultByteArray, # 5.14, why? + "PySide2.QtCore.QCborStreamReader.QCborStringResultString": + PySide2.QtCore.QCborStringResultString, # 5.14, why? "PySide2.QtCore.QUrl.ComponentFormattingOptions": PySide2.QtCore.QUrl.ComponentFormattingOption, # mismatch option/enum, why??? "PyUnicode": typing.Text, diff --git a/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/parser.py b/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/parser.py index df2b9fa92..2053c3e9d 100644 --- a/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/parser.py +++ b/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/signature/parser.py @@ -106,7 +106,8 @@ def _parse_line(line): $ """ ret = SimpleNamespace(**re.match(line_re, line, re.VERBOSE).groupdict()) - argstr = ret.arglist + # PYSIDE-1095: Handle arbitrary default expressions + argstr = ret.arglist.replace("->", ".deref.") arglist = _parse_arglist(argstr) args = [] for arg in arglist: @@ -164,6 +165,11 @@ def try_to_guess(thing, valtype): return ret return None +def get_name(thing): + if isinstance(thing, type): + return getattr(thing, "__qualname__", thing.__name__) + else: + return thing.__name__ def _resolve_value(thing, valtype, line): if thing in ("0", "None") and valtype: @@ -171,7 +177,7 @@ def _resolve_value(thing, valtype, line): return None map = type_map[valtype] # typing.Any: '_SpecialForm' object has no attribute '__name__' - name = map.__name__ if hasattr(map, "__name__") else str(map) + name = get_name(map) if hasattr(map, "__name__") else str(map) thing = "zero({})".format(name) if thing in type_map: return type_map[thing] @@ -211,7 +217,8 @@ def to_string(thing): return thing if hasattr(thing, "__name__"): dot = "." in str(thing) - return thing.__module__ + "." + thing.__name__ if dot else thing.__name__ + name = get_name(thing) + return thing.__module__ + "." + name if dot else name # Note: This captures things from the typing module: return str(thing) diff --git a/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/typing27.py b/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/typing27.py index 44d78c433..41ed456cc 100644 --- a/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/typing27.py +++ b/sources/shiboken2/shibokenmodule/files.dir/shibokensupport/typing27.py @@ -184,11 +184,8 @@ __all__ = [ def _qualname(x): - if sys.version_info[:2] >= (3, 3): - return x.__qualname__ - else: - # Fall back to just name. - return x.__name__ + # PYSIDE-1286: Support __qualname__ in Python 2 + return getattr(x, "__qualname__", x.__name__) def _trim_name(nm): |