Skip to content

encoding: improve UTF-8 decode strategy#1469

Open
ralian wants to merge 1 commit into
libgit2:masterfrom
ralian:dev/ralian/path-encoding
Open

encoding: improve UTF-8 decode strategy#1469
ralian wants to merge 1 commit into
libgit2:masterfrom
ralian:dev/ralian/path-encoding

Conversation

@ralian

@ralian ralian commented Jun 30, 2026

Copy link
Copy Markdown

Work related to #1451

@ralian

ralian commented Jun 30, 2026

Copy link
Copy Markdown
Author

I did realize I ran ruff with my settings from another project. I'll have to amend this when I get a chance, but let me know if this change looks ok otherwise.

@jdavid

jdavid commented Jul 1, 2026

Copy link
Copy Markdown
Member

Thanks @ralian

PEP-383 says:

File names, environment variables, and command line arguments are defined as being character data in POSIX...

In pygit2 we don't have vars or args I think, so we only care about paths.

maybe_string/to_bytes are general helpers used not only for filesystem paths. I would rather add 2 new helpers: for example decode_fs_path/encode_fs_path; then the work will be to go through the code and use the correct helper (if in doubt keep the old helper call and flag it for discussion).

Also, there is a reference leak in Repository_status :

-        err = PyDict_SetItemString(dict, path, status);
+        PyObject *py_path = PyUnicode_DecodeFSDefault(path);
+        if (py_path == NULL)
+            goto error;
+
+        err = PyDict_SetItem(dict, py_path, status);
+        Py_DECREF(py_path);
         Py_CLEAR(status);

If py_path is NULL then status is not cleared.

@ralian

ralian commented Jul 1, 2026

Copy link
Copy Markdown
Author

Hi David,

Thanks for the feedback. The new helper functions are a good idea, this weekend I will take a crack at that and fix the half broken test too (I have it fixed locally.)

For the reference leak good point. I don't like using goto for this exact reason, but I was trying to match the style of the rest of the codebase. I will think about a better way to handle this, but worst case I can just clear the status in that branch before the goto.

Will

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants