(partial) review the way we handle Python int/long

This commit is contained in:
J. David Ibáñez 2017-04-09 10:17:01 +02:00
parent f37cf25b8e
commit f2c89a760a
12 changed files with 94 additions and 90 deletions

View File

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

View File

@ -80,7 +80,7 @@ PyDoc_STRVAR(Commit_commit_time__doc__, "Commit time.");
PyObject *
Commit_commit_time__get__(Commit *commit)
{
return PyLong_FromLongLong(git_commit_time(commit->commit));
return PyInt_FromLongLong(git_commit_time(commit->commit));
}
@ -89,7 +89,7 @@ PyDoc_STRVAR(Commit_commit_time_offset__doc__, "Commit time offset.");
PyObject *
Commit_commit_time_offset__get__(Commit *commit)
{
return PyLong_FromLong(git_commit_time_offset(commit->commit));
return PyInt_FromLong(git_commit_time_offset(commit->commit));
}

View File

@ -587,7 +587,7 @@ PyDoc_STRVAR(DiffStats_insertions__doc__, "Total number of insertions");
PyObject *
DiffStats_insertions__get__(DiffStats *self)
{
return PyLong_FromSize_t(git_diff_stats_insertions(self->stats));
return PyInt_FromSize_t(git_diff_stats_insertions(self->stats));
}
PyDoc_STRVAR(DiffStats_deletions__doc__, "Total number of deletions");
@ -595,7 +595,7 @@ PyDoc_STRVAR(DiffStats_deletions__doc__, "Total number of deletions");
PyObject *
DiffStats_deletions__get__(DiffStats *self)
{
return PyLong_FromSize_t(git_diff_stats_deletions(self->stats));
return PyInt_FromSize_t(git_diff_stats_deletions(self->stats));
}
PyDoc_STRVAR(DiffStats_files_changed__doc__, "Total number of files changed");
@ -603,7 +603,7 @@ PyDoc_STRVAR(DiffStats_files_changed__doc__, "Total number of files changed");
PyObject *
DiffStats_files_changed__get__(DiffStats *self)
{
return PyLong_FromSize_t(git_diff_stats_files_changed(self->stats));
return PyInt_FromSize_t(git_diff_stats_files_changed(self->stats));
}
PyDoc_STRVAR(DiffStats_format__doc__,
@ -805,10 +805,10 @@ Diff_getitem(Diff *self, PyObject *value)
{
size_t i;
if (!PyLong_Check(value))
return NULL;
if (!PyInt_Check(value))
return NULL; /* FIXME Raise error */
i = PyLong_AsUnsignedLong(value);
i = PyInt_AsSize_t(value);
return diff_get_patch_byindex(self->diff, i);
}

View File

@ -97,7 +97,7 @@ PyDoc_STRVAR(Object_type__doc__,
PyObject *
Object_type__get__(Object *self)
{
return PyLong_FromLong(git_object_type(self->obj));
return PyInt_FromLong(git_object_type(self->obj));
}
PyDoc_STRVAR(Object__pointer__doc__, "Get the object's pointer. For internal use only.");
@ -143,14 +143,15 @@ PyDoc_STRVAR(Object_peel__doc__,
PyObject *
Object_peel(Object *self, PyObject *py_type)
{
int type = -1, err;
int err;
git_otype otype;
git_object *peeled;
type = py_object_to_object_type(py_type);
if (type == -1)
otype = py_object_to_otype(py_type);
if (otype == GIT_OBJ_BAD)
return NULL;
err = git_object_peel(&peeled, self->obj, (git_otype)type);
err = git_object_peel(&peeled, self->obj, otype);
if (err < 0)
return Error_set(err);

View File

@ -65,11 +65,11 @@ option(PyObject *self, PyObject *args)
if (!py_option)
return NULL;
if (!PyLong_Check(py_option))
if (!PyInt_Check(py_option))
return Error_type_error(
"option should be an integer, got %.200s", py_option);
option = PyLong_AsLong(py_option);
option = PyInt_AsLong(py_option);
switch (option) {
case GIT_OPT_GET_SEARCH_PATH:
@ -80,11 +80,11 @@ option(PyObject *self, PyObject *args)
if (!py_level)
return NULL;
if (!PyLong_Check(py_level))
if (!PyInt_Check(py_level))
return Error_type_error(
"level should be an integer, got %.200s", py_level);
return get_search_path(PyLong_AsLong(py_level));
return get_search_path(PyInt_AsLong(py_level));
}
case GIT_OPT_SET_SEARCH_PATH:
@ -101,7 +101,7 @@ option(PyObject *self, PyObject *args)
if (!py_path)
return NULL;
if (!PyLong_Check(py_level))
if (!PyInt_Check(py_level))
return Error_type_error(
"level should be an integer, got %.200s", py_level);
@ -110,7 +110,7 @@ option(PyObject *self, PyObject *args)
return NULL;
err = git_libgit2_opts(
GIT_OPT_SET_SEARCH_PATH, PyLong_AsLong(py_level), path);
GIT_OPT_SET_SEARCH_PATH, PyInt_AsLong(py_level), path);
Py_DECREF(tpath);
if (err < 0)
@ -127,7 +127,7 @@ option(PyObject *self, PyObject *args)
if (error < 0)
return Error_set(error);
return PyLong_FromSize_t(size);
return PyInt_FromSize_t(size);
}
case GIT_OPT_SET_MWINDOW_SIZE:
@ -139,11 +139,11 @@ option(PyObject *self, PyObject *args)
if (!py_size)
return NULL;
if (!PyLong_Check(py_size))
if (!PyInt_Check(py_size))
return Error_type_error(
"size should be an integer, got %.200s", py_size);
size = PyLong_AsSize_t(py_size);
size = PyInt_AsSize_t(py_size);
error = git_libgit2_opts(GIT_OPT_SET_MWINDOW_SIZE, size);
if (error < 0)
return Error_set(error);
@ -159,7 +159,7 @@ option(PyObject *self, PyObject *args)
if (error < 0)
return Error_set(error);
return PyLong_FromSize_t(limit);
return PyInt_FromSize_t(limit);
}
case GIT_OPT_SET_MWINDOW_MAPPED_LIMIT:
@ -171,11 +171,11 @@ option(PyObject *self, PyObject *args)
if (!py_limit)
return NULL;
if (!PyLong_Check(py_limit))
if (!PyInt_Check(py_limit))
return Error_type_error(
"limit should be an integer, got %.200s", py_limit);
limit = PyLong_AsSize_t(py_limit);
limit = PyInt_AsSize_t(py_limit);
error = git_libgit2_opts(GIT_OPT_SET_MWINDOW_MAPPED_LIMIT, limit);
if (error < 0)
return Error_set(error);
@ -197,12 +197,12 @@ option(PyObject *self, PyObject *args)
if (!py_limit)
return NULL;
if (!PyLong_Check(py_limit))
if (!PyInt_Check(py_limit))
return Error_type_error(
"limit should be an integer, got %.200s", py_limit);
object_type = PyLong_AsLong(py_object_type);
limit = PyLong_AsSize_t(py_limit);
object_type = PyInt_AsLong(py_object_type);
limit = PyInt_AsSize_t(py_limit);
error = git_libgit2_opts(
GIT_OPT_SET_CACHE_OBJECT_LIMIT, object_type, limit);
@ -221,11 +221,11 @@ option(PyObject *self, PyObject *args)
if (!py_max_size)
return NULL;
if (!PyLong_Check(py_max_size))
if (!PyInt_Check(py_max_size))
return Error_type_error(
"max_size should be an integer, got %.200s", py_max_size);
max_size = PyLong_AsSize_t(py_max_size);
max_size = PyInt_AsSize_t(py_max_size);
error = git_libgit2_opts(GIT_OPT_SET_CACHE_MAX_SIZE, max_size);
if (error < 0)
return Error_set(error);
@ -240,11 +240,11 @@ option(PyObject *self, PyObject *args)
py_flag = PyTuple_GetItem(args, 1);
if (!PyLong_Check(py_flag))
if (!PyInt_Check(py_flag))
return Error_type_error(
"flag should be an integer, got %.200s", py_flag);
flag = PyLong_AsSize_t(py_flag);
flag = PyInt_AsSize_t(py_flag);
error = git_libgit2_opts(GIT_OPT_ENABLE_CACHING, flag);
if (error < 0)
return Error_set(error);
@ -262,8 +262,8 @@ option(PyObject *self, PyObject *args)
if (error < 0)
return Error_set(error);
PyTuple_SetItem(tup, 0, PyLong_FromLong(current));
PyTuple_SetItem(tup, 1, PyLong_FromLong(allowed));
PyTuple_SetItem(tup, 0, PyInt_FromLong(current));
PyTuple_SetItem(tup, 1, PyInt_FromLong(allowed));
return tup;
}

