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 /Python/compile.c | |
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 'Python/compile.c')
-rw-r--r-- | Python/compile.c | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/Python/compile.c b/Python/compile.c index 31a75bd..d310e35 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1874,14 +1874,20 @@ com_term(struct compiling *c, node *n) op = BINARY_MULTIPLY; break; case SLASH: - op = BINARY_DIVIDE; + if (c->c_flags & CO_FUTURE_DIVISION) + op = BINARY_TRUE_DIVIDE; + else + op = BINARY_DIVIDE; break; case PERCENT: op = BINARY_MODULO; break; + case DOUBLESLASH: + op = BINARY_FLOOR_DIVIDE; + break; default: com_error(c, PyExc_SystemError, - "com_term: operator not *, / or %"); + "com_term: operator not *, /, // or %"); op = 255; } com_addbyte(c, op); @@ -2475,7 +2481,14 @@ com_augassign(struct compiling *c, node *n) switch (STR(CHILD(CHILD(n, 1), 0))[0]) { case '+': opcode = INPLACE_ADD; break; case '-': opcode = INPLACE_SUBTRACT; break; - case '/': opcode = INPLACE_DIVIDE; break; + case '/': + if (STR(CHILD(CHILD(n, 1), 0))[1] == '/') + opcode = INPLACE_FLOOR_DIVIDE; + else if (c->c_flags & CO_FUTURE_DIVISION) + opcode = INPLACE_TRUE_DIVIDE; + else + opcode = INPLACE_DIVIDE; + break; case '%': opcode = INPLACE_MODULO; break; case '<': opcode = INPLACE_LSHIFT; break; case '>': opcode = INPLACE_RSHIFT; break; @@ -3945,7 +3958,8 @@ jcompile(node *n, char *filename, struct compiling *base, if (base->c_nested || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION)) sc.c_nested = 1; - sc.c_flags |= base->c_flags & CO_GENERATOR_ALLOWED; + sc.c_flags |= base->c_flags & (CO_GENERATOR_ALLOWED | + CO_FUTURE_DIVISION); } else { sc.c_private = NULL; sc.c_future = PyNode_Future(n, filename); @@ -3963,6 +3977,11 @@ jcompile(node *n, char *filename, struct compiling *base, sc.c_future->ff_generators = 1; else if (sc.c_future->ff_generators) flags->cf_flags |= PyCF_GENERATORS; + + if (flags->cf_flags & PyCF_DIVISION) + sc.c_future->ff_division = 1; + else if (sc.c_future->ff_division) + flags->cf_flags |= PyCF_DIVISION; } if (symtable_build(&sc, n) < 0) { com_free(&sc); @@ -4437,6 +4456,8 @@ symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste, c->c_flags |= CO_NESTED; if (c->c_future->ff_generators) c->c_flags |= CO_GENERATOR_ALLOWED; + if (c->c_future->ff_division) + c->c_flags |= CO_FUTURE_DIVISION; } if (ste->ste_generator) c->c_flags |= CO_GENERATOR; |