Skip to content

Commit

Permalink
Ensure UTF-8 encoded paths are used
Browse files Browse the repository at this point in the history
  • Loading branch information
CCP-Aporia committed Dec 7, 2020
1 parent d340e35 commit 5142713
Showing 1 changed file with 46 additions and 13 deletions.
59 changes: 46 additions & 13 deletions src/watchdog_fsevents.c
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,48 @@ watchdog_FSEventStreamCallback(ConstFSEventStreamRef stream_ref,
PyGILState_Release(gil_state);
}

/**
* Converts a Python string object to an UTF-8 encoded ``CFStringRef``.
*
* :param py_string:
* A Python unicode or utf-8 encoded bytestring object.
* :returns:
* A new ``CFStringRef`` with the contents of ``py_string``, or ``NULL`` if an error occurred.
*/
CFStringRef PyString_AsUTF8EncodedCFStringRef(PyObject *py_string)
{
CFStringRef cf_string = NULL;
const char *c_string = NULL;
PyObject *helper = NULL;

if (PyUnicode_Check(py_string)) {
helper = PyUnicode_AsUTF8String(py_string);
} else if (PyBytes_Check(py_string)) {
PyObject *utf8 = PyUnicode_FromEncodedObject(py_string, NULL, "strict");
if (!utf8) {
return NULL;
}
Py_DECREF(utf8);
helper = PyObject_Bytes(py_string);
} else {
PyErr_SetString(PyExc_TypeError, "Path to watch must be a string or a UTF-8 encoded bytes object.");
return NULL;
}

if (!helper)
return NULL;

c_string = PyBytes_AsString(helper);
if (c_string) {
cf_string = CFStringCreateWithCString(kCFAllocatorDefault, c_string,kCFStringEncodingUTF8);
Py_DECREF(c_string);
}

Py_XDECREF(helper);

return cf_string;
}


/**
* Converts a list of Python strings to a ``CFMutableArray`` of
Expand All @@ -375,7 +417,6 @@ watchdog_CFMutableArrayRef_from_PyStringList(PyObject *py_string_list)
{
Py_ssize_t i = 0;
Py_ssize_t string_list_size = 0;
const char *c_string = NULL;
CFMutableArrayRef array_of_cf_string = NULL;
CFStringRef cf_string = NULL;
PyObject *py_string = NULL;
Expand All @@ -395,18 +436,10 @@ watchdog_CFMutableArrayRef_from_PyStringList(PyObject *py_string_list)
{
py_string = PyList_GetItem(py_string_list, i);
G_RETURN_NULL_IF_NULL(py_string);
#if PY_MAJOR_VERSION >= 3
if (PyUnicode_Check(py_string)) {
c_string = PyUnicode_AsUTF8(py_string);
} else {
c_string = PyBytes_AS_STRING(py_string);
}
#else
c_string = PyString_AS_STRING(py_string);
#endif
cf_string = CFStringCreateWithCString(kCFAllocatorDefault,
c_string,
kCFStringEncodingUTF8);

cf_string = PyString_AsUTF8EncodedCFStringRef(py_string);
G_RETURN_NULL_IF_NULL(cf_string);

CFArraySetValueAtIndex(array_of_cf_string, i, cf_string);
CFRelease(cf_string);
}
Expand Down

0 comments on commit 5142713

Please sign in to comment.