Skip to content

Commit 31bb749

Browse files
author
neal.norwitz
committed
Get rid of most of the rest of coerce (slot is still there for now).
git-svn-id: http://svn.python.org/projects/python/branches/p3yk@51431 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 4b2b233 commit 31bb749

19 files changed

Lines changed: 131 additions & 497 deletions

BROKEN

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,31 @@ Traceback (most recent call last):
110110
File "../Lib/test/test_set.py", line 291, in test_remove
111111
self.assert_(self.thetype(self.word) in s)
112112
AssertionError
113+
114+
////////////////////////////////////////////////////////////////////////
115+
test_compare
116+
////////////////////////////////////////////////////////////////////////
117+
test test_compare failed -- Traceback (most recent call last):
118+
File "/Users/nnorwitz/build/python/py3k.2/Lib/test/test_compare.py", line 28, in test_comparisons
119+
self.assertEqual(a, b)
120+
AssertionError: 2 != (2+0j)
121+
122+
////////////////////////////////////////////////////////////////////////
123+
test_complex
124+
////////////////////////////////////////////////////////////////////////
125+
======================================================================
126+
FAIL: test_pow (test.test_complex.ComplexTest)
127+
----------------------------------------------------------------------
128+
Traceback (most recent call last):
129+
File "/Users/nnorwitz/build/python/py3k.2/Lib/test/test_complex.py", line 130, in test_pow
130+
self.assertEqual(a ** 0j, 1)
131+
AssertionError: (1+0j) != 1
132+
133+
======================================================================
134+
FAIL: test_richcompare (test.test_complex.ComplexTest)
135+
----------------------------------------------------------------------
136+
Traceback (most recent call last):
137+
File "/Users/nnorwitz/build/python/py3k.2/Lib/test/test_complex.py", line 96, in test_richcompare
138+
self.assertRaises(OverflowError, complex.__eq__, 1+1j, 1L<<10000)
139+
AssertionError: OverflowError not raised
140+

Doc/api/abstract.tex

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -654,20 +654,6 @@ \section{Number Protocol \label{number}}
654654
statement \samp{\var{o1} |= \var{o2}}.
655655
\end{cfuncdesc}
656656

657-
\begin{cfuncdesc}{int}{PyNumber_Coerce}{PyObject **p1, PyObject **p2}
658-
This function takes the addresses of two variables of type
659-
\ctype{PyObject*}. If the objects pointed to by \code{*\var{p1}}
660-
and \code{*\var{p2}} have the same type, increment their reference
661-
count and return \code{0} (success). If the objects can be converted
662-
to a common numeric type, replace \code{*p1} and \code{*p2} by their
663-
converted value (with 'new' reference counts), and return \code{0}.
664-
If no conversion is possible, or if some other error occurs, return
665-
\code{-1} (failure) and don't increment the reference counts. The
666-
call \code{PyNumber_Coerce(\&o1, \&o2)} is equivalent to the Python
667-
statement \samp{\var{o1}, \var{o2} = coerce(\var{o1}, \var{o2})}.
668-
\bifuncindex{coerce}
669-
\end{cfuncdesc}
670-
671657
\begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o}
672658
Returns the \var{o} converted to an integer object on success, or
673659
\NULL{} on failure. If the argument is outside the integer range

Doc/ref/ref3.tex

Lines changed: 2 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,7 +1645,7 @@ \subsection{Emulating container types\label{sequence-types}}
16451645
multiplication (meaning repetition) by defining the methods
16461646
\method{__add__()}, \method{__radd__()}, \method{__iadd__()},
16471647
\method{__mul__()}, \method{__rmul__()} and \method{__imul__()} described
1648-
below; they should not define \method{__coerce__()} or other numerical
1648+
below; they should not define other numerical
16491649
operators. It is recommended that both mappings and sequences
16501650
implement the \method{__contains__()} method to allow efficient use of
16511651
the \code{in} operator; for mappings, \code{in} should be equivalent
@@ -1689,7 +1689,7 @@ \subsection{Emulating container types\label{sequence-types}}
16891689
\ttindex{__imul__()}
16901690
\ttindex{__contains__()}
16911691
\ttindex{__iter__()}}
1692-
\withsubitem{(numeric object method)}{\ttindex{__coerce__()}}
1692+
\withsubitem{(numeric object method)}
16931693

16941694
\begin{methoddesc}[container object]{__len__}{self}
16951695
Called to implement the built-in function
@@ -2012,129 +2012,6 @@ \subsection{Emulating numeric types\label{numeric-types}}
20122012
\versionadded{2.5}
20132013
\end{methoddesc}
20142014

