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)
This commit is contained in:
Nico von Geyso
2013-03-12 16:21:17 +01:00
parent 78cddb6c91
commit b60f24c127
18 changed files with 61 additions and 60 deletions

View File

@@ -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); PyObject* git_oid_to_py_str(const git_oid *oid);
#define git_oid_to_python(id) \ #define git_oid_to_python(id) \
PyString_FromStringAndSize((const char*)id, GIT_OID_RAWSZ) PyBytes_FromStringAndSize((const char*)id, GIT_OID_RAWSZ)
#endif #endif

View File

@@ -33,25 +33,22 @@
#include <git2.h> #include <git2.h>
#include <pygit2/types.h> #include <pygit2/types.h>
/* Python 2 support */
/* 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
#if PY_MAJOR_VERSION == 2 #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_path(x) to_bytes(x)
#define to_encoding(x) to_bytes(x) #define to_encoding(x) to_bytes(x)
#define PyLong_FromSize_t PyInt_FromSize_t
#else #else
#define to_path(x) to_unicode(x, Py_FileSystemDefaultEncoding, "strict") #define to_path(x) to_unicode(x, Py_FileSystemDefaultEncoding, "strict")
#define to_encoding(x) PyUnicode_DecodeASCII(x, strlen(x), "strict") #define to_encoding(x) PyUnicode_DecodeASCII(x, strlen(x), "strict")
@@ -69,9 +66,12 @@
return -1;\ return -1;\
} }
/* Utilities */ /* Utilities */
#define to_unicode(x, encoding, errors) to_unicode_n(x, strlen(x), encoding, errors)
Py_LOCAL_INLINE(PyObject*) 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 (encoding == NULL) {
/* If the encoding is not explicit, it may not be UTF-8, so it /* 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"; encoding = "utf-8";
errors = "replace"; errors = "replace";
} }
return PyUnicode_Decode(value, strlen(value), encoding, errors);
return PyUnicode_Decode(value, len, encoding, errors);
} }
Py_LOCAL_INLINE(PyObject*) Py_LOCAL_INLINE(PyObject*)
to_bytes(const char * value) to_bytes(const char * value)
{ {
return PyString_FromString(value); return PyBytes_FromString(value);
} }
char * py_str_to_c_str(PyObject *value, const char *encoding); char * py_str_to_c_str(PyObject *value, const char *encoding);

View File

@@ -37,7 +37,7 @@ PyDoc_STRVAR(Blob_size__doc__, "Size.");
PyObject * PyObject *
Blob_size__get__(Blob *self) Blob_size__get__(Blob *self)
{ {
return PyInt_FromLong(git_blob_rawsize(self->blob)); return PyLong_FromLong(git_blob_rawsize(self->blob));
} }

View File

@@ -68,7 +68,7 @@ PyDoc_STRVAR(Commit__message__doc__, "Message (bytes).");
PyObject * PyObject *
Commit__message__get__(Commit *commit) Commit__message__get__(Commit *commit)
{ {
return PyString_FromString(git_commit_message(commit->commit)); return PyBytes_FromString(git_commit_message(commit->commit));
} }

View File

@@ -191,7 +191,7 @@ Config_getitem(Config *self, PyObject *py_key)
else if(git_config_parse_bool(&value_bool, value_str) == 0) else if(git_config_parse_bool(&value_bool, value_str) == 0)
py_value = PyBool_FromLong(value_bool); py_value = PyBool_FromLong(value_bool);
else else
py_value = PyUnicode_FromString(value_str); py_value = to_unicode(value_str, NULL, NULL);
cleanup: cleanup:
free(key); free(key);
@@ -223,9 +223,9 @@ Config_setitem(Config *self, PyObject *py_key, PyObject *py_value)
else if (PyBool_Check(py_value)) { else if (PyBool_Check(py_value)) {
err = git_config_set_bool(self->config, key, err = git_config_set_bool(self->config, key,
(int)PyObject_IsTrue(py_value)); (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, err = git_config_set_int64(self->config, key,
(int64_t)PyInt_AsLong(py_value)); (int64_t)PyLong_AsLong(py_value));
} else { } else {
value = py_str_to_c_str(py_value, NULL); value = py_str_to_c_str(py_value, NULL);
err = git_config_set_string(self->config, key, value); 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, ret = git_config_foreach(self->config, Config_foreach_callback_wrapper,
(void *)args); (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; PyObject *item = NULL;
if (!(item = PyUnicode_FromString(value->value))) if (!(item = to_unicode(value->value, NULL, NULL)))
return -2; return -2;
PyList_Append((PyObject *)data, item); PyList_Append((PyObject *)data, item);

View File

@@ -85,8 +85,8 @@ diff_get_patch_byindex(git_diff_list* list, size_t idx)
py_hunk->new_lines = range->new_lines; py_hunk->new_lines = range->new_lines;
py_hunk->lines = PyList_New(lines_in_hunk + 1); py_hunk->lines = PyList_New(lines_in_hunk + 1);
PyList_SetItem(py_hunk->lines, 0, PyList_SetItem(py_hunk->lines, 0,
PyUnicode_FromStringAndSize(header, header_len)); to_unicode_n(header, header_len, NULL, NULL));
for (j=1; j < lines_in_hunk + 1; ++j) { for (j=1; j < lines_in_hunk + 1; ++j) {
err = git_diff_patch_get_line_in_hunk(NULL, &line, err = git_diff_patch_get_line_in_hunk(NULL, &line,
&line_len, NULL, NULL, patch, i, j - 1); &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; goto cleanup;
PyList_SetItem(py_hunk->lines, j, 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, 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); err = git_diff_get_patch(&patch, &delta, self->list, i);
if (err < 0) if (err < 0)
goto cleanup; goto cleanup;
err = git_diff_patch_to_str(&(strings[i]), patch); err = git_diff_patch_to_str(&(strings[i]), patch);
if (err < 0) if (err < 0)
goto cleanup; goto cleanup;
@@ -263,7 +263,7 @@ Diff_patch__get__(Diff *self)
} }
free(strings); free(strings);
py_patch = PyUnicode_FromString(buffer); py_patch = to_unicode(buffer, NULL, NULL);
free(buffer); free(buffer);
cleanup: cleanup:

View File

@@ -178,7 +178,7 @@ Index__find(Index *self, PyObject *py_path)
size_t idx; size_t idx;
int err; int err;
path = PyString_AsString(py_path); path = PyBytes_AsString(py_path);
if (!path) if (!path)
return NULL; return NULL;
@@ -236,8 +236,8 @@ Index_get_position(Index *self, PyObject *value)
int err; int err;
/* Case 1: integer */ /* Case 1: integer */
if (PyInt_Check(value)) { if (PyLong_Check(value)) {
err = (int)PyInt_AsLong(value); err = (int)PyLong_AsLong(value);
if (err == -1 && PyErr_Occurred()) if (err == -1 && PyErr_Occurred())
return -1; return -1;
if (err < 0) { if (err < 0) {
@@ -567,7 +567,7 @@ PyDoc_STRVAR(IndexEntry_mode__doc__, "Mode.");
PyObject * PyObject *
IndexEntry_mode__get__(IndexEntry *self) IndexEntry_mode__get__(IndexEntry *self)
{ {
return PyInt_FromLong(self->entry->mode); return PyLong_FromLong(self->entry->mode);
} }

View File

@@ -82,7 +82,7 @@ PyDoc_STRVAR(Note_message__doc__,
PyObject * PyObject *
Note_message__get__(Note *self) Note_message__get__(Note *self)
{ {
return PyUnicode_FromString(git_note_message(self->note)); return to_unicode(git_note_message(self->note), NULL, NULL);
} }

View File

@@ -86,7 +86,7 @@ PyDoc_STRVAR(Object_type__doc__,
PyObject * PyObject *
Object_type__get__(Object *self) 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) if (obj == NULL)
return NULL; return NULL;
aux = PyString_FromStringAndSize( aux = PyBytes_FromStringAndSize(
git_odb_object_data(obj), git_odb_object_data(obj),
git_odb_object_size(obj)); git_odb_object_size(obj));

View File

@@ -41,8 +41,8 @@ py_str_to_git_oid(PyObject *py_str, git_oid *oid)
Py_ssize_t len; Py_ssize_t len;
/* Case 1: raw sha */ /* Case 1: raw sha */
if (PyString_Check(py_str)) { if (PyBytes_Check(py_str)) {
hex_or_bin = PyString_AsString(py_str); hex_or_bin = PyBytes_AsString(py_str);
if (hex_or_bin == NULL) if (hex_or_bin == NULL)
return -1; return -1;
git_oid_fromraw(oid, (const unsigned char*)hex_or_bin); 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); py_hex = PyUnicode_AsASCIIString(py_str);
if (py_hex == NULL) if (py_hex == NULL)
return -1; return -1;
err = PyString_AsStringAndSize(py_hex, &hex_or_bin, &len); err = PyBytes_AsStringAndSize(py_hex, &hex_or_bin, &len);
if (err) { if (err) {
Py_DECREF(py_hex); Py_DECREF(py_hex);
return -1; return -1;
@@ -118,6 +118,6 @@ git_oid_to_py_str(const git_oid *oid)
char hex[GIT_OID_HEXSZ]; char hex[GIT_OID_HEXSZ];
git_oid_fmt(hex, oid); git_oid_fmt(hex, oid);
return PyUnicode_DecodeASCII(hex, GIT_OID_HEXSZ, "strict"); return to_unicode_n(hex, GIT_OID_HEXSZ, "utf-8", "strict");
} }

View File

@@ -342,7 +342,7 @@ Reference_type__get__(Reference *self)
CHECK_REFERENCE(self); CHECK_REFERENCE(self);
c_type = git_reference_type(self->reference); c_type = git_reference_type(self->reference);
return PyInt_FromLong(c_type); return PyLong_FromLong(c_type);
} }

View File

@@ -71,7 +71,7 @@ PyDoc_STRVAR(Remote_name__doc__, "Name of the remote refspec");
PyObject * PyObject *
Remote_name__get__(Remote *self) Remote_name__get__(Remote *self)
{ {
return PyUnicode_FromString(git_remote_name(self->remote)); return to_unicode(git_remote_name(self->remote), NULL, NULL);
} }
int int
@@ -100,7 +100,7 @@ PyDoc_STRVAR(Remote_url__doc__, "Url of the remote");
PyObject * PyObject *
Remote_url__get__(Remote *self) Remote_url__get__(Remote *self)
{ {
return PyUnicode_FromString(git_remote_url(self->remote)); return to_unicode(git_remote_url(self->remote), NULL, NULL);
} }

View File

@@ -892,11 +892,11 @@ Repository_create_symbolic_reference(Repository *self, PyObject *args,
return NULL; return NULL;
#if PY_MAJOR_VERSION == 2 #if PY_MAJOR_VERSION == 2
c_target = PyString_AsString(py_obj); c_target = PyBytes_AsString(py_obj);
#else #else
// increases ref counter, so we have to release it afterwards // increases ref counter, so we have to release it afterwards
PyObject* py_str = PyUnicode_AsASCIIString(py_obj); PyObject* py_str = PyUnicode_AsASCIIString(py_obj);
c_target = PyString_AsString(py_str); c_target = PyBytes_AsString(py_str);
#endif #endif
if (c_target == NULL) if (c_target == NULL)
return NULL; return NULL;
@@ -928,7 +928,7 @@ read_status_cb(const char *path, unsigned int status_flags, void *payload)
PyObject *flags; PyObject *flags;
int err; int err;
flags = PyInt_FromLong((long) status_flags); flags = PyLong_FromLong((long) status_flags);
err = PyDict_SetItemString(payload, path, flags); err = PyDict_SetItemString(payload, path, flags);
Py_CLEAR(flags); Py_CLEAR(flags);
@@ -972,7 +972,7 @@ Repository_status_file(Repository *self, PyObject *value)
free(path); free(path);
return err_obj; return err_obj;
} }
return PyInt_FromLong(status); return PyLong_FromLong(status);
} }

View File

@@ -148,7 +148,7 @@ PyDoc_STRVAR(Signature_time__doc__, "Unix time.");
PyObject * PyObject *
Signature_time__get__(Signature *self) 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 * PyObject *
Signature_offset__get__(Signature *self) Signature_offset__get__(Signature *self)
{ {
return PyInt_FromLong(self->signature->when.offset); return PyLong_FromLong(self->signature->when.offset);
} }
PyGetSetDef Signature_getseters[] = { PyGetSetDef Signature_getseters[] = {

View File

@@ -91,7 +91,7 @@ PyDoc_STRVAR(Tag__message__doc__, "Tag message (bytes).");
PyObject * PyObject *
Tag__message__get__(Tag *self) Tag__message__get__(Tag *self)
{ {
return PyString_FromString(git_tag_message(self->tag)); return PyBytes_FromString(git_tag_message(self->tag));
} }
PyGetSetDef Tag_getseters[] = { PyGetSetDef Tag_getseters[] = {

View File

@@ -53,7 +53,7 @@ PyDoc_STRVAR(TreeEntry_filemode__doc__, "Filemode.");
PyObject * PyObject *
TreeEntry_filemode__get__(TreeEntry *self) 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; size_t len;
long slen; long slen;
index = PyInt_AsLong(py_index); index = PyLong_AsLong(py_index);
if (PyErr_Occurred()) if (PyErr_Occurred())
return -1; return -1;
@@ -263,7 +263,7 @@ Tree_getitem(Tree *self, PyObject *value)
int err; int err;
/* Case 1: integer */ /* Case 1: integer */
if (PyInt_Check(value)) if (PyLong_Check(value))
return Tree_getitem_by_index(self, value); return Tree_getitem_by_index(self, value);
/* Case 2: byte or text string */ /* Case 2: byte or text string */

View File

@@ -38,8 +38,8 @@ char * py_str_to_c_str(PyObject *value, const char *encoding)
{ {
char *c_str = NULL; char *c_str = NULL;
/* Case 1: byte string */ /* Case 1: byte string */
if (PyString_Check(value)) if (PyBytes_Check(value))
return strdup(PyString_AsString(value)); return strdup(PyBytes_AsString(value));
/* Case 2: text string */ /* Case 2: text string */
if (PyUnicode_Check(value)) { 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"); value = PyUnicode_AsEncodedString(value, encoding, "strict");
if (value == NULL) if (value == NULL)
return NULL; return NULL;
c_str = strdup(PyString_AsString(value)); c_str = strdup(PyBytes_AsString(value));
Py_DECREF(value); Py_DECREF(value);
return c_str; return c_str;
} }

View File

@@ -100,7 +100,7 @@ Walker_sort(Walker *self, PyObject *py_sort_mode)
{ {
int 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()) if (sort_mode == -1 && PyErr_Occurred())
return NULL; return NULL;