[python-win32] UnicodeEncodeError getting Query String Data from ActiveX Scripting engine in classic asp

Chris Lambacher chris at kateandchris.net
Fri Jul 1 04:57:43 CEST 2011

Hi again,

Decided to try something that I was thinking about from reading the
code for __str__ in win32com\client\dispatch.py If I bypass the
__str__ method by using unicode against __call__() I get some success.

<script language="python" runat="server">
# Next line works

#next line doesn't

Maybe there should be a __unicode__ method on the class? Maybe __str__
should set an explicit encoding?

Something like:

	def __unicode__(self):
			return unicode(self.__call__())
		except pythoncom.com_error, details:
			if details.hresult not in ERRORS_BAD_CONTEXT:
			return self.__repr__()

	def __str__(self):
		return unicode(self).encode('utf-8')

__unicode__ was introduced in python 2.2 and therefore is supported by
every 2.x version of python that a build is provided for on
sourceforge download page.


On Thu, Jun 30, 2011 at 10:40 PM, Chris Lambacher
<chris at kateandchris.net> wrote:
> Hi,
> I am embedding python code into an existing legacy classic asp
> application. I need to access utf-8 encoded unicode query string data
> from python.
> I have something along the lines of (simplified):
> <script language="python" runat="server">
> # have set in IIS7 ASP configuration for default session code page to
> be CodePage = 65001
> a = unicode(Request('ErrMsg'))
> </script>
> in test.asp and I use a get request to:
> http://localhost/test.asp?ErrMsg=La+connexion+%C3%A0+la+base+de+donn%C3%A9es+a+%C3%A9chou%C3%A9
> If I run the the raw query parameter through urllib.unquote_plus, I
> get a correctly utf-8 encoded string. If I attempt to get a python
> unicode object I get:
> Python ActiveX Scripting Engine error '80020009'
> Traceback (most recent call last): File "<Script Block >", line 3, in
> <module> a = unicode(Request('ErrMsg')) File
> "C:\Python26\lib\site-packages\win32com\client\dynamic.py", line 201,
> in __str__ return str(self.__call__()) UnicodeEncodeError: 'ascii'
> codec can't encode character u'\xe0' in position 13: ordinal not in
> range(128)
> /test_python.asp, line 5
> a = unicode(Request('ErrMsg'))
> I think this is because there is no direct method that win32com
> provides to get to a unicode object so the unicode built in first
> attempts to convert it to a string which results in the com Unicode
> string being encoded in ascii and failing on à (\xe0 or %C3%A0 in
> utf-8).
> The 'Quick Start to Server side COM and Python' document in the
> PyWin32 Help file says:
> """
> At this stage, Python and Unicode don’t really work well together. All
> strings which come from COM will actually be Unicode objects rather
> than string objects.
> To make this code work in a COM environment, the last line of the
> "Hello" method must become:
> return "Hello" + " " * self.softspace + str(who)
> Note the conversion of the "who" to "str(who)". This forces the
> Unicode object into a native Python string object.
> """
> Which is totally unhelpful in the case where you have a real Unicode
> string and want to get the unicode characters out of it. Is there a
> way to do this? How do I get a unicode string?
> Thanks in advance for any help or guidance,
> Chris
> --
> Christopher Lambacher
> chris at kateandchris.net

Christopher Lambacher
chris at kateandchris.net

More information about the python-win32 mailing list