2015-
\begin{methoddesc}[numeric object]{__coerce__}{self, other}
2016-
Called to implement ``mixed-mode'' numeric arithmetic. Should either
2017-
return a 2-tuple containing \var{self} and \var{other} converted to
2018-
a common numeric type, or \code{None} if conversion is impossible. When
2019-
the common type would be the type of \code{other}, it is sufficient to
2020-
return \code{None}, since the interpreter will also ask the other
2021-
object to attempt a coercion (but sometimes, if the implementation of
2022-
the other type cannot be changed, it is useful to do the conversion to
2023-
the other type here). A return value of \code{NotImplemented} is
2024-
equivalent to returning \code{None}.
2025-
\end{methoddesc}
2026-
2027-
\subsection{Coercion rules\label{coercion-rules}}
2028-
2029-
This section used to document the rules for coercion. As the language
2030-
has evolved, the coercion rules have become hard to document
2031-
precisely; documenting what one version of one particular
2032-
implementation does is undesirable. Instead, here are some informal
2033-
guidelines regarding coercion. In Python 3.0, coercion will not be
2034-
supported.
2035-
2036-
\begin{itemize}
2037-
2038-
\item
2039-
2040-
If the left operand of a \% operator is a string or Unicode object, no
2041-
coercion takes place and the string formatting operation is invoked
2042-
instead.
2043-
2044-
\item
2045-
2046-
It is no longer recommended to define a coercion operation.
2047-
Mixed-mode operations on types that don't define coercion pass the
2048-
original arguments to the operation.
2049-
2050-
\item
2051-
2052-
New-style classes (those derived from \class{object}) never invoke the
2053-
\method{__coerce__()} method in response to a binary operator; the only
2054-
time \method{__coerce__()} is invoked is when the built-in function
2055-
\function{coerce()} is called.
2056-
2057-
\item
2058-
2059-
For most intents and purposes, an operator that returns
2060-
\code{NotImplemented} is treated the same as one that is not
2061-
implemented at all.
2062-
2063-
\item
2064-
2065-
Below, \method{__op__()} and \method{__rop__()} are used to signify
2066-
the generic method names corresponding to an operator;
2067-
\method{__iop__()} is used for the corresponding in-place operator. For
2068-
example, for the operator `\code{+}', \method{__add__()} and
2069-
\method{__radd__()} are used for the left and right variant of the
2070-
binary operator, and \method{__iadd__()} for the in-place variant.
2071-
2072-
\item
2073-
2074-
For objects \var{x} and \var{y}, first \code{\var{x}.__op__(\var{y})}
2075-
is tried. If this is not implemented or returns \code{NotImplemented},
2076-
\code{\var{y}.__rop__(\var{x})} is tried. If this is also not
2077-
implemented or returns \code{NotImplemented}, a \exception{TypeError}
2078-
exception is raised. But see the following exception:
2079-
2080-
\item
2081-
2082-
Exception to the previous item: if the left operand is an instance of
2083-
a built-in type or a new-style class, and the right operand is an instance
2084-
of a proper subclass of that type or class and overrides the base's
2085-
\method{__rop__()} method, the right operand's \method{__rop__()} method
2086-
is tried \emph{before} the left operand's \method{__op__()} method.
2087-
2088-
This is done so that a subclass can completely override binary operators.
2089-
Otherwise, the left operand's \method{__op__()} method would always
2090-
accept the right operand: when an instance of a given class is expected,
2091-
an instance of a subclass of that class is always acceptable.
2092-
2093-
\item
2094-
2095-
When either operand type defines a coercion, this coercion is called
2096-
before that type's \method{__op__()} or \method{__rop__()} method is
2097-
called, but no sooner. If the coercion returns an object of a
2098-
different type for the operand whose coercion is invoked, part of the
2099-
process is redone using the new object.
2100-
2101-
\item
2102-
2103-
When an in-place operator (like `\code{+=}') is used, if the left
2104-
operand implements \method{__iop__()}, it is invoked without any
2105-
coercion. When the operation falls back to \method{__op__()} and/or
2106-
\method{__rop__()}, the normal coercion rules apply.
2107-
2108-
\item
2109-
2110-
In \var{x}\code{+}\var{y}, if \var{x} is a sequence that implements
2111-
sequence concatenation, sequence concatenation is invoked.
2112-
2113-
\item
2114-
2115-
In \var{x}\code{*}\var{y}, if one operator is a sequence that
2116-
implements sequence repetition, and the other is an integer
2117-
(\class{int} or \class{long}), sequence repetition is invoked.
2118-
2119-
\item
2120-
2121-
Rich comparisons (implemented by methods \method{__eq__()} and so on)
2122-
never use coercion. Three-way comparison (implemented by
2123-
\method{__cmp__()}) does use coercion under the same conditions as
2124-
other binary operations use it.
2125-
2126-
\item
2127-
2128-
In the current implementation, the built-in numeric types \class{int},
2129-
\class{long} and \class{float} do not use coercion; the type
2130-
\class{complex} however does use it. The difference can become
2131-
apparent when subclassing these types. Over time, the type
2132-
\class{complex} may be fixed to avoid coercion. All these types
2133-
implement a \method{__coerce__()} method, for use by the built-in
2134-
\function{coerce()} function.
2135-
2136-
\end{itemize}
2137-
21382015
\subsection{With Statement Context Managers\label{context-managers}}
21392016

