[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=
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
    myfile =3D 1
    f =3D open(str(filename), 'wb')

another pattern I have used for getting around supporting non-unicode pyt=
is something like:

    from types import StringType, UnicodeType
    StringTypes =3D (StringType, UnicodeType)
except ImportError:
    from types import StringType
    StringTypes =3D (StringType,)

if type(s) in StringTypes:


On Tuesday 05 November 2002 11:55 am, Andy Robinson wrote:
> > exceptions.AttributeError
> > 'unicode' object has no attribute 'write'
> > <traceback object at 0x0328A4D0>
> 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
> The relevant code is in pdfdoc, class PDFDocument, method saveToFile
> and does this!
>  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!
> So if you pass a Unicode string it assumes it's something
> writable.
> Solution:  either do=20
>    filename =3D str(filename)
> in your code before setting up the canvas, or explicitly pass
> in an open file object.
> HTH,
> Andy Robinson