Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8e18ac4

Browse files
committed
STY: Break some long lines.
There remains a lot of code cleanup to do.
1 parent cfca318 commit 8e18ac4

4 files changed

Lines changed: 90 additions & 43 deletions

File tree

npytypes/rational/.gitignore

Lines changed: 0 additions & 10 deletions
This file was deleted.

npytypes/rational/LICENSE

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,24 @@ Copyright 2011, Geoffrey Irving, Eugene d'Eon.
22

33
All rights reserved.
44

5-
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
67

7-
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8-
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9-
3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
8+
1. Redistributions of source code must retain the above copyright notice,
9+
this list of conditions and the following disclaimer.
10+
2. Redistributions in binary form must reproduce the above copyright
11+
notice, this list of conditions and the following disclaimer in the
12+
documentation and/or other materials provided with the distribution.
13+
3. The name of the author may not be used to endorse or promote products
14+
derived from this software without specific prior written permission.
1015

11-
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17+
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18+
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
19+
NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21+
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

npytypes/rational/rational.c

Lines changed: 69 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ set_overflow(void) {
2121
PyGILState_STATE state = PyGILState_Ensure();
2222
#endif
2323
if (!PyErr_Occurred()) {
24-
PyErr_SetString(PyExc_OverflowError,"overflow in rational arithmetic");
24+
PyErr_SetString(PyExc_OverflowError,
25+
"overflow in rational arithmetic");
2526
}
2627
#ifdef ACQUIRE_GIL
2728
PyGILState_Release(state);
@@ -35,7 +36,8 @@ set_zero_divide(void) {
3536
PyGILState_STATE state = PyGILState_Ensure();
3637
#endif
3738
if (!PyErr_Occurred()) {
38-
PyErr_SetString(PyExc_ZeroDivisionError,"zero divide in rational arithmetic");
39+
PyErr_SetString(PyExc_ZeroDivisionError,
40+
"zero divide in rational arithmetic");
3941
}
4042
#ifdef ACQUIRE_GIL
4143
PyGILState_Release(state);
@@ -112,7 +114,10 @@ lcm(int64_t x, int64_t y) {
112114
typedef struct {
113115
/* numerator */
114116
int32_t n;
115-
/* denominator minus one: numpy.zeros() uses memset(0) for non-object types, so need to ensure that rational(0) has all zero bytes */
117+
/*
118+
* denominator minus one: numpy.zeros() uses memset(0) for non-object
119+
* types, so need to ensure that rational(0) has all zero bytes
120+
*/
116121
int32_t dmm;
117122
} rational;
118123

@@ -181,7 +186,10 @@ rational_negative(rational r) {
181186

182187
static NPY_INLINE rational
183188
rational_add(rational x, rational y) {
184-
/* Note that the numerator computation can never overflow int128_t, since each term is strictly under 2**128/4 (since d > 0). */
189+
/*
190+
* Note that the numerator computation can never overflow int128_t,
191+
* since each term is strictly under 2**128/4 (since d > 0).
192+
*/
185193
return make_rational_fast((int64_t)x.n*d(y)+(int64_t)d(x)*y.n,(int64_t)d(x)*d(y));
186194
}
187195

@@ -208,7 +216,10 @@ rational_floor(rational x) {
208216
if (x.n>=0) {
209217
return x.n/d(x);
210218
}
211-
/* This can be done without casting up to 64 bits, but it requires working out all the sign cases */
219+
/*
220+
* This can be done without casting up to 64 bits, but it requires
221+
* working out all the sign cases
222+
*/
212223
return -((-(int64_t)x.n+d(x)-1)/d(x));
213224
}
214225

@@ -219,7 +230,8 @@ rational_ceil(rational x) {
219230

220231
static NPY_INLINE rational
221232
rational_remainder(rational x, rational y) {
222-
return rational_subtract(x,rational_multiply(y,make_rational_int(rational_floor(rational_divide(x,y)))));
233+
return rational_subtract(x, rational_multiply(y,make_rational_int(
234+
rational_floor(rational_divide(x,y)))));
223235
}
224236

225237
static NPY_INLINE rational
@@ -232,7 +244,10 @@ rational_abs(rational x) {
232244

233245
static NPY_INLINE int64_t
234246
rational_rint(rational x) {
235-
/* Round towards nearest integer, moving exact half integers towards zero */
247+
/*
248+
* Round towards nearest integer, moving exact half integers towards
249+
* zero
250+
*/
236251
int32_t d_ = d(x);
237252
return (2*(int64_t)x.n+(x.n<0?-d_:d_))/(2*(int64_t)d_);
238253
}
@@ -262,7 +277,10 @@ rational_inverse(rational x) {
262277

263278
static NPY_INLINE int
264279
rational_eq(rational x, rational y) {
265-
/* Since we enforce d > 0, and store fractions in reduced form, equality is easy. */
280+
/*
281+
* Since we enforce d > 0, and store fractions in reduced form,
282+
* equality is easy.
283+
*/
266284
return x.n==y.n && x.dmm==y.dmm;
267285
}
268286

@@ -354,12 +372,14 @@ PyRational_FromRational(rational x) {
354372
static PyObject*
355373
pyrational_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
356374
if (kwds && PyDict_Size(kwds)) {
357-
PyErr_SetString(PyExc_TypeError,"constructor takes no keyword arguments");
375+
PyErr_SetString(PyExc_TypeError,
376+
"constructor takes no keyword arguments");
358377
return 0;
359378
}
360379
Py_ssize_t size = PyTuple_GET_SIZE(args);
361380
if (size>2) {
362-
PyErr_SetString(PyExc_TypeError,"expected rational or numerator and optional denominator");
381+
PyErr_SetString(PyExc_TypeError,
382+
"expected rational or numerator and optional denominator");
363383
return 0;
364384
}
365385
PyObject* x[2] = {PyTuple_GET_ITEM(args,0),PyTuple_GET_ITEM(args,1)};
@@ -381,7 +401,8 @@ pyrational_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
381401
return PyRational_FromRational(x);
382402
}
383403
bad:
384-
PyErr_Format(PyExc_ValueError,"invalid rational literal '%s'",s);
404+
PyErr_Format(PyExc_ValueError,
405+
"invalid rational literal '%s'",s);
385406
return 0;
386407
}
387408
}
@@ -391,7 +412,10 @@ pyrational_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
391412
n[i] = PyInt_AsLong(x[i]);
392413
if (n[i]==-1 && PyErr_Occurred()) {
393414
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
394-
PyErr_Format(PyExc_TypeError,"expected integer %s, got %s",(i?"denominator":"numerator"),x[i]->ob_type->tp_name);
415+
PyErr_Format(PyExc_TypeError,
416+
"expected integer %s, got %s",
417+
(i ? "denominator" : "numerator"),
418+
x[i]->ob_type->tp_name);
395419
}
396420
return 0;
397421
}
@@ -406,7 +430,10 @@ pyrational_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
406430
return 0;
407431
}
408432
if (!eq) {
409-
PyErr_Format(PyExc_TypeError,"expected integer %s, got %s",(i?"denominator":"numerator"),x[i]->ob_type->tp_name);
433+
PyErr_Format(PyExc_TypeError,
434+
"expected integer %s, got %s",
435+
(i ? "denominator" : "numerator"),
436+
x[i]->ob_type->tp_name);
410437
return 0;
411438
}
412439
}
@@ -417,7 +444,10 @@ pyrational_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
417444
return PyRational_FromRational(r);
418445
}
419446

420-
/* Returns Py_NotImplemented on most conversion failures, or raises an overflow error for too long ints */
447+
/*
448+
* Returns Py_NotImplemented on most conversion failures, or raises an
449+
* overflow error for too long ints
450+
*/
421451
#define AS_RATIONAL(dst,object) \
422452
rational dst = {0}; \
423453
if (PyRational_Check(object)) { \
@@ -471,21 +501,25 @@ static PyObject*
471501
pyrational_repr(PyObject* self) {
472502
rational x = ((PyRational*)self)->r;
473503
if (d(x)!=1) {
474-
return PyString_FromFormat("rational(%ld,%ld)",(long)x.n,(long)d(x));
504+
return PyString_FromFormat(
505+
"rational(%ld,%ld)",(long)x.n,(long)d(x));
475506
}
476507
else {
477-
return PyString_FromFormat("rational(%ld)",(long)x.n);
508+
return PyString_FromFormat(
509+
"rational(%ld)",(long)x.n);
478510
}
479511
}
480512

481513
static PyObject*
482514
pyrational_str(PyObject* self) {
483515
rational x = ((PyRational*)self)->r;
484516
if (d(x)!=1) {
485-
return PyString_FromFormat("%ld/%ld",(long)x.n,(long)d(x));
517+
return PyString_FromFormat(
518+
"%ld/%ld",(long)x.n,(long)d(x));
486519
}
487520
else {
488-
return PyString_FromFormat("%ld",(long)x.n);
521+
return PyString_FromFormat(
522+
"%ld",(long)x.n);
489523
}
490524
}
491525

@@ -677,7 +711,8 @@ npyrational_setitem(PyObject* item, void* data, void* arr) {
677711
return -1;
678712
}
679713
if (!eq) {
680-
PyErr_Format(PyExc_TypeError,"expected rational, got %s",item->ob_type->tp_name);
714+
PyErr_Format(PyExc_TypeError,
715+
"expected rational, got %s", item->ob_type->tp_name);
681716
return -1;
682717
}
683718
r = make_rational_int(n);
@@ -699,7 +734,8 @@ byteswap(int32_t* x) {
699734
}
700735

701736
static void
702-
npyrational_copyswapn(void* dst_, npy_intp dstride, void* src_, npy_intp sstride, npy_intp n, int swap, void* arr) {
737+
npyrational_copyswapn(void* dst_, npy_intp dstride, void* src_,
738+
npy_intp sstride, npy_intp n, int swap, void* arr) {
703739
char *dst = (char*)dst_, *src = (char*)src_;
704740
if (!src) {
705741
return;
@@ -713,12 +749,12 @@ npyrational_copyswapn(void* dst_, npy_intp dstride, void* src_, npy_intp sstride
713749
byteswap(&r->dmm);
714750
}
715751
}
716-
else if (dstride==sizeof(rational) && sstride==sizeof(rational)) {
717-
memcpy(dst,src,n*sizeof(rational));
752+
else if (dstride == sizeof(rational) && sstride == sizeof(rational)) {
753+
memcpy(dst, src, n*sizeof(rational));
718754
}
719755
else {
720756
for (i = 0; i < n; i++) {
721-
memcpy(dst+dstride*i,src+sstride*i,sizeof(rational));
757+
memcpy(dst + dstride*i, src + sstride*i, sizeof(rational));
722758
}
723759
}
724760
}
@@ -766,7 +802,8 @@ FIND_EXTREME(argmin,lt)
766802
FIND_EXTREME(argmax,gt)
767803

768804
static void
769-
npyrational_dot(void* ip0_, npy_intp is0, void* ip1_, npy_intp is1, void* op, npy_intp n, void* arr) {
805+
npyrational_dot(void* ip0_, npy_intp is0, void* ip1_, npy_intp is1,
806+
void* op, npy_intp n, void* arr) {
770807
rational r = {0};
771808
const char *ip0 = (char*)ip0_, *ip1 = (char*)ip1_;
772809
npy_intp i;
@@ -799,7 +836,8 @@ npyrational_fill(void* data_, npy_intp length, void* arr) {
799836
}
800837

801838
static int
802-
npyrational_fillwithscalar(void* buffer_, npy_intp length, void* value, void* arr) {
839+
npyrational_fillwithscalar(void* buffer_, npy_intp length,
840+
void* value, void* arr) {
803841
rational r = *(rational*)value;
804842
rational* buffer = (rational*)buffer_;
805843
npy_intp i;
@@ -819,8 +857,12 @@ PyArray_Descr npyrational_descr = {
819857
'V', /* kind */
820858
'r', /* type */
821859
'=', /* byteorder */
822-
/* For now, we need NPY_NEEDS_PYAPI in order to make numpy detect our exceptions. This isn't technically necessary,
823-
since we're careful about thread safety, and hopefully future versions of numpy will recognize that. */
860+
/*
861+
* For now, we need NPY_NEEDS_PYAPI in order to make numpy detect our
862+
* exceptions. This isn't technically necessary,
863+
* since we're careful about thread safety, and hopefully future
864+
* versions of numpy will recognize that.
865+
*/
824866
NPY_NEEDS_PYAPI | NPY_USE_GETITEM | NPY_USE_SETITEM, /* hasobject */
825867
0, /* type_num */
826868
sizeof(rational), /* elsize */

npytypes/rational/test_rational.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ def test_errors():
117117
except OverflowError:
118118
pass
119119
# Check for overflow in multiplication
120-
p = R(1262081,1262083) # Twin primes from http://primes.utm.edu/lists/small/10ktwins.txt
120+
# Twin primes from http://primes.utm.edu/lists/small/10ktwins.txt
121+
p = R(1262081,1262083)
121122
r = p
122123
for _ in xrange(int(log(2.**31)/log(r.d))-1):
123124
r *= p

0 commit comments

Comments
 (0)