Skip to content

Commit 23fe1ef

Browse files
committed
FIX: fixed viewer being inusable in a script run via PyCharm "run with console" (closes #253)
1 parent 1ad9834 commit 23fe1ef

File tree

4 files changed

+32
-40
lines changed

4 files changed

+32
-40
lines changed
Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,6 @@
11
.. py:currentmodule:: larray_editor
22

3-
Syntax changes
4-
^^^^^^^^^^^^^^
5-
6-
* renamed ``MappingEditor.old_method_name()`` to :py:obj:`MappingEditor.new_method_name()` (closes :editor_issue:`1`).
7-
8-
* renamed ``old_argument_name`` argument of :py:obj:`MappingEditor.method_name()` to ``new_argument_name``.
9-
10-
11-
Backward incompatible changes
12-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13-
14-
* other backward incompatible changes
15-
16-
17-
New features
18-
^^^^^^^^^^^^
19-
20-
* added a feature (see the :ref:`miscellaneous section <misc_editor>` for details).
21-
22-
* added another feature in the editor (closes :editor_issue:`1`).
23-
24-
.. note::
25-
26-
- It works for foo bar !
27-
- It does not work for foo baz !
28-
29-
30-
.. _misc_editor:
31-
32-
Miscellaneous improvements
33-
^^^^^^^^^^^^^^^^^^^^^^^^^^
34-
35-
* improved something.
36-
37-
383
Fixes
394
^^^^^
405

41-
* fixed something (closes :editor_issue:`1`).
6+
* fixed viewer being inusable in a script run via PyCharm "run with console" (closes :editor_issue:`253`).

larray_editor/api.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,19 @@ def _show_dialog(app_name, create_dialog_func, *args, **kwargs):
2626
The function which creates the dialog.
2727
"""
2828
qt_app = QApplication.instance()
29-
new_app = qt_app is None
30-
if new_app:
29+
if qt_app is None:
3130
qt_app = QApplication(sys.argv)
3231
qt_app.setOrganizationName("LArray")
3332
qt_app.setApplicationName(app_name)
3433
parent = None
3534
else:
35+
# activeWindow is defined only if the Window has keyboard focus,
36+
# so it could be None even if the app has a window open
3637
parent = qt_app.activeWindow()
38+
if parent is None:
39+
app_windows = qt_app.topLevelWindows()
40+
if len(app_windows) > 0:
41+
parent = app_windows[0]
3742

3843
if 'depth' in kwargs:
3944
kwargs['depth'] += 1
@@ -43,7 +48,16 @@ def _show_dialog(app_name, create_dialog_func, *args, **kwargs):
4348
raise RuntimeError('Could not create dialog')
4449

4550
dlg.show()
46-
if new_app:
51+
52+
# We used to test whether qt_app was None, but it failed when an instance existed with no
53+
# event loop started such as when running code via PyCharm's "Run File in Python Console"
54+
# feature. See https://github.com/larray-project/larray-editor/issues/253.
55+
56+
# We use whether any Qt window exists in the application as a proxy to test whether
57+
# Qt main event loop runs. This is probably wrong for an application which uses Qt event
58+
# system but not its GUI (i.e. using QtCoreApplication) but I haven't found any way
59+
# to check explicitly whether the main event loop is already running.
60+
if parent is None:
4761
# We do not use install_except_hook/restore_except_hook so that we can restore the hook actually used when
4862
# this function is called instead of the one which was used when the module was loaded.
4963

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from larray import view, ndtest
2+
3+
4+
# run via right-click -> Run File in Python Console
5+
def test_view():
6+
arr = ndtest(3)
7+
view(arr)
8+
9+
10+
if __name__ == '__main__':
11+
test_view()

setup.cfg

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ test=pytest
33

44
[tool:pytest]
55
testpaths = larray_editor
6-
addopts = -v --doctest-modules --ignore=larray_editor/tests/test_api_larray.py
6+
addopts = -v --doctest-modules
7+
--ignore=larray_editor/tests/test_api_larray.py
8+
--ignore=larray_editor/tests/run_with_console.py
79
#--maxfail=1 --cov (requires pytest-cov) --pep8 (requires pytest-pep8)
810
#pep8maxlinelength = 119

0 commit comments

Comments
 (0)