SF bug #488514: -Qnew needs work

Big Hammer to implement -Qnew as PEP 238 says it should work (a global
option affecting all instances of "/").

pydebug.h, main.c, pythonrun.c:  define a private _Py_QnewFlag flag, true
iff -Qnew is passed on the command line.  This should go away (as the
comments say) when true division becomes The Rule.  This is
deliberately not exposed to runtime inspection or modification:  it's
a one-way one-shot switch to pretend you're using Python 3.

ceval.c:  when _Py_QnewFlag is set, treat BINARY_DIVIDE as
BINARY_TRUE_DIVIDE.

test_{descr, generators, zipfile}.py:  fiddle so these pass under
-Qnew too.  This was just a matter of s!/!//! in test_generators and
test_zipfile.  test_descr was trickier, as testbinop() is passed
assumptions that "/" is the same as calling a "__div__" method; put
a temporary hack there to call "__truediv__" instead when the method
name is "__div__" and 1/2 evaluates to 0.5.

Three standard tests still fail under -Qnew (on Windows; somebody
please try the Linux tests with -Qnew too!  Linux runs a whole bunch
of tests Windows doesn't):
    test_augassign
    test_class
    test_coercion
I can't stay awake longer to stare at this (be my guest).  Offhand
cures weren't obvious, nor was it even obvious that cures are possible
without major hackery.

Question:  when -Qnew is in effect, should calls to __div__ magically
change into calls to __truediv__?  See "major hackery" at tail end of
last paragraph <wink>.
This commit is contained in:
Tim Peters 2001-12-06 06:23:26 +00:00
parent e50959a58e
commit 3caca2326e
8 changed files with 55 additions and 25 deletions

View file

@ -23,6 +23,11 @@ def testunop(a, res, expr="len(a)", meth="__len__"):
def testbinop(a, b, res, expr="a+b", meth="__add__"):
if verbose: print "checking", expr
dict = {'a': a, 'b': b}
# XXX Hack so this passes before 2.3 when -Qnew is specified.
if meth == "__div__" and 1/2 == 0.5:
meth = "__truediv__"
vereq(eval(expr, dict), res)
t = type(a)
m = getattr(t, meth)

View file

@ -182,7 +182,7 @@ Specification: Return
Specification: Generators and Exception Propagation
>>> def f():
... return 1/0
... return 1//0
>>> def g():
... yield f() # the zero division exception propagates
... yield 42 # and we'll never get here
@ -206,7 +206,7 @@ Specification: Try/Except/Finally
... yield 1
... try:
... yield 2
... 1/0
... 1//0
... yield 3 # never get here
... except ZeroDivisionError:
... yield 4
@ -253,7 +253,7 @@ Guido's binary tree example.
... n = len(list)
... if n == 0:
... return []
... i = n / 2
... i = n // 2
... return Tree(list[i], tree(list[:i]), tree(list[i+1:]))
>>> # Show it off: create a tree.
@ -691,7 +691,7 @@ SyntaxError: 'yield' not allowed in a 'try' block with a 'finally' clause (<stri
>>> def f():
... try:
... try:
... 1/0
... 1//0
... except ZeroDivisionError:
... yield 666 # bad because *outer* try has finally
... except:
@ -708,7 +708,7 @@ But this is fine:
... try:
... try:
... yield 12
... 1/0
... 1//0
... except ZeroDivisionError:
... yield 666
... except:
@ -751,7 +751,7 @@ SyntaxError: invalid syntax
... pass
... elif 0:
... try:
... 1/0
... 1//0
... except SyntaxError:
... pass
... else: