[reportlab-users] Puzzling Error
Casey Duncan
reportlab-users@reportlab.com
Tue, 5 Nov 2002 12:59:19 -0500
Hmm, wouldn't it be better to just test for the needed file methods and i=
f=20
they aren't there assume it is a file name?
Something like:
if hasattr(filename, 'write'):
myfile =3D 0
f =3D filename # A file-like ob
else:
myfile =3D 1
f =3D open(str(filename), 'wb')
another pattern I have used for getting around supporting non-unicode pyt=
hons=20
is something like:
try:
from types import StringType, UnicodeType
StringTypes =3D (StringType, UnicodeType)
except ImportError:
from types import StringType
StringTypes =3D (StringType,)
if type(s) in StringTypes:
...
-Casey
On Tuesday 05 November 2002 11:55 am, Andy Robinson wrote:
> > exceptions.AttributeError
> > 'unicode' object has no attribute 'write'
> > <traceback object at 0x0328A4D0>
>=20
> My guess is you are passing a unicode string as a filename
> and not an 8-bit python string. Currently we cannot easily test
> for unicode as we have to run on pre-Unicode versions of
> Python. =20
>=20
> The relevant code is in pdfdoc, class PDFDocument, method saveToFile
> and does this!
>=20
> if type(filename) is StringType:
> myfile =3D 1
> f =3D open(filename, "wb")
> else:
> myfile =3D 0
> f =3D filename # IT BETTER BE A FILE-LIKE OBJECT!
>=20
> So if you pass a Unicode string it assumes it's something
> writable.
>=20
> Solution: either do=20
> filename =3D str(filename)
> in your code before setting up the canvas, or explicitly pass
> in an open file object.
>=20
> HTH,
>=20
> Andy Robinson