Skip to content

Commit

Permalink
Implement PEP 238 in its (almost) full glory.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gvanrossum committed Aug 8, 2001
1 parent 074c9d2 commit 4668b00
Show file tree
Hide file tree
Showing 19 changed files with 476 additions and 226 deletions.
4 changes: 2 additions & 2 deletions Grammar/Grammar
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ stmt: simple_stmt | compound_stmt
simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | import_stmt | global_stmt | exec_stmt | assert_stmt
expr_stmt: testlist (augassign testlist | ('=' testlist)*)
augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | '<<=' | '>>=' | '**='
augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | '<<=' | '>>=' | '**=' | '//='
# For normal assignments, additional restrictions enforced by the interpreter
print_stmt: 'print' ( [ test (',' test)* [','] ] | '>>' test [ (',' test)+ [','] ] )
del_stmt: 'del' exprlist
Expand Down Expand Up @@ -77,7 +77,7 @@ xor_expr: and_expr ('^' and_expr)*
and_expr: shift_expr ('&' shift_expr)*
shift_expr: arith_expr (('<<'|'>>') arith_expr)*
arith_expr: term (('+'|'-') term)*
term: factor (('*'|'/'|'%') factor)*
term: factor (('*'|'/'|'%'|'//') factor)*
factor: ('+'|'-'|'~') factor | power
power: atom trailer* ('**' factor)*
atom: '(' [testlist] ')' | '[' [listmaker] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
Expand Down
42 changes: 42 additions & 0 deletions Include/abstract.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/*
Expand Down Expand Up @@ -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);

/*
Expand Down
6 changes: 6 additions & 0 deletions Include/compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 *);
Expand All @@ -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) \
Expand Down
8 changes: 7 additions & 1 deletion Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 */
Expand Down
4 changes: 4 additions & 0 deletions Include/opcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
1 change: 1 addition & 0 deletions Include/pythonrun.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions Include/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down
1 change: 1 addition & 0 deletions Lib/__future__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@ def __repr__(self):

nested_scopes = _Feature((2, 1, 0, "beta", 1), (2, 2, 0, "alpha", 0))
generators = _Feature((2, 2, 0, "alpha", 1), (2, 3, 0, "final", 0))
division = _Feature((2, 2, 0, "alpha", 2), (3, 0, 0, "alpha", 0))
30 changes: 30 additions & 0 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,20 @@ PyNumber_Add(PyObject *v, PyObject *w)
return result;
}

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)
{
Expand Down Expand Up @@ -630,6 +644,22 @@ INPLACE_BINOP(PyNumber_InPlaceRshift, nb_inplace_rshift, nb_rshift, ">>=")
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 *
PyNumber_InPlaceAdd(PyObject *v, PyObject *w)
{
Expand Down
8 changes: 8 additions & 0 deletions Objects/classobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,8 @@ BINARY(instance_mul, "mul", PyNumber_Multiply)
BINARY(instance_div, "div", PyNumber_Divide)
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)
BINARY_INPLACE(instance_ixor, "xor", PyNumber_InPlaceXor)
Expand All @@ -1446,6 +1448,8 @@ BINARY_INPLACE(instance_isub, "sub", PyNumber_InPlaceSubtract)
BINARY_INPLACE(instance_imul, "mul", PyNumber_InPlaceMultiply)
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:
-2 for an exception;
Expand Down Expand Up @@ -1900,6 +1904,10 @@ static PyNumberMethods instance_as_number = {
(binaryfunc)instance_iand, /* nb_inplace_and */
(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 */
};

PyTypeObject PyInstance_Type = {
Expand Down
30 changes: 30 additions & 0 deletions Objects/complexobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,21 @@ complex_pow(PyComplexObject *v, PyObject *w, PyComplexObject *z)
return PyComplex_FromCComplex(p);
}

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)
{
Expand Down Expand Up @@ -859,6 +874,21 @@ static PyNumberMethods complex_as_number = {
(unaryfunc)complex_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 */
(binaryfunc)complex_int_div, /* nb_floor_divide */
(binaryfunc)complex_div, /* nb_true_divide */
0, /* nb_inplace_floor_divide */
0, /* nb_inplace_true_divide */
};

PyTypeObject PyComplex_Type = {
Expand Down
45 changes: 32 additions & 13 deletions Objects/floatobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,21 @@ float_pow(PyObject *v, PyObject *w, PyObject *z)
return PyFloat_FromDouble(ix);
}

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)
{
Expand Down Expand Up @@ -678,19 +693,23 @@ static PyNumberMethods float_as_number = {
(unaryfunc)float_int, /*nb_int*/
(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*/
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 */
};

PyTypeObject PyFloat_Type = {
Expand Down
10 changes: 10 additions & 0 deletions Objects/intobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,12 @@ int_or(PyIntObject *v, PyIntObject *w)
return PyInt_FromLong(a | b);
}

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)
{
Expand Down Expand Up @@ -812,6 +818,10 @@ static PyNumberMethods int_as_number = {
0, /*nb_inplace_and*/
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 */
};

PyTypeObject PyInt_Type = {
Expand Down
Loading

0 comments on commit 4668b00

Please sign in to comment.