Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
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
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
cython
Commits
2dadb472
Commit
2dadb472
authored
Jan 28, 2013
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix a corner case where float(x) would not call x.__float__() for a float subtype
parent
110e2ae4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
2 deletions
+44
-2
Cython/Utility/Optimize.c
Cython/Utility/Optimize.c
+10
-2
tests/run/int_float_builtins_as_casts_T400.pyx
tests/run/int_float_builtins_as_casts_T400.pyx
+34
-0
No files found.
Cython/Utility/Optimize.c
View file @
2dadb472
...
...
@@ -476,8 +476,16 @@ static double __Pyx__PyObject_AsDouble(PyObject* obj) {
#if CYTHON_COMPILING_IN_PYPY
float_value
=
PyNumber_Float
(
obj
);
#else
if
(
Py_TYPE
(
obj
)
->
tp_as_number
&&
Py_TYPE
(
obj
)
->
tp_as_number
->
nb_float
)
{
return
PyFloat_AsDouble
(
obj
);
PyNumberMethods
*
nb
=
Py_TYPE
(
obj
)
->
tp_as_number
;
if
(
likely
(
nb
)
&&
likely
(
nb
->
nb_float
))
{
float_value
=
nb
->
nb_float
(
obj
);
if
(
likely
(
float_value
)
&&
unlikely
(
!
PyFloat_Check
(
float_value
)))
{
PyErr_Format
(
PyExc_TypeError
,
"__float__ returned non-float (type %.200s)"
,
Py_TYPE
(
float_value
)
->
tp_name
);
Py_DECREF
(
float_value
);
goto
bad
;
}
}
else
if
(
PyUnicode_CheckExact
(
obj
)
||
PyBytes_CheckExact
(
obj
))
{
#if PY_MAJOR_VERSION >= 3
float_value
=
PyFloat_FromString
(
obj
);
...
...
tests/run/int_float_builtins_as_casts_T400.pyx
View file @
2dadb472
...
...
@@ -137,3 +137,37 @@ def double_to_double_int(double x):
"""
cdef
double
r
=
int
(
x
)
return
r
class
MyFloat
(
float
):
"""
>>> x = MyFloat(1.0)
>>> x
1.0
>>> float(x)
12.0
>>> x.float()
12.0
"""
def
__float__
(
self
):
return
12.0
def
float
(
self
):
return
float
(
self
)
class
MyInt
(
int
):
"""
>>> x = MyInt(1)
>>> x
1
>>> int(x)
2
>>> x.int()
2
"""
def
__int__
(
self
):
return
2
def
int
(
self
):
return
int
(
self
)
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