code style and simplification

This commit is contained in:
Benjamin Peterson 2010-11-20 22:44:32 +00:00
parent 7e5343b882
commit 155614b104
1 changed files with 31 additions and 72 deletions

View File

@ -20,8 +20,7 @@ typedef struct {
NULL on error. NULL on error.
*/ */
static PyObject * static PyObject *
validate_step(PyObject *step) validate_step(PyObject *step) {
{
/* No step specified, use a step of 1. */ /* No step specified, use a step of 1. */
if (!step) if (!step)
return PyLong_FromLong(1); return PyLong_FromLong(1);
@ -49,8 +48,7 @@ validate_step(PyObject *step)
range(0, 5, -1) range(0, 5, -1)
*/ */
static PyObject * static PyObject *
range_new(PyTypeObject *type, PyObject *args, PyObject *kw) range_new(PyTypeObject *type, PyObject *args, PyObject *kw) {
{
rangeobject *obj = NULL; rangeobject *obj = NULL;
PyObject *start = NULL, *stop = NULL, *step = NULL; PyObject *start = NULL, *stop = NULL, *step = NULL;
@ -118,8 +116,7 @@ PyDoc_STRVAR(range_doc,
Returns an iterator that generates the numbers in the range on demand."); Returns an iterator that generates the numbers in the range on demand.");
static void static void
range_dealloc(rangeobject *r) range_dealloc(rangeobject *r) {
{
Py_DECREF(r->start); Py_DECREF(r->start);
Py_DECREF(r->stop); Py_DECREF(r->stop);
Py_DECREF(r->step); Py_DECREF(r->step);
@ -131,8 +128,7 @@ range_dealloc(rangeobject *r)
* PyLong_Check(). Return NULL when there is an error. * PyLong_Check(). Return NULL when there is an error.
*/ */
static PyObject* static PyObject*
range_length_obj(rangeobject *r) range_length_obj(rangeobject *r) {
{
/* ------------------------------------------------------------- /* -------------------------------------------------------------
Algorithm is equal to that of get_len_of_range(), but it operates Algorithm is equal to that of get_len_of_range(), but it operates
on PyObjects (which are assumed to be PyLong objects). on PyObjects (which are assumed to be PyLong objects).
@ -204,8 +200,7 @@ range_length_obj(rangeobject *r)
} }
static Py_ssize_t static Py_ssize_t
range_length(rangeobject *r) range_length(rangeobject *r) {
{
PyObject *len = range_length_obj(r); PyObject *len = range_length_obj(r);
Py_ssize_t result = -1; Py_ssize_t result = -1;
if (len) { if (len) {
@ -218,8 +213,7 @@ range_length(rangeobject *r)
/* range(...)[x] is necessary for: seq[:] = range(...) */ /* range(...)[x] is necessary for: seq[:] = range(...) */
static PyObject * static PyObject *
range_item(rangeobject *r, Py_ssize_t i) range_item(rangeobject *r, Py_ssize_t i) {
{
Py_ssize_t len = range_length(r); Py_ssize_t len = range_length(r);
PyObject *rem, *incr, *result; PyObject *rem, *incr, *result;
@ -246,8 +240,7 @@ range_item(rangeobject *r, Py_ssize_t i)
} }
static PyObject * static PyObject *
range_repr(rangeobject *r) range_repr(rangeobject *r) {
{
Py_ssize_t istep; Py_ssize_t istep;
/* Check for special case values for printing. We don't always /* Check for special case values for printing. We don't always
@ -267,16 +260,14 @@ range_repr(rangeobject *r)
/* Pickling support */ /* Pickling support */
static PyObject * static PyObject *
range_reduce(rangeobject *r, PyObject *args) range_reduce(rangeobject *r, PyObject *args) {
{
return Py_BuildValue("(O(OOO))", Py_TYPE(r), return Py_BuildValue("(O(OOO))", Py_TYPE(r),
r->start, r->stop, r->step); r->start, r->stop, r->step);
} }
/* Assumes (PyLong_CheckExact(ob) || PyBool_Check(ob)) */ /* Assumes (PyLong_CheckExact(ob) || PyBool_Check(ob)) */
static int static int
range_contains_long(rangeobject *r, PyObject *ob) range_contains_long(rangeobject *r, PyObject *ob) {
{
int cmp1, cmp2, cmp3; int cmp1, cmp2, cmp3;
PyObject *tmp1 = NULL; PyObject *tmp1 = NULL;
PyObject *tmp2 = NULL; PyObject *tmp2 = NULL;
@ -334,8 +325,7 @@ range_contains(rangeobject *r, PyObject *ob) {
} }
static PyObject * static PyObject *
range_count(rangeobject *r, PyObject *ob) range_count(rangeobject *r, PyObject *ob) {
{
if (PyLong_CheckExact(ob) || PyBool_Check(ob)) { if (PyLong_CheckExact(ob) || PyBool_Check(ob)) {
int result = range_contains_long(r, ob); int result = range_contains_long(r, ob);
if (result == -1) if (result == -1)
@ -354,12 +344,8 @@ range_count(rangeobject *r, PyObject *ob)
} }
static PyObject * static PyObject *
range_index(rangeobject *r, PyObject *ob) range_index(rangeobject *r, PyObject *ob) {
{
PyObject *idx, *tmp;
int contains; int contains;
PyObject *format_tuple, *err_string;
static PyObject *err_format = NULL;
if (!PyLong_CheckExact(ob) && !PyBool_Check(ob)) { if (!PyLong_CheckExact(ob) && !PyBool_Check(ob)) {
Py_ssize_t index; Py_ssize_t index;
@ -373,35 +359,18 @@ range_index(rangeobject *r, PyObject *ob)
if (contains == -1) if (contains == -1)
return NULL; return NULL;
if (!contains) if (contains) {
goto value_error; PyObject *idx, *tmp = PyNumber_Subtract(ob, r->start);
if (tmp == NULL)
tmp = PyNumber_Subtract(ob, r->start); return NULL;
if (tmp == NULL) /* idx = (ob - r.start) // r.step */
return NULL; idx = PyNumber_FloorDivide(tmp, r->step);
Py_DECREF(tmp);
/* idx = (ob - r.start) // r.step */ return idx;
idx = PyNumber_FloorDivide(tmp, r->step); }
Py_DECREF(tmp);
return idx;
value_error:
/* object is not in the range */ /* object is not in the range */
if (err_format == NULL) { PyErr_Format(PyExc_ValueError, "%R is not in range", ob);
err_format = PyUnicode_FromString("%r is not in range");
if (err_format == NULL)
return NULL;
}
format_tuple = PyTuple_Pack(1, ob);
if (format_tuple == NULL)
return NULL;
err_string = PyUnicode_Format(err_format, format_tuple);
Py_DECREF(format_tuple);
if (err_string == NULL)
return NULL;
PyErr_SetObject(PyExc_ValueError, err_string);
Py_DECREF(err_string);
return NULL; return NULL;
} }
@ -494,8 +463,7 @@ typedef struct {
} rangeiterobject; } rangeiterobject;
static PyObject * static PyObject *
rangeiter_next(rangeiterobject *r) rangeiter_next(rangeiterobject *r) {
{
if (r->index < r->len) if (r->index < r->len)
/* cast to unsigned to avoid possible signed overflow /* cast to unsigned to avoid possible signed overflow
in intermediate calculations. */ in intermediate calculations. */
@ -505,8 +473,7 @@ rangeiter_next(rangeiterobject *r)
} }
static PyObject * static PyObject *
rangeiter_len(rangeiterobject *r) rangeiter_len(rangeiterobject *r) {
{
return PyLong_FromLong(r->len - r->index); return PyLong_FromLong(r->len - r->index);
} }
@ -519,8 +486,7 @@ typedef struct {
} longrangeiterobject; } longrangeiterobject;
static PyObject * static PyObject *
longrangeiter_len(longrangeiterobject *r, PyObject *no_args) longrangeiter_len(longrangeiterobject *r, PyObject *no_args) {
{
return PyNumber_Subtract(r->len, r->index); return PyNumber_Subtract(r->len, r->index);
} }
@ -581,8 +547,7 @@ PyTypeObject PyRangeIter_Type = {
* required. The result always fits in an unsigned long. * required. The result always fits in an unsigned long.
*/ */
static unsigned long static unsigned long
get_len_of_range(long lo, long hi, long step) get_len_of_range(long lo, long hi, long step) {
{
/* ------------------------------------------------------------- /* -------------------------------------------------------------
If step > 0 and lo >= hi, or step < 0 and lo <= hi, the range is empty. If step > 0 and lo >= hi, or step < 0 and lo <= hi, the range is empty.
Else for step > 0, if n values are in the range, the last one is Else for step > 0, if n values are in the range, the last one is
@ -609,8 +574,7 @@ get_len_of_range(long lo, long hi, long step)
is not representable as a C long, OverflowError is raised. */ is not representable as a C long, OverflowError is raised. */
static PyObject * static PyObject *
int_range_iter(long start, long stop, long step) int_range_iter(long start, long stop, long step) {
{
rangeiterobject *it = PyObject_New(rangeiterobject, &PyRangeIter_Type); rangeiterobject *it = PyObject_New(rangeiterobject, &PyRangeIter_Type);
unsigned long ulen; unsigned long ulen;
if (it == NULL) if (it == NULL)
@ -630,8 +594,7 @@ int_range_iter(long start, long stop, long step)
} }
static PyObject * static PyObject *
rangeiter_new(PyTypeObject *type, PyObject *args, PyObject *kw) rangeiter_new(PyTypeObject *type, PyObject *args, PyObject *kw) {
{
long start, stop, step; long start, stop, step;
if (!_PyArg_NoKeywords("rangeiter()", kw)) if (!_PyArg_NoKeywords("rangeiter()", kw))
@ -651,8 +614,7 @@ static PyMethodDef longrangeiter_methods[] = {
}; };
static void static void
longrangeiter_dealloc(longrangeiterobject *r) longrangeiter_dealloc(longrangeiterobject *r) {
{
Py_XDECREF(r->index); Py_XDECREF(r->index);
Py_XDECREF(r->start); Py_XDECREF(r->start);
Py_XDECREF(r->step); Py_XDECREF(r->step);
@ -661,8 +623,7 @@ longrangeiter_dealloc(longrangeiterobject *r)
} }
static PyObject * static PyObject *
longrangeiter_next(longrangeiterobject *r) longrangeiter_next(longrangeiterobject *r) {
{
PyObject *one, *product, *new_index, *result; PyObject *one, *product, *new_index, *result;
if (PyObject_RichCompareBool(r->index, r->len, Py_LT) != 1) if (PyObject_RichCompareBool(r->index, r->len, Py_LT) != 1)
return NULL; return NULL;
@ -729,8 +690,7 @@ PyTypeObject PyLongRangeIter_Type = {
}; };
static PyObject * static PyObject *
range_iter(PyObject *seq) range_iter(PyObject *seq) {
{
rangeobject *r = (rangeobject *)seq; rangeobject *r = (rangeobject *)seq;
longrangeiterobject *it; longrangeiterobject *it;
long lstart, lstop, lstep; long lstart, lstop, lstep;
@ -790,8 +750,7 @@ create_failure:
} }
static PyObject * static PyObject *
range_reverse(PyObject *seq) range_reverse(PyObject *seq) {
{
rangeobject *range = (rangeobject*) seq; rangeobject *range = (rangeobject*) seq;
longrangeiterobject *it; longrangeiterobject *it;
PyObject *one, *sum, *diff, *len = NULL, *product; PyObject *one, *sum, *diff, *len = NULL, *product;