View File

@ -325,7 +325,7 @@ Reference_type__get__(Reference *self)
CHECK_REFERENCE(self);
c_type = git_reference_type(self->reference);
return PyLong_FromLong(c_type);
return PyInt_FromLong(c_type);
}
@ -379,7 +379,8 @@ PyDoc_STRVAR(Reference_peel__doc__,
PyObject *
Reference_peel(Reference *self, PyObject *args)
{
int err, type;
int err;
git_otype otype;
git_object *obj;
PyObject *py_type = Py_None;
@ -388,11 +389,11 @@ Reference_peel(Reference *self, PyObject *args)
if (!PyArg_ParseTuple(args, "|O", &py_type))
return NULL;
type = py_object_to_object_type(py_type);
if (type == -1)
otype = py_object_to_otype(py_type);
if (otype == GIT_OBJ_BAD)
return NULL;
err = git_reference_peel(&obj, self->reference, type);
err = git_reference_peel(&obj, self->reference, otype);
if (err < 0)
return Error_set(err);

View File

@ -1564,7 +1564,7 @@ Repository_status(Repository *self)
path = entry->head_to_index->old_file.path;
else
path = entry->index_to_workdir->old_file.path;
status = PyLong_FromLong((long) entry->status);
status = PyInt_FromLong((long) entry->status);
err = PyDict_SetItemString(dict, path, status);
Py_CLEAR(status);
@ -1606,7 +1606,7 @@ Repository_status_file(Repository *self, PyObject *value)
free(path);
return err_obj;
}
return PyLong_FromLong(status);
return PyInt_FromLong(status);
}

