From: Ralph Bean Date: Mon, 16 Nov 2015 14:19:29 +0000 (-0500) Subject: More explicit string type checking. X-Git-Tag: upstream/0.10.0~20^2~4 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a416082799a1527d74df44777f11bba25489f623;p=services%2Fcreaterepo_c.git More explicit string type checking. --- diff --git a/src/python/contentstat-py.c b/src/python/contentstat-py.c index 700a699..4c537ad 100644 --- a/src/python/contentstat-py.c +++ b/src/python/contentstat-py.c @@ -196,8 +196,8 @@ set_str(_ContentStatObject *self, PyObject *value, void *member_offset) { if (check_ContentStatStatus(self)) return -1; - if (!PyUnicode_Check(value) && value != Py_None) { - PyErr_SetString(PyExc_TypeError, "String or None expected!"); + if (!PyUnicode_Check(value) && !PyBytes_Check(value) && value != Py_None) { + PyErr_SetString(PyExc_TypeError, "Unicode, bytes, or None expected!"); return -1; } cr_ContentStat *rec = self->stat; diff --git a/src/python/locate_metadata-py.c b/src/python/locate_metadata-py.c index c99436d..a7a079a 100644 --- a/src/python/locate_metadata-py.c +++ b/src/python/locate_metadata-py.c @@ -132,7 +132,6 @@ PyObject * getitem(_MetadataLocationObject *self, PyObject *pykey) { char *key, *value; - PyObject *bytekey; if (check_MetadataLocationStatus(self)) return NULL; @@ -143,11 +142,11 @@ getitem(_MetadataLocationObject *self, PyObject *pykey) } if (PyUnicode_Check(pykey)) { - bytekey = PyUnicode_AsUTF8String(pykey); - key = PyBytes_AsString(bytekey); - } else { - key = PyBytes_AsString(pykey); + pykey = PyUnicode_AsUTF8String(pykey); } + + key = PyBytes_AsString(pykey); + value = NULL; if (!strcmp(key, "primary")) { diff --git a/src/python/package-py.c b/src/python/package-py.c index 6c93471..e45224b 100644 --- a/src/python/package-py.c +++ b/src/python/package-py.c @@ -416,14 +416,11 @@ set_str(_PackageObject *self, PyObject *value, void *member_offset) if (!pkg->chunk) pkg->chunk = g_string_chunk_new(0); - PyObject *bytes; if (PyUnicode_Check(value)) { - bytes = PyUnicode_AsUTF8String(value); - } else { - bytes = value; + value = PyUnicode_AsUTF8String(value); } - char *str = g_string_chunk_insert(pkg->chunk, PyBytes_AsString(bytes)); + char *str = g_string_chunk_insert(pkg->chunk, PyBytes_AsString(value)); *((char **) ((size_t) pkg + (size_t) member_offset)) = str; return 0; } diff --git a/src/python/repomd-py.c b/src/python/repomd-py.c index 22ee880..015546c 100644 --- a/src/python/repomd-py.c +++ b/src/python/repomd-py.c @@ -396,8 +396,8 @@ set_str(_RepomdObject *self, PyObject *value, void *member_offset) { if (check_RepomdStatus(self)) return -1; - if (!PyUnicode_Check(value) && value != Py_None) { - PyErr_SetString(PyExc_TypeError, "String or None expected!"); + if (!PyUnicode_Check(value) && !PyBytes_Check(value) && value != Py_None) { + PyErr_SetString(PyExc_TypeError, "Unicode, bytes, or None expected!"); return -1; } cr_Repomd *repomd = self->repomd; diff --git a/src/python/repomdrecord-py.c b/src/python/repomdrecord-py.c index 787600e..c5e3ccb 100644 --- a/src/python/repomdrecord-py.c +++ b/src/python/repomdrecord-py.c @@ -350,8 +350,8 @@ set_str(_RepomdRecordObject *self, PyObject *value, void *member_offset) { if (check_RepomdRecordStatus(self)) return -1; - if (!PyUnicode_Check(value) && value != Py_None) { - PyErr_SetString(PyExc_TypeError, "String or None expected!"); + if (!PyUnicode_Check(value) && !PyBytes_Check(value) && value != Py_None) { + PyErr_SetString(PyExc_TypeError, "Unicode, bytes, or None expected!"); return -1; } cr_RepomdRecord *rec = self->record; diff --git a/src/python/typeconversion.c b/src/python/typeconversion.c index 9d85803..761ba91 100644 --- a/src/python/typeconversion.c +++ b/src/python/typeconversion.c @@ -49,9 +49,11 @@ PyErr_ToGError(GError **err) g_set_error(err, ERR_DOMAIN, CRE_XMLPARSER, "Error while error handling"); } else { - PyObject *bytes = PyUnicode_AsUTF8String(pystr); + if (PyUnicode_Check(pystr)) { + pystr = PyUnicode_AsUTF8String(pystr); + } g_set_error(err, ERR_DOMAIN, CRE_XMLPARSER, - "%s", PyBytes_AsString(bytes)); + "%s", PyBytes_AsString(pystr)); } Py_XDECREF(pystr); @@ -70,9 +72,13 @@ PyObject_ToStrOrNull(PyObject *pyobj) { // String returned by this function shoud not be freed or modified if (PyUnicode_Check(pyobj)) { - PyObject *bytes = PyUnicode_AsUTF8String(pyobj); - return PyBytes_AsString(bytes); + pyobj = PyUnicode_AsUTF8String(pyobj); } + + if (PyBytes_Check(pyobj)) { + return PyBytes_AsString(pyobj); + } + // TODO: ? Add support for pyobj like: ("xxx",) and ["xxx"] return NULL; } @@ -254,11 +260,14 @@ GSList_FromPyList_Str(PyObject *py_list) for (Py_ssize_t x=0; x < size; x++) { PyObject *py_str = PyList_GetItem(py_list, x); assert(py_str != NULL); - if (!PyUnicode_Check(py_str)) + if (!PyUnicode_Check(py_str) && !PyBytes_Check(py_str)) // Hmm, element is not a string, just skip it continue; - PyObject *py_bytes = PyUnicode_AsUTF8String(py_str); - list = g_slist_prepend(list, PyBytes_AsString(py_bytes)); + + if (PyUnicode_Check(py_str)) + py_str = PyUnicode_AsUTF8String(py_str); + + list = g_slist_prepend(list, PyBytes_AsString(py_str)); } return list; diff --git a/src/python/updatecollection-py.c b/src/python/updatecollection-py.c index dd82c57..3a791be 100644 --- a/src/python/updatecollection-py.c +++ b/src/python/updatecollection-py.c @@ -254,8 +254,8 @@ set_str(_UpdateCollectionObject *self, PyObject *value, void *member_offset) { if (check_UpdateCollectionStatus(self)) return -1; - if (!PyUnicode_Check(value) && value != Py_None) { - PyErr_SetString(PyExc_TypeError, "String or None expected!"); + if (!PyUnicode_Check(value) && !PyBytes_Check(value) && value != Py_None) { + PyErr_SetString(PyExc_TypeError, "Unicode, bytes, or None expected!"); return -1; } cr_UpdateCollection *rec = self->collection; diff --git a/src/python/updatecollectionpackage-py.c b/src/python/updatecollectionpackage-py.c index eb3eda2..b36e5b4 100644 --- a/src/python/updatecollectionpackage-py.c +++ b/src/python/updatecollectionpackage-py.c @@ -195,8 +195,8 @@ set_str(_UpdateCollectionPackageObject *self, PyObject *value, void *member_offs { if (check_UpdateCollectionPackageStatus(self)) return -1; - if (!PyUnicode_Check(value) && value != Py_None) { - PyErr_SetString(PyExc_TypeError, "String or None expected!"); + if (!PyUnicode_Check(value) && !PyBytes_Check(value) && value != Py_None) { + PyErr_SetString(PyExc_TypeError, "Unicode, bytes, or None expected!"); return -1; } cr_UpdateCollectionPackage *pkg = self->pkg; diff --git a/src/python/updaterecord-py.c b/src/python/updaterecord-py.c index 2b8b76b..6c5a4fd 100644 --- a/src/python/updaterecord-py.c +++ b/src/python/updaterecord-py.c @@ -316,8 +316,8 @@ set_str(_UpdateRecordObject *self, PyObject *value, void *member_offset) { if (check_UpdateRecordStatus(self)) return -1; - if (!PyUnicode_Check(value) && value != Py_None) { - PyErr_SetString(PyExc_TypeError, "String or None expected!"); + if (!PyUnicode_Check(value) && !PyBytes_Check(value) && value != Py_None) { + PyErr_SetString(PyExc_TypeError, "Unicode, bytes, or None expected!"); return -1; } cr_UpdateRecord *rec = self->record; diff --git a/src/python/updatereference-py.c b/src/python/updatereference-py.c index e3cd456..a8b8903 100644 --- a/src/python/updatereference-py.c +++ b/src/python/updatereference-py.c @@ -165,13 +165,19 @@ set_str(_UpdateReferenceObject *self, PyObject *value, void *member_offset) { if (check_UpdateReferenceStatus(self)) return -1; - if (!PyUnicode_Check(value) && value != Py_None) { - PyErr_SetString(PyExc_TypeError, "String or None expected!"); + if (!PyUnicode_Check(value) && !PyBytes_Check(value) && value != Py_None) { + PyErr_SetString(PyExc_TypeError, "Unicode, bytes, or None expected!"); return -1; } + + if (PyUnicode_Check(value)) { + value = PyUnicode_AsUTF8String(value); + } + cr_UpdateReference *ref = self->reference; char *str = cr_safe_string_chunk_insert(ref->chunk, PyObject_ToStrOrNull(value)); + *((char **) ((size_t) ref + (size_t) member_offset)) = str; return 0; }