Python, ASP, ADO, and Image string/binary problems

Alex Martelli aleaxit at yahoo.com
Wed Mar 7 03:14:23 EST 2001


"Shonn Gilson" <shonn at shonn.com> wrote in message
news:tabjei1p4a9481 at corp.supernews.com...
    [snip]
> that '\377' is octal not decimal.). The problem seems to be that when
Python
> passes strings through COM that they get translated to Unicode. This
happens

Yes, this IS what COM (actually, Automation) specifies -- *all* strings
are Unicode (note that Java specifies exactly the same thing, but COM
was there first:-).

Bravo for diagnosing that this was the problem in your case, though!


> for things like Response.BinaryWrite along with things like recordsets
> (objRS = Server.CreateObject("ADODB.Recordset")).  The result is that the
> beginning of my jpg file looks like
> '\377\000\330\000\377\000\340\000\000\000\020\000J\000F\000I\000F' instead
> of '\377\330\377\340\000\020JFIF'.
>
> Does anyone know how to handle this?

If you don't want to use *STRINGS* (normally-readable sequences of
characters), then don't.  Python offers other datatypes, which will
help you map (for example) *sequences of BYTES* (a byte is not the
same thing as a character -- even though decades of programming with
single-byte-characters strings have obscured this for many:).


Specifically, I think that what you want is the built-in *BUFFER*
type, which Python/COM will map into (in low-level COM terms) a
SAFEARRAY of VT_UI8 (a sequence-of-bytes, uninterpreted).


Try just changing this line:

> > Response.BinaryWrite(bstrOut.getvalue())

to:

> > Response.BinaryWrite(buffer(bstrOut.getvalue()))

and see if that solves it...


Alex






More information about the Python-list mailing list