fsmonitor: reapply b1f62cd39b5c stable
authorGregory Szorc <gregory.szorc@gmail.com>
Sat, 02 Nov 2019 12:54:47 -0700
branchstable
changeset 43388 51316de92ad0
parent 43387 9eed959cd8ae
child 43389 e6ce1599af74
fsmonitor: reapply b1f62cd39b5c The recent revendoring of pywatchman undid this changeset. Let's reapply it. This commit was generated by running `hg graft -f b1f62cd39b5c`. It applied cleanly. Differential Revision: https://phab.mercurial-scm.org/D7204
hgext/fsmonitor/pywatchman/bser.c
--- a/hgext/fsmonitor/pywatchman/bser.c	Sat Nov 02 12:52:58 2019 -0700
+++ b/hgext/fsmonitor/pywatchman/bser.c	Sat Nov 02 12:54:47 2019 -0700
@@ -128,27 +128,38 @@
   Py_ssize_t i, n;
   PyObject* name_bytes = NULL;
   PyObject* ret = NULL;
-  const char* namestr;
+  const char* namestr = NULL;
 
   if (PyIndex_Check(name)) {
     i = PyNumber_AsSsize_t(name, PyExc_IndexError);
     if (i == -1 && PyErr_Occurred()) {
       goto bail;
     }
-    ret = PySequence_GetItem(obj->values, i);
-    goto bail;
-  }
 
-  // We can be passed in Unicode objects here -- we don't support anything other
-  // than UTF-8 for keys.
-  if (PyUnicode_Check(name)) {
-    name_bytes = PyUnicode_AsUTF8String(name);
-    if (name_bytes == NULL) {
+    if (i == 8 && PySequence_Size(obj->values) < 9) {
+      // Hack alert: Python 3 removed support for os.stat().st_mtime
+      // being an integer.Instead, if you need an integer, you have to
+      // use os.stat()[stat.ST_MTIME] instead. stat.ST_MTIME is 8, and
+      // our stat tuples are shorter than that, so we can detect
+      // requests for index 8 on tuples shorter than that and return
+      // st_mtime instead.
+      namestr = "st_mtime";
+    } else {
+      ret = PySequence_GetItem(obj->values, i);
       goto bail;
     }
-    namestr = PyBytes_AsString(name_bytes);
   } else {
-    namestr = PyBytes_AsString(name);
+    // We can be passed in Unicode objects here -- we don't support anything other
+    // than UTF-8 for keys.
+    if (PyUnicode_Check(name)) {
+      name_bytes = PyUnicode_AsUTF8String(name);
+      if (name_bytes == NULL) {
+        goto bail;
+      }
+      namestr = PyBytes_AsString(name_bytes);
+    } else {
+      namestr = PyBytes_AsString(name);
+    }
   }
 
   if (namestr == NULL) {