Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
e27f795b
Kaydet (Commit)
e27f795b
authored
Agu 23, 2001
tarafından
Guido van Rossum
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Change all case where we used to raise OverflowError to issue a
warning and then redo the operation using long ints.
üst
87780dfa
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
88 additions
and
56 deletions
+88
-56
intobject.c
Objects/intobject.c
+88
-56
No files found.
Objects/intobject.c
Dosyayı görüntüle @
e27f795b
...
@@ -22,11 +22,16 @@ PyIntObject _Py_TrueStruct = {
...
@@ -22,11 +22,16 @@ PyIntObject _Py_TrueStruct = {
1
1
};
};
static
PyObject
*
/* Return 1 if exception raised, 0 if caller should retry using longs */
static
int
err_ovf
(
char
*
msg
)
err_ovf
(
char
*
msg
)
{
{
PyErr_SetString
(
PyExc_OverflowError
,
msg
);
if
(
PyErr_Warn
(
PyExc_OverflowWarning
,
msg
)
<
0
)
{
return
NULL
;
PyErr_SetString
(
PyExc_OverflowError
,
msg
);
return
1
;
}
else
return
0
;
}
}
/* Integers are quite normal objects, to make object handling uniform.
/* Integers are quite normal objects, to make object handling uniform.
...
@@ -277,9 +282,11 @@ int_add(PyIntObject *v, PyIntObject *w)
...
@@ -277,9 +282,11 @@ int_add(PyIntObject *v, PyIntObject *w)
CONVERT_TO_LONG
(
v
,
a
);
CONVERT_TO_LONG
(
v
,
a
);
CONVERT_TO_LONG
(
w
,
b
);
CONVERT_TO_LONG
(
w
,
b
);
x
=
a
+
b
;
x
=
a
+
b
;
if
((
x
^
a
)
<
0
&&
(
x
^
b
)
<
0
)
if
((
x
^
a
)
>=
0
||
(
x
^
b
)
>=
0
)
return
err_ovf
(
"integer addition"
);
return
PyInt_FromLong
(
x
);
return
PyInt_FromLong
(
x
);
if
(
err_ovf
(
"integer addition"
))
return
NULL
;
return
PyLong_Type
.
tp_as_number
->
nb_add
((
PyObject
*
)
v
,
(
PyObject
*
)
w
);
}
}
static
PyObject
*
static
PyObject
*
...
@@ -289,9 +296,12 @@ int_sub(PyIntObject *v, PyIntObject *w)
...
@@ -289,9 +296,12 @@ int_sub(PyIntObject *v, PyIntObject *w)
CONVERT_TO_LONG
(
v
,
a
);
CONVERT_TO_LONG
(
v
,
a
);
CONVERT_TO_LONG
(
w
,
b
);
CONVERT_TO_LONG
(
w
,
b
);
x
=
a
-
b
;
x
=
a
-
b
;
if
((
x
^
a
)
<
0
&&
(
x
^~
b
)
<
0
)
if
((
x
^
a
)
>=
0
||
(
x
^~
b
)
>=
0
)
return
err_ovf
(
"integer subtraction"
);
return
PyInt_FromLong
(
x
);
return
PyInt_FromLong
(
x
);
if
(
err_ovf
(
"integer subtraction"
))
return
NULL
;
return
PyLong_Type
.
tp_as_number
->
nb_subtract
((
PyObject
*
)
v
,
(
PyObject
*
)
w
);
}
}
/*
/*
...
@@ -432,10 +442,19 @@ int_mul(PyObject *v, PyObject *w)
...
@@ -432,10 +442,19 @@ int_mul(PyObject *v, PyObject *w)
return
PyInt_FromLong
(
x
*
s
);
return
PyInt_FromLong
(
x
*
s
);
bad:
bad:
return
err_ovf
(
"integer multiplication"
);
if
(
err_ovf
(
"integer multiplication"
))
return
NULL
;
return
PyLong_Type
.
tp_as_number
->
nb_multiply
(
v
,
w
);
}
}
static
int
/* Return type of i_divmod */
enum
divmod_result
{
DIVMOD_OK
,
/* Correct result */
DIVMOD_OVERFLOW
,
/* Overflow, try again using longs */
DIVMOD_ERROR
/* Exception raised */
};
static
enum
divmod_result
i_divmod
(
register
long
x
,
register
long
y
,
i_divmod
(
register
long
x
,
register
long
y
,
long
*
p_xdivy
,
long
*
p_xmody
)
long
*
p_xdivy
,
long
*
p_xmody
)
{
{
...
@@ -444,12 +463,13 @@ i_divmod(register long x, register long y,
...
@@ -444,12 +463,13 @@ i_divmod(register long x, register long y,
if
(
y
==
0
)
{
if
(
y
==
0
)
{
PyErr_SetString
(
PyExc_ZeroDivisionError
,
PyErr_SetString
(
PyExc_ZeroDivisionError
,
"integer division or modulo by zero"
);
"integer division or modulo by zero"
);
return
-
1
;
return
DIVMOD_ERROR
;
}
}
/* (-sys.maxint-1)/-1 is the only overflow case. */
/* (-sys.maxint-1)/-1 is the only overflow case. */
if
(
y
==
-
1
&&
x
<
0
&&
x
==
-
x
)
{
if
(
y
==
-
1
&&
x
<
0
&&
x
==
-
x
)
{
err_ovf
(
"integer division"
);
if
(
err_ovf
(
"integer division"
))
return
-
1
;
return
DIVMOD_ERROR
;
return
DIVMOD_OVERFLOW
;
}
}
xdivy
=
x
/
y
;
xdivy
=
x
/
y
;
xmody
=
x
-
xdivy
*
y
;
xmody
=
x
-
xdivy
*
y
;
...
@@ -465,7 +485,7 @@ i_divmod(register long x, register long y,
...
@@ -465,7 +485,7 @@ i_divmod(register long x, register long y,
}
}
*
p_xdivy
=
xdivy
;
*
p_xdivy
=
xdivy
;
*
p_xmody
=
xmody
;
*
p_xmody
=
xmody
;
return
0
;
return
DIVMOD_OK
;
}
}
static
PyObject
*
static
PyObject
*
...
@@ -475,9 +495,15 @@ int_div(PyIntObject *x, PyIntObject *y)
...
@@ -475,9 +495,15 @@ int_div(PyIntObject *x, PyIntObject *y)
long
d
,
m
;
long
d
,
m
;
CONVERT_TO_LONG
(
x
,
xi
);
CONVERT_TO_LONG
(
x
,
xi
);
CONVERT_TO_LONG
(
y
,
yi
);
CONVERT_TO_LONG
(
y
,
yi
);
if
(
i_divmod
(
xi
,
yi
,
&
d
,
&
m
)
<
0
)
switch
(
i_divmod
(
xi
,
yi
,
&
d
,
&
m
))
{
case
DIVMOD_OK
:
return
PyInt_FromLong
(
d
);
case
DIVMOD_OVERFLOW
:
return
PyLong_Type
.
tp_as_number
->
nb_divide
((
PyObject
*
)
x
,
(
PyObject
*
)
y
);
default:
return
NULL
;
return
NULL
;
return
PyInt_FromLong
(
d
);
}
}
}
static
PyObject
*
static
PyObject
*
...
@@ -487,9 +513,15 @@ int_mod(PyIntObject *x, PyIntObject *y)
...
@@ -487,9 +513,15 @@ int_mod(PyIntObject *x, PyIntObject *y)
long
d
,
m
;
long
d
,
m
;
CONVERT_TO_LONG
(
x
,
xi
);
CONVERT_TO_LONG
(
x
,
xi
);
CONVERT_TO_LONG
(
y
,
yi
);
CONVERT_TO_LONG
(
y
,
yi
);
if
(
i_divmod
(
xi
,
yi
,
&
d
,
&
m
)
<
0
)
switch
(
i_divmod
(
xi
,
yi
,
&
d
,
&
m
))
{
case
DIVMOD_OK
:
return
PyInt_FromLong
(
m
);
case
DIVMOD_OVERFLOW
:
return
PyLong_Type
.
tp_as_number
->
nb_remainder
((
PyObject
*
)
x
,
(
PyObject
*
)
y
);
default:
return
NULL
;
return
NULL
;
return
PyInt_FromLong
(
m
);
}
}
}
static
PyObject
*
static
PyObject
*
...
@@ -499,15 +531,20 @@ int_divmod(PyIntObject *x, PyIntObject *y)
...
@@ -499,15 +531,20 @@ int_divmod(PyIntObject *x, PyIntObject *y)
long
d
,
m
;
long
d
,
m
;
CONVERT_TO_LONG
(
x
,
xi
);
CONVERT_TO_LONG
(
x
,
xi
);
CONVERT_TO_LONG
(
y
,
yi
);
CONVERT_TO_LONG
(
y
,
yi
);
if
(
i_divmod
(
xi
,
yi
,
&
d
,
&
m
)
<
0
)
switch
(
i_divmod
(
xi
,
yi
,
&
d
,
&
m
))
{
case
DIVMOD_OK
:
return
Py_BuildValue
(
"(ll)"
,
d
,
m
);
case
DIVMOD_OVERFLOW
:
return
PyLong_Type
.
tp_as_number
->
nb_divmod
((
PyObject
*
)
x
,
(
PyObject
*
)
y
);
default:
return
NULL
;
return
NULL
;
return
Py_BuildValue
(
"(ll)"
,
d
,
m
);
}
}
}
static
PyObject
*
static
PyObject
*
int_pow
(
PyIntObject
*
v
,
PyIntObject
*
w
,
PyIntObject
*
z
)
int_pow
(
PyIntObject
*
v
,
PyIntObject
*
w
,
PyIntObject
*
z
)
{
{
#if 1
register
long
iv
,
iw
,
iz
=
0
,
ix
,
temp
,
prev
;
register
long
iv
,
iw
,
iz
=
0
,
ix
,
temp
,
prev
;
CONVERT_TO_LONG
(
v
,
iv
);
CONVERT_TO_LONG
(
v
,
iv
);
CONVERT_TO_LONG
(
w
,
iw
);
CONVERT_TO_LONG
(
w
,
iw
);
...
@@ -542,15 +579,25 @@ int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
...
@@ -542,15 +579,25 @@ int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
ix
=
ix
*
temp
;
ix
=
ix
*
temp
;
if
(
temp
==
0
)
if
(
temp
==
0
)
break
;
/* Avoid ix / 0 */
break
;
/* Avoid ix / 0 */
if
(
ix
/
temp
!=
prev
)
if
(
ix
/
temp
!=
prev
)
{
return
err_ovf
(
"integer exponentiation"
);
if
(
err_ovf
(
"integer exponentiation"
))
return
NULL
;
return
PyLong_Type
.
tp_as_number
->
nb_power
(
(
PyObject
*
)
v
,
(
PyObject
*
)
w
,
(
PyObject
*
)
w
);
}
}
}
iw
>>=
1
;
/* Shift exponent down by 1 bit */
iw
>>=
1
;
/* Shift exponent down by 1 bit */
if
(
iw
==
0
)
break
;
if
(
iw
==
0
)
break
;
prev
=
temp
;
prev
=
temp
;
temp
*=
temp
;
/* Square the value of temp */
temp
*=
temp
;
/* Square the value of temp */
if
(
prev
!=
0
&&
temp
/
prev
!=
prev
)
if
(
prev
!=
0
&&
temp
/
prev
!=
prev
)
{
return
err_ovf
(
"integer exponentiation"
);
if
(
err_ovf
(
"integer exponentiation"
))
return
NULL
;
return
PyLong_Type
.
tp_as_number
->
nb_power
(
(
PyObject
*
)
v
,
(
PyObject
*
)
w
,
(
PyObject
*
)
z
);
}
if
(
iz
)
{
if
(
iz
)
{
/* If we did a multiplication, perform a modulo */
/* If we did a multiplication, perform a modulo */
ix
=
ix
%
iz
;
ix
=
ix
%
iz
;
...
@@ -559,36 +606,18 @@ int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
...
@@ -559,36 +606,18 @@ int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
}
}
if
(
iz
)
{
if
(
iz
)
{
long
div
,
mod
;
long
div
,
mod
;
if
(
i_divmod
(
ix
,
iz
,
&
div
,
&
mod
)
<
0
)
switch
(
i_divmod
(
ix
,
iz
,
&
div
,
&
mod
))
{
return
(
NULL
);
case
DIVMOD_OK
:
ix
=
mod
;
ix
=
mod
;
}
break
;
return
PyInt_FromLong
(
ix
);
case
DIVMOD_OVERFLOW
:
#else
return
PyLong_Type
.
tp_as_number
->
nb_power
(
register
long
iv
,
iw
,
ix
;
(
PyObject
*
)
v
,
(
PyObject
*
)
w
,
(
PyObject
*
)
z
);
CONVERT_TO_LONG
(
v
,
iv
);
default:
CONVERT_TO_LONG
(
w
,
iw
);
return
NULL
;
if
(
iw
<
0
)
{
}
PyErr_SetString
(
PyExc_ValueError
,
"integer to the negative power"
);
return
NULL
;
}
if
((
PyObject
*
)
z
!=
Py_None
)
{
PyErr_SetString
(
PyExc_TypeError
,
"pow(int, int, int) not yet supported"
);
return
NULL
;
}
ix
=
1
;
while
(
--
iw
>=
0
)
{
long
prev
=
ix
;
ix
=
ix
*
iv
;
if
(
iv
==
0
)
break
;
/* 0 to some power -- avoid ix / 0 */
if
(
ix
/
iv
!=
prev
)
return
err_ovf
(
"integer exponentiation"
);
}
}
return
PyInt_FromLong
(
ix
);
return
PyInt_FromLong
(
ix
);
#endif
}
}
static
PyObject
*
static
PyObject
*
...
@@ -597,8 +626,11 @@ int_neg(PyIntObject *v)
...
@@ -597,8 +626,11 @@ int_neg(PyIntObject *v)
register
long
a
,
x
;
register
long
a
,
x
;
a
=
v
->
ob_ival
;
a
=
v
->
ob_ival
;
x
=
-
a
;
x
=
-
a
;
if
(
a
<
0
&&
x
<
0
)
if
(
a
<
0
&&
x
<
0
)
{
return
err_ovf
(
"integer negation"
);
if
(
err_ovf
(
"integer negation"
))
return
NULL
;
return
PyNumber_Negative
(
PyLong_FromLong
(
a
));
}
return
PyInt_FromLong
(
x
);
return
PyInt_FromLong
(
x
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment