This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: c_char_p.value does not return a bytes object in Windows.
Type: behavior Stage:
Components: ctypes, Windows Versions: Python 3.1
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: theller Nosy List: DavidCzech, brian.curtin, ocean-city, theller
Priority: normal Keywords:

Created on 2010-02-25 05:11 by DavidCzech, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test_c_bug.py DavidCzech, 2010-02-25 05:11 Python Script demonstrating bug.
Messages (3)
msg100084 - (view) Author: DavidCzech (DavidCzech) Date: 2010-02-25 05:11
c_char_p.value doesn't return a bytes object on Windows.
http://docs.python.org/3.1/library/ctypes.html#fundamental-data-types states that c_char_p is either a "bytes object or None" in Python, not str.
----------
test_c_bug.py
----------
import ctypes

test_string = ctypes.c_char_p("This Is a test string, that should be of type bytes")

print (test_string.value)
print ("Typeof test_string {}",type(test_string))
print ("Typeof test_string {}",type(test_string.value))

assert(type(test_string.value) == bytes)

-----------------
Windows Xp 5.1 SP3
Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit (Intel)] on win32
-----------------
C:\>python test_c_bug.py
This Is a test string, that should be of type bytes
Typeof test_string {} <class 'ctypes.c_char_p'>
Typeof test_string {} <class 'str'>
Traceback (most recent call last):
  File "test_c_bug.py", line 9, in <module>
    assert(type(test_string.value) == bytes)
AssertionError

-----------------
Ubuntu 9.10 Karmic 
Python 3.1.1+ (r311:74480, Nov  2 2009, 14:49:22) 
[GCC 4.4.1] on linux2
-----------------
david@Waldorf:~/dev/gtype/gtypes$ python3 test_c_bug.py
b'This Is a test string, that should be of type bytes'
Typeof test_string {} <class 'ctypes.c_char_p'>
Typeof test_string {} <class 'bytes'>
msg100085 - (view) Author: Brian Curtin (brian.curtin) * (Python committer) Date: 2010-02-25 05:22
FWIW, this isn't an issue on py3k (on Mac or Windows).
msg117926 - (view) Author: Hirokazu Yamamoto (ocean-city) * (Python committer) Date: 2010-10-03 19:31
You need to change

test_string = ctypes.c_char_p("This Is a test string, that should be of type bytes")

to

test_string = ctypes.c_char_p(b"This Is a test string, that should be of type bytes")

but this issue itself seems to be already fixed.
History
Date User Action Args
2022-04-11 14:56:58adminsetgithub: 52265
2010-10-03 19:31:25ocean-citysetstatus: open -> closed

nosy: + ocean-city
messages: + msg117926

resolution: fixed
stage: needs patch ->
2010-02-25 05:22:39brian.curtinsetnosy: + brian.curtin
messages: + msg100085
2010-02-25 05:21:20brian.curtinsetpriority: normal
nosy: theller, DavidCzech
components: + Windows
stage: needs patch
2010-02-25 05:11:39DavidCzechcreate