| 1 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 2 |
from pypy.module.cpyext.api import (PyObjectFields, bootstrap_function, |
| 3 |
cpython_struct, build_type_checkers, init_function, |
| 4 |
CANNOT_FAIL, cpython_api, PyObject, CONST_STRING) |
| 5 |
from pypy.module.cpyext.pyobject import ( |
| 6 |
make_typedescr, track_reference, from_ref) |
| 7 |
from rpython.rlib.rstruct import runpack |
| 8 |
from pypy.objspace.std.floatobject import W_FloatObject |
| 9 |
from pypy.module.cpyext.state import State |
| 10 |
from pypy.interpreter.error import OperationError, oefmt |
| 11 |
|
| 12 |
PyFloatObjectStruct = lltype.ForwardReference() |
| 13 |
PyFloatObject = lltype.Ptr(PyFloatObjectStruct) |
| 14 |
PyFloatObjectFields = PyObjectFields + \ |
| 15 |
(("ob_fval", rffi.DOUBLE),) |
| 16 |
cpython_struct("PyFloatObject", PyFloatObjectFields, PyFloatObjectStruct) |
| 17 |
|
| 18 |
PyFloat_Check, PyFloat_CheckExact = build_type_checkers("Float", "w_float") |
| 19 |
|
| 20 |
@bootstrap_function |
| 21 |
def init_floatobject(space): |
| 22 |
"Type description of PyFloatObject" |
| 23 |
make_typedescr(space.w_float.layout.typedef, |
| 24 |
basestruct=PyFloatObject.TO, |
| 25 |
attach=float_attach, |
| 26 |
realize=float_realize) |
| 27 |
|
| 28 |
@init_function |
| 29 |
def call_init(space): |
| 30 |
state = space.fromcache(State) |
| 31 |
state.C.float_init() |
| 32 |
|
| 33 |
def float_attach(space, py_obj, w_obj, w_userdata=None): |
| 34 |
""" |
| 35 |
Fills a newly allocated PyFloatObject with the given float object. The |
| 36 |
value must not be modified. |
| 37 |
""" |
| 38 |
py_float = rffi.cast(PyFloatObject, py_obj) |
| 39 |
py_float.c_ob_fval = space.float_w(w_obj) |
| 40 |
|
| 41 |
def float_realize(space, obj): |
| 42 |
floatval = rffi.cast(lltype.Float, rffi.cast(PyFloatObject, obj).c_ob_fval) |
| 43 |
w_type = from_ref(space, rffi.cast(PyObject, obj.c_ob_type)) |
| 44 |
w_obj = space.allocate_instance(W_FloatObject, w_type) |
| 45 |
w_obj.__init__(floatval) |
| 46 |
track_reference(space, obj, w_obj) |
| 47 |
return w_obj |
| 48 |
|
| 49 |
@cpython_api([lltype.Float], PyObject) |
| 50 |
def PyFloat_FromDouble(space, value): |
| 51 |
return space.newfloat(value) |
| 52 |
|
| 53 |
@cpython_api([PyObject], lltype.Float, error=-1) |
| 54 |
def PyFloat_AsDouble(space, w_obj): |
| 55 |
if not space.isinstance_w(w_obj, space.w_float): |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
w_float_method = space.lookup(w_obj, '__float__') |
| 60 |
if w_float_method is not None: |
| 61 |
w_value = space.float(w_obj) |
| 62 |
else: |
| 63 |
w_index_method = space.lookup(w_obj, '__index__') |
| 64 |
if w_index_method is not None: |
| 65 |
return space.float_w(space.float(space.index(w_obj))) |
| 66 |
raise oefmt(space.w_TypeError, |
| 67 |
"must be real number, not %T", w_obj) |
| 68 |
w_value_type = space.type(w_value) |
| 69 |
if not space.is_w(w_value_type, space.w_float): |
| 70 |
space.warn(space.newtext( |
| 71 |
"%s.__float__ returned non-float (type %s). " |
| 72 |
"The ability to return an instance of a strict subclass " |
| 73 |
"of float is deprecated, and may be removed " |
| 74 |
"in a future version of Python." % |
| 75 |
(space.type(w_obj).name, w_value_type.name)), |
| 76 |
space.w_DeprecationWarning) |
| 77 |
else: |
| 78 |
w_value = w_obj |
| 79 |
return space.float_w(w_value) |
| 80 |
|
| 81 |
@cpython_api([rffi.VOIDP], lltype.Float, error=CANNOT_FAIL) |
| 82 |
def PyFloat_AS_DOUBLE(space, w_float): |
| 83 |
"""Return a C double representation of the contents of w_float, but |
| 84 |
without error checking.""" |
| 85 |
return space.float_w(w_float) |
| 86 |
|
| 87 |
@cpython_api([PyObject], PyObject) |
| 88 |
def PyNumber_Float(space, w_obj): |
| 89 |
""" |
| 90 |
Returns the o converted to a float object on success, or NULL on failure. |
| 91 |
This is the equivalent of the Python expression float(o).""" |
| 92 |
return space.call_function(space.w_float, w_obj) |
| 93 |
|
| 94 |
@cpython_api([PyObject], PyObject) |
| 95 |
def PyFloat_FromString(space, w_obj): |
| 96 |
""" |
| 97 |
Create a PyFloatObject object based on the string value in str, or |
| 98 |
NULL on failure. |
| 99 |
""" |
| 100 |
return space.call_function(space.w_float, w_obj) |