diff options
author | Guido van Rossum <guido@python.org> | 2001-08-31 17:40:15 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2001-08-31 17:40:15 (GMT) |
commit | 393661d15fa76ec46e9f7b71e97bf2ed38470620 (patch) | |
tree | 99af5c4d5a19a77fb12428d6ef40ce3896d9e2fd /Objects/intobject.c | |
parent | 29d55a38ce15062bcabf78a52faa828002b5bbd4 (diff) | |
download | cpython-393661d15fa76ec46e9f7b71e97bf2ed38470620.zip cpython-393661d15fa76ec46e9f7b71e97bf2ed38470620.tar.gz cpython-393661d15fa76ec46e9f7b71e97bf2ed38470620.tar.bz2 |
Add warning mode for classic division, almost exactly as specified in
PEP 238. Changes:
- add a new flag variable Py_DivisionWarningFlag, declared in
pydebug.h, defined in object.c, set in main.c, and used in
{int,long,float,complex}object.c. When this flag is set, the
classic division operator issues a DeprecationWarning message.
- add a new API PyRun_SimpleStringFlags() to match
PyRun_SimpleString(). The main() function calls this so that
commands run with -c can also benefit from -Dnew.
- While I was at it, I changed the usage message in main() somewhat:
alphabetized the options, split it in *four* parts to fit in under
512 bytes (not that I still believe this is necessary -- doc strings
elsewhere are much longer), and perhaps most visibly, don't display
the full list of options on each command line error. Instead, the
full list is only displayed when -h is used, and otherwise a brief
reminder of -h is displayed. When -h is used, write to stdout so
that you can do `python -h | more'.
Notes:
- I don't want to use the -W option to control whether the classic
division warning is issued or not, because the machinery to decide
whether to display the warning or not is very expensive (it involves
calling into the warnings.py module). You can use -Werror to turn
the warnings into exceptions though.
- The -Dnew option doesn't select future division for all of the
program -- only for the __main__ module. I don't know if I'll ever
change this -- it would require changes to the .pyc file magic
number to do it right, and a more global notion of compiler flags.
- You can usefully combine -Dwarn and -Dnew: this gives the __main__
module new division, and warns about classic division everywhere
else.
Diffstat (limited to 'Objects/intobject.c')
-rw-r--r-- | Objects/intobject.c | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/Objects/intobject.c b/Objects/intobject.c index d1f241d..108e658 100644 --- a/Objects/intobject.c +++ b/Objects/intobject.c @@ -512,6 +512,27 @@ int_div(PyIntObject *x, PyIntObject *y) } static PyObject * +int_classic_div(PyIntObject *x, PyIntObject *y) +{ + long xi, yi; + long d, m; + CONVERT_TO_LONG(x, xi); + CONVERT_TO_LONG(y, yi); + if (Py_DivisionWarningFlag && + PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0) + return NULL; + 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; + } +} + +static PyObject * int_mod(PyIntObject *x, PyIntObject *y) { long xi, yi; @@ -744,7 +765,7 @@ int_or(PyIntObject *v, PyIntObject *w) static PyObject * int_true_divide(PyObject *v, PyObject *w) { - return PyFloat_Type.tp_as_number->nb_divide(v, w); + return PyFloat_Type.tp_as_number->nb_true_divide(v, w); } static PyObject * @@ -855,7 +876,7 @@ static PyNumberMethods int_as_number = { (binaryfunc)int_add, /*nb_add*/ (binaryfunc)int_sub, /*nb_subtract*/ (binaryfunc)int_mul, /*nb_multiply*/ - (binaryfunc)int_div, /*nb_divide*/ + (binaryfunc)int_classic_div, /*nb_divide*/ (binaryfunc)int_mod, /*nb_remainder*/ (binaryfunc)int_divmod, /*nb_divmod*/ (ternaryfunc)int_pow, /*nb_power*/ |