[Python-checkins] CVS: python/dist/src/Include abstract.h,2.35,2.36 compile.h,2.33,2.34 object.h,2.82,2.83 opcode.h,2.36,2.37 pythonrun.h,2.44,2.45 token.h,2.18,2.19

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


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

Modified Files:
	abstract.h compile.h object.h opcode.h pythonrun.h token.h 
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.h
===================================================================
RCS file: /cvsroot/python/python/dist/src/Include/abstract.h,v
retrieving revision 2.35
retrieving revision 2.36
diff -C2 -d -r2.35 -r2.36
*** abstract.h	2001/08/02 04:15:00	2.35
--- abstract.h	2001/08/08 05:00:17	2.36
***************
*** 548,551 ****
--- 548,571 ----
         */
  
+      DL_IMPORT(PyObject *) PyNumber_FloorDivide(PyObject *o1, PyObject *o2);
+ 
+        /*
+ 	 Returns the result of dividing o1 by o2 giving an integral result,
+ 	 or null on failure.
+ 	 This is the equivalent of the Python expression: o1//o2.
+ 
+ 
+        */
+ 
+      DL_IMPORT(PyObject *) PyNumber_TrueDivide(PyObject *o1, PyObject *o2);
+ 
+        /*
+ 	 Returns the result of dividing o1 by o2 giving a float result,
+ 	 or null on failure.
+ 	 This is the equivalent of the Python expression: o1/o2.
+ 
+ 
+        */
+ 
       DL_IMPORT(PyObject *) PyNumber_Remainder(PyObject *o1, PyObject *o2);
  
***************
*** 739,742 ****
--- 759,784 ----
  	 Returns the result of dividing o1 by o2, possibly in-place, or null
  	 on failure.  This is the equivalent of the Python expression:
+ 	 o1 /= o2.
+ 
+        */
+ 
+      DL_IMPORT(PyObject *) PyNumber_InPlaceFloorDivide(PyObject *o1,
+ 						       PyObject *o2);
+ 
+        /*
+ 	 Returns the result of dividing o1 by o2 giving an integral result,
+ 	 possibly in-place, or null on failure.
+ 	 This is the equivalent of the Python expression:
+ 	 o1 /= o2.
+ 
+        */
+ 
+      DL_IMPORT(PyObject *) PyNumber_InPlaceTrueDivide(PyObject *o1,
+ 						      PyObject *o2);
+ 
+        /*
+ 	 Returns the result of dividing o1 by o2 giving a float result,
+ 	 possibly in-place, or null on failure.
+ 	 This is the equivalent of the Python expression:
  	 o1 /= o2.
  

Index: compile.h
===================================================================
RCS file: /cvsroot/python/python/dist/src/Include/compile.h,v
retrieving revision 2.33
retrieving revision 2.34
diff -C2 -d -r2.33 -r2.34
*** compile.h	2001/07/16 02:29:45	2.33
--- compile.h	2001/08/08 05:00:17	2.34
***************
*** 42,45 ****
--- 42,47 ----
     in effect when the code block was compiled. */
  #define CO_GENERATOR_ALLOWED    0x1000
+ /* XXX Ditto for future division */
+ #define CO_FUTURE_DIVISION    	0x2000
  
  extern DL_IMPORT(PyTypeObject) PyCode_Type;
***************
*** 65,68 ****
--- 67,71 ----
      int ff_nested_scopes;
      int ff_generators;
+     int ff_division;
  } PyFutureFeatures;
  
***************
*** 76,79 ****
--- 79,85 ----
  #define GENERATORS_DEFAULT 0
  #define FUTURE_GENERATORS "generators"
+ 
+ #define DIVISION_DEFAULT 0
+ #define FUTURE_DIVISION "division"
  
  /* for internal use only */

Index: object.h
===================================================================
RCS file: /cvsroot/python/python/dist/src/Include/object.h,v
retrieving revision 2.82
retrieving revision 2.83
diff -C2 -d -r2.82 -r2.83
*** object.h	2001/08/07 17:24:28	2.82
--- object.h	2001/08/08 05:00:17	2.83
***************
*** 162,165 ****
--- 162,171 ----
  	binaryfunc nb_inplace_xor;
  	binaryfunc nb_inplace_or;
+ 
+ 	/* The following require the Py_TPFLAGS_HAVE_CLASS flag */
+ 	binaryfunc nb_floor_divide;
+ 	binaryfunc nb_true_divide;
+ 	binaryfunc nb_inplace_floor_divide;
+ 	binaryfunc nb_inplace_true_divide;
  } PyNumberMethods;
  
***************
*** 397,401 ****
  #define Py_TPFLAGS_HAVE_ITER (1L<<7)
  
! /* Experimental stuff for healing the type/class split */
  #define Py_TPFLAGS_HAVE_CLASS (1L<<8)
  
--- 403,407 ----
  #define Py_TPFLAGS_HAVE_ITER (1L<<7)
  
! /* New members introduced by Python 2.2 exist */
  #define Py_TPFLAGS_HAVE_CLASS (1L<<8)
  

Index: opcode.h
===================================================================
RCS file: /cvsroot/python/python/dist/src/Include/opcode.h,v
retrieving revision 2.36
retrieving revision 2.37
diff -C2 -d -r2.36 -r2.37
*** opcode.h	2001/06/18 22:08:13	2.36
--- opcode.h	2001/08/08 05:00:17	2.37
***************
*** 30,33 ****
--- 30,37 ----
  #define BINARY_SUBTRACT	24
  #define BINARY_SUBSCR	25
+ #define BINARY_FLOOR_DIVIDE 26
+ #define BINARY_TRUE_DIVIDE 27
+ #define INPLACE_FLOOR_DIVIDE 28
+ #define INPLACE_TRUE_DIVIDE 29
  
  #define SLICE		30

Index: pythonrun.h
===================================================================
RCS file: /cvsroot/python/python/dist/src/Include/pythonrun.h,v
retrieving revision 2.44
retrieving revision 2.45
diff -C2 -d -r2.44 -r2.45
*** pythonrun.h	2001/08/02 04:15:00	2.44
--- pythonrun.h	2001/08/08 05:00:17	2.45
***************
*** 13,16 ****
--- 13,17 ----
  #define PyCF_NESTED_SCOPES	(0x00000001UL)
  #define PyCF_GENERATORS		(0x00000002UL)
+ #define PyCF_DIVISION		(0x00000004UL)
  typedef struct {
  	unsigned long cf_flags;  /* bitmask of PyCF_xxx flags */

Index: token.h
===================================================================
RCS file: /cvsroot/python/python/dist/src/Include/token.h,v
retrieving revision 2.18
retrieving revision 2.19
diff -C2 -d -r2.18 -r2.19
*** token.h	2000/09/01 23:29:26	2.18
--- token.h	2001/08/08 05:00:17	2.19
***************
*** 56,63 ****
  #define RIGHTSHIFTEQUAL	46
  #define DOUBLESTAREQUAL	47
  /* Don't forget to update the table _PyParser_TokenNames in tokenizer.c! */
! #define OP		48
! #define ERRORTOKEN	49
! #define N_TOKENS	50
  
  /* Special definitions for cooperation with parser */
--- 56,65 ----
  #define RIGHTSHIFTEQUAL	46
  #define DOUBLESTAREQUAL	47
+ #define DOUBLESLASH	48
+ #define DOUBLESLASHEQUAL 49
  /* Don't forget to update the table _PyParser_TokenNames in tokenizer.c! */
! #define OP		50
! #define ERRORTOKEN	51
! #define N_TOKENS	52
  
  /* Special definitions for cooperation with parser */