View File

@ -158,7 +158,7 @@ PyDoc_STRVAR(Signature_time__doc__, "Unix time.");
PyObject *
Signature_time__get__(Signature *self)
{
return PyLong_FromLongLong(self->signature->when.time);
return PyInt_FromLongLong(self->signature->when.time);
}
@ -167,7 +167,7 @@ PyDoc_STRVAR(Signature_offset__doc__, "Offset from UTC in minutes.");
PyObject *
Signature_offset__get__(Signature *self)
{
return PyLong_FromLong(self->signature->when.offset);
return PyInt_FromLong(self->signature->when.offset);
}
PyGetSetDef Signature_getseters[] = {

View File

@ -54,7 +54,7 @@ PyDoc_STRVAR(TreeEntry_filemode__doc__, "Filemode.");
PyObject *
TreeEntry_filemode__get__(TreeEntry *self)
{
return PyLong_FromLong(git_tree_entry_filemode(self->entry));
return PyInt_FromLong(git_tree_entry_filemode(self->entry));
}
@ -290,7 +290,7 @@ Tree_fix_index(Tree *self, PyObject *py_index)
size_t len;
long slen;
index = PyLong_AsLong(py_index);
index = PyInt_AsLong(py_index);
if (PyErr_Occurred())
return -1;
@ -359,7 +359,7 @@ Tree_getitem(Tree *self, PyObject *value)
int err;
/* Case 1: integer */
if (PyLong_Check(value))
if (PyInt_Check(value))
return Tree_getitem_by_index(self, value);
/* Case 2: byte or text string */

View File

@ -160,40 +160,39 @@ on_error:
static git_otype
py_type_to_git_type(PyTypeObject *py_type)
{
git_otype type = GIT_OBJ_BAD;
if (py_type == &CommitType)
return GIT_OBJ_COMMIT;
else if (py_type == &TreeType)
return GIT_OBJ_TREE;
else if (py_type == &BlobType)
return GIT_OBJ_BLOB;
else if (py_type == &TagType)
return GIT_OBJ_TAG;
if (py_type == &CommitType) {
type = GIT_OBJ_COMMIT;
} else if (py_type == &TreeType) {
type = GIT_OBJ_TREE;
} else if (py_type == &BlobType) {
type = GIT_OBJ_BLOB;
} else if (py_type == &TagType) {
type = GIT_OBJ_TAG;
}
return type;
PyErr_SetString(PyExc_ValueError, "invalid target type");
return GIT_OBJ_BAD; /* -1 */
}
int
py_object_to_object_type(PyObject *py_type)
git_otype
py_object_to_otype(PyObject *py_type)
{
int type = -1;
long value;
if (py_type == Py_None)
return GIT_OBJ_ANY;
if (PyLong_Check(py_type)) {
type = PyLong_AsLong(py_type);
if (type == -1 && PyErr_Occurred())
return -1;
} else if (PyType_Check(py_type)) {
type = py_type_to_git_type((PyTypeObject *) py_type);
if (PyInt_Check(py_type)) {
value = PyInt_AsLong(py_type);
if (value == -1 && PyErr_Occurred())
return GIT_OBJ_BAD;
/* TODO Check whether the value is a valid value */
return (git_otype)value;
}
if (type == -1) {
PyErr_SetString(PyExc_ValueError, "invalid target type");
}
if (PyType_Check(py_type))
return py_type_to_git_type((PyTypeObject *) py_type);
return type;
PyErr_SetString(PyExc_ValueError, "invalid target type");
return GIT_OBJ_BAD; /* -1 */
}

View File

@ -40,13 +40,17 @@
#endif
/* Python 2 support */
#ifndef Py_hash_t
#define Py_hash_t long
#endif
#ifndef PyLong_AsSize_t
#define PyLong_AsSize_t (size_t)PyLong_AsUnsignedLong
#endif
#if PY_MAJOR_VERSION == 2
#define PyLong_FromSize_t PyInt_FromSize_t
#define PyLong_AsSize_t (size_t)PyInt_AsSsize_t
#define PyLong_AsLong PyInt_AsLong
#undef PyLong_Check
#define PyLong_Check PyInt_Check
#define PyLong_FromLong PyInt_FromLong
#define PyInt_AsSize_t (size_t)PyInt_AsLong
#define PyInt_FromLongLong PyInt_FromLong
#define PyBytes_AS_STRING PyString_AS_STRING
#define PyBytes_AsString PyString_AsString
#define PyBytes_AsStringAndSize PyString_AsStringAndSize
@ -57,19 +61,18 @@
#define to_path(x) to_bytes(x)
#define to_encoding(x) to_bytes(x)
#else
#define PyInt_Check PyLong_Check
#define PyInt_FromSize_t PyLong_FromSize_t
#define PyInt_FromLong PyLong_FromLong
#define PyInt_FromLongLong PyLong_FromLongLong
#define PyInt_AsLong PyLong_AsLong
#define PyInt_AsSize_t PyLong_AsSize_t
#define to_path(x) to_unicode(x, Py_FileSystemDefaultEncoding, "strict")
#define to_encoding(x) PyUnicode_DecodeASCII(x, strlen(x), "strict")
#define PyString_FromFormat(s, ...) PyUnicode_FromFormat(s, __VA_ARGS__)
#endif
#ifdef PYPY_VERSION
#define PyLong_AsSize_t (size_t)PyLong_AsUnsignedLong
#endif
#ifndef Py_hash_t
#define Py_hash_t long
#endif
#define CHECK_REFERENCE(self)\
if (self->reference == NULL) {\
@ -118,7 +121,7 @@ const char *py_str_borrow_c_str(PyObject **tvaue, PyObject *value, const char *e
PyObject * get_pylist_from_git_strarray(git_strarray *strarray);
int get_strarraygit_from_pylist(git_strarray *array, PyObject *pylist);
int py_object_to_object_type(PyObject *py_type);
int py_object_to_otype(PyObject *py_type);
#define py_path_to_c_str(py_path) \
py_str_to_c_str(py_path, Py_FileSystemDefaultEncoding)

View File

@ -98,13 +98,13 @@ PyDoc_STRVAR(Walker_sort__doc__,
PyObject *
Walker_sort(Walker *self, PyObject *py_sort_mode)
{
int sort_mode;
long sort_mode;
sort_mode = (int)PyLong_AsLong(py_sort_mode);
sort_mode = PyInt_AsLong(py_sort_mode);
if (sort_mode == -1 && PyErr_Occurred())
return NULL;
git_revwalk_sorting(self->walk, sort_mode);
git_revwalk_sorting(self->walk, (unsigned int)sort_mode);
Py_RETURN_NONE;
}