Author: Carl Friedrich Bolz-Tereick <[email protected]>
Branch: py3.6
Changeset: r96131:438c53ddd510
Date: 2019-02-22 17:00 +0100
http://bitbucket.org/pypy/pypy/changeset/438c53ddd510/
Log: follow more closely the complicated logic that CPython uses for code
object equality
diff --git a/pypy/interpreter/pycode.py b/pypy/interpreter/pycode.py
--- a/pypy/interpreter/pycode.py
+++ b/pypy/interpreter/pycode.py
@@ -350,7 +350,7 @@
return space.w_False
for i in range(len(self.co_consts_w)):
- if not space.eq_w(self.co_consts_w[i], w_other.co_consts_w[i]):
+ if not _code_const_eq(space, self.co_consts_w[i],
w_other.co_consts_w[i]):
return space.w_False
return space.w_True
@@ -454,3 +454,35 @@
return space.newtext(b'<code object %s at 0x%s, file "%s", line %d>' %
(
name, self.getaddrstring(space), fn,
-1 if self.co_firstlineno == 0 else self.co_firstlineno))
+
+def _code_const_eq(space, w_a, w_b):
+ # this is a mess! CPython has complicated logic for this. essentially this
+ # is supposed to be a "strong" equal, that takes types and signs of numbers
+ # into account, quite similar to how PyPy's 'is' behaves, but recursively
+ # in tuples and frozensets as well. Since PyPy already implements these
+ # rules correctly for ints, floats, bools, complex in 'is' and 'id', just
+ # use those.
+ return space.eq_w(_convert_const(space, w_a), _convert_const(space, w_b))
+
+def _convert_const(space, w_a):
+ # use id to convert constants. for tuples and frozensets use tuples and
+ # frozensets of converted contents.
+ w_type = space.type(w_a)
+ if space.is_w(w_type, space.w_unicode):
+ # unicodes are supposed to compare by value
+ return w_a
+ if isinstance(w_a, PyCode):
+ # for code objects we use the logic recursively
+ return w_a
+ # for tuples and frozensets convert recursively
+ if space.is_w(w_type, space.w_tuple):
+ elements_w = [_convert_const(space, w_x)
+ for w_x in space.unpackiterable(w_a)]
+ return space.newtuple(elements_w)
+ if space.is_w(w_type, space.w_frozenset):
+ elements_w = [_convert_const(space, w_x)
+ for w_x in space.unpackiterable(w_a)]
+ return space.newfrozenset(elements_w)
+ # use id for the rest
+ return space.id(w_a)
+
diff --git a/pypy/interpreter/test/test_compiler.py
b/pypy/interpreter/test/test_compiler.py
--- a/pypy/interpreter/test/test_compiler.py
+++ b/pypy/interpreter/test/test_compiler.py
@@ -1053,7 +1053,7 @@
assert d['c1'] == tuple(sorted(d['c1']))
assert d['r1'] == d['r2'] == d['c1']
- def test_ast_equality(self):
+ def test_code_equality(self):
import _ast
sample_code = [
['<assign>', 'x = 5'],
diff --git a/pypy/interpreter/test/test_pycode.py
b/pypy/interpreter/test/test_pycode.py
--- a/pypy/interpreter/test/test_pycode.py
+++ b/pypy/interpreter/test/test_pycode.py
@@ -1,4 +1,5 @@
import sys, StringIO
+from pypy.interpreter.pycode import _code_const_eq
def test_dump(space):
"""test that pycode.dump kind of works with py3 opcodes"""
@@ -17,3 +18,54 @@
assert ' 0 (7)' in output
assert ' 4 (None)' in output
assert ' 16 RETURN_VALUE' in output
+
+
+def test_strong_const_equal(space):
+ # test that the stronger equal that code objects are supposed to use for
+ # consts works
+ s = 'Python'
+ values = [
+ space.newint(1),
+ space.newfloat(0.0),
+ space.newfloat(-0.0),
+ space.newfloat(1.0),
+ space.newfloat(-1.0),
+ space.w_True,
+ space.w_False,
+ space.w_None,
+ space.w_Ellipsis,
+ space.newcomplex(0.0, 0.0),
+ space.newcomplex(0.0, -0.0),
+ space.newcomplex(-0.0, 0.0),
+ space.newcomplex(-0.0, -0.0),
+ space.newcomplex(1.0, 1.0),
+ space.newcomplex(1.0, -1.0),
+ space.newcomplex(-1.0, 1.0),
+ space.newcomplex(-1.0, -1.0),
+ space.newfrozenset(),
+ space.newtuple([]),
+ space.newutf8(s, len(s)),
+ ]
+ for w_a in values:
+ assert _code_const_eq(space, w_a, w_a)
+ assert _code_const_eq(space, space.newtuple([w_a]),
+ space.newtuple([w_a]))
+ assert _code_const_eq(space, space.newfrozenset([w_a]),
+ space.newfrozenset([w_a]))
+ for w_a in values:
+ for w_b in values:
+ if w_a is w_b:
+ continue
+ assert not _code_const_eq(space, w_a, w_b)
+ assert _code_const_eq(space, space.newtuple([w_a, w_b]),
+ space.newtuple([w_a, w_b]))
+ assert not _code_const_eq(space, space.newtuple([w_a]),
+ space.newtuple([w_b]))
+ assert not _code_const_eq(space, space.newtuple([w_a, w_b]),
+ space.newtuple([w_b, w_a]))
+ assert not _code_const_eq(space, space.newfrozenset([w_a]),
+ space.newfrozenset([w_b]))
+ s1 = 'Python' + str(1) + str(1)
+ s2 = 'Python' + str(11)
+ assert _code_const_eq(space, space.newutf8(s1, len(s1)),
+ space.newutf8(s2, len(s2)))
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit