Merge lp:~gz/brz/py3_static_tuple_mismatched_types into lp:brz

Proposed by Martin Packman
Status: Merged
Approved by: Martin Packman
Approved revision: no longer in the source branch.
Merge reported by: The Breezy Bot
Merged at revision: not available
Proposed branch: lp:~gz/brz/py3_static_tuple_mismatched_types
Merge into: lp:brz
Prerequisite: lp:~gz/brz/py3_static_tuple_import
Diff against target: 163 lines (+47/-28)
2 files modified
breezy/_static_tuple_c.c (+15/-7)
breezy/tests/test__static_tuple.py (+32/-21)
To merge this branch: bzr merge lp:~gz/brz/py3_static_tuple_mismatched_types
Reviewer Review Type Date Requested Status
Jelmer Vernooij Approve
Review via email: mp+326330@code.launchpad.net

Commit message

Make StaticTuple tests pass on Python 3

Description of the change

Fix remaining StaticTuple test failures on Python 3.

All related to the change in semantics of lt/gt comparisons with mismatched types. I'm hoping we don't actually have core logic trying to sort tuples with different contents anywhere.

To post a comment you must log in.
Revision history for this message
Jelmer Vernooij (jelmer) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'breezy/_static_tuple_c.c'
--- breezy/_static_tuple_c.c 2017-06-25 23:37:57 +0000
+++ breezy/_static_tuple_c.c 2017-06-27 01:23:29 +0000
@@ -428,14 +428,22 @@
428 } else if (w == Py_None) {428 } else if (w == Py_None) {
429 // None is always less than the object429 // None is always less than the object
430 switch (op) {430 switch (op) {
431 case Py_NE:case Py_GT:case Py_GE:431 case Py_NE:
432#if PY_MAJOR_VERSION >= 3
433#else
434 case Py_GT:case Py_GE:
435#endif
432 Py_INCREF(Py_True);436 Py_INCREF(Py_True);
433 return Py_True;437 return Py_True;
434 case Py_EQ:case Py_LT:case Py_LE:438 case Py_EQ:
439#if PY_MAJOR_VERSION >= 3
440#else
441 case Py_LT:case Py_LE:
442#endif
435 Py_INCREF(Py_False);443 Py_INCREF(Py_False);
436 return Py_False;444 return Py_False;
437 default: // Should never happen445 default: // Should only happen on Python 3
438 return Py_NotImplemented;446 return Py_NotImplemented;
439 }447 }
440 } else {448 } else {
441 /* We don't special case this comparison, we just let python handle449 /* We don't special case this comparison, we just let python handle
@@ -725,10 +733,10 @@
725static PyObject *733static PyObject *
726StaticTuple_sizeof(StaticTuple *self)734StaticTuple_sizeof(StaticTuple *self)
727{735{
728 Py_ssize_t res;736 Py_ssize_t res;
729737
730 res = _PyObject_SIZE(&StaticTuple_Type) + (int)self->size * sizeof(void*);738 res = _PyObject_SIZE(&StaticTuple_Type) + (int)self->size * sizeof(void*);
731 return PyInt_FromSsize_t(res);739 return PyInt_FromSsize_t(res);
732}740}
733741
734742
735743
=== modified file 'breezy/tests/test__static_tuple.py'
--- breezy/tests/test__static_tuple.py 2017-06-22 00:41:56 +0000
+++ breezy/tests/test__static_tuple.py 2017-06-27 01:23:29 +0000
@@ -20,6 +20,7 @@
20 import cPickle as pickle20 import cPickle as pickle
21except ImportError:21except ImportError:
22 import pickle22 import pickle
23import operator
23import sys24import sys
2425
25from breezy import (26from breezy import (
@@ -287,15 +288,24 @@
287 k6 = self.module.StaticTuple(k3, k4)288 k6 = self.module.StaticTuple(k3, k4)
288 self.assertCompareEqual(k5, k6)289 self.assertCompareEqual(k5, k6)
289290
290 def assertCompareDifferent(self, k_small, k_big):291 def check_strict_compare(self, k1, k2, mismatched_types):
292 """True if on Python 3 and stricter comparison semantics are used."""
293 if PY3 and mismatched_types:
294 for op in ("ge", "gt", "le", "lt"):
295 self.assertRaises(TypeError, getattr(operator, op), k1, k2)
296 return True
297 return False
298
299 def assertCompareDifferent(self, k_small, k_big, mismatched_types=False):
291 self.assertFalse(k_small == k_big)300 self.assertFalse(k_small == k_big)
292 self.assertFalse(k_small >= k_big)
293 self.assertFalse(k_small > k_big)
294 self.assertTrue(k_small != k_big)301 self.assertTrue(k_small != k_big)
295 self.assertTrue(k_small <= k_big)302 if not self.check_strict_compare(k_small, k_big, mismatched_types):
296 self.assertTrue(k_small < k_big)303 self.assertFalse(k_small >= k_big)
304 self.assertFalse(k_small > k_big)
305 self.assertTrue(k_small <= k_big)
306 self.assertTrue(k_small < k_big)
297307
298 def assertCompareNoRelation(self, k1, k2):308 def assertCompareNoRelation(self, k1, k2, mismatched_types=False):
299 """Run the comparison operators, make sure they do something.309 """Run the comparison operators, make sure they do something.
300310
301 However, we don't actually care what comes first or second. This is311 However, we don't actually care what comes first or second. This is
@@ -304,20 +314,21 @@
304 """314 """
305 self.assertFalse(k1 == k2)315 self.assertFalse(k1 == k2)
306 self.assertTrue(k1 != k2)316 self.assertTrue(k1 != k2)
307 # Do the comparison, but we don't care about the result317 if not self.check_strict_compare(k1, k2, mismatched_types):
308 k1 >= k2318 # Do the comparison, but we don't care about the result
309 k1 > k2319 k1 >= k2
310 k1 <= k2320 k1 > k2
311 k1 < k2321 k1 <= k2
322 k1 < k2
312323
313 def test_compare_vs_none(self):324 def test_compare_vs_none(self):
314 k1 = self.module.StaticTuple('baz', 'bing')325 k1 = self.module.StaticTuple('baz', 'bing')
315 self.assertCompareDifferent(None, k1)326 self.assertCompareDifferent(None, k1, mismatched_types=True)
316 327
317 def test_compare_cross_class(self):328 def test_compare_cross_class(self):
318 k1 = self.module.StaticTuple('baz', 'bing')329 k1 = self.module.StaticTuple('baz', 'bing')
319 self.assertCompareNoRelation(10, k1)330 self.assertCompareNoRelation(10, k1, mismatched_types=True)
320 self.assertCompareNoRelation('baz', k1)331 self.assertCompareNoRelation('baz', k1, mismatched_types=True)
321332
322 def test_compare_all_different_same_width(self):333 def test_compare_all_different_same_width(self):
323 k1 = self.module.StaticTuple('baz', 'bing')334 k1 = self.module.StaticTuple('baz', 'bing')
@@ -344,8 +355,8 @@
344 k4 = self.module.StaticTuple(k1, k2)355 k4 = self.module.StaticTuple(k1, k2)
345 self.assertCompareDifferent(k3, k4)356 self.assertCompareDifferent(k3, k4)
346 k5 = self.module.StaticTuple('foo', None)357 k5 = self.module.StaticTuple('foo', None)
347 self.assertCompareDifferent(k5, k1)358 self.assertCompareDifferent(k5, k1, mismatched_types=True)
348 self.assertCompareDifferent(k5, k2)359 self.assertCompareDifferent(k5, k2, mismatched_types=True)
349360
350 def test_compare_diff_width(self):361 def test_compare_diff_width(self):
351 k1 = self.module.StaticTuple('foo')362 k1 = self.module.StaticTuple('foo')
@@ -359,13 +370,13 @@
359 k1 = self.module.StaticTuple('foo', 'bar')370 k1 = self.module.StaticTuple('foo', 'bar')
360 k2 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,371 k2 = self.module.StaticTuple('foo', 1, None, u'\xb5', 1.2, 2**65, True,
361 k1)372 k1)
362 self.assertCompareNoRelation(k1, k2)373 self.assertCompareNoRelation(k1, k2, mismatched_types=True)
363 k3 = self.module.StaticTuple('foo')374 k3 = self.module.StaticTuple('foo')
364 self.assertCompareDifferent(k3, k1)375 self.assertCompareDifferent(k3, k1)
365 k4 = self.module.StaticTuple(None)376 k4 = self.module.StaticTuple(None)
366 self.assertCompareDifferent(k4, k1)377 self.assertCompareDifferent(k4, k1, mismatched_types=True)
367 k5 = self.module.StaticTuple(1)378 k5 = self.module.StaticTuple(1)
368 self.assertCompareNoRelation(k1, k5)379 self.assertCompareNoRelation(k1, k5, mismatched_types=True)
369380
370 def test_compare_to_tuples(self):381 def test_compare_to_tuples(self):
371 k1 = self.module.StaticTuple('foo')382 k1 = self.module.StaticTuple('foo')
@@ -381,7 +392,7 @@
381 self.assertCompareDifferent(('foo',), k2)392 self.assertCompareDifferent(('foo',), k2)
382 self.assertCompareDifferent(('foo', 'aaa'), k2)393 self.assertCompareDifferent(('foo', 'aaa'), k2)
383 self.assertCompareDifferent(('baz', 'bing'), k2)394 self.assertCompareDifferent(('baz', 'bing'), k2)
384 self.assertCompareDifferent(('foo', 10), k2)395 self.assertCompareDifferent(('foo', 10), k2, mismatched_types=True)
385396
386 k3 = self.module.StaticTuple(k1, k2)397 k3 = self.module.StaticTuple(k1, k2)
387 self.assertCompareEqual(k3, (('foo',), ('foo', 'bar')))398 self.assertCompareEqual(k3, (('foo',), ('foo', 'bar')))
@@ -397,7 +408,7 @@
397 # This requires comparing a StaticTuple to a 'string', and then408 # This requires comparing a StaticTuple to a 'string', and then
398 # interpreting that value in the next higher StaticTuple. This used to409 # interpreting that value in the next higher StaticTuple. This used to
399 # generate a PyErr_BadIternalCall. We now fall back to *something*.410 # generate a PyErr_BadIternalCall. We now fall back to *something*.
400 self.assertCompareNoRelation(k1, k2)411 self.assertCompareNoRelation(k1, k2, mismatched_types=True)
401412
402 def test_hash(self):413 def test_hash(self):
403 k = self.module.StaticTuple('foo')414 k = self.module.StaticTuple('foo')

Subscribers

People subscribed via source and target branches