Open GoogleCodeExporter opened 9 years ago
Could also create float() at the same time:
def float(n):
return n * 1.0
It only accepts Int/Bool arguments (string's absence is notable), but it's a
start.
Original comment by dwhall...@gmail.com
on 16 Oct 2010 at 7:36
Original comment by dwhall...@gmail.com
on 22 Oct 2010 at 3:28
I created an implementation of int() and float() that accepts all types,
converts ints floats and strings, and raises a TypeError for other types.
However, I can't put this in the default branch because it requires HAVE_FLOAT
and not all platforms support that. I checked in this code on a branch
(issue_0154_dwhall_int_float_builtins) so people can use it. I won't be able
to put this in the default branch until I can figure out how to conditionally
compile parts of python code (such as in __bi.py).
Original comment by dwhall...@gmail.com
on 26 Oct 2010 at 9:50
r2b92d3dd03
Implemented int() and float() in lib/__bi.py.
Also updated abs() with one that doesn't create a tuple.
Checked in on branch: issue_0154_dwhall_int_float_builtins.
Original comment by dwhall...@gmail.com
on 26 Oct 2010 at 10:06
Original comment by dwhall...@gmail.com
on 16 Nov 2010 at 2:41
If you contain the conditional compiles within the boundaries of the __NATIVE
portion of the code both float and int can be made to compile with or without
HAVE_FLOAT.
Obviously float() on a non-float platform is made to always raise a type error!
def float(v):
"""__NATIVE__
pPmObj_t pv;
pPmObj_t pn;
PmReturn_t retval = PM_RET_OK;
#ifdef HAVE_FLOAT
float n;
/* If wrong number of args, raise TypeError */
if (NATIVE_GET_NUM_ARGS() != 1)
{
PM_RAISE(retval, PM_RET_EX_TYPE);
return retval;
}
pv = NATIVE_GET_LOCAL(0);
/* Convert from int */
if (OBJ_GET_TYPE(pv) == OBJ_TYPE_INT)
{
n = (float)(((pPmInt_t)pv)->val);
retval = float_new(n, &pn);
}
/* Convert from string */
else if (OBJ_GET_TYPE(pv) == OBJ_TYPE_STR)
{
n = (float)atof((const char *)((pPmString_t)pv)->val);
retval = float_new(n, &pn);
}
/* Raise TypeError if arg is not correct type */
else
#endif
/* turn this function in a dummy that always raise type error */
{
PM_RAISE(retval, PM_RET_EX_TYPE);
return retval;
}
NATIVE_SET_TOS(pn);
return retval;
"""
pass
def int(v):
"""__NATIVE__
int32_t n;
pPmObj_t pv;
pPmObj_t pn;
PmReturn_t retval = PM_RET_OK;
/* If wrong number of args, raise TypeError */
if (NATIVE_GET_NUM_ARGS() != 1)
{
PM_RAISE(retval, PM_RET_EX_TYPE);
return retval;
}
pv = NATIVE_GET_LOCAL(0);
/* No conversion required if already an integer */
if (OBJ_GET_TYPE(pv) == OBJ_TYPE_INT)
{
n = ((pPmInt_t)pv)->val;
}
/* Convert from string */
else if (OBJ_GET_TYPE(pv) == OBJ_TYPE_STR)
{
n = (int)atoi((const char *)((pPmString_t)pv)->val);
}
#ifdef HAVE_FLOAT
/* Convert from float */
else if (OBJ_GET_TYPE(pv) == OBJ_TYPE_FLT)
{
n = (int)(((pPmFloat_t)pv)->val);
}
#endif
/* Raise TypeError if arg is not correct type */
else
{
PM_RAISE(retval, PM_RET_EX_TYPE);
return retval;
}
retval = int_new(n, &pn);
NATIVE_SET_TOS(pn);
return retval;
"""
pass
Original comment by lucio.di...@googlemail.com
on 11 Jul 2013 at 12:21
Original issue reported on code.google.com by
dwhall...@gmail.com
on 4 Oct 2010 at 9:34