Commits

Brian Curtin committed 96f3f9e

Support 2 and 3 together.

Modified the initializer to work with strings, bytes, unicode, whatever.
Modified the callback to return version-relevant information, e.g.,
int instead of long when on 2.x, and an encoded string rather than Unicode
when on 2.x. 3.x behavior is unchanged.

Comments (0)

Files changed (1)

     watcher_object *self;
     PyGILState_STATE gil_state;
     PyObject *call_rslt, *py_file_name, *args;
+#ifndef PYTHON3
+    PyObject *py_file_name2;
+#endif
     Py_ssize_t idx, args_len;
     int pos;
 
                 file_name[notify_info->FileNameLength / sizeof(wchar_t) + 1] = '\0';
                 py_file_name = PyUnicode_FromWideChar(file_name,
                                                       wcslen(file_name));
+#ifndef PYTHON3
+                /* Encode what we get from above and get it back as a
+                   traditional string. */
+                py_file_name2 = PyUnicode_AsEncodedString(py_file_name,
+                                          Py_FileSystemDefaultEncoding, NULL);
+#endif
 
                 /* Take the args tuple given in the constructor and place
                    it after the info we have to call the user with.
                 pos = 0;
                 /* Action should correspond to one of the constants listed
                    at the bottom, titled Notify Actions. */
+#ifdef PYTHON3
                 PyTuple_SET_ITEM(args, pos++,
                                  PyLong_FromLongLong(notify_info->Action));
                 PyTuple_SET_ITEM(args, pos++, py_file_name);
+#else
+                PyTuple_SET_ITEM(args, pos++,
+                                 PyInt_FromLong(notify_info->Action));
+                PyTuple_SET_ITEM(args, pos++, py_file_name2);
+#endif
 
                 for (idx = 0; idx < args_len; ++idx) {
                     PyTuple_SET_ITEM(args, pos++,
     }
 
     path = PyTuple_GET_ITEM(args, 0);
-    if (!PyUnicode_Check(path)) {
-        PyErr_SetString(PyExc_TypeError, "path must be unicode");
-        return NULL;
-    }
 
     callback = PyTuple_GET_ITEM(args, 1);
     if (!PyCallable_Check(callback)) {
     if (self == NULL)
         return NULL;
 
-    self->path = PyUnicode_AS_UNICODE(path);
-    Py_INCREF(path);
+#ifdef PYTHON3
+    if (PyUnicode_Check(path)) {
+        self->path = PyUnicode_AS_UNICODE(path);
+        Py_INCREF(path);
+    } else if (PyBytes_Check(path)) {
+        PyObject *encoded;
+        encoded = PyUnicode_FromEncodedObject(path,
+                                              Py_FileSystemDefaultEncoding,
+                                              NULL);
+        self->path = PyUnicode_AS_UNICODE(encoded);
+        Py_INCREF(encoded);
+    } else {
+        PyErr_SetString(PyExc_TypeError, "path must be unicode");
+        return NULL;
+    }
+#else
+    if (PyString_Check(path)) {
+        PyObject *encoded, *unicode;
+        encoded = PyString_AsEncodedObject(path, Py_FileSystemDefaultEncoding,
+                                           NULL);
+        unicode = PyUnicode_FromEncodedObject(encoded,
+                                              Py_FileSystemDefaultEncoding,
+                                              NULL);
+        self->path = PyUnicode_AS_UNICODE(unicode);
+        Py_INCREF(encoded);
+        Py_INCREF(unicode);
+        Py_INCREF(path);
+    } else if (PyUnicode_Check(path)) {
+        self->path = PyUnicode_AS_UNICODE(path);
+        Py_INCREF(path);
+    } else {
+        PyErr_SetString(PyExc_TypeError, "path must be str or unicode");
+        return NULL;
+    }
+#endif
 
     self->callback = callback;
     Py_INCREF(callback);
     self->flags = 0;
     self->recursive = 0;
     
-    self->handle = CreateFileW(PyUnicode_AS_UNICODE(path),
+    self->handle = CreateFileW(self->path,
                    FILE_LIST_DIRECTORY, /* required */
                    FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
                    NULL,