Skip to content

Commit 9923385

Browse files
committed
Avoid duplicate Kivy exception handlers
1 parent ba64125 commit 9923385

2 files changed

Lines changed: 16 additions & 31 deletions

File tree

pythonhere/exception_manager_here.kv

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
font_size: '18sp'
1010
height: '24sp'
1111
text: 'Exception details: '
12-
CodeInput:
13-
id: catched_exception_code_input_here
14-
size_hint: 1, 1
15-
text: root.message
16-
size_hint: 1, 1
12+
ScrollView:
13+
CodeInput:
14+
id: catched_exception_code_input_here
15+
text: root.message
16+
size_hint: 1, None
17+
height: self.minimum_height
1718
Button:
1819
size_hint_y: None
1920
height: '40sp'

pythonhere/exception_manager_here.py

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import asyncio
44
import traceback
5+
from pathlib import Path
56

67
from kivy.base import (
78
ExceptionHandler,
@@ -16,31 +17,10 @@
1617

1718
def load_exception_popup_style():
1819
"""Load KV rules for `UnhandledExceptionPopupHere`."""
19-
Builder.load_string(
20-
"""<-UnhandledExceptionPopupHere>:
21-
title: "Unhandled Exception catched"
22-
BoxLayout:
23-
orientation: 'vertical'
24-
padding: 10
25-
spacing: 20
26-
Label:
27-
size_hint_y: None
28-
font_size: '18sp'
29-
height: '24sp'
30-
text: 'Exception details: '
31-
ScrollView:
32-
CodeInput:
33-
id: catched_exception_code_input_here
34-
text: root.message
35-
size_hint: 1, None
36-
height: self.minimum_height
37-
Button:
38-
size_hint_y: None
39-
height: '40sp'
40-
text: 'OK, continue'
41-
on_press: root.dismiss()
42-
"""
43-
)
20+
kv_path = str(Path(__file__).with_suffix(".kv"))
21+
22+
if kv_path not in Builder.files:
23+
Builder.load_file(kv_path)
4424

4525

4626
class ErrorMessageOnException(ExceptionHandler):
@@ -63,7 +43,11 @@ class UnhandledExceptionPopupHere(Popup):
6343

6444
def install_exception_handler():
6545
"""Install `ErrorMessageOnException` exception handler."""
66-
ExceptionManager.add_handler(ErrorMessageOnException())
46+
if not any(
47+
isinstance(handler, ErrorMessageOnException)
48+
for handler in ExceptionManager.handlers
49+
):
50+
ExceptionManager.add_handler(ErrorMessageOnException())
6751

6852

6953
def show_exception_popup(exc: Exception | None = None):

0 commit comments

Comments
 (0)