Skip to content

Commit f0ec709

Browse files
committed
Fix QRCode.clear() to properly reset the QRCode object. Fixes #392
1 parent ca92eed commit f0ec709

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

.claude/settings.local.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"permissions": {
3+
"allow": [
4+
"Bash(python -m pytest qrcode/tests/test_qrcode.py::test_qrcode_factory -xvs)"
5+
],
6+
"deny": []
7+
}
8+
}

qrcode/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
"""

qrcode/tests/regression/__init__.py

Whitespace-only changes.
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 qrcode
6+
7+
if TYPE_CHECKING:
8+
from pathlib import Path
9+
10+
11+
def test_qrcode_clear_resets_size(tmp_path: Path):
12+
"""
13+
Test that QRCode.clear() properly resets the QRCode object.
14+
15+
Regression test for:
16+
17+
QRCode class not resizing down between runs
18+
https://github.com/lincolnloop/python-qrcode/issues/392
19+
"""
20+
test1_path = tmp_path / "test1.png"
21+
test2_path = tmp_path / "test2.png"
22+
test3_path = tmp_path / "test3.png"
23+
24+
# Create a QR code instance
25+
qr = qrcode.QRCode(version=None)
26+
27+
# Generate first QR code
28+
qr.add_data("https://example.com/")
29+
qr.make(fit=True)
30+
img1 = qr.make_image()
31+
img1.save(test1_path)
32+
33+
# Clear and generate second QR code with different data
34+
qr.clear()
35+
qr.add_data("https://example.net/some/other/path")
36+
qr.make(fit=True)
37+
img2 = qr.make_image()
38+
img2.save(test2_path)
39+
40+
# Clear and generate third QR code with same data as first
41+
qr.clear()
42+
qr.add_data("https://example.com/")
43+
qr.make(fit=True)
44+
img3 = qr.make_image()
45+
img3.save(test3_path)
46+
47+
# Compare the images. Image 1 and 3 must be binary identical.
48+
with test1_path.open("rb") as file1, test3_path.open("rb") as file3:
49+
file1_data = file1.read()
50+
file3_data = file3.read()
51+
52+
# Check that the first and third QR codes are identical
53+
assert file1_data == file3_data, (
54+
"First and third QR codes should be identical after clearing"
55+
)

0 commit comments

Comments
 (0)