[reportlab-users] Canvas does not work with SpooledTemporaryFile
rschroll at gmail.com
Sun Mar 14 20:54:05 EDT 2021
I understand this is the correct place to report bugs in ReportLab? If not
please point me to the right location.
I've found that Canvas's save function doesn't work if the canvas was
created with a tempfile.SpooledTemporaryFile. Specifically, it raises
> AttributeError: 'NoneType' object has no attribute 'decode'
I've attached a test case at the end of this email. It fails on several
recent versions of ReportLab, including 3.5.65, which seems to be the
latest available through PyPI. Note that the example works just fine with
a standard TemporaryFile.
The problem seems to be around line 212 of pdfdoc.py. It gets the name
property of the file object, defaulting to empty string if it does not
exist. If the name is an integer (as it is for a TemporaryFile), a special
string is formatted with that integer. Otherwise, it passes the name
property to `makeFileName`, which is expecting a string argument. The
implicit assumption is that the name property of a file object is a string,
an int, or nonexistent.
However, a SpooledTemporaryFile has a name property of `None` (at least
until it rolls over to disk), violating this assumption. I see two
relatively easy fixes:
1. Default to empty string for any falsey name attribute:
filename = getattr(f, 'name', None) or ''
This would catch this problem, but it would still be open to any file
object that defines a name property of another type.
2. Explicitly cast the name to a string:
filename = makeFileName(str(filename))
This would avoid all such problems, at the risk of producing some
strange-looking filenames. But given that the existing code already
produces such strange-looking filenames for ints, I suspect this is okay.
Hope this makes sense. Let me know if you have any questions,
from reportlab.pdfgen import canvas
f = tempfile.SpooledTemporaryFile()
c = canvas.Canvas(f)
c.save() # Raises AttributeError
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the reportlab-users