21402017
\versionadded{2.5}

Doc/tut/glossary.tex

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,6 @@ \chapter{Glossary\label{glossary}}
3535
Any class which does not inherit from \class{object}. See
3636
\emph{new-style class}.
3737

38-
\index{coercion}
39-
\item[coercion]
40-
The implicit conversion of an instance of one type to another during an
41-
operation which involves two arguments of the same type. For example,
42-
{}\code{int(3.15)} converts the floating point number to the integer
43-
{}\code{3}, but in {}\code{3+4.5}, each argument is of a different type (one
44-
int, one float), and both must be converted to the same type before they can
45-
be added or it will raise a {}\code{TypeError}. Coercion between two
46-
operands can be performed with the {}\code{coerce} builtin function; thus,
47-
{}\code{3+4.5} is equivalent to calling {}\code{operator.add(*coerce(3,
48-
4.5))} and results in {}\code{operator.add(3.0, 4.5)}. Without coercion,
49-
all arguments of even compatible types would have to be normalized to the
50-
same value by the programmer, e.g., {}\code{float(3)+4.5} rather than just
51-
{}\code{3+4.5}.
52-
5338
\index{complex number}
5439
\item[complex number]
5540
An extension of the familiar real number system in which all numbers are
@@ -106,17 +91,14 @@ \chapter{Glossary\label{glossary}}
10691
\index{__future__}
10792
\item[__future__]
10893
A pseudo module which programmers can use to enable new language
109-
features which are not compatible with the current interpreter. For
110-
example, the expression \code{11/4} currently evaluates to \code{2}.
111-
If the module in which it is executed had enabled \emph{true division}
112-
by executing:
94+
features which are not compatible with the current interpreter.
95+
To enable \code{new_feature}
11396

11497
\begin{verbatim}
115-
from __future__ import division
98+
from __future__ import new_feature
11699
\end{verbatim}
117100

118-
the expression \code{11/4} would evaluate to \code{2.75}. By
119-
importing the \ulink{\module{__future__}}{../lib/module-future.html}
101+
By importing the \ulink{\module{__future__}}{../lib/module-future.html}
120102
module and evaluating its variables, you can see when a new feature
121103
was first added to the language and when it will become the default:
122104

@@ -183,17 +165,10 @@ \chapter{Glossary\label{glossary}}
183165

