Author: Ronan Lamy <[email protected]>
Branch: anntype2
Changeset: r80876:be00d62b05dc
Date: 2015-11-24 04:08 +0000
http://bitbucket.org/pypy/pypy/changeset/be00d62b05dc/
Log: Turn isinstance into a SpaceOperation
diff --git a/rpython/annotator/builtin.py b/rpython/annotator/builtin.py
--- a/rpython/annotator/builtin.py
+++ b/rpython/annotator/builtin.py
@@ -181,14 +181,10 @@
# from bool to int, notice
that isinstance( , bool|int)
# is quite border case for
RPython
r.const = False
+ for v in variables:
+ assert v.annotation == s_obj
+ knowntypedata = defaultdict(dict)
bk = getbookkeeper()
- if variables is None:
- op = bk._find_current_op("simple_call", 3)
- assert op.args[0] == Constant(isinstance)
- variables = [op.args[1]]
- for variable in variables:
- assert bk.annotator.binding(variable) == s_obj
- knowntypedata = defaultdict(dict)
if not hasattr(typ, '_freeze_') and isinstance(s_type, SomePBC):
add_knowntypedata(knowntypedata, True, variables,
bk.valueoftype(typ))
r.set_knowntypedata(knowntypedata)
diff --git a/rpython/annotator/test/test_annrpython.py
b/rpython/annotator/test/test_annrpython.py
--- a/rpython/annotator/test/test_annrpython.py
+++ b/rpython/annotator/test/test_annrpython.py
@@ -1173,6 +1173,13 @@
s = a.build_types(g, [int])
assert s.const == True
+ def test_isinstance_basic(self):
+ def f():
+ return isinstance(IndexError(), type)
+ a = self.RPythonAnnotator()
+ s = a.build_types(f, [])
+ assert s.const == False
+
def test_alloc_like(self):
class Base(object):
pass
diff --git a/rpython/annotator/unaryop.py b/rpython/annotator/unaryop.py
--- a/rpython/annotator/unaryop.py
+++ b/rpython/annotator/unaryop.py
@@ -32,6 +32,12 @@
def type_SomeObject(annotator, v_arg):
return SomeTypeOf([v_arg])
[email protected](SomeObject)
+def isinstance_SomeObject(annotator, v_obj, v_cls):
+ return builtin.builtin_isinstance(annotator.annotation(v_obj),
+ annotator.annotation(v_cls),
+ variables=[v_obj])
+
@op.bool.register(SomeObject)
def bool_SomeObject(annotator, obj):
diff --git a/rpython/flowspace/flowcontext.py b/rpython/flowspace/flowcontext.py
--- a/rpython/flowspace/flowcontext.py
+++ b/rpython/flowspace/flowcontext.py
@@ -597,7 +597,7 @@
Returns an FSException object whose w_value is an instance of w_type.
"""
- w_is_type = op.simple_call(const(isinstance), w_arg1,
const(type)).eval(self)
+ w_is_type = op.isinstance(w_arg1, const(type)).eval(self)
if self.guessbool(w_is_type):
# this is for all cases of the form (Class, something)
if self.guessbool(op.is_(w_arg2, w_None).eval(self)):
diff --git a/rpython/flowspace/generator.py b/rpython/flowspace/generator.py
--- a/rpython/flowspace/generator.py
+++ b/rpython/flowspace/generator.py
@@ -156,8 +156,7 @@
regular_entry_block = Block([Variable('entry')])
block = regular_entry_block
for Resume in mappings:
- op_check = op.simple_call(
- const(isinstance), block.inputargs[0], const(Resume))
+ op_check = op.isinstance(block.inputargs[0], const(Resume))
block.operations.append(op_check)
block.exitswitch = op_check.result
link1 = Link([block.inputargs[0]], Resume.block)
diff --git a/rpython/flowspace/operation.py b/rpython/flowspace/operation.py
--- a/rpython/flowspace/operation.py
+++ b/rpython/flowspace/operation.py
@@ -413,6 +413,7 @@
add_operator('id', 1, dispatch=1, pyfunc=id)
add_operator('type', 1, dispatch=1, pyfunc=new_style_type, pure=True)
add_operator('issubtype', 2, dispatch=1, pyfunc=issubclass, pure=True) # not
for old-style classes
+add_operator('isinstance', 2, dispatch=1, pyfunc=isinstance, pure=True)
add_operator('repr', 1, dispatch=1, pyfunc=repr, pure=True)
add_operator('str', 1, dispatch=1, pyfunc=str, pure=True)
add_operator('format', 2, pyfunc=unsupported)
diff --git a/rpython/flowspace/specialcase.py b/rpython/flowspace/specialcase.py
--- a/rpython/flowspace/specialcase.py
+++ b/rpython/flowspace/specialcase.py
@@ -1,5 +1,5 @@
import os
-from rpython.flowspace.model import Constant, const
+from rpython.flowspace.model import Constant
SPECIAL_CASES = {}
@@ -40,12 +40,6 @@
"pytest.ini from the root of the PyPy repository into your "
"own project.")
-@register_flow_sc(isinstance)
-def sc_isinstance(ctx, w_instance, w_type):
- if w_instance.foldable() and w_type.foldable():
- return const(isinstance(w_instance.value, w_type.value))
- return ctx.appcall(isinstance, w_instance, w_type)
-
@register_flow_sc(getattr)
def sc_getattr(ctx, w_obj, w_index, w_default=None):
if w_default is not None:
diff --git a/rpython/rtyper/rbuiltin.py b/rpython/rtyper/rbuiltin.py
--- a/rpython/rtyper/rbuiltin.py
+++ b/rpython/rtyper/rbuiltin.py
@@ -676,24 +676,6 @@
resulttype=llmemory.Address)
-@typer_for(isinstance)
-def rtype_builtin_isinstance(hop):
- hop.exception_cannot_occur()
- if hop.s_result.is_constant():
- return hop.inputconst(lltype.Bool, hop.s_result.const)
-
- if hop.args_s[1].is_constant() and hop.args_s[1].const in (str, list,
unicode):
- if hop.args_s[0].knowntype not in (str, list, unicode):
- raise TyperError("isinstance(x, str/list/unicode) expects x to be
known"
- " statically to be a str/list/unicode or None")
- rstrlist = hop.args_r[0]
- vstrlist = hop.inputarg(rstrlist, arg=0)
- cnone = hop.inputconst(rstrlist, None)
- return hop.genop('ptr_ne', [vstrlist, cnone], resulttype=lltype.Bool)
-
- assert isinstance(hop.args_r[0], rclass.InstanceRepr)
- return hop.args_r[0].rtype_isinstance(hop)
-
@typer_for(objectmodel.instantiate)
def rtype_instantiate(hop, i_nonmovable=None):
hop.exception_cannot_occur()
diff --git a/rpython/rtyper/rmodel.py b/rpython/rtyper/rmodel.py
--- a/rpython/rtyper/rmodel.py
+++ b/rpython/rtyper/rmodel.py
@@ -204,6 +204,21 @@
else:
return hop.genop('int_is_true', [vlen], resulttype=Bool)
+ def rtype_isinstance(self, hop):
+ hop.exception_cannot_occur()
+ if hop.s_result.is_constant():
+ return hop.inputconst(lltype.Bool, hop.s_result.const)
+
+ if hop.args_s[1].is_constant() and hop.args_s[1].const in (str, list,
unicode):
+ if hop.args_s[0].knowntype not in (str, list, unicode):
+ raise TyperError("isinstance(x, str/list/unicode) expects x to
be known"
+ " statically to be a str/list/unicode or None")
+ rstrlist = hop.args_r[0]
+ vstrlist = hop.inputarg(rstrlist, arg=0)
+ cnone = hop.inputconst(rstrlist, None)
+ return hop.genop('ptr_ne', [vstrlist, cnone],
resulttype=lltype.Bool)
+ raise TyperError
+
def rtype_hash(self, hop):
ll_hash = self.get_ll_hash_function()
v, = hop.inputargs(self)
diff --git a/rpython/translator/simplify.py b/rpython/translator/simplify.py
--- a/rpython/translator/simplify.py
+++ b/rpython/translator/simplify.py
@@ -414,7 +414,7 @@
CanRemove = {}
for _op in '''
newtuple newlist newdict bool
- is_ id type issubtype repr str len hash getattr getitem
+ is_ id type issubtype isinstance repr str len hash getattr getitem
pos neg abs hex oct ord invert add sub mul
truediv floordiv div mod divmod pow lshift rshift and_ or_
xor int float long lt le eq ne gt ge cmp coerce contains
@@ -425,7 +425,6 @@
CanRemove[_op] = True
del _op
CanRemoveBuiltins = {
- isinstance: True,
hasattr: True,
}
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit