0

I try send a string from c++ to python string using:

PyObject* pyString = PyUnicode_FromString("/abc/def.html/a%22.php?abc=&def=%22;%00s%01");
....
PyObject* pyArgs = Py_BuildValue("(z)", pyString);
...
PyObject_CallObject(pFunc, pyArgs);

But in script the string is bad:

function(data):
    print(data)

The result is:

/abc/def.html/a              bogus %pp?abc=&def=                    %;(null)%

What happened?, If I try to escape % chars with %% works fine, but PyUnicode_FromString is not a printf format.

Is this a bug of PyUnicode_FromString function?, Do I need a native python escape? Or do I need to manually escape?

DavidW
  • 29,336
  • 6
  • 55
  • 86
e-info128
  • 3,727
  • 10
  • 40
  • 57

1 Answers1

0
PyObject* pyArgs = Py_BuildValue("(z)", pyString);

This line is wrong. 'z' in a Py_BuildValue tells it that the argument you're passing is a const char* and Python is to convert this into a Python string. However, the argument you're passing is already a Python string. Therefore it tries to reinterpret a PyObject* as a const char* and so generates rubbish.

The correct solution is

PyObject* pyArgs = Py_BuildValue("(O)", pyString);

which simply interprets pyString as a Python object (which it is!), or

PyObject* pyArgs = Py_BuildValue("(z)", "/abc/def.html/a%22.php?abc=&def=%22;%00s%01");

to skip creating pyString yourself.

DavidW
  • 29,336
  • 6
  • 55
  • 86