简体   繁体   中英

Where is the stream being closing? using PyQRCode to create a png QR with io stream

I'm using PyQRCode [0] to generate a QR in PNG format under Python 2.7.6.

When I try to write a file on disk using all works ok:

import pyqrcode
from io import BytesIO
qr = pyqrcode.create("my qr string", mode='binary', version=7)
qr.png("myqr.png", scale=4)

But when I try to use a io stream using this code:

import pyqrcode
from io import BytesIO
qr = pyqrcode.create("my qr string", mode='binary', version=7)
f=BytesIO()
qr.png(f, scale=4)
f.getvalue()

I get on the f.getvalue() line:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
ValueError: I/O operation on closed file.

The PyQRCode documentation says that the file parameter could be a writeable stream.

I tried hard to debug this but I can't find where the stream is being closed and there is not any .close() statement for the stream. [1]

[0] https://pypi.python.org/pypi/PyQRCode

[1] http://pythonhosted.org//PyQRCode/_modules/pyqrcode.html#QRCode.png

It's a bug in PyQRCode, there is a PR (not merged yet) in the Github repo that resolve this:

https://github.com/mnooner256/pyqrcode/pull/6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM