Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions qrcode/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ def clear(self):
self.modules_count = 0
self.data_cache = None
self.data_list = []
self._version = None
Comment thread
bartTC marked this conversation as resolved.

def add_data(self, data, optimize=20):
"""
Expand Down
Empty file.
55 changes: 55 additions & 0 deletions qrcode/tests/regression/test_pil_clear.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import qrcode

if TYPE_CHECKING:
from pathlib import Path


def test_qrcode_clear_resets_size(tmp_path: Path):
"""
Test that QRCode.clear() properly resets the QRCode object.

Regression test for:

QRCode class not resizing down between runs
https://github.com/lincolnloop/python-qrcode/issues/392
"""
test1_path = tmp_path / "test1.png"
test2_path = tmp_path / "test2.png"
test3_path = tmp_path / "test3.png"

# Create a QR code instance
qr = qrcode.QRCode(version=None)

# Generate first QR code
qr.add_data("https://example.com/")
qr.make(fit=True)
img1 = qr.make_image()
img1.save(test1_path)

# Clear and generate second QR code with different data
qr.clear()
qr.add_data("https://example.net/some/other/path")
qr.make(fit=True)
img2 = qr.make_image()
img2.save(test2_path)

# Clear and generate third QR code with same data as first
qr.clear()
qr.add_data("https://example.com/")
qr.make(fit=True)
img3 = qr.make_image()
img3.save(test3_path)

# Compare the images. Image 1 and 3 must be binary identical.
with test1_path.open("rb") as file1, test3_path.open("rb") as file3:
file1_data = file1.read()
file3_data = file3.read()

# Check that the first and third QR codes are identical
assert file1_data == file3_data, (
"First and third QR codes should be identical after clearing"
)
Loading