Patchwork D7210: fsmonitor: handle unicode keys in tuples

login
register
mail settings
Submitter phabricator
Date Nov. 2, 2019, 10:15 p.m.
Message ID <d8898b53855438ea90d9eef4b0e1af7a@localhost.localdomain>
Download mbox | patch
Permalink /patch/42699/
State Not Applicable
Headers show

Comments

phabricator - Nov. 2, 2019, 10:15 p.m.
indygreg edited the summary of this revision.
indygreg updated this revision to Diff 17513.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D7210?vs=17508&id=17513

BRANCH
  stable

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7210/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D7210

AFFECTED FILES
  hgext/fsmonitor/pywatchman/bser.c

CHANGE DETAILS




To: indygreg, #hg-reviewers
Cc: mercurial-devel

Patch

diff --git a/hgext/fsmonitor/pywatchman/bser.c b/hgext/fsmonitor/pywatchman/bser.c
--- a/hgext/fsmonitor/pywatchman/bser.c
+++ b/hgext/fsmonitor/pywatchman/bser.c
@@ -175,7 +175,22 @@ 
     const char* item_name = NULL;
     PyObject* key = PyTuple_GET_ITEM(obj->keys, i);
 
-    item_name = PyBytes_AsString(key);
+    if (PyUnicode_Check(key)) {
+#if PY_MAJOR_VERSION >= 3
+      item_name = PyUnicode_AsUTF8(key);
+#else
+      PyObject* utf = PyUnicode_AsEncodedString(sval, "utf-8", "ignore");
+      if (utf == NULL) {
+        goto bail;
+      }
+      item_name = PyBytes_AsString(utf);
+#endif
+    } else {
+      item_name = PyBytes_AsString(key);
+    }
+    if (item_name == NULL) {
+      goto bail;
+    }
     if (!strcmp(item_name, namestr)) {
       ret = PySequence_GetItem(obj->values, i);
       goto bail;