diff options
author | Guido van Rossum <guido@python.org> | 2001-08-08 05:00:18 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2001-08-08 05:00:18 (GMT) |
commit | 4668b000a1d9113394941ad39875c827634feb49 (patch) | |
tree | 734560442fef1ce34912ceb772099b9a006e010a /Include | |
parent | 074c9d2b2081237c1071a3775f6e36252cf16ad7 (diff) | |
download | cpython-4668b000a1d9113394941ad39875c827634feb49.zip cpython-4668b000a1d9113394941ad39875c827634feb49.tar.gz cpython-4668b000a1d9113394941ad39875c827634feb49.tar.bz2 |
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.
Diffstat (limited to 'Include')
-rw-r--r-- | Include/abstract.h | 42 | ||||
-rw-r--r-- | Include/compile.h | 6 | ||||
-rw-r--r-- | Include/object.h | 8 | ||||
-rw-r--r-- | Include/opcode.h | 4 | ||||
-rw-r--r-- | Include/pythonrun.h | 1 | ||||
-rw-r--r-- | Include/token.h | 8 |
6 files changed, 65 insertions, 4 deletions
diff --git a/Include/abstract.h b/Include/abstract.h index 799438e..f4c1b3e 100644 --- a/Include/abstract.h +++ b/Include/abstract.h @@ -547,6 +547,26 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/ */ + 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); /* @@ -742,6 +762,28 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/ */ + 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. + + */ + DL_IMPORT(PyObject *) PyNumber_InPlaceRemainder(PyObject *o1, PyObject *o2); /* diff --git a/Include/compile.h b/Include/compile.h index 89c5ba4..e5840af 100644 --- a/Include/compile.h +++ b/Include/compile.h @@ -41,6 +41,8 @@ typedef struct { effect, this passes on the "from __future__ import generators" state 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; @@ -64,6 +66,7 @@ typedef struct { int ff_last_lineno; int ff_nested_scopes; int ff_generators; + int ff_division; } PyFutureFeatures; DL_IMPORT(PyFutureFeatures *) PyNode_Future(struct _node *, char *); @@ -76,6 +79,9 @@ DL_IMPORT(PyCodeObject *) PyNode_CompileFlags(struct _node *, char *, #define GENERATORS_DEFAULT 0 #define FUTURE_GENERATORS "generators" +#define DIVISION_DEFAULT 0 +#define FUTURE_DIVISION "division" + /* for internal use only */ #define _PyCode_GETCODEPTR(co, pp) \ ((*(co)->co_code->ob_type->tp_as_buffer->bf_getreadbuffer) \ diff --git a/Include/object.h b/Include/object.h index 9f679b0..f6c6ac6 100644 --- a/Include/object.h +++ b/Include/object.h @@ -161,6 +161,12 @@ typedef struct { binaryfunc nb_inplace_and; 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; typedef struct { @@ -396,7 +402,7 @@ given type object has a specified feature. /* tp_iter is defined */ #define Py_TPFLAGS_HAVE_ITER (1L<<7) -/* Experimental stuff for healing the type/class split */ +/* New members introduced by Python 2.2 exist */ #define Py_TPFLAGS_HAVE_CLASS (1L<<8) /* Set if the type object is dynamically allocated */ diff --git a/Include/opcode.h b/Include/opcode.h index c1e8362..9508499 100644 --- a/Include/opcode.h +++ b/Include/opcode.h @@ -29,6 +29,10 @@ extern "C" { #define BINARY_ADD 23 #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 /* Also uses 31-33 */ diff --git a/Include/pythonrun.h b/Include/pythonrun.h index 7d947df..55e1ddd 100644 --- a/Include/pythonrun.h +++ b/Include/pythonrun.h @@ -12,6 +12,7 @@ extern "C" { accordingly then. */ #define PyCF_NESTED_SCOPES (0x00000001UL) #define PyCF_GENERATORS (0x00000002UL) +#define PyCF_DIVISION (0x00000004UL) typedef struct { unsigned long cf_flags; /* bitmask of PyCF_xxx flags */ } PyCompilerFlags; diff --git a/Include/token.h b/Include/token.h index 77e3de7..3f58106 100644 --- a/Include/token.h +++ b/Include/token.h @@ -55,10 +55,12 @@ extern "C" { #define LEFTSHIFTEQUAL 45 #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 48 -#define ERRORTOKEN 49 -#define N_TOKENS 50 +#define OP 50 +#define ERRORTOKEN 51 +#define N_TOKENS 52 /* Special definitions for cooperation with parser */ |