[Python-checkins] CVS: python/dist/src/Objects complexobject.c,2.39,2.40 floatobject.c,2.89,2.90 intobject.c,2.67,2.68 longobject.c,1.94,1.95 object.c,2.142,2.143
Guido van Rossum
gvanrossum@users.sourceforge.net
Fri, 31 Aug 2001 10:40:17 -0700
Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv8024/Objects
Modified Files:
complexobject.c floatobject.c intobject.c longobject.c
object.c
Log Message:
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.
Index: complexobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/complexobject.c,v
retrieving revision 2.39
retrieving revision 2.40
diff -C2 -d -r2.39 -r2.40
*** complexobject.c 2001/08/17 18:39:25 2.39
--- complexobject.c 2001/08/31 17:40:15 2.40
***************
*** 374,377 ****
--- 374,398 ----
static PyObject *
+ complex_classic_div(PyComplexObject *v, PyComplexObject *w)
+ {
+ Py_complex quot;
+
+ if (Py_DivisionWarningFlag &&
+ PyErr_Warn(PyExc_DeprecationWarning,
+ "classic complex division") < 0)
+ return NULL;
+
+ PyFPE_START_PROTECT("complex_classic_div", return 0)
+ errno = 0;
+ quot = c_quot(v->cval,w->cval);
+ PyFPE_END_PROTECT(quot)
+ if (errno == EDOM) {
+ PyErr_SetString(PyExc_ZeroDivisionError, "complex division");
+ return NULL;
+ }
+ return PyComplex_FromCComplex(quot);
+ }
+
+ static PyObject *
complex_remainder(PyComplexObject *v, PyComplexObject *w)
{
***************
*** 855,859 ****
(binaryfunc)complex_sub, /* nb_subtract */
(binaryfunc)complex_mul, /* nb_multiply */
! (binaryfunc)complex_div, /* nb_divide */
(binaryfunc)complex_remainder, /* nb_remainder */
(binaryfunc)complex_divmod, /* nb_divmod */
--- 876,880 ----
(binaryfunc)complex_sub, /* nb_subtract */
(binaryfunc)complex_mul, /* nb_multiply */
! (binaryfunc)complex_classic_div, /* nb_divide */
(binaryfunc)complex_remainder, /* nb_remainder */
(binaryfunc)complex_divmod, /* nb_divmod */
Index: floatobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/floatobject.c,v
retrieving revision 2.89
retrieving revision 2.90
diff -C2 -d -r2.89 -r2.90
*** floatobject.c 2001/08/30 03:09:31 2.89
--- floatobject.c 2001/08/31 17:40:15 2.90
***************
*** 415,418 ****
--- 415,437 ----
static PyObject *
+ float_classic_div(PyObject *v, PyObject *w)
+ {
+ double a,b;
+ CONVERT_TO_DOUBLE(v, a);
+ CONVERT_TO_DOUBLE(w, b);
+ if (Py_DivisionWarningFlag &&
+ PyErr_Warn(PyExc_DeprecationWarning, "classic float division") < 0)
+ return NULL;
+ if (b == 0.0) {
+ PyErr_SetString(PyExc_ZeroDivisionError, "float division");
+ return NULL;
+ }
+ PyFPE_START_PROTECT("divide", return 0)
+ a = a / b;
+ PyFPE_END_PROTECT(a)
+ return PyFloat_FromDouble(a);
+ }
+
+ static PyObject *
float_rem(PyObject *v, PyObject *w)
{
***************
*** 678,682 ****
(binaryfunc)float_sub, /*nb_subtract*/
(binaryfunc)float_mul, /*nb_multiply*/
! (binaryfunc)float_div, /*nb_divide*/
(binaryfunc)float_rem, /*nb_remainder*/
(binaryfunc)float_divmod, /*nb_divmod*/
--- 697,701 ----
(binaryfunc)float_sub, /*nb_subtract*/
(binaryfunc)float_mul, /*nb_multiply*/
! (binaryfunc)float_classic_div, /*nb_divide*/
(binaryfunc)float_rem, /*nb_remainder*/
(binaryfunc)float_divmod, /*nb_divmod*/
Index: intobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/intobject.c,v
retrieving revision 2.67
retrieving revision 2.68
diff -C2 -d -r2.67 -r2.68
*** intobject.c 2001/08/30 03:09:31 2.67
--- intobject.c 2001/08/31 17:40:15 2.68
***************
*** 513,516 ****
--- 513,537 ----
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)
{
***************
*** 745,749 ****
int_true_divide(PyObject *v, PyObject *w)
{
! return PyFloat_Type.tp_as_number->nb_divide(v, w);
}
--- 766,770 ----
int_true_divide(PyObject *v, PyObject *w)
{
! return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
}
***************
*** 856,860 ****
(binaryfunc)int_sub, /*nb_subtract*/
(binaryfunc)int_mul, /*nb_multiply*/
! (binaryfunc)int_div, /*nb_divide*/
(binaryfunc)int_mod, /*nb_remainder*/
(binaryfunc)int_divmod, /*nb_divmod*/
--- 877,881 ----
(binaryfunc)int_sub, /*nb_subtract*/
(binaryfunc)int_mul, /*nb_multiply*/
! (binaryfunc)int_classic_div, /*nb_divide*/
(binaryfunc)int_mod, /*nb_remainder*/
(binaryfunc)int_divmod, /*nb_divmod*/
Index: longobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/longobject.c,v
retrieving revision 1.94
retrieving revision 1.95
diff -C2 -d -r1.94 -r1.95
*** longobject.c 2001/08/30 15:54:44 1.94
--- longobject.c 2001/08/31 17:40:15 1.95
***************
*** 1510,1513 ****
--- 1510,1533 ----
static PyObject *
+ long_classic_div(PyObject *v, PyObject *w)
+ {
+ PyLongObject *a, *b, *div, *mod;
+
+ CONVERT_BINOP(v, w, &a, &b);
+
+ if (Py_DivisionWarningFlag &&
+ PyErr_Warn(PyExc_DeprecationWarning, "classic long division") < 0)
+ div = NULL;
+ else if (l_divmod(a, b, &div, &mod) < 0)
+ div = NULL;
+ else
+ Py_DECREF(mod);
+
+ Py_DECREF(a);
+ Py_DECREF(b);
+ return (PyObject *)div;
+ }
+
+ static PyObject *
long_mod(PyObject *v, PyObject *w)
{
***************
*** 2116,2120 ****
(binaryfunc) long_sub, /*nb_subtract*/
(binaryfunc) long_mul, /*nb_multiply*/
! (binaryfunc) long_div, /*nb_divide*/
(binaryfunc) long_mod, /*nb_remainder*/
(binaryfunc) long_divmod, /*nb_divmod*/
--- 2136,2140 ----
(binaryfunc) long_sub, /*nb_subtract*/
(binaryfunc) long_mul, /*nb_multiply*/
! (binaryfunc) long_classic_div, /*nb_divide*/
(binaryfunc) long_mod, /*nb_remainder*/
(binaryfunc) long_divmod, /*nb_divmod*/
Index: object.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/object.c,v
retrieving revision 2.142
retrieving revision 2.143
diff -C2 -d -r2.142 -r2.143
*** object.c 2001/08/30 20:26:05 2.142
--- object.c 2001/08/31 17:40:15 2.143
***************
*** 17,20 ****
--- 17,22 ----
#endif
+ DL_IMPORT(int) Py_DivisionWarningFlag;
+
/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
These are used by the individual routines for object creation.