pythonqtside 1.1.12__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pythonqtside/Qt.py +14 -0
- pythonqtside/QtCore.py +14 -0
- pythonqtside/QtGui.py +14 -0
- pythonqtside/QtNetwork.py +14 -0
- pythonqtside/QtOpenGL.py +14 -0
- pythonqtside/QtSvg.py +14 -0
- pythonqtside/QtUiTools.py +14 -0
- pythonqtside/QtWidgets.py +14 -0
- pythonqtside/QtXmlPatterns.py +14 -0
- pythonqtside/__init__.py +34 -0
- pythonqtside/_app.py +319 -0
- pythonqtside/_app_cleanup.py +216 -0
- pythonqtside/_app_conversions.py +106 -0
- pythonqtside/_app_dialogs.py +74 -0
- pythonqtside/_app_flags.py +79 -0
- pythonqtside/_app_imports.py +146 -0
- pythonqtside/_app_keywords.py +210 -0
- pythonqtside/_app_native.py +404 -0
- pythonqtside/_app_proxy.py +43 -0
- pythonqtside/_app_source.py +110 -0
- pythonqtside/_differences.json +9808 -0
- pythonqtside/_flags.json +210 -0
- pythonqtside/_implicit_conversions.json +1975 -0
- pythonqtside/_inventory.json +115478 -0
- pythonqtside/_keywords.json +1 -0
- pythonqtside/_out_parameters.json +465 -0
- pythonqtside/_qtwidgets.json +202 -0
- pythonqtside/_routing.py +95 -0
- pythonqtside/compat.py +44 -0
- pythonqtside/tooling/__init__.py +2 -0
- pythonqtside/tooling/conversions.py +433 -0
- pythonqtside/tooling/differences.py +290 -0
- pythonqtside/tooling/flags.py +156 -0
- pythonqtside/tooling/inventory.py +149 -0
- pythonqtside/tooling/join.py +184 -0
- pythonqtside/tooling/keywords.py +259 -0
- pythonqtside/tooling/surface.py +191 -0
- pythonqtside-1.1.12.dist-info/METADATA +468 -0
- pythonqtside-1.1.12.dist-info/RECORD +42 -0
- pythonqtside-1.1.12.dist-info/WHEEL +4 -0
- pythonqtside-1.1.12.dist-info/licenses/LICENSE +201 -0
- pythonqtside-1.1.12.dist-info/licenses/NOTICE +7 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
# this_file: src/pythonqtside/_app_keywords.py
|
|
2
|
+
"""PySide2 keyword arguments for native PythonQt calls, which reject every keyword.
|
|
3
|
+
|
|
4
|
+
``QLabel("x", parent=w)`` raises "called slot does not support kwargs" in FontLab's
|
|
5
|
+
PythonQt (pqs-g04/probe_keywords.json). The generated ``_keywords.json`` lists the
|
|
6
|
+
PySide2 parameter names of each routed constructor and method overload. A keyword
|
|
7
|
+
call becomes a positional one when exactly one positional form fits; a skipped
|
|
8
|
+
parameter needs a recorded Qt declaration default. Constructor keywords that name
|
|
9
|
+
no parameter set a Qt property or connect a signal after construction, as PySide2
|
|
10
|
+
does. Anything ambiguous or unmapped keeps the native call, and its native error.
|
|
11
|
+
The table loads on the first call that actually passes keywords.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
import json
|
|
15
|
+
from collections.abc import Callable
|
|
16
|
+
from functools import cache
|
|
17
|
+
from pathlib import Path
|
|
18
|
+
from typing import Any
|
|
19
|
+
|
|
20
|
+
from ._app_conversions import _native
|
|
21
|
+
from ._app_flags import convert
|
|
22
|
+
|
|
23
|
+
SIGNAL_METHODS = frozenset({"connect", "disconnect", "emit"})
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@cache
|
|
27
|
+
def _table() -> dict[str, tuple[tuple[str, dict], ...]]:
|
|
28
|
+
"""Generated table by method name: (native class path, entry) pairs."""
|
|
29
|
+
path = Path(__file__).with_name("_keywords.json")
|
|
30
|
+
data = json.loads(path.read_text(encoding="utf-8"))
|
|
31
|
+
by_method: dict[str, list] = {}
|
|
32
|
+
for key, methods in sorted(data["classes"].items()):
|
|
33
|
+
for method, index in sorted(methods.items()):
|
|
34
|
+
by_method.setdefault(method, []).append((key, data["entries"][index]))
|
|
35
|
+
return {method: tuple(pairs) for method, pairs in by_method.items()}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def native_base(cls: type) -> type | None:
|
|
39
|
+
"""The first PythonQt class in a (possibly Python-derived) class's MRO."""
|
|
40
|
+
return next((b for b in cls.__mro__ if b.__module__.startswith("PythonQt")), None)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def native_init(cls: type) -> bool:
|
|
44
|
+
"""True when ``cls.__init__`` is PythonQt's: no Python class before the first
|
|
45
|
+
PythonQt base defines one (``object.__init__`` comes after it and is ignored)."""
|
|
46
|
+
for base in cls.__mro__:
|
|
47
|
+
if base.__module__.startswith("PythonQt"):
|
|
48
|
+
return True
|
|
49
|
+
if "__init__" in vars(base):
|
|
50
|
+
return False
|
|
51
|
+
return False
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _entry(receiver: Any, name: str) -> dict | None:
|
|
55
|
+
"""Most-derived entry matching an instance, a class (static methods) or, for
|
|
56
|
+
``__init__``, exactly the native class being constructed."""
|
|
57
|
+
matches = []
|
|
58
|
+
for key, entry in _table().get(name, ()):
|
|
59
|
+
cls = _native(key)
|
|
60
|
+
if not isinstance(cls, type):
|
|
61
|
+
continue
|
|
62
|
+
if name == "__init__":
|
|
63
|
+
matched = receiver is cls
|
|
64
|
+
elif isinstance(receiver, type):
|
|
65
|
+
matched = entry["static"] and issubclass(receiver, cls)
|
|
66
|
+
else:
|
|
67
|
+
matched = isinstance(receiver, cls)
|
|
68
|
+
if matched:
|
|
69
|
+
matches.append((len(cls.__mro__), entry))
|
|
70
|
+
return max(matches, key=lambda m: m[0])[1] if matches else None
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
_MISSING = object()
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _default(value: Any) -> Any:
|
|
77
|
+
"""A recorded default; {"native": path} resolves now or is missing."""
|
|
78
|
+
if isinstance(value, dict):
|
|
79
|
+
resolved = _native(value["native"])
|
|
80
|
+
return _MISSING if resolved is None else resolved
|
|
81
|
+
return value
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _fit(overload: dict, args: tuple, kwargs: dict, leftover: bool) -> tuple | None:
|
|
85
|
+
"""(positional, unused keywords) for one overload, or None if it cannot fit."""
|
|
86
|
+
names = overload["parameters"]
|
|
87
|
+
if len(args) > len(names) or any(n in kwargs for n in names[: len(args)]):
|
|
88
|
+
return None
|
|
89
|
+
unused = {k: v for k, v in kwargs.items() if k not in names}
|
|
90
|
+
if unused and not leftover:
|
|
91
|
+
return None
|
|
92
|
+
named = [i for i, n in enumerate(names) if n in kwargs]
|
|
93
|
+
positional = list(args)
|
|
94
|
+
for index in range(len(args), max(named, default=len(args) - 1) + 1):
|
|
95
|
+
name = names[index]
|
|
96
|
+
value = (
|
|
97
|
+
kwargs[name]
|
|
98
|
+
if name in kwargs
|
|
99
|
+
else _default(overload["defaults"].get(name, _MISSING))
|
|
100
|
+
)
|
|
101
|
+
if value is _MISSING:
|
|
102
|
+
return None
|
|
103
|
+
positional.append(value)
|
|
104
|
+
if len(positional) < overload["required"] and not all(
|
|
105
|
+
n in overload["defaults"] for n in names[len(positional) : overload["required"]]
|
|
106
|
+
):
|
|
107
|
+
return None
|
|
108
|
+
return tuple(positional), unused
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def map_keywords(
|
|
112
|
+
entry: dict | None, args: tuple, kwargs: dict, *, leftover: bool
|
|
113
|
+
) -> tuple | None:
|
|
114
|
+
"""The single positional form the keywords select, with the fewest unused names."""
|
|
115
|
+
if entry is None:
|
|
116
|
+
return (tuple(args), dict(kwargs)) if leftover else None
|
|
117
|
+
fits = [f for o in entry["overloads"] if (f := _fit(o, args, kwargs, leftover))]
|
|
118
|
+
if leftover and not fits:
|
|
119
|
+
parameters = {n for o in entry["overloads"] for n in o["parameters"]}
|
|
120
|
+
if parameters & set(kwargs): # a parameter keyword that no form can place
|
|
121
|
+
return None
|
|
122
|
+
fits = [(tuple(args), dict(kwargs))]
|
|
123
|
+
if not fits:
|
|
124
|
+
return None
|
|
125
|
+
fewest = min(len(f[1]) for f in fits)
|
|
126
|
+
best = [f for f in fits if len(f[1]) == fewest]
|
|
127
|
+
# Distinct positional forms are ambiguous; native values are compared by identity.
|
|
128
|
+
first = best[0]
|
|
129
|
+
if any(
|
|
130
|
+
len(f[0]) != len(first[0]) or any(a is not b for a, b in zip(f[0], first[0]))
|
|
131
|
+
for f in best[1:]
|
|
132
|
+
):
|
|
133
|
+
return None
|
|
134
|
+
return first
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _connect(signal: Any, slot: Any, owner: Any = None, name: str | None = None) -> Any:
|
|
138
|
+
return signal.connect(slot)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def apply_leftovers(
|
|
142
|
+
obj: Any, leftover: dict, connect: Callable[[Any, Any], Any] = _connect
|
|
143
|
+
) -> None:
|
|
144
|
+
"""PySide2 constructor keywords: a Qt property is set, a signal is connected."""
|
|
145
|
+
for name, value in leftover.items():
|
|
146
|
+
meta = getattr(obj, "metaObject", None)
|
|
147
|
+
if callable(meta) and meta().indexOfProperty(name) >= 0:
|
|
148
|
+
if not obj.setProperty(name, value):
|
|
149
|
+
raise TypeError(f"Qt property {name!r} rejected {value!r}")
|
|
150
|
+
continue
|
|
151
|
+
signal = getattr(obj, name, None)
|
|
152
|
+
if callable(getattr(signal, "connect", None)):
|
|
153
|
+
connect(signal, value, obj, name)
|
|
154
|
+
continue
|
|
155
|
+
raise AttributeError(
|
|
156
|
+
f"{type(obj).__name__!r} has no Qt property or signal named {name!r}"
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def construct(
|
|
161
|
+
cls: type,
|
|
162
|
+
args: tuple,
|
|
163
|
+
kwargs: dict,
|
|
164
|
+
connect: Callable = _connect,
|
|
165
|
+
own: Callable[[Any], Any] = lambda obj: obj,
|
|
166
|
+
) -> Any:
|
|
167
|
+
"""``cls(*args, **kwargs)`` for a class whose ``__init__`` is native.
|
|
168
|
+
|
|
169
|
+
``own`` registers the new object before any property or signal keyword is
|
|
170
|
+
applied, so a rejected keyword cannot leave it outside cleanup."""
|
|
171
|
+
base = native_base(cls)
|
|
172
|
+
mapped = map_keywords(_entry(base, "__init__"), args, kwargs, leftover=True)
|
|
173
|
+
if mapped is None:
|
|
174
|
+
return cls(*args, **kwargs) # PythonQt's own keyword error
|
|
175
|
+
obj = own(cls(*convert(base, "__init__", mapped[0])))
|
|
176
|
+
apply_leftovers(obj, mapped[1], connect)
|
|
177
|
+
return obj
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def initialize(
|
|
181
|
+
init: Callable,
|
|
182
|
+
instance: Any,
|
|
183
|
+
base: type,
|
|
184
|
+
args: tuple,
|
|
185
|
+
kwargs: dict,
|
|
186
|
+
connect: Callable = _connect,
|
|
187
|
+
) -> Any:
|
|
188
|
+
"""``Base.__init__(self, ...)`` or ``super().__init__(...)`` with keywords.
|
|
189
|
+
|
|
190
|
+
``init`` takes the positional arguments only (the instance already bound)."""
|
|
191
|
+
mapped = map_keywords(_entry(base, "__init__"), args, kwargs, leftover=True)
|
|
192
|
+
if mapped is None:
|
|
193
|
+
return init(*args, **kwargs)
|
|
194
|
+
result = init(*convert(base, "__init__", mapped[0]))
|
|
195
|
+
apply_leftovers(instance, mapped[1], connect)
|
|
196
|
+
return result
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def keyword_method(receiver: Any, name: str, inner: Callable) -> Callable:
|
|
200
|
+
"""Wrap a native method so PySide2 keywords become positional arguments."""
|
|
201
|
+
|
|
202
|
+
def call(*args: Any, **kwargs: Any) -> Any:
|
|
203
|
+
if not kwargs:
|
|
204
|
+
return inner(*args)
|
|
205
|
+
mapped = map_keywords(_entry(receiver, name), args, kwargs, leftover=False)
|
|
206
|
+
if mapped is None:
|
|
207
|
+
return inner(*args, **kwargs)
|
|
208
|
+
return inner(*mapped[0])
|
|
209
|
+
|
|
210
|
+
return call
|
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
# this_file: src/pythonqtside/_app_native.py
|
|
2
|
+
"""Measured PythonQt property access and FontLab's existing application."""
|
|
3
|
+
|
|
4
|
+
import json
|
|
5
|
+
import threading
|
|
6
|
+
from functools import cache, partial
|
|
7
|
+
from importlib import import_module
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from types import BuiltinMethodType
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
from ._app_conversions import conversion_call
|
|
13
|
+
from ._app_dialogs import file_dialog_call
|
|
14
|
+
from ._app_flags import flags_call
|
|
15
|
+
from ._app_keywords import SIGNAL_METHODS, keyword_method
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def native_instance(value: Any) -> bool:
|
|
19
|
+
"""Recognize PythonQt's actual wrapper ancestry, including user subclasses."""
|
|
20
|
+
return not isinstance(value, type) and any(
|
|
21
|
+
base.__module__.startswith("PythonQt") for base in type(value).__mro__
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def native_class(value: Any) -> bool:
|
|
26
|
+
"""Preserve actual class objects; detect native and Python-derived constructors."""
|
|
27
|
+
return isinstance(value, type) and any(
|
|
28
|
+
base.__module__.startswith("PythonQt") for base in value.__mro__
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def native_signal_instance(value: Any) -> bool:
|
|
33
|
+
"""Measured QtCore.Signal has Signal/object ancestry; identify its actual type."""
|
|
34
|
+
try:
|
|
35
|
+
signal_type = import_module("PythonQt").QtCore.Signal
|
|
36
|
+
except (ModuleNotFoundError, AttributeError):
|
|
37
|
+
return False
|
|
38
|
+
return type(value) is signal_type and all(
|
|
39
|
+
callable(getattr(value, name, None)) for name in ("connect", "disconnect")
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def same_connection_receiver(left: Any, right: Any) -> bool:
|
|
44
|
+
"""Native Signal wrappers compare backend identity; QObject/Python objects use is."""
|
|
45
|
+
if left is right:
|
|
46
|
+
return True
|
|
47
|
+
if native_signal_instance(left) and native_signal_instance(right):
|
|
48
|
+
return bool(left == right)
|
|
49
|
+
return False
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def native_signal_method(value: Any) -> tuple[Any, str] | None:
|
|
53
|
+
"""Recognize measured C-bound Signal methods without probing user callables."""
|
|
54
|
+
if type(value) is not BuiltinMethodType:
|
|
55
|
+
return None
|
|
56
|
+
name = value.__name__
|
|
57
|
+
if name in {"connect", "disconnect"} and native_signal_instance(value.__self__):
|
|
58
|
+
return value.__self__, name
|
|
59
|
+
return None
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def qobject_instance(value: Any) -> bool:
|
|
63
|
+
"""PythonQt QWidget is not isinstance(private.QObject); use measured capabilities."""
|
|
64
|
+
return native_instance(value) and all(
|
|
65
|
+
callable(getattr(value, name, None))
|
|
66
|
+
for name in ("metaObject", "parent", "setParent", "deleteLater")
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def widget_instance(value: Any) -> bool:
|
|
71
|
+
"""Use Qt inheritance metadata, never the unrelated private QObject wrapper."""
|
|
72
|
+
inherits = getattr(value, "inherits", None)
|
|
73
|
+
return qobject_instance(value) and callable(inherits) and bool(inherits("QWidget"))
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def destroyed(value: Any) -> bool:
|
|
77
|
+
"""PythonQt guards wrappers of deleted QObjects: a call raises
|
|
78
|
+
ValueError("Trying to call 'parent' on a destroyed QSpinBox object") (measured)."""
|
|
79
|
+
try:
|
|
80
|
+
value.parent()
|
|
81
|
+
except ValueError as error:
|
|
82
|
+
return "on a destroyed" in str(error)
|
|
83
|
+
return False
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def byte_call(kind: type, *args: Any, **kwargs: Any) -> Any:
|
|
87
|
+
"""``bytes(qbytearray)`` raises in PythonQt; PySide2 reads the buffer. Native
|
|
88
|
+
QByteArray arguments become their ``data()`` bytes (measured)."""
|
|
89
|
+
if args and native_instance(args[0]) and type(args[0]).__name__ == "QByteArray":
|
|
90
|
+
args = (args[0].data(), *args[1:])
|
|
91
|
+
return kind(*args, **kwargs)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def ui_loader(value: Any) -> bool:
|
|
95
|
+
"""A native QUiLoader (or Python subclass) instance."""
|
|
96
|
+
return native_instance(value) and any(
|
|
97
|
+
base.__name__ == "QUiLoader" and base.__module__.startswith("PythonQt")
|
|
98
|
+
for base in type(value).__mro__
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def close_owned_widget(value: Any) -> None:
|
|
103
|
+
if widget_instance(value):
|
|
104
|
+
value.close()
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def stop_owned(value: Any) -> None:
|
|
108
|
+
"""Stop the supported timer/process/thread primitives before scheduled deletion."""
|
|
109
|
+
inherits = getattr(value, "inherits", None)
|
|
110
|
+
if not callable(inherits):
|
|
111
|
+
return
|
|
112
|
+
for kind, operation in (
|
|
113
|
+
("QTimer", "stop"),
|
|
114
|
+
("QProcess", "kill"),
|
|
115
|
+
("QThread", "quit"),
|
|
116
|
+
):
|
|
117
|
+
if inherits(kind):
|
|
118
|
+
if kind == "QProcess" and resolve(value, "state")() == 0:
|
|
119
|
+
continue
|
|
120
|
+
if kind == "QThread" and not resolve(value, "isRunning")():
|
|
121
|
+
continue
|
|
122
|
+
method = getattr(value, operation, None)
|
|
123
|
+
if callable(method):
|
|
124
|
+
method()
|
|
125
|
+
if kind in {"QProcess", "QThread"}:
|
|
126
|
+
wait = getattr(
|
|
127
|
+
value, "waitForFinished" if kind == "QProcess" else "wait", None
|
|
128
|
+
)
|
|
129
|
+
if not callable(wait):
|
|
130
|
+
raise RuntimeError(
|
|
131
|
+
f"Owned {kind} cannot confirm termination; deletion withheld"
|
|
132
|
+
)
|
|
133
|
+
# waitForFinished is false once errorOccurred(Crashed) fires after
|
|
134
|
+
# kill(), although the process stopped (measured): check the state.
|
|
135
|
+
stopped = wait(1000) or (
|
|
136
|
+
kind == "QProcess" and resolve(value, "state")() == 0
|
|
137
|
+
)
|
|
138
|
+
if not stopped:
|
|
139
|
+
raise RuntimeError(
|
|
140
|
+
f"Owned {kind} did not stop within 1000 ms; deletion withheld"
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def require_host_thread() -> None:
|
|
145
|
+
"""The tested FontLab runtime has no QtCore.QThread.currentThread API."""
|
|
146
|
+
if threading.current_thread() is not threading.main_thread():
|
|
147
|
+
raise RuntimeError("run_app must execute on FontLab's main Python thread")
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def host_application() -> tuple[Any, tuple[type, ...]]:
|
|
151
|
+
"""Require the measured host GUI thread and reuse its Qt application."""
|
|
152
|
+
require_host_thread()
|
|
153
|
+
try:
|
|
154
|
+
qt = import_module("PythonQt")
|
|
155
|
+
except ModuleNotFoundError as error:
|
|
156
|
+
raise RuntimeError("run_app requires FontLab's native PythonQt") from error
|
|
157
|
+
app = qt.QtGui.QApplication.instance()
|
|
158
|
+
if app is None:
|
|
159
|
+
raise RuntimeError("FontLab's existing QApplication is unavailable")
|
|
160
|
+
classes = tuple(
|
|
161
|
+
getattr(module, name)
|
|
162
|
+
for module, name in [
|
|
163
|
+
(qt.QtGui, "QApplication"),
|
|
164
|
+
(qt.QtGui, "QGuiApplication"),
|
|
165
|
+
(qt.QtCore, "QCoreApplication"),
|
|
166
|
+
]
|
|
167
|
+
if hasattr(module, name)
|
|
168
|
+
)
|
|
169
|
+
return app, classes
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def _getter(instance: Any, name: str) -> Any:
|
|
173
|
+
"""Only adapt properties confirmed by the instance's actual Qt metaobject."""
|
|
174
|
+
if not native_instance(instance):
|
|
175
|
+
return None
|
|
176
|
+
meta = getattr(instance, "metaObject", None)
|
|
177
|
+
if not callable(meta) or meta().indexOfProperty(name) < 0:
|
|
178
|
+
return None
|
|
179
|
+
return lambda: instance.property(name)
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def _super_bases(receiver: super) -> tuple[type, ...]:
|
|
183
|
+
"""Search the bound MRO after super's anchor, including class-bound super."""
|
|
184
|
+
if receiver.__self_class__ is None:
|
|
185
|
+
return ()
|
|
186
|
+
mro = receiver.__self_class__.__mro__
|
|
187
|
+
return mro[mro.index(receiver.__thisclass__) + 1 :]
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def _super_fallback(receiver: super, name: str) -> Any:
|
|
191
|
+
"""Prepare explicit native base dispatch without adding an invocation frame."""
|
|
192
|
+
for base in _super_bases(receiver):
|
|
193
|
+
if not base.__module__.startswith("PythonQt"):
|
|
194
|
+
continue
|
|
195
|
+
method = getattr(base, name, None)
|
|
196
|
+
if callable(method):
|
|
197
|
+
return partial(method, receiver.__self__)
|
|
198
|
+
return _getter(receiver.__self__, name)
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
@cache
|
|
202
|
+
def _out_parameter_tables() -> tuple[dict, dict, frozenset[str]]:
|
|
203
|
+
"""Generated _out_parameters.json: predicate and BoolResult tables, method names."""
|
|
204
|
+
path = Path(__file__).with_name("_out_parameters.json")
|
|
205
|
+
table = json.loads(path.read_text(encoding="utf-8"))
|
|
206
|
+
names = {
|
|
207
|
+
m
|
|
208
|
+
for kind in ("predicate", "bool_result")
|
|
209
|
+
for c in table[kind].values()
|
|
210
|
+
for m in c
|
|
211
|
+
}
|
|
212
|
+
return table["predicate"], table["bool_result"], frozenset(names)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def _native_class(key: str) -> type | None:
|
|
216
|
+
module, _, name = key.partition(".")
|
|
217
|
+
try:
|
|
218
|
+
return getattr(getattr(import_module("PythonQt"), module), name)
|
|
219
|
+
except (ModuleNotFoundError, AttributeError):
|
|
220
|
+
return None
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def _python_defined(receiver: Any, name: str) -> bool:
|
|
224
|
+
"""Instance or non-PythonQt class definitions keep their own Python behavior."""
|
|
225
|
+
if not isinstance(receiver, type) and name in getattr(receiver, "__dict__", {}):
|
|
226
|
+
return True
|
|
227
|
+
mro = receiver.__mro__ if isinstance(receiver, type) else type(receiver).__mro__
|
|
228
|
+
return any(
|
|
229
|
+
name in vars(base) for base in mro if not base.__module__.startswith("PythonQt")
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def _measured(table: dict, receiver: Any, name: str) -> Any:
|
|
234
|
+
"""The table entry whose native class matches an instance (or a class, if static)."""
|
|
235
|
+
for key, methods in table.items():
|
|
236
|
+
entry = methods.get(name)
|
|
237
|
+
cls = _native_class(key) if entry is not None else None
|
|
238
|
+
if cls is None:
|
|
239
|
+
continue
|
|
240
|
+
if isinstance(receiver, type):
|
|
241
|
+
if (
|
|
242
|
+
isinstance(entry, dict)
|
|
243
|
+
and entry["static"]
|
|
244
|
+
and issubclass(receiver, cls)
|
|
245
|
+
):
|
|
246
|
+
return entry
|
|
247
|
+
elif isinstance(receiver, cls):
|
|
248
|
+
return entry
|
|
249
|
+
return None
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def _out_parameter_call(
|
|
253
|
+
receiver: Any, name: str, method: Any, native_method: bool = False
|
|
254
|
+
) -> Any:
|
|
255
|
+
"""Measured PySide2 (value, ok) results; returns None when no adapter applies."""
|
|
256
|
+
predicates, bool_results, names = _out_parameter_tables()
|
|
257
|
+
if name not in names or (not native_method and _python_defined(receiver, name)):
|
|
258
|
+
return None
|
|
259
|
+
if not (native_instance(receiver) or native_class(receiver)):
|
|
260
|
+
return None
|
|
261
|
+
if not isinstance(receiver, type):
|
|
262
|
+
predicate = _measured(predicates, receiver, name)
|
|
263
|
+
if predicate is not None:
|
|
264
|
+
return partial(_with_ok, receiver, method, predicate)
|
|
265
|
+
entry = _measured(bool_results, receiver, name)
|
|
266
|
+
return None if entry is None else partial(_with_bool_result, method, entry)
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def _with_ok(
|
|
270
|
+
receiver: Any, method: Any, predicate: str, *args: Any, **kwargs: Any
|
|
271
|
+
) -> Any:
|
|
272
|
+
"""PySide2 zero-argument form returns (native value, native predicate result)."""
|
|
273
|
+
if args or kwargs: # PythonQt-style out-parameter use stays native.
|
|
274
|
+
return method(*args, **kwargs)
|
|
275
|
+
return method(), getattr(receiver, predicate)()
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
def _native_default(value: Any) -> Any:
|
|
279
|
+
"""Qt declaration default; {"native": "QtGui.QLineEdit.Normal"} resolves lazily."""
|
|
280
|
+
if not isinstance(value, dict):
|
|
281
|
+
return value
|
|
282
|
+
result = import_module("PythonQt")
|
|
283
|
+
for part in value["native"].split("."):
|
|
284
|
+
result = getattr(result, part)
|
|
285
|
+
return result
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
def _with_bool_result(method: Any, entry: dict, *args: Any, **kwargs: Any) -> Any:
|
|
289
|
+
"""PySide2 positional call -> native call with BoolResult -> (value, bool(ok))."""
|
|
290
|
+
bool_result = import_module("PythonQt").BoolResult
|
|
291
|
+
index, params, defaults = entry["out_index"], entry["parameters"], entry["defaults"]
|
|
292
|
+
native_style = kwargs or len(args) > entry["max_arguments"]
|
|
293
|
+
missing = [p for p in params[len(args) :] if p not in defaults]
|
|
294
|
+
if native_style or missing or any(isinstance(a, bool_result) for a in args):
|
|
295
|
+
return method(*args, **kwargs)
|
|
296
|
+
filled = [
|
|
297
|
+
*args[:index],
|
|
298
|
+
*(_native_default(defaults[p]) for p in params[len(args) :]),
|
|
299
|
+
]
|
|
300
|
+
ok = bool_result()
|
|
301
|
+
value = method(*filled, ok, *args[index:])
|
|
302
|
+
return value, bool(ok)
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
def _conversion_call(
|
|
306
|
+
receiver: Any, name: str, method: Any, native_method: bool = False
|
|
307
|
+
) -> Any:
|
|
308
|
+
"""Measured implicit enum -> value-class wrapping on native, unoverridden methods."""
|
|
309
|
+
if not (native_instance(receiver) or native_class(receiver)):
|
|
310
|
+
return None
|
|
311
|
+
if not native_method and _python_defined(receiver, name):
|
|
312
|
+
return None
|
|
313
|
+
return conversion_call(receiver, name, method)
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def captured_call(method: Any, keywords: bool = False) -> Any:
|
|
317
|
+
"""Adapt a bound native slot stored earlier, with or without keywords
|
|
318
|
+
(``resize = label.resize; resize(1, 2)``, or ``base = super().resize``).
|
|
319
|
+
|
|
320
|
+
Only the slot's measured ``__self__``/``__name__`` select the keyword table
|
|
321
|
+
entry; the captured callable itself is called, so a captured base dispatch
|
|
322
|
+
never re-resolves to a Python override."""
|
|
323
|
+
if type(method).__name__ != "builtin_qt_slot":
|
|
324
|
+
return method
|
|
325
|
+
receiver = getattr(method, "__self__", None)
|
|
326
|
+
name = getattr(method, "__name__", None)
|
|
327
|
+
if receiver is None or not name or name in SIGNAL_METHODS:
|
|
328
|
+
return method
|
|
329
|
+
return _adapt(receiver, name, method, keywords, native_method=True)
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def _keyword_call(
|
|
333
|
+
receiver: Any, name: str, method: Any, native_method: bool = False
|
|
334
|
+
) -> Any:
|
|
335
|
+
"""Measured keyword-to-positional mapping on native, unoverridden methods."""
|
|
336
|
+
if name in SIGNAL_METHODS or name == "__init__" or native_signal_instance(method):
|
|
337
|
+
return method
|
|
338
|
+
if not (native_instance(receiver) or native_class(receiver)):
|
|
339
|
+
return method
|
|
340
|
+
if not native_method and _python_defined(receiver, name):
|
|
341
|
+
return method
|
|
342
|
+
return keyword_method(receiver, name, method)
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
def _adapt(
|
|
346
|
+
receiver: Any, name: str, method: Any, keywords: bool, native_method: bool = False
|
|
347
|
+
) -> Any:
|
|
348
|
+
"""The adapter chain for a native method: out-parameter or enum conversion,
|
|
349
|
+
then QFlags, then (for keyword call sites) keyword mapping outermost.
|
|
350
|
+
|
|
351
|
+
``native_method`` marks a captured bound native slot: it is native by
|
|
352
|
+
construction, so a Python override of the same name on the receiver (a
|
|
353
|
+
cooperative ``base = super().resize``) must not switch the adapters off."""
|
|
354
|
+
adapted = _out_parameter_call(receiver, name, method, native_method)
|
|
355
|
+
if adapted is None and (native_method or not _python_defined(receiver, name)):
|
|
356
|
+
adapted = file_dialog_call(receiver, name, method)
|
|
357
|
+
if adapted is None:
|
|
358
|
+
adapted = _conversion_call(receiver, name, method, native_method)
|
|
359
|
+
adapted = method if adapted is None else adapted
|
|
360
|
+
if (native_method or not _python_defined(receiver, name)) and (
|
|
361
|
+
native_instance(receiver) or native_class(receiver)
|
|
362
|
+
):
|
|
363
|
+
adapted = flags_call(receiver, name, adapted)
|
|
364
|
+
if keywords:
|
|
365
|
+
adapted = _keyword_call(receiver, name, adapted, native_method)
|
|
366
|
+
return adapted
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
def resolve(receiver: Any, name: str, keywords: bool = False) -> Any:
|
|
370
|
+
"""Retain Python behavior; adapt native Qt property getters and measured out-params.
|
|
371
|
+
|
|
372
|
+
``keywords`` marks a call site that passes keyword arguments: native methods then
|
|
373
|
+
map PySide2 keyword names to positions before the other adapters run."""
|
|
374
|
+
if isinstance(receiver, super):
|
|
375
|
+
instance = receiver.__self__
|
|
376
|
+
try:
|
|
377
|
+
value = getattr(receiver, name)
|
|
378
|
+
except AttributeError:
|
|
379
|
+
fallback = _super_fallback(receiver, name)
|
|
380
|
+
if fallback is not None:
|
|
381
|
+
native = isinstance(fallback, partial) and name not in SIGNAL_METHODS
|
|
382
|
+
return (
|
|
383
|
+
_adapt(instance, name, fallback, keywords, native_method=True)
|
|
384
|
+
if native
|
|
385
|
+
else fallback
|
|
386
|
+
)
|
|
387
|
+
raise
|
|
388
|
+
owner = next(
|
|
389
|
+
(base for base in _super_bases(receiver) if name in vars(base)), None
|
|
390
|
+
)
|
|
391
|
+
if callable(value):
|
|
392
|
+
native = owner is None or owner.__module__.startswith("PythonQt")
|
|
393
|
+
if native and name not in SIGNAL_METHODS:
|
|
394
|
+
return _adapt(instance, name, value, keywords, native_method=True)
|
|
395
|
+
return value
|
|
396
|
+
if owner is not None and not owner.__module__.startswith("PythonQt"):
|
|
397
|
+
return value
|
|
398
|
+
getter = _getter(instance, name)
|
|
399
|
+
return getter if getter is not None else value
|
|
400
|
+
value = getattr(receiver, name)
|
|
401
|
+
if callable(value):
|
|
402
|
+
return _adapt(receiver, name, value, keywords)
|
|
403
|
+
getter = _getter(receiver, name)
|
|
404
|
+
return getter if getter is not None else value
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# this_file: src/pythonqtside/_app_proxy.py
|
|
2
|
+
"""Explicit host application identity exception for the embedded runner."""
|
|
3
|
+
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class ApplicationProxy:
|
|
8
|
+
"""Explicit identity exception: embedded application lifecycle is handle-local."""
|
|
9
|
+
|
|
10
|
+
def __init__(self, handle: Any, native: Any) -> None:
|
|
11
|
+
self._handle = handle
|
|
12
|
+
self.native_application = native
|
|
13
|
+
|
|
14
|
+
def __call__(self, *args: Any, **kwargs: Any) -> "ApplicationProxy":
|
|
15
|
+
self._handle._require_open()
|
|
16
|
+
return self
|
|
17
|
+
|
|
18
|
+
def __mro_entries__(self, bases: tuple) -> tuple:
|
|
19
|
+
raise NotImplementedError("Embedded application subclasses are unsupported")
|
|
20
|
+
|
|
21
|
+
def instance(self) -> "ApplicationProxy":
|
|
22
|
+
self._handle._require_open()
|
|
23
|
+
return self
|
|
24
|
+
|
|
25
|
+
def exec(self) -> int:
|
|
26
|
+
self._handle._require_open()
|
|
27
|
+
return 0
|
|
28
|
+
|
|
29
|
+
exec_ = exec
|
|
30
|
+
|
|
31
|
+
def quit(self) -> None:
|
|
32
|
+
self._handle.close()
|
|
33
|
+
|
|
34
|
+
def exit(self, returnCode: int = 0) -> None:
|
|
35
|
+
self._handle.close()
|
|
36
|
+
|
|
37
|
+
closeAllWindows = quit
|
|
38
|
+
|
|
39
|
+
def __getattr__(self, name: str) -> Any:
|
|
40
|
+
raise NotImplementedError(
|
|
41
|
+
f"Embedded application member {name!r} is not faithfully scoped; "
|
|
42
|
+
"global application settings and operations are unsupported"
|
|
43
|
+
)
|