Author: Spenser Bauman <[email protected]>
Branch: value-classes
Changeset: r87257:261d2a991cda
Date: 2016-09-20 13:01 -0400
http://bitbucket.org/pypy/pypy/changeset/261d2a991cda/
Log: _value_class_=True implies _immutable_=True for now May wish to do
away with _immutable_ in the future
diff --git a/rpython/rtyper/rclass.py b/rpython/rtyper/rclass.py
--- a/rpython/rtyper/rclass.py
+++ b/rpython/rtyper/rclass.py
@@ -545,6 +545,14 @@
hints = hints.copy()
classdesc = self.classdef.classdesc
immut = classdesc.get_param('_immutable_', inherit=False)
+ value_class = classdesc.get_param('_value_class_', inherit=False)
+
+ if immut is None:
+ immut = value_class
+ elif value_class is not None and value_class and not immut:
+ raise ImmutableConflictError(
+ "class %r: _immutable_ != True and _value_class_ = True")
+
if immut is None:
if classdesc.get_param('_immutable_', inherit=True):
raise ImmutableConflictError(
@@ -568,7 +576,7 @@
"""Look for value class hints in the class heirarchy to extract the
proper
hints and ensure consistency of the _value_class_ annotation. This is
mostly equivalent to _check_for_immutable_hints except that
- _value_class_=True requires _immutable_=True as well."""
+ _value_class_=True imples _immutable_=True as well."""
hints = hints.copy()
classdesc = self.classdef.classdesc
value_class = classdesc.get_param('_value_class_', inherit=False)
@@ -582,12 +590,14 @@
raise TyperError(
"class %r: _value_class_ = something else than True" % (
self.classdef,))
- elif not hints.get('immutable', False):
- raise ValueClassConflictError(
- "class %r: _value_class_ = True requires that "
- "_immutable_ = True as well")
+ # elif not hints.get('immutable', False):
+ # raise ValueClassConflictError(
+ # "class %r: _value_class_ = True requires that "
+ # "_immutable_ = True as well")
else:
+ # _value_class_ = True implies _immutable_ = True
hints['value_class'] = True
+ hints['immutable'] = True
return hints
def __repr__(self):
diff --git a/rpython/rtyper/test/test_rclass.py
b/rpython/rtyper/test/test_rclass.py
--- a/rpython/rtyper/test/test_rclass.py
+++ b/rpython/rtyper/test/test_rclass.py
@@ -1309,7 +1309,6 @@
def test_value_class(self):
class I(object):
- _immutable_ = True
_value_class_ = True
def __init__(self, v):
@@ -1322,8 +1321,24 @@
t, typer, graph = self.gengraph(f, [], backendopt=True)
assert summary(graph) == {}
- def test_value_class_not_immutable(self):
- from rpython.rtyper.rclass import ValueClassConflictError
+ def test_value_class_conflicts_with_immut(self):
+ from rpython.rtyper.rclass import ImmutableConflictError
+
+ class I(object):
+ _immutable_ = False
+ _value_class_ = True
+
+ def __init__(self, v):
+ self.v = v
+
+ i = I(3)
+ def f():
+ return i.v
+
+ py.test.raises(ImmutableConflictError, self.gengraph, f, [])
+
+ def test_value_class_implies_immutable(self):
+ from rpython.jit.metainterp.typesystem import deref
class I(object):
_value_class_ = True
@@ -1333,15 +1348,17 @@
i = I(3)
def f():
- return i.v
+ return i
- py.test.raises(ValueClassConflictError, self.gengraph, f, [])
+ t, typer, graph = self.gengraph(f, [])
+ I_TYPE = deref(graph.getreturnvar().concretetype)
+ assert I_TYPE._hints['immutable']
+ assert I_TYPE._hints['value_class']
def test_value_class_subclass_not_value_class(self):
from rpython.rtyper.rclass import ValueClassConflictError
class Base(object):
- _immutable_ = True
_value_class_ = True
class I(Base):
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit