[Python-checkins] CVS: python/dist/src/Objects abstract.c,2.70,2.71 classobject.c,2.134,2.135 complexobject.c,2.36,2.37 floatobject.c,2.84,2.85 intobject.c,2.60,2.61 longobject.c,1.90,1.91

Guido van Rossum gvanrossum@users.sourceforge.net
Tue, 07 Aug 2001 22:00:20 -0700


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv23513/Objects

Modified Files:
	abstract.c classobject.c complexobject.c floatobject.c 
	intobject.c longobject.c 
Log Message:
Implement PEP 238 in its (almost) full glory.

This introduces:

- A new operator // that means floor division (the kind of division
  where 1/2 is 0).

- The "future division" statement ("from __future__ import division)
  which changes the meaning of the / operator to implement "true
  division" (where 1/2 is 0.5).

- New overloadable operators __truediv__ and __floordiv__.

- New slots in the PyNumberMethods struct for true and floor division,
  new abstract APIs for them, new opcodes, and so on.

I emphasize that without the future division statement, the semantics
of / will remain unchanged until Python 3.0.

Not yet implemented are warnings (default off) when / is used with int
or long arguments.

This has been on display since 7/31 as SF patch #443474.

Flames to /dev/null.



Index: abstract.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/abstract.c,v
retrieving revision 2.70
retrieving revision 2.71
diff -C2 -d -r2.70 -r2.71
*** abstract.c	2001/08/02 04:15:00	2.70
--- abstract.c	2001/08/08 05:00:18	2.71
***************
*** 566,569 ****
--- 566,583 ----
  
  PyObject *
+ PyNumber_FloorDivide(PyObject *v, PyObject *w)
+ {
+ 	/* XXX tp_flags test */
+ 	return binary_op(v, w, NB_SLOT(nb_floor_divide), "//");
+ }
+ 
+ PyObject *
+ PyNumber_TrueDivide(PyObject *v, PyObject *w)
+ {
+ 	/* XXX tp_flags test */
+ 	return binary_op(v, w, NB_SLOT(nb_true_divide), "/");
+ }
+ 
+ PyObject *
  PyNumber_Remainder(PyObject *v, PyObject *w)
  {
***************
*** 630,633 ****
--- 644,663 ----
  INPLACE_BINOP(PyNumber_InPlaceSubtract, nb_inplace_subtract, nb_subtract, "-=")
  INPLACE_BINOP(PyNumber_InPlaceDivide, nb_inplace_divide, nb_divide, "/=")
+ 
+ PyObject *
+ PyNumber_InPlaceFloorDivide(PyObject *v, PyObject *w)
+ {
+ 	/* XXX tp_flags test */
+ 	return binary_iop(v, w, NB_SLOT(nb_inplace_floor_divide),
+ 			  NB_SLOT(nb_floor_divide), "//=");
+ }
+ 
+ PyObject *
+ PyNumber_InPlaceTrueDivide(PyObject *v, PyObject *w)
+ {
+ 	/* XXX tp_flags test */
+ 	return binary_iop(v, w, NB_SLOT(nb_inplace_true_divide),
+ 			  NB_SLOT(nb_true_divide), "/=");
+ }
  
  PyObject *

Index: classobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/classobject.c,v
retrieving revision 2.134
retrieving revision 2.135
diff -C2 -d -r2.134 -r2.135
*** classobject.c	2001/08/02 04:45:20	2.134
--- classobject.c	2001/08/08 05:00:18	2.135
***************
*** 1436,1439 ****
--- 1436,1441 ----
  BINARY(instance_mod, "mod", PyNumber_Remainder)
  BINARY(instance_divmod, "divmod", PyNumber_Divmod)
+ BINARY(instance_floordiv, "floordiv", PyNumber_FloorDivide)
+ BINARY(instance_truediv, "truediv", PyNumber_TrueDivide)
  
  BINARY_INPLACE(instance_ior, "or", PyNumber_InPlaceOr)
***************
*** 1447,1450 ****
--- 1449,1454 ----
  BINARY_INPLACE(instance_idiv, "div", PyNumber_InPlaceDivide)
  BINARY_INPLACE(instance_imod, "mod", PyNumber_InPlaceRemainder)
+ BINARY_INPLACE(instance_ifloordiv, "floordiv", PyNumber_InPlaceFloorDivide)
+ BINARY_INPLACE(instance_itruediv, "truediv", PyNumber_InPlaceTrueDivide)
  
  /* Try a 3-way comparison, returning an int; v is an instance.  Return:
***************
*** 1901,1904 ****
--- 1905,1912 ----
  	(binaryfunc)instance_ixor,		/* nb_inplace_xor */
  	(binaryfunc)instance_ior,		/* nb_inplace_or */
+ 	(binaryfunc)instance_floordiv,		/* nb_floor_divide */
+ 	(binaryfunc)instance_truediv,		/* nb_true_divide */
+ 	(binaryfunc)instance_ifloordiv,		/* nb_inplace_floor_divide */
+ 	(binaryfunc)instance_itruediv,		/* nb_inplace_true_divide */
  };
  

Index: complexobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/complexobject.c,v
retrieving revision 2.36
retrieving revision 2.37
diff -C2 -d -r2.36 -r2.37
*** complexobject.c	2001/08/02 04:15:00	2.36
--- complexobject.c	2001/08/08 05:00:18	2.37
***************
*** 443,446 ****
--- 443,461 ----
  
  static PyObject *
+ complex_int_div(PyComplexObject *v, PyComplexObject *w)
+ {
+ 	PyObject *t, *r;
+ 	
+ 	t = complex_divmod(v, w);
+ 	if (t != NULL) {
+ 		r = PyTuple_GET_ITEM(t, 0);
+ 		Py_INCREF(r);
+ 		Py_DECREF(t);
+ 		return r;
+ 	}
+ 	return NULL;
+ }
+ 
+ static PyObject *
  complex_neg(PyComplexObject *v)
  {
***************
*** 860,863 ****
--- 875,893 ----
  	0,					/* nb_oct */
  	0,					/* nb_hex */
+ 	0,					/* nb_inplace_add */
+ 	0,					/* nb_inplace_subtract */
+ 	0,					/* nb_inplace_multiply*/
+ 	0,					/* nb_inplace_divide */
+ 	0,					/* nb_inplace_remainder */
+ 	0, 					/* nb_inplace_power */
+ 	0,					/* nb_inplace_lshift */
+ 	0,					/* nb_inplace_rshift */
+ 	0,					/* nb_inplace_and */
+ 	0,					/* nb_inplace_xor */
+ 	0,					/* nb_inplace_or */
+ 	(binaryfunc)complex_int_div,		/* nb_floor_divide */
+ 	(binaryfunc)complex_div,		/* nb_true_divide */
+ 	0,					/* nb_inplace_floor_divide */
+ 	0,					/* nb_inplace_true_divide */
  };
  

Index: floatobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/floatobject.c,v
retrieving revision 2.84
retrieving revision 2.85
diff -C2 -d -r2.84 -r2.85
*** floatobject.c	2001/08/02 04:15:00	2.84
--- floatobject.c	2001/08/08 05:00:18	2.85
***************
*** 559,562 ****
--- 559,577 ----
  
  static PyObject *
+ float_int_div(PyObject *v, PyObject *w)
+ {
+ 	PyObject *t, *r;
+ 	
+ 	t = float_divmod(v, w);
+ 	if (t != NULL) {
+ 		r = PyTuple_GET_ITEM(t, 0);
+ 		Py_INCREF(r);
+ 		Py_DECREF(t);
+ 		return r;
+ 	}
+ 	return NULL;
+ }
+ 
+ static PyObject *
  float_neg(PyFloatObject *v)
  {
***************
*** 679,695 ****
  	(unaryfunc)float_long, /*nb_long*/
  	(unaryfunc)float_float, /*nb_float*/
! 	0,		/*nb_oct*/
! 	0,		/*nb_hex*/
! 	0,		/*nb_inplace_add*/
! 	0,		/*nb_inplace_subtract*/
! 	0,		/*nb_inplace_multiply*/
! 	0,		/*nb_inplace_divide*/
! 	0,		/*nb_inplace_remainder*/
! 	0, 		/*nb_inplace_power*/
! 	0,		/*nb_inplace_lshift*/
! 	0,		/*nb_inplace_rshift*/
! 	0,		/*nb_inplace_and*/
! 	0,		/*nb_inplace_xor*/
! 	0,		/*nb_inplace_or*/
  };
  
--- 694,714 ----
  	(unaryfunc)float_long, /*nb_long*/
  	(unaryfunc)float_float, /*nb_float*/
! 	0,		/* nb_oct */
! 	0,		/* nb_hex */
! 	0,		/* nb_inplace_add */
! 	0,		/* nb_inplace_subtract */
! 	0,		/* nb_inplace_multiply */
! 	0,		/* nb_inplace_divide */
! 	0,		/* nb_inplace_remainder */
! 	0, 		/* nb_inplace_power */
! 	0,		/* nb_inplace_lshift */
! 	0,		/* nb_inplace_rshift */
! 	0,		/* nb_inplace_and */
! 	0,		/* nb_inplace_xor */
! 	0,		/* nb_inplace_or */
! 	float_int_div,	/* nb_floor_divide */
! 	float_div,	/* nb_true_divide */
! 	0,		/* nb_inplace_floor_divide */
! 	0,		/* nb_inplace_true_divide */
  };
  

Index: intobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/intobject.c,v
retrieving revision 2.60
retrieving revision 2.61
diff -C2 -d -r2.60 -r2.61
*** intobject.c	2001/08/02 04:15:00	2.60
--- intobject.c	2001/08/08 05:00:18	2.61
***************
*** 704,707 ****
--- 704,713 ----
  
  static PyObject *
+ int_true_divide(PyObject *v, PyObject *w)
+ {
+ 	return PyFloat_Type.tp_as_number->nb_divide(v, w);
+ }
+ 
+ static PyObject *
  int_int(PyIntObject *v)
  {
***************
*** 813,816 ****
--- 819,826 ----
  	0,			/*nb_inplace_xor*/
  	0,			/*nb_inplace_or*/
+ 	(binaryfunc)int_div,	/* nb_floor_divide */
+ 	int_true_divide,	/* nb_true_divide */
+ 	0,			/* nb_inplace_floor_divide */
+ 	0,			/* nb_inplace_true_divide */
  };
  

Index: longobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/longobject.c,v
retrieving revision 1.90
retrieving revision 1.91
diff -C2 -d -r1.90 -r1.91
*** longobject.c	2001/08/02 04:15:00	1.90
--- longobject.c	2001/08/08 05:00:18	1.91
***************
*** 1982,1985 ****
--- 1982,1991 ----
  }
  
+ static PyObject *
+ long_true_divide(PyObject *v, PyObject *w)
+ {
+ 	return PyFloat_Type.tp_as_number->nb_divide(v, w);
+ }
+ 
  static int
  long_coerce(PyObject **pv, PyObject **pw)
***************
*** 2093,2107 ****
  	(unaryfunc)	long_oct,	/*nb_oct*/
  	(unaryfunc)	long_hex,	/*nb_hex*/
! 	0,				/*nb_inplace_add*/
! 	0,				/*nb_inplace_subtract*/
! 	0,				/*nb_inplace_multiply*/
! 	0,				/*nb_inplace_divide*/
! 	0,				/*nb_inplace_remainder*/
! 	0,				/*nb_inplace_power*/
! 	0,				/*nb_inplace_lshift*/
! 	0,				/*nb_inplace_rshift*/
! 	0,				/*nb_inplace_and*/
! 	0,				/*nb_inplace_xor*/
! 	0,				/*nb_inplace_or*/
  };
  
--- 2099,2117 ----
  	(unaryfunc)	long_oct,	/*nb_oct*/
  	(unaryfunc)	long_hex,	/*nb_hex*/
! 	0,				/* nb_inplace_add */
! 	0,				/* nb_inplace_subtract */
! 	0,				/* nb_inplace_multiply */
! 	0,				/* nb_inplace_divide */
! 	0,				/* nb_inplace_remainder */
! 	0,				/* nb_inplace_power */
! 	0,				/* nb_inplace_lshift */
! 	0,				/* nb_inplace_rshift */
! 	0,				/* nb_inplace_and */
! 	0,				/* nb_inplace_xor */
! 	0,				/* nb_inplace_or */
! 	(binaryfunc)long_div,		/* nb_floor_divide */
! 	long_true_divide,		/* nb_true_divide */
! 	0,				/* nb_inplace_floor_divide */
! 	0,				/* nb_inplace_true_divide */
  };