Skip to content

Commit e030a49

Browse files
committed
Merge branch 'release/3.51.1'
2 parents 8d961d7 + 76c5a8c commit e030a49

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed

.github/FUNDING.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github: WoLpH

progressbar/__about__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
long running operations.
2020
'''.strip().split())
2121
__email__ = 'wolph@wol.ph'
22-
__version__ = '3.51.0'
22+
__version__ = '3.51.1'
2323
__license__ = 'BSD'
2424
__copyright__ = 'Copyright 2015 Rick van Hattem (Wolph)'
2525
__url__ = 'https://github.com/WoLpH/python-progressbar'

progressbar/bar.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,19 @@ def __init__(self, min_value=0, max_value=None, widgets=None,
308308
self.min_value = min_value
309309
self.max_value = max_value
310310
self.max_error = max_error
311-
self.widgets = deepcopy(widgets)
311+
312+
# Only copy the widget if it's safe to copy. Most widgets are so we
313+
# assume this to be true
314+
if widgets is None:
315+
self.widgets = widgets
316+
else:
317+
self.widgets = []
318+
for widget in widgets:
319+
if getattr(widget, 'copy', True):
320+
widget = deepcopy(widget)
321+
self.widgets.append(widget)
322+
323+
self.widgets = widgets
312324
self.prefix = prefix
313325
self.suffix = suffix
314326
self.widget_kwargs = widget_kwargs or {}

progressbar/widgets.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,11 @@ class WidgetBase(WidthWidgetMixin):
189189
- max_width: Only display the widget if at most `max_width` is left
190190
- weight: Widgets with a higher `weigth` will be calculated before widgets
191191
with a lower one
192+
- copy: Copy this widget when initializing the progress bar so the
193+
progressbar can be reused. Some widgets such as the FormatCustomText
194+
require the shared state so this needs to be optional
192195
'''
196+
copy = True
193197

194198
@abc.abstractmethod
195199
def __call__(self, progress, data):
@@ -782,6 +786,7 @@ def __call__(self, progress, data, width):
782786

783787
class FormatCustomText(FormatWidgetMixin, WidgetBase):
784788
mapping = {}
789+
copy = False
785790

786791
def __init__(self, format, mapping=mapping, **kwargs):
787792
self.format = format

tests/test_custom_widgets.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,22 @@ def test_variable_widget_widget():
6161
i += 1
6262
p.update(i, text=True, error='a')
6363
p.finish()
64+
65+
66+
def test_format_custom_text_widget():
67+
widget = progressbar.FormatCustomText(
68+
'Spam: %(spam).1f kg, eggs: %(eggs)d',
69+
dict(
70+
spam=0.25,
71+
eggs=3,
72+
),
73+
)
74+
75+
bar = progressbar.ProgressBar(widgets=[
76+
widget,
77+
])
78+
79+
for i in bar(range(5)):
80+
widget.update_mapping(eggs=i * 2)
81+
assert widget.mapping['eggs'] == bar.widgets[0].mapping['eggs']
82+

0 commit comments

Comments
 (0)