Skip to content

Commit cd85593

Browse files
committed
Merge branch 'qrcode-clear-reset-object'
2 parents 290b1ea + be8595b commit cd85593

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ WIP 8.x
4949
- Improved test coveraged (akx in `#315`_)
5050
- Implement Ruff rules and perform comprehensive code cleanup. (bartTC in `#408`_)
5151
- Modernize development setup: use ``uv`` and ``just``, add support for Python 3.14, and drop support for Python 3.9. (hugovk in `#420`_)
52+
- Fix ``QRCode.clear()`` to properly reset the internal state, allowing instance reuse. (m000 in `#411`_)
5253

5354
.. _#315: https://github.com/lincolnloop/python-qrcode/pull/315
5455
.. _#349: https://github.com/lincolnloop/python-qrcode/pull/349
@@ -57,6 +58,7 @@ WIP 8.x
5758
.. _#399: https://github.com/lincolnloop/python-qrcode/pull/399
5859
.. _#400: https://github.com/lincolnloop/python-qrcode/pull/400
5960
.. _#408: https://github.com/lincolnloop/python-qrcode/pull/408
61+
.. _#411: https://github.com/lincolnloop/python-qrcode/pull/411
6062
.. _#412: https://github.com/lincolnloop/python-qrcode/pull/412
6163
.. _#420: https://github.com/lincolnloop/python-qrcode/pull/420
6264

qrcode/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ def __init__(
8181
):
8282
_check_box_size(box_size)
8383
_check_border(border)
84+
self.clear()
8485
self.version = version
8586
self.error_correction = int(error_correction)
8687
self.box_size = int(box_size)
@@ -91,7 +92,6 @@ def __init__(
9192
self.image_factory = image_factory
9293
if image_factory is not None:
9394
assert issubclass(image_factory, BaseImage)
94-
self.clear()
9595

9696
@property
9797
def version(self) -> int:
@@ -123,6 +123,7 @@ def clear(self):
123123
self.modules_count = 0
124124
self.data_cache = None
125125
self.data_list = []
126+
self._version = None
126127

127128
def add_data(self, data, optimize=20):
128129
"""
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
5+
import pytest
6+
7+
import qrcode
8+
from qrcode.constants import PIL_AVAILABLE
9+
10+
if TYPE_CHECKING:
11+
from pathlib import Path
12+
13+
14+
@pytest.mark.skipif(not PIL_AVAILABLE, reason="PIL is not installed")
15+
def test_qrcode_clear_resets_size(tmp_path: Path):
16+
"""
17+
Test that QRCode.clear() properly resets the QRCode object.
18+
19+
Regression test for:
20+
21+
QRCode class not resizing down between runs
22+
https://github.com/lincolnloop/python-qrcode/issues/392
23+
"""
24+
test1_path = tmp_path / "test1.png"
25+
test2_path = tmp_path / "test2.png"
26+
test3_path = tmp_path / "test3.png"
27+
28+
# Create a QR code instance
29+
qr = qrcode.QRCode(version=None)
30+
31+
# Generate first QR code
32+
qr.add_data("https://example.com/")
33+
qr.make(fit=True)
34+
img1 = qr.make_image()
35+
img1.save(test1_path)
36+
37+
# Clear and generate second QR code with different data
38+
qr.clear()
39+
qr.add_data("https://example.net/some/other/path")
40+
qr.make(fit=True)
41+
img2 = qr.make_image()
42+
img2.save(test2_path)
43+
44+
# Clear and generate third QR code with same data as first
45+
qr.clear()
46+
qr.add_data("https://example.com/")
47+
qr.make(fit=True)
48+
img3 = qr.make_image()
49+
img3.save(test3_path)
50+
51+
# Check that the first and third QR codes are identical.
52+
with test1_path.open("rb") as file1, test3_path.open("rb") as file3:
53+
assert file1.read() == file3.read(), (
54+
"First and third QR codes should be identical after clearing"
55+
)

0 commit comments

Comments
 (0)