heuer/qrcode-artistic

apparent infinite loop with scale=float

abe-winter opened this issue · 3 comments

Hi! I think there's an infinite loop in to_artistic() when scale is a float. Example:

>>> qr.to_artistic(background='whatever.png', target='qr.png', scale=4.5)
^CTraceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../.direnv/python-3.10.4/lib/python3.10/site-packages/qrcode_artistic.py", line 134, in write_artistic
    while scale % 3:
KeyboardInterrupt

I'm using qrcode-artistic==2.1.0, segno==1.5.2

I think this is happening here:
https://github.com/heuer/qrcode-artistic/blob/b93c548/qrcode_artistic.py#L140-L141

as you can see, this loops forever with any non-integer

>>> x = 4.5
>>> for i in range(20):
...  print(x % 3)
...  x += 1
... 
1.5
2.5
0.5
1.5
2.5
0.5

is it reasonable to add a check before the loop which crashes if round(scale) != scale? I'm happy to submit this PR if that's useful to you.

thanks for this library, I love the results

heuer commented

Thanks for your report.

You're right, I didn't expect float numbers.

Segno itself deals with this problem (i.e. the PNG serializer) by silently converting the number to an integer int(scale). Probably round(scale) would also have been a good solution, maybe even the better one. I would suggest that the number is simply converted to an integer without throwing an exception. This should then be pointed out in the documentation.

'always round' sounds good to me

do you want me to send this PR?

heuer commented

I implemented a solution: Convert scale to int since it is the default behaviour of Segno.

I will publish a new release in the next few days. Thanks for finding the bug!