Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Pyston
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
Pyston
Commits
81c62fe1
Commit
81c62fe1
authored
Jul 15, 2015
by
Boxiang Sun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add long.__float__ attribute, check overflow in PyLong_AsDouble
parent
94687bdb
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
1 deletion
+37
-1
src/runtime/long.cpp
src/runtime/long.cpp
+23
-1
test/tests/float.py
test/tests/float.py
+14
-0
No files found.
src/runtime/long.cpp
View file @
81c62fe1
...
...
@@ -338,7 +338,15 @@ extern "C" long PyLong_AsLongAndOverflow(Box* vv, int* overflow) noexcept {
extern
"C"
double
PyLong_AsDouble
(
PyObject
*
vv
)
noexcept
{
RELEASE_ASSERT
(
PyLong_Check
(
vv
),
""
);
BoxedLong
*
l
=
static_cast
<
BoxedLong
*>
(
vv
);
return
mpz_get_d
(
l
->
n
);
double
result
=
mpz_get_d
(
l
->
n
);
if
(
std
::
isinf
(
result
))
{
PyErr_SetString
(
PyExc_OverflowError
,
"long int too large to convert to float"
);
return
-
1
;
}
return
result
;
}
/* Convert the long to a string object with given base,
...
...
@@ -688,6 +696,19 @@ Box* longInt(Box* v) {
return
new
BoxedInt
(
n
);
}
Box
*
longFloat
(
BoxedLong
*
v
)
{
if
(
!
isSubclass
(
v
->
cls
,
long_cls
))
raiseExcHelper
(
TypeError
,
"descriptor '__float__' requires a 'long' object but received a '%s'"
,
getTypeName
(
v
));
double
result
=
PyLong_AsDouble
(
v
);
if
(
result
==
-
1
)
checkAndThrowCAPIException
();
return
new
BoxedFloat
(
result
);
}
Box
*
longRepr
(
BoxedLong
*
v
)
{
if
(
!
isSubclass
(
v
->
cls
,
long_cls
))
raiseExcHelper
(
TypeError
,
"descriptor '__repr__' requires a 'long' object but received a '%s'"
,
getTypeName
(
v
));
...
...
@@ -1388,6 +1409,7 @@ void setupLong() {
long_cls
->
giveAttr
(
"__rshift__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
longRshift
,
UNKNOWN
,
2
)));
long_cls
->
giveAttr
(
"__int__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
longInt
,
UNKNOWN
,
1
)));
long_cls
->
giveAttr
(
"__float__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
longFloat
,
UNKNOWN
,
1
)));
long_cls
->
giveAttr
(
"__repr__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
longRepr
,
STR
,
1
)));
long_cls
->
giveAttr
(
"__str__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
longStr
,
STR
,
1
)));
long_cls
->
giveAttr
(
"__hex__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
longHex
,
STR
,
1
)));
...
...
test/tests/float.py
View file @
81c62fe1
...
...
@@ -74,3 +74,17 @@ try:
0.0
**
(
-
1.0
)
except
ZeroDivisionError
as
e
:
print
e
.
message
print
float
(
1
l
)
print
float
(
0
l
)
print
float
(
-
1
l
)
print
(
1
l
).
__float__
()
l
=
1024
<<
1024
try
:
float
(
l
)
except
OverflowError
as
e
:
print
e
.
message
try
:
float
(
-
l
)
except
OverflowError
as
e
:
print
e
.
message
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