Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3.5, ctypes: TypeError: bytes or integer address expected instead of str instance

Tags:

python

ctypes

I'm having an issue with ctypes. I think my type conversion is correct and the error isn't making sense to me. Error on line " arg - ct.c_char_p(logfilepath) " TypeError: bytes or integer address expected instead of str instance

I tried in both python 3.5 and 3.4.

function i'm calling:

stream_initialize('stream_log.txt')

Stream_initialize code"

def stream_initialize(logfilepath):
    f = shim.stream_initialize
    arg = ct.c_char_p(logfilepath)
    result = f(arg)

    if result:
        print(find_shim_error(result))
like image 992
toshbar Avatar asked Jun 17 '16 18:06

toshbar


1 Answers

c_char_p takes bytes object so you have to convert your string to bytes first:

ct.c_char_p(logfilepath.encode('utf-8'))

Another solution is using the c_wchar_p type which takes a string.

like image 176
user1238367 Avatar answered Sep 23 '22 15:09

user1238367