184166
\index{integer division}
185167
\item[integer division]
186-
Mathematical division discarding any remainder. For example, the
187-
expression \code{11/4} currently evaluates to \code{2} in contrast
188-
to the \code{2.75} returned by float division. Also called
189-
{}\emph{floor division}. When dividing two integers the outcome will
190-
always be another integer (having the floor function applied to it).
191-
However, if one of the operands is another numeric type (such as a
192-
{}\class{float}), the result will be coerced (see \emph{coercion}) to
193-
a common type. For example, an integer divided by a float will result
194-
in a float value, possibly with a decimal fraction. Integer division
195-
can be forced by using the \code{//} operator instead of the \code{/}
196-
operator. See also \emph{__future__}.
168+
Mathematical division including any remainder. The result will always
169+
be a float. For example, the expression \code{11/4} evaluates to \code{2.75}.
170+
Integer division can be forced by using the \code{//} operator instead
171+
of the \code{/} operator.
197172

198173
\index{interactive}
199174
\item[interactive]

Include/abstract.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -694,24 +694,6 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
694694
expression: o1|o2.
695695
*/
696696

697-
/* Implemented elsewhere:
698-
699-
int PyNumber_Coerce(PyObject **p1, PyObject **p2);
700-
701-
This function takes the addresses of two variables of type
702-
PyObject*.
703-
704-
If the objects pointed to by *p1 and *p2 have the same type,
705-
increment their reference count and return 0 (success).
706-
If the objects can be converted to a common numeric type,
707-
replace *p1 and *p2 by their converted value (with 'new'
708-
reference counts), and return 0.
709-
If no conversion is possible, or if some other error occurs,
710-
return -1 (failure) and don't increment the reference counts.
711-
The call PyNumber_Coerce(&o1, &o2) is equivalent to the Python
712-
statement o1, o2 = coerce(o1, o2).
713-
*/
714-
715697
PyAPI_FUNC(Py_ssize_t) PyNumber_Index(PyObject *);
716698

717699
/*

Include/object.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,6 @@ PyAPI_FUNC(long) PyObject_Hash(PyObject *);
393393
PyAPI_FUNC(int) PyObject_IsTrue(PyObject *);
394394
PyAPI_FUNC(int) PyObject_Not(PyObject *);
395395
PyAPI_FUNC(int) PyCallable_Check(PyObject *);
396-
PyAPI_FUNC(int) PyNumber_Coerce(PyObject **, PyObject **);
397396
PyAPI_FUNC(int) PyNumber_CoerceEx(PyObject **, PyObject **);
398397

399398
PyAPI_FUNC(void) PyObject_ClearWeakRefs(PyObject *);

Lib/test/test_builtin.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -196,17 +196,6 @@ def test_cmp(self):
196196
a.pop(); b.pop(); c.pop()
197197
self.assertRaises(TypeError, cmp)
198198

199-
def test_coerce(self):
200-
self.assert_(not fcmp(coerce(1, 1.1), (1.0, 1.1)))
201-
self.assertEqual(coerce(1, 1L), (1L, 1L))
202-
self.assert_(not fcmp(coerce(1L, 1.1), (1.0, 1.1)))
203-
self.assertRaises(TypeError, coerce)
204-
class BadNumber:
205-
def __coerce__(self, other):
206-
raise ValueError
207-
self.assertRaises(ValueError, coerce, 42, BadNumber())
208-
self.assertRaises(OverflowError, coerce, 0.5, int("12345" * 1000))
209-
210199
def test_compile(self):
211200
compile('print 1\n', '', 'exec')
212201
bom = '\xef\xbb\xbf'

Lib/test/test_complex.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,6 @@ def test_floordiv(self):
9292
self.assertAlmostEqual(complex.__floordiv__(3+0j, 1.5+0j), 2)
9393
self.assertRaises(ZeroDivisionError, complex.__floordiv__, 3+0j, 0+0j)
9494

95-
def test_coerce(self):
96-
self.assertRaises(OverflowError, complex.__coerce__, 1+1j, 1L<<10000)
97-
9895
def test_richcompare(self):
9996
self.assertRaises(OverflowError, complex.__eq__, 1+1j, 1L<<10000)
10097
self.assertEqual(complex.__lt__(1+1j, None), NotImplemented)

Lib/test/test_descr.py

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2567,33 +2567,6 @@ def __ge__(self, other):
25672567
verify(eval("x %s c[y]" % op) == eval("x %s y" % op),
25682568
"x=%d, y=%d" % (x, y))
25692569

2570-
def coercions():
2571-
if verbose: print "Testing coercions..."
2572-
class I(int): pass
2573-
coerce(I(0), 0)
2574-
coerce(0, I(0))
2575-
class L(long): pass
2576-
coerce(L(0), 0)
2577-
coerce(L(0), 0L)
2578-
coerce(0, L(0))
2579-
coerce(0L, L(0))
2580-
class F(float): pass
2581-
coerce(F(0), 0)
2582-
coerce(F(0), 0L)
2583-
coerce(F(0), 0.)
2584-
coerce(0, F(0))
2585-
coerce(0L, F(0))
2586-
coerce(0., F(0))
2587-
class C(complex): pass
2588-
coerce(C(0), 0)
2589-
coerce(C(0), 0L)
2590-
coerce(C(0), 0.)
2591-
coerce(C(0), 0j)
2592-
coerce(0, C(0))
2593-
coerce(0L, C(0))
2594-
coerce(0., C(0))
2595-
coerce(0j, C(0))
2596-
25972570
def descrdoc():
25982571
if verbose: print "Testing descriptor doc strings..."
25992572
def check(descr, what):
@@ -3961,11 +3934,8 @@ def check(expr, x, y):
39613934
('__and__', 'x & y', 'x &= y'),
39623935
('__or__', 'x | y', 'x |= y'),
39633936
('__xor__', 'x ^ y', 'x ^= y'),
3964-
('__coerce__', 'coerce(x, y)', None)]:
3965-
if name == '__coerce__':
3966-
rname = name
3967-
else:
3968-
rname = '__r' + name[2:]
3937+
]:
3938+
rname = '__r' + name[2:]
39693939
A = metaclass('A', (), {name: specialmethod})
39703940
B = metaclass('B', (), {rname: specialmethod})
39713941
a = A()
@@ -4043,7 +4013,6 @@ def test_main():
40434013
str_subclass_as_dict_key()
40444014
classic_comparisons()
40454015
rich_comparisons()
4046-
coercions()
40474016
descrdoc()
40484017
setclass()
40494018
setdict()

0 commit comments

Comments
 (0)