Skip to content

Commit 5bb3c34

Browse files
authored
Merge pull request #136 from abstractfactory/fix135
Fix #135
2 parents 83e8044 + 0d67bf9 commit 5bb3c34

File tree

3 files changed

+76
-10
lines changed

3 files changed

+76
-10
lines changed

Qt.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import os
3232
import sys
3333

34-
__version__ = "0.4.1"
34+
__version__ = "0.4.2"
3535

3636
# All unique members of Qt.py
3737
__added__ = list()
@@ -68,14 +68,20 @@ def remap(object, name, value, safe=True):
6868
raise AttributeError("%s != 'module': Cannot alter "
6969
"anything but modules" % object)
7070

71-
__remapped__.append(name)
71+
elif hasattr(object, name):
72+
# Keep track of modifications
73+
__modified__.append(name)
74+
75+
if name not in __added__:
76+
__remapped__.append(name)
77+
7278
setattr(object, name, value)
7379

7480

75-
def add(object, name, value):
81+
def add(object, name, value, safe=True):
7682
"""Identical to :func:`remap` and provided for readability only"""
7783
__added__.append(name)
78-
remap(object, name, value)
84+
remap(object, name, value, safe)
7985

8086

8187
def pyqt5():
@@ -89,7 +95,7 @@ def pyqt5():
8995
add(PyQt5, "__wrapper_version__", __version__)
9096
add(PyQt5, "__binding__", "PyQt5")
9197
add(PyQt5, "__binding_version__", QtCore.PYQT_VERSION_STR)
92-
add(PyQt5, "__qt_version__", QtCore.QT_VERSION_STR)
98+
add(PyQt5, "__qt_version__", QtCore.QT_VERSION_STR, safe=False)
9399
add(PyQt5, "__added__", __added__)
94100
add(PyQt5, "__remapped__", __remapped__)
95101
add(PyQt5, "__modified__", __modified__)
@@ -244,20 +250,24 @@ def init():
244250
try:
245251
binding = binding()
246252

253+
except ImportError as e:
254+
log(" - ImportError(\"%s\")\n" % e, verbose)
255+
continue
256+
257+
else:
258+
# Reference to this module
259+
binding.__shim__ = sys.modules[__name__]
260+
247261
sys.modules.update({
248262
__name__: binding,
249263

250264
# Fix #133, `from Qt.QtWidgets import QPushButton`
251265
__name__ + ".QtWidgets": binding.QtWidgets
266+
252267
})
253268

254269
return
255270

256-
except ImportError as e:
257-
log(" - ImportError(\"%s\")\n" % e, verbose)
258-
259-
continue
260-
261271
# If not binding were found, throw this error
262272
raise ImportError("No Qt binding were found.")
263273

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ All members of `Qt` stem directly from those available via PySide2, along with t
8888
| `__added__` | `list(str)` | All unique members of Qt.py
8989
| `__remapped__` | `list(str)` | Members copied from elsewhere, such as QtGui -> QtWidgets
9090
| `__modified__` | `list(str)` | Existing members modified in some way
91+
| `__shim__` | `str` | Reference to original Qt.py Python module
9192
| `load_ui(fname=str)` | `QObject` | Minimal wrapper of PyQt4.loadUi and PySide equivalent
9293

9394
<br>

tests.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,61 @@ def test_vendoring():
159159
) == 0
160160

161161

162+
def test_meta_add():
163+
"""Qt.add() appends to __added__"""
164+
import types
165+
import Qt
166+
167+
module = imp.new_module("QtMock")
168+
169+
Qt.__shim__.add(module, "MyAttr", None)
170+
171+
assert "MyAttr" in Qt.__added__, Qt.__added__
172+
assert "MyAttr" not in Qt.__remapped__
173+
assert "MyAttr" not in Qt.__modified__
174+
175+
def test_meta_remap():
176+
"""Qt.remap() appends to __modified__"""
177+
import types
178+
import Qt
179+
180+
module = imp.new_module("QtMock")
181+
182+
Qt.__shim__.remap(module, "MyAttr", None)
183+
184+
assert "MyAttr" not in Qt.__added__, Qt.__added__
185+
assert "MyAttr" in Qt.__remapped__
186+
assert "MyAttr" not in Qt.__modified__
187+
188+
189+
190+
def test_meta_add_existing():
191+
"""Qt.add() appends to __added__"""
192+
import types
193+
import Qt
194+
195+
module = imp.new_module("QtMock")
196+
module.MyAttr = None
197+
198+
assert_raises(AttributeError, Qt.__shim__.add, module, "MyAttr", None)
199+
200+
201+
def test_meta_force_add_existing():
202+
"""Unsafe Qt.add() of existing member adds to modified"""
203+
import types
204+
import Qt
205+
206+
module = imp.new_module("QtMock")
207+
module.MyAttr = 123
208+
209+
Qt.__shim__.add(module, "MyAttr", None, safe=False)
210+
211+
assert "MyAttr" in Qt.__added__, Qt.__added__
212+
assert "MyAttr" not in Qt.__remapped__
213+
assert "MyAttr" in Qt.__modified__
214+
assert module.MyAttr is None, module.MyAttr
215+
216+
162217
def test_import_from_qtwidgets():
163218
"""Fix #133, `from Qt.QtWidgets import XXX` works"""
164219
from Qt.QtWidgets import QPushButton

0 commit comments

Comments
 (0)