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

View File

@@ -33,25 +33,22 @@
#include <git2.h>
#include <pygit2/types.h>
/* 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);

View File

@@ -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));
}

View File

@@ -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));
}

View File

@@ -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);

View File

@@ -86,7 +86,7 @@ diff_get_patch_byindex(git_diff_list* list, size_t idx)
py_hunk->lines = PyList_New(lines_in_hunk + 1);
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) {
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,
@@ -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:

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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));

View File

@@ -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");
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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[] = {

View File

@@ -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[] = {

View File

@@ -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 */

View File

@@ -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;
}

View File

@@ -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;