Unexpected Path behaviour when calling Document
While using the py_yysjon module I came across something unexpected. Here is a screenshot of the few lines of code involved:
And here is a screenshot of the terminal output for these prints:
Notice the difference at the start of the paths. / and .
Somehow calling Document() is changing Path.
Well, that's a funny one.
- When we get a path object, we convert it to a string with
as_str = PyObject_Str(content). -
str = PyUnicode_AsUTF8AndSize(as_str, &str_len)to get a char* to its buffer - Do document things
-
Py_XDECREF(str)
Buuuut str is the char*, not the python string. So it decrements the first byte of the string itself, which in a PyObject would have been the reference count. And because ASCII for / is 47 and the ASCII for . is 46, it ends up still being a "valid" path.
Haha, this seems interesting.
I have written C and Python, but I have yet to convert one into the other as this library does. So forgive me if I'm asking something obvious.
But why should the reference count in a PyObject be decremented?