diff options
author | Armin Rigo <arigo@tunes.org> | 2004-12-23 22:13:13 (GMT) |
---|---|---|
committer | Armin Rigo <arigo@tunes.org> | 2004-12-23 22:13:13 (GMT) |
commit | a1748131132ebc83f443b2a8ddc5073bfefa504f (patch) | |
tree | 174f8f8bfecdb210261d30a889fb5102f2f86b06 /Lib | |
parent | 0a6864ecc5d0188c5dcaf2b5e7fb725668b13749 (diff) | |
download | cpython-a1748131132ebc83f443b2a8ddc5073bfefa504f.zip cpython-a1748131132ebc83f443b2a8ddc5073bfefa504f.tar.gz cpython-a1748131132ebc83f443b2a8ddc5073bfefa504f.tar.bz2 |
Dima Dorfman's patch for coercion/comparison of C types (patch #995939), with
a minor change after the coercion, to accept two objects not necessarily of
the same type but with the same tp_compare.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_coercion.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/Lib/test/test_coercion.py b/Lib/test/test_coercion.py index 331771f..37d735b 100644 --- a/Lib/test/test_coercion.py +++ b/Lib/test/test_coercion.py @@ -125,9 +125,45 @@ def do_prefix_binops(): else: print '=', format_result(x) +# New-style class version of CoerceNumber +class CoerceTo(object): + def __init__(self, arg): + self.arg = arg + def __coerce__(self, other): + if isinstance(other, CoerceTo): + return self.arg, other.arg + else: + return self.arg, other + +def assert_(expr, msg=None): + if not expr: + raise AssertionError, msg + +def do_cmptypes(): + # Built-in tp_compare slots expect their arguments to have the + # same type, but a user-defined __coerce__ doesn't have to obey. + # SF #980352 + evil_coercer = CoerceTo(42) + # Make sure these don't crash any more + assert_(cmp(u'fish', evil_coercer) != 0) + assert_(cmp(slice(1), evil_coercer) != 0) + # ...but that this still works + class WackyComparer(object): + def __cmp__(self, other): + assert_(other == 42, 'expected evil_coercer, got %r' % other) + return 0 + assert_(cmp(WackyComparer(), evil_coercer) == 0) + # ...and classic classes too, since that code path is a little different + class ClassicWackyComparer: + def __cmp__(self, other): + assert_(other == 42, 'expected evil_coercer, got %r' % other) + return 0 + assert_(cmp(ClassicWackyComparer(), evil_coercer) == 0) + warnings.filterwarnings("ignore", r'complex divmod\(\), // and % are deprecated', DeprecationWarning, r'test.test_coercion$') do_infix_binops() do_prefix_binops() +do_cmptypes() |