Python C API - Issue with string as arugments - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Python C API - Issue with string as arugments (/thread-22808.html) |
Python C API - Issue with string as arugments - JRHeisey - Nov-27-2019 Greetings, Using Python 3.4.3
I have tried using these C functions to create the PyUnicode string and they all behave the same way.
# 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)
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. RE: Python C API - Issue with string as arugments - JRHeisey - Nov-27-2019 I have discovered a less efficient workaround. I would prefer to not deallocate the tuple each time.
RE: Python C API - Issue with string as arugments - casevh - Nov-30-2019 Since you haven't provided enough source code, I can only make some general comments. How do you create pTuple? You aren't checking the return value of of PyTuple_SetItem() or Py_BuildValue() or any of the PUnicode_() functions. You really should get in the habit of checking the return value. I don't understand why you don't like the Py_BuildValue() option. It is simpler code. And you should be deleting pTuple after your call back to Python. If you don't delete the tuple (either pTuple or arguments), how are you decrementing the reference counts for objects contained in the tuple? casevh |