4

I am facing some problem with strings switching from python 2.x to python 3

Issue 1:

from ctypes import*
charBuffer=create_string_buffer(1000)
var = charBuffer.value  # var contains like this "abc:def:ghi:1234"
a,b,c,d= var.split(':')

It works fine in python 2.x but not in 3.x it is throwing some errors like this a,b,c,d= var.split(':') TypeError: 'str' does not support the buffer interface

I got the links after doing some research in stackoverflow link link2

If I print, desired output would be

a= abc
b =def
c=ghi 
d=1234

Issue2:

from ctypes import*
cdll = "Windll"
var = 0x1fffffffffffffffffffffff # I want to send this long variable to character pointer which is in cdll

charBuf =create_string_buffer(var.to_bytes(32,'little'))
cdll.createBuff (charBuf )

cdll function
int createBuff (char * charBuff){
   print charBuff
   return 0;
}

I want to send this long variable to character pointer which is in cdll, since its a character pointer its throwing errors. Need your valuable inputs on how could I achieve this. Thanks in advance

Community
  • 1
  • 1
  • @ Vineet Kumar Doshi, added the desired output in my problem statement –  Aug 18 '15 at 13:01

2 Answers2

2

In Python 3.x , '.value' on return of create_string_buffer() returns a byte string .

In your example you are trying to split the byte string using a Unicode string (which is the normal string in Python 3.x ) . This is what is causing your issue.

You would need to either split with byte string . Example -

a,b,c,d = var.split(b':')

Or you can decode the byte string to a Unicode string using '.decode()' method on it .

Example -

var = var.decode('<encoding>')
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • I would discourage the second example. Splitting a unicode string with a byte string is asymmetric. That is, it works on py2, but not py3. eg. `u"a b".split(b" ")`. – Dunes Aug 18 '15 at 13:08
  • Thanks a lot, worked!! I have added the clear goal for problem statement, could you please check. –  Aug 18 '15 at 13:26
  • True , but I was just giving that option of var or other variables were needed as normal string (and not byte string) afterwards. If it is only required for sending to c function , then the first is the recommended approach – Anand S Kumar Aug 18 '15 at 13:26
  • @ Anand S Kumar, I am facing some issue in sending 128 nit Long integer to character pointer in cdll, completely stuck with this.could you please help? –  Aug 18 '15 at 14:41
0

Split using b":" and you will be fine in both versions of python.

In py2 str is a bytestring, in py3 str is a unicode object. The object returned by the ctypes string buffer is a bytestring (str on py2 and bytes on py3). By writing the string literal as b"... you force it to be a bytestring in both version of python.

Dunes
  • 37,291
  • 7
  • 81
  • 97
  • That's great!! it worked, I have added the clear goal for problem statement, could you please check. –  Aug 18 '15 at 13:25
  • @ Dunes in issue2 facing some problem in sending 128 nit Long integer to character pointer in cdll, how can I compatible with python, could you please help? –  Aug 18 '15 at 14:50