[issue17991] ctypes.c_char gives a misleading error when passed a one-character unicode string

Shriramana Sharma report at bugs.python.org
Sun Jun 2 16:10:55 CEST 2013


Shriramana Sharma added the comment:

I came upon this too. In Python 2 it used to expect a one character string. Apparently the same error message has been carried forward to Python 3 too, though now the actual expected input is either a one character bytes type and not a str type, or an int corresponding to the ord() value of that char.

Minimal demonstration:

$ python
Python 2.7.4 (default, Apr 19 2013, 18:28:01) 
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import *
>>> class test ( Structure ) :
...     _fields_ = [ ( "ch", c_char ) ]
... 
>>> a = test()
>>> a.ch = ord('a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: one character string expected
>>> a.ch = 'c'
>>> a.ch
'c'
>>> 

$ python3
Python 3.3.1 (default, Apr 17 2013, 22:30:32) 
[GCC 4.7.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import *
>>> class test ( Structure ) :
...     _fields_ = [ ( "ch", c_char ) ]
... 
>>> a = test()
>>> a.ch = 'c'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: one character string expected
>>> a.ch = b'c'
>>> a.ch
b'c'
>>> a.ch = ord('c')
>>> a.ch
b'c'
>>>

----------
nosy: +jamadagni

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue17991>
_______________________________________


More information about the Python-bugs-list mailing list