Python Forum
Python C API - Issue with string as arugments
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python C API - Issue with string as arugments
#1
Greetings,

Using Python 3.4.3
  • I am creating a PyUnicode string from a C string.
  • I am passing this string as a argument to a Python function from C.
  • In the setter function I can print out parameter.
  • When I assign the parameter to an array then print out the array the string value is corrupted.
  • I've been referencing PEP 393
  • Performing the same activity in purely Python prints out the string argument and array fine.

I have tried using these C functions to create the PyUnicode string and they all behave the same way.
Output:
// C code to create a PyUnicode string and set it into a tuple void SetParameter(PyObject * pTuple, const char * psValue, unsigned int uPosition) { // PyObject * obj = PyUnicode_FromStringAndSize(psValue, strlen(psValue)); // PyObject * obj = _PyUnicode_FromASCII(psValue, strlen(psValue)); // PyObject * obj = PyUnicode_FromFormat("%s", psValue); PyObject * obj = PyUnicode_FromKindAndData(PyUnicode_1BYTE_KIND, psValue, strlen(psValue)); PyTuple_SetItem(pTuple, uPosition, obj); }
# Python 3 function receiving the tuple with a strings
def configure(self, ip1, ip2, ip3):
    print ("configure[ip list]", ip1, ip2, ip3)
    self.string_args.append('--ip')
    self.string_args.append(ip1)
    self.string_args.append('--ip')
    self.string_args.append(ip2)
    self.string_args.append('--ip')
    self.string_args.append(ip3)
    print ("configure[self.string_args]", self.string_args)
Output:
# Printed output when configure() is executed from C configure[ip list] 127.0.0.1 127.0.0.1 127.0.0.1 configure [self.string_args] ['--ip', ''27.0.0.1', '--ip', "\"\\\"\\\\\\, '--ip', '"\\"\\\\\\"\\\\\\\\\\\\', '--ip', '"\\"\\\\\\"\\\\\\\\\\\\']
Output:
# Printed output when configure() is executed from Python $ ./Test.py configure [ip list] 127.0.0.1 127.0.0.2 127.0.0.3 configure [self.string_args] ['--ip', '127.0.0.1', '--ip', '127.0.0.2', '--ip', '127.0.0.3']
Due to the difference in behavior I conclude that it is something about the way the C code creates the PyUnicode objects. Perhaps I need to create some other object type?

I have the source for the C Python engine but I haven't figure it out yet.
Reply


Messages In This Thread
Python C API - Issue with string as arugments - by JRHeisey - Nov-27-2019, 08:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Last caracter of a string truncated issue when working from the end of the string Teknohead23 3 1,668 Oct-03-2021, 01:08 PM
Last Post: snippsat
  connection string issue racone 2 3,831 Feb-03-2020, 02:22 AM
Last Post: racone
  List/String seperation issue YoungGrassHopper 13 5,754 Sep-20-2019, 11:57 AM
Last Post: perfringo
  Mixed string,Integer input variable issue maderdash 2 2,821 Nov-06-2018, 09:46 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020