py_yyjson icon indicating copy to clipboard operation
py_yyjson copied to clipboard

Unexpected Path behaviour when calling Document

Open lightiverson opened this issue 6 months ago • 2 comments

While using the py_yysjon module I came across something unexpected. Here is a screenshot of the few lines of code involved: Image

And here is a screenshot of the terminal output for these prints: Image

Notice the difference at the start of the paths. / and .

Somehow calling Document() is changing Path.

lightiverson avatar Oct 27 '25 13:10 lightiverson

Well, that's a funny one.

  1. When we get a path object, we convert it to a string with as_str = PyObject_Str(content).
  2. str = PyUnicode_AsUTF8AndSize(as_str, &str_len) to get a char* to its buffer
  3. Do document things
  4. 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.

TkTech avatar Oct 30 '25 09:10 TkTech

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?

lightiverson avatar Nov 03 '25 17:11 lightiverson