From b60f24c12784251ba26fae15511e5f82fff1fe4e Mon Sep 17 00:00:00 2001 From: Nico von Geyso Date: Tue, 12 Mar 2013 16:21:17 +0100 Subject: [PATCH] use utils helper macros/functions through the whole lib * use to_unicode()/to_unicode_n() for creation of unicode strings * main support switch to python3 instead of python2 (provide defines for python2) --- include/pygit2/oid.h | 2 +- include/pygit2/utils.h | 39 ++++++++++++++++++++------------------- src/blob.c | 2 +- src/commit.c | 2 +- src/config.c | 10 +++++----- src/diff.c | 10 +++++----- src/index.c | 8 ++++---- src/note.c | 2 +- src/object.c | 4 ++-- src/oid.c | 8 ++++---- src/reference.c | 2 +- src/remote.c | 4 ++-- src/repository.c | 8 ++++---- src/signature.c | 4 ++-- src/tag.c | 2 +- src/tree.c | 6 +++--- src/utils.c | 6 +++--- src/walker.c | 2 +- 18 files changed, 61 insertions(+), 60 deletions(-) diff --git a/include/pygit2/oid.h b/include/pygit2/oid.h index 4f22337..7dce303 100644 --- a/include/pygit2/oid.h +++ b/include/pygit2/oid.h @@ -38,6 +38,6 @@ int py_str_to_git_oid_expand(git_repository *repo, PyObject *py_str, PyObject* git_oid_to_py_str(const git_oid *oid); #define git_oid_to_python(id) \ - PyString_FromStringAndSize((const char*)id, GIT_OID_RAWSZ) + PyBytes_FromStringAndSize((const char*)id, GIT_OID_RAWSZ) #endif diff --git a/include/pygit2/utils.h b/include/pygit2/utils.h index d1ecc42..a47c18a 100644 --- a/include/pygit2/utils.h +++ b/include/pygit2/utils.h @@ -33,25 +33,22 @@ #include #include - -/* Python 3 support */ -#if PY_MAJOR_VERSION >= 3 - #define PyInt_AsLong PyLong_AsLong - #define PyInt_Check PyLong_Check - #define PyInt_FromLong PyLong_FromLong - #define PyString_AS_STRING PyBytes_AS_STRING - #define PyString_AsString PyBytes_AsString - #define PyString_AsStringAndSize PyBytes_AsStringAndSize - #define PyString_Check PyBytes_Check - #define PyString_FromString PyBytes_FromString - #define PyString_FromStringAndSize PyBytes_FromStringAndSize - #define PyString_Size PyBytes_Size -#endif - +/* Python 2 support */ #if PY_MAJOR_VERSION == 2 + #define PyLong_FromSize_t PyInt_FromSize_t + #define PyLong_AsLong PyInt_AsLong + #undef PyLong_Check + #define PyLong_Check PyInt_Check + #define PyLong_FromLong PyInt_FromLong + #define PyBytes_AS_STRING PyString_AS_STRING + #define PyBytes_AsString PyString_AsString + #define PyBytes_AsStringAndSize PyString_AsStringAndSize + #define PyBytes_Check PyString_Check + #define PyBytes_FromString PyString_FromString + #define PyBytes_FromStringAndSize PyString_FromStringAndSize + #define PyBytes_Size PyString_Size #define to_path(x) to_bytes(x) #define to_encoding(x) to_bytes(x) - #define PyLong_FromSize_t PyInt_FromSize_t #else #define to_path(x) to_unicode(x, Py_FileSystemDefaultEncoding, "strict") #define to_encoding(x) PyUnicode_DecodeASCII(x, strlen(x), "strict") @@ -69,9 +66,12 @@ return -1;\ } + /* Utilities */ +#define to_unicode(x, encoding, errors) to_unicode_n(x, strlen(x), encoding, errors) + Py_LOCAL_INLINE(PyObject*) -to_unicode(const char *value, const char *encoding, const char *errors) +to_unicode_n(const char *value, size_t len, const char *encoding, const char *errors) { if (encoding == NULL) { /* If the encoding is not explicit, it may not be UTF-8, so it @@ -81,13 +81,14 @@ to_unicode(const char *value, const char *encoding, const char *errors) encoding = "utf-8"; errors = "replace"; } - return PyUnicode_Decode(value, strlen(value), encoding, errors); + + return PyUnicode_Decode(value, len, encoding, errors); } Py_LOCAL_INLINE(PyObject*) to_bytes(const char * value) { - return PyString_FromString(value); + return PyBytes_FromString(value); } char * py_str_to_c_str(PyObject *value, const char *encoding); diff --git a/src/blob.c b/src/blob.c index 446101b..c21ce07 100644 --- a/src/blob.c +++ b/src/blob.c @@ -37,7 +37,7 @@ PyDoc_STRVAR(Blob_size__doc__, "Size."); PyObject * Blob_size__get__(Blob *self) { - return PyInt_FromLong(git_blob_rawsize(self->blob)); + return PyLong_FromLong(git_blob_rawsize(self->blob)); } diff --git a/src/commit.c b/src/commit.c index 2f6dbc4..7d2e2fc 100644 --- a/src/commit.c +++ b/src/commit.c @@ -68,7 +68,7 @@ PyDoc_STRVAR(Commit__message__doc__, "Message (bytes)."); PyObject * Commit__message__get__(Commit *commit) { - return PyString_FromString(git_commit_message(commit->commit)); + return PyBytes_FromString(git_commit_message(commit->commit)); } diff --git a/src/config.c b/src/config.c index 9cf0b55..b06c557 100644 --- a/src/config.c +++ b/src/config.c @@ -191,7 +191,7 @@ Config_getitem(Config *self, PyObject *py_key) else if(git_config_parse_bool(&value_bool, value_str) == 0) py_value = PyBool_FromLong(value_bool); else - py_value = PyUnicode_FromString(value_str); + py_value = to_unicode(value_str, NULL, NULL); cleanup: free(key); @@ -223,9 +223,9 @@ Config_setitem(Config *self, PyObject *py_key, PyObject *py_value) else if (PyBool_Check(py_value)) { err = git_config_set_bool(self->config, key, (int)PyObject_IsTrue(py_value)); - } else if (PyInt_Check(py_value)) { + } else if (PyLong_Check(py_value)) { err = git_config_set_int64(self->config, key, - (int64_t)PyInt_AsLong(py_value)); + (int64_t)PyLong_AsLong(py_value)); } else { value = py_str_to_c_str(py_value, NULL); err = git_config_set_string(self->config, key, value); @@ -300,7 +300,7 @@ Config_foreach(Config *self, PyObject *args) ret = git_config_foreach(self->config, Config_foreach_callback_wrapper, (void *)args); - return PyInt_FromLong((long)ret); + return PyLong_FromLong((long)ret); } @@ -344,7 +344,7 @@ Config_get_multivar_fn_wrapper(const git_config_entry *value, void *data) { PyObject *item = NULL; - if (!(item = PyUnicode_FromString(value->value))) + if (!(item = to_unicode(value->value, NULL, NULL))) return -2; PyList_Append((PyObject *)data, item); diff --git a/src/diff.c b/src/diff.c index e6d53b0..619d142 100644 --- a/src/diff.c +++ b/src/diff.c @@ -85,8 +85,8 @@ diff_get_patch_byindex(git_diff_list* list, size_t idx) py_hunk->new_lines = range->new_lines; py_hunk->lines = PyList_New(lines_in_hunk + 1); - PyList_SetItem(py_hunk->lines, 0, - PyUnicode_FromStringAndSize(header, header_len)); + PyList_SetItem(py_hunk->lines, 0, + to_unicode_n(header, header_len, NULL, NULL)); for (j=1; j < lines_in_hunk + 1; ++j) { err = git_diff_patch_get_line_in_hunk(NULL, &line, &line_len, NULL, NULL, patch, i, j - 1); @@ -95,7 +95,7 @@ diff_get_patch_byindex(git_diff_list* list, size_t idx) goto cleanup; PyList_SetItem(py_hunk->lines, j, - PyUnicode_FromStringAndSize(line, line_len)); + to_unicode_n(line, line_len, NULL, NULL)); } PyList_SetItem((PyObject*) py_patch->hunks, i, @@ -247,7 +247,7 @@ Diff_patch__get__(Diff *self) err = git_diff_get_patch(&patch, &delta, self->list, i); if (err < 0) goto cleanup; - + err = git_diff_patch_to_str(&(strings[i]), patch); if (err < 0) goto cleanup; @@ -263,7 +263,7 @@ Diff_patch__get__(Diff *self) } free(strings); - py_patch = PyUnicode_FromString(buffer); + py_patch = to_unicode(buffer, NULL, NULL); free(buffer); cleanup: diff --git a/src/index.c b/src/index.c index 81b20cb..7eb3b43 100644 --- a/src/index.c +++ b/src/index.c @@ -178,7 +178,7 @@ Index__find(Index *self, PyObject *py_path) size_t idx; int err; - path = PyString_AsString(py_path); + path = PyBytes_AsString(py_path); if (!path) return NULL; @@ -236,8 +236,8 @@ Index_get_position(Index *self, PyObject *value) int err; /* Case 1: integer */ - if (PyInt_Check(value)) { - err = (int)PyInt_AsLong(value); + if (PyLong_Check(value)) { + err = (int)PyLong_AsLong(value); if (err == -1 && PyErr_Occurred()) return -1; if (err < 0) { @@ -567,7 +567,7 @@ PyDoc_STRVAR(IndexEntry_mode__doc__, "Mode."); PyObject * IndexEntry_mode__get__(IndexEntry *self) { - return PyInt_FromLong(self->entry->mode); + return PyLong_FromLong(self->entry->mode); } diff --git a/src/note.c b/src/note.c index 2dffbae..c887036 100644 --- a/src/note.c +++ b/src/note.c @@ -82,7 +82,7 @@ PyDoc_STRVAR(Note_message__doc__, PyObject * Note_message__get__(Note *self) { - return PyUnicode_FromString(git_note_message(self->note)); + return to_unicode(git_note_message(self->note), NULL, NULL); } diff --git a/src/object.c b/src/object.c index 314bac2..c3a33fa 100644 --- a/src/object.c +++ b/src/object.c @@ -86,7 +86,7 @@ PyDoc_STRVAR(Object_type__doc__, PyObject * Object_type__get__(Object *self) { - return PyInt_FromLong(git_object_type(self->obj)); + return PyLong_FromLong(git_object_type(self->obj)); } @@ -107,7 +107,7 @@ Object_read_raw(Object *self) if (obj == NULL) return NULL; - aux = PyString_FromStringAndSize( + aux = PyBytes_FromStringAndSize( git_odb_object_data(obj), git_odb_object_size(obj)); diff --git a/src/oid.c b/src/oid.c index 102424a..773d71f 100644 --- a/src/oid.c +++ b/src/oid.c @@ -41,8 +41,8 @@ py_str_to_git_oid(PyObject *py_str, git_oid *oid) Py_ssize_t len; /* Case 1: raw sha */ - if (PyString_Check(py_str)) { - hex_or_bin = PyString_AsString(py_str); + if (PyBytes_Check(py_str)) { + hex_or_bin = PyBytes_AsString(py_str); if (hex_or_bin == NULL) return -1; git_oid_fromraw(oid, (const unsigned char*)hex_or_bin); @@ -54,7 +54,7 @@ py_str_to_git_oid(PyObject *py_str, git_oid *oid) py_hex = PyUnicode_AsASCIIString(py_str); if (py_hex == NULL) return -1; - err = PyString_AsStringAndSize(py_hex, &hex_or_bin, &len); + err = PyBytes_AsStringAndSize(py_hex, &hex_or_bin, &len); if (err) { Py_DECREF(py_hex); return -1; @@ -118,6 +118,6 @@ git_oid_to_py_str(const git_oid *oid) char hex[GIT_OID_HEXSZ]; git_oid_fmt(hex, oid); - return PyUnicode_DecodeASCII(hex, GIT_OID_HEXSZ, "strict"); + return to_unicode_n(hex, GIT_OID_HEXSZ, "utf-8", "strict"); } diff --git a/src/reference.c b/src/reference.c index 41b2d27..774166b 100644 --- a/src/reference.c +++ b/src/reference.c @@ -342,7 +342,7 @@ Reference_type__get__(Reference *self) CHECK_REFERENCE(self); c_type = git_reference_type(self->reference); - return PyInt_FromLong(c_type); + return PyLong_FromLong(c_type); } diff --git a/src/remote.c b/src/remote.c index 0d55a54..a01f798 100644 --- a/src/remote.c +++ b/src/remote.c @@ -71,7 +71,7 @@ PyDoc_STRVAR(Remote_name__doc__, "Name of the remote refspec"); PyObject * Remote_name__get__(Remote *self) { - return PyUnicode_FromString(git_remote_name(self->remote)); + return to_unicode(git_remote_name(self->remote), NULL, NULL); } int @@ -100,7 +100,7 @@ PyDoc_STRVAR(Remote_url__doc__, "Url of the remote"); PyObject * Remote_url__get__(Remote *self) { - return PyUnicode_FromString(git_remote_url(self->remote)); + return to_unicode(git_remote_url(self->remote), NULL, NULL); } diff --git a/src/repository.c b/src/repository.c index 5c83cc7..b07b819 100644 --- a/src/repository.c +++ b/src/repository.c @@ -892,11 +892,11 @@ Repository_create_symbolic_reference(Repository *self, PyObject *args, return NULL; #if PY_MAJOR_VERSION == 2 - c_target = PyString_AsString(py_obj); + c_target = PyBytes_AsString(py_obj); #else // increases ref counter, so we have to release it afterwards PyObject* py_str = PyUnicode_AsASCIIString(py_obj); - c_target = PyString_AsString(py_str); + c_target = PyBytes_AsString(py_str); #endif if (c_target == NULL) return NULL; @@ -928,7 +928,7 @@ read_status_cb(const char *path, unsigned int status_flags, void *payload) PyObject *flags; int err; - flags = PyInt_FromLong((long) status_flags); + flags = PyLong_FromLong((long) status_flags); err = PyDict_SetItemString(payload, path, flags); Py_CLEAR(flags); @@ -972,7 +972,7 @@ Repository_status_file(Repository *self, PyObject *value) free(path); return err_obj; } - return PyInt_FromLong(status); + return PyLong_FromLong(status); } diff --git a/src/signature.c b/src/signature.c index 25dae0b..982713e 100644 --- a/src/signature.c +++ b/src/signature.c @@ -148,7 +148,7 @@ PyDoc_STRVAR(Signature_time__doc__, "Unix time."); PyObject * Signature_time__get__(Signature *self) { - return PyInt_FromLong(self->signature->when.time); + return PyLong_FromLong(self->signature->when.time); } @@ -157,7 +157,7 @@ PyDoc_STRVAR(Signature_offset__doc__, "Offset from UTC in minutes."); PyObject * Signature_offset__get__(Signature *self) { - return PyInt_FromLong(self->signature->when.offset); + return PyLong_FromLong(self->signature->when.offset); } PyGetSetDef Signature_getseters[] = { diff --git a/src/tag.c b/src/tag.c index ee2b329..2a9ba82 100644 --- a/src/tag.c +++ b/src/tag.c @@ -91,7 +91,7 @@ PyDoc_STRVAR(Tag__message__doc__, "Tag message (bytes)."); PyObject * Tag__message__get__(Tag *self) { - return PyString_FromString(git_tag_message(self->tag)); + return PyBytes_FromString(git_tag_message(self->tag)); } PyGetSetDef Tag_getseters[] = { diff --git a/src/tree.c b/src/tree.c index c092e5e..d2e5deb 100644 --- a/src/tree.c +++ b/src/tree.c @@ -53,7 +53,7 @@ PyDoc_STRVAR(TreeEntry_filemode__doc__, "Filemode."); PyObject * TreeEntry_filemode__get__(TreeEntry *self) { - return PyInt_FromLong(git_tree_entry_filemode(self->entry)); + return PyLong_FromLong(git_tree_entry_filemode(self->entry)); } @@ -201,7 +201,7 @@ Tree_fix_index(Tree *self, PyObject *py_index) size_t len; long slen; - index = PyInt_AsLong(py_index); + index = PyLong_AsLong(py_index); if (PyErr_Occurred()) return -1; @@ -263,7 +263,7 @@ Tree_getitem(Tree *self, PyObject *value) int err; /* Case 1: integer */ - if (PyInt_Check(value)) + if (PyLong_Check(value)) return Tree_getitem_by_index(self, value); /* Case 2: byte or text string */ diff --git a/src/utils.c b/src/utils.c index efd7887..488ffa1 100644 --- a/src/utils.c +++ b/src/utils.c @@ -38,8 +38,8 @@ char * py_str_to_c_str(PyObject *value, const char *encoding) { char *c_str = NULL; /* Case 1: byte string */ - if (PyString_Check(value)) - return strdup(PyString_AsString(value)); + if (PyBytes_Check(value)) + return strdup(PyBytes_AsString(value)); /* Case 2: text string */ if (PyUnicode_Check(value)) { @@ -50,7 +50,7 @@ char * py_str_to_c_str(PyObject *value, const char *encoding) value = PyUnicode_AsEncodedString(value, encoding, "strict"); if (value == NULL) return NULL; - c_str = strdup(PyString_AsString(value)); + c_str = strdup(PyBytes_AsString(value)); Py_DECREF(value); return c_str; } diff --git a/src/walker.c b/src/walker.c index 7e48ab3..9e483f7 100644 --- a/src/walker.c +++ b/src/walker.c @@ -100,7 +100,7 @@ Walker_sort(Walker *self, PyObject *py_sort_mode) { int sort_mode; - sort_mode = (int)PyInt_AsLong(py_sort_mode); + sort_mode = (int)PyLong_AsLong(py_sort_mode); if (sort_mode == -1 && PyErr_Occurred()) return NULL;