PythonCOM and Unicode

tim at scheer.com tim at scheer.com
Mon Oct 2 15:06:55 EDT 2000


I am having a problem with passing a unicode string to the Python COM
extension.  I am testing with Python 2.0b2 and Pythonwin ver 134.

I pass in a unicode string that contains chinese characters from a
file.  When I call a sample COM object (which I created to simply
accept strings) the COM layer returns with an error.  Below is attached
a interactive session demonstrating the error.

The problem seems to me that the COM layer is attempting to convert the
unicode string even though it is already being passed as a unicode
string.

Python Session:

Python 2.0b2 (#6, Sep 26 2000, 14:59:21) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.6 -- press F1 for help
>>> import unicodedata
>>> import codecs
>>> import os
>>> os.chdir('c:\\testunicode')
>>> ufile = codecs.open('123.u8', 'rb', 'utf-8')
>>> ufile
<codecs.StreamReaderWriter instance at 00A38E4C>
>>> ustring = ufile.read()
>>> ustring
u'123 = \u4E00\u4E8C\u4E09\015\012'
>>> type(ustring)
<type 'unicode'>
>>> # test sending output in utf-8
>>> y = ustring.encode('utf-8')
>>> y
'123 = \344\270\200\344\272\214\344\270\211\015\012'
>>> type(y)
<type 'string'>
>>> # Test sending to COM object
>>> from win32com.client import Dispatch
>>> ctest = Dispatch('BeXcom.TestUnicode')
>>> ctest.read(ustring)
Traceback (innermost last):
  File "<pyshell#16>", line 1, in ?
    ctest.read(ustring)
  File "<COMObject BeXcom.TestUnicode>", line 2, in read
com_error: (-2147352567, 'Exception occurred.', (0, 'Python COM Server
Internal Error', 'Unexpected Python Error: exceptions.UnicodeError:
ASCII encoding error: ordinal not in range(128)', None, 0, -
2147467259), None)
>>> # It cannot read a direct unicode string (using extended characters)
>>> # test 2: unicode string with no extended characters
>>> ustring2 = u"This is a test"
>>> type(ustring2)
<type 'unicode'>
>>> ctest.read(ustring2)
>>> # worked ok!
>>> # test 3: send string in utf-8 format
>>> utf8 = ustring.encode('utf-8')
>>> utf8
'123 = \344\270\200\344\272\214\344\270\211\015\012'
>>> ctest.read(utf8)
Traceback (innermost last):
  File "<pyshell#26>", line 1, in ?
    ctest.read(utf8)
  File "<COMObject BeXcom.TestUnicode>", line 2, in read
com_error: (-2147352567, 'Exception occurred.', (0, 'Python COM Server
Internal Error', 'Unexpected Python Error: exceptions.UnicodeError:
ASCII encoding error: ordinal not in range(128)', None, 0, -
2147467259), None)
>>>


Tim


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list