[pypy-commit] pypy kill-someobject: (arigo, fijal)
fijal
noreply at buildbot.pypy.org
Fri Oct 12 14:46:03 CEST 2012
Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: kill-someobject
Changeset: r58027:c4813b21659f
Date: 2012-10-12 14:44 +0200
http://bitbucket.org/pypy/pypy/changeset/c4813b21659f/
Log: (arigo, fijal) Rename _freeze_ returning False to _cleanup_, now we
can have hasattr(x, '_freeze_') without strange hacks to check for
frozen PBCs.
diff --git a/pypy/annotation/bookkeeper.py b/pypy/annotation/bookkeeper.py
--- a/pypy/annotation/bookkeeper.py
+++ b/pypy/annotation/bookkeeper.py
@@ -148,7 +148,6 @@
self.descs = {} # map Python objects to their XxxDesc wrappers
self.methoddescs = {} # map (funcdesc, classdef) to the MethodDesc
self.classdefs = [] # list of all ClassDefs
- self.pbctypes = {}
self.seen_mutable = {}
self.listdefs = {} # map position_keys to ListDefs
self.dictdefs = {} # map position_keys to DictDefs
@@ -461,7 +460,8 @@
result = None
if result is None:
result = SomePBC([self.getdesc(x)])
- elif hasattr(x, '_freeze_') and x._freeze_():
+ elif hasattr(x, '_freeze_'):
+ assert x._freeze_() is True
# user-defined classes can define a method _freeze_(), which
# is called when a prebuilt instance is found. If the method
# returns True, the instance is considered immutable and becomes
@@ -469,6 +469,8 @@
result = SomePBC([self.getdesc(x)])
elif hasattr(x, '__class__') \
and x.__class__.__module__ != '__builtin__':
+ if hasattr(x, '_cleanup_'):
+ x._cleanup_()
self.see_mutable(x)
result = SomeInstance(self.getuniqueclassdef(x.__class__))
elif x is None:
@@ -502,8 +504,10 @@
elif isinstance(pyobj, types.MethodType):
if pyobj.im_self is None: # unbound
return self.getdesc(pyobj.im_func)
- elif (hasattr(pyobj.im_self, '_freeze_') and
- pyobj.im_self._freeze_()): # method of frozen
+ if hasattr(pyobj.im_self, '_cleanup_'):
+ pyobj.im_self._cleanup_()
+ if hasattr(pyobj.im_self, '_freeze_'): # method of frozen
+ assert pyobj.im_self._freeze_() is True
result = description.MethodOfFrozenDesc(self,
self.getdesc(pyobj.im_func), # funcdesc
self.getdesc(pyobj.im_self)) # frozendesc
@@ -522,9 +526,9 @@
name)
else:
# must be a frozen pre-built constant, but let's check
- try:
- assert pyobj._freeze_()
- except AttributeError:
+ if hasattr(pyobj, '_freeze_'):
+ assert pyobj._freeze_() is True
+ else:
if hasattr(pyobj, '__call__'):
msg = "object with a __call__ is not RPython"
else:
@@ -544,11 +548,7 @@
return False
def getfrozen(self, pyobj):
- result = description.FrozenDesc(self, pyobj)
- cls = result.knowntype
- if cls not in self.pbctypes:
- self.pbctypes[cls] = True
- return result
+ return description.FrozenDesc(self, pyobj)
def getmethoddesc(self, funcdesc, originclassdef, selfclassdef, name,
flags={}):
diff --git a/pypy/annotation/builtin.py b/pypy/annotation/builtin.py
--- a/pypy/annotation/builtin.py
+++ b/pypy/annotation/builtin.py
@@ -189,7 +189,8 @@
for variable in variables:
assert bk.annotator.binding(variable) == s_obj
r.knowntypedata = {}
- if isinstance(s_obj, SomeInstance) and isinstance(s_type, SomePBC):
+
+ if not hasattr(typ, '_freeze_') and isinstance(s_type, SomePBC):
add_knowntypedata(r.knowntypedata, True, variables, bk.valueoftype(typ))
return r
diff --git a/pypy/annotation/signature.py b/pypy/annotation/signature.py
--- a/pypy/annotation/signature.py
+++ b/pypy/annotation/signature.py
@@ -83,11 +83,11 @@
elif bookkeeper and extregistry.is_registered_type(t, bookkeeper.policy):
entry = extregistry.lookup_type(t, bookkeeper.policy)
return entry.compute_annotation_bk(bookkeeper)
- elif bookkeeper and t.__module__ != '__builtin__' and t not in bookkeeper.pbctypes:
+ elif t is type:
+ return SomeType()
+ elif bookkeeper and not hasattr(t, '_freeze_'):
classdef = bookkeeper.getuniqueclassdef(t)
return SomeInstance(classdef)
- elif t is type:
- return SomeType()
else:
raise AssertionError("annotationoftype(%r)" % (t,))
diff --git a/pypy/annotation/test/test_annrpython.py b/pypy/annotation/test/test_annrpython.py
--- a/pypy/annotation/test/test_annrpython.py
+++ b/pypy/annotation/test/test_annrpython.py
@@ -763,19 +763,25 @@
def test_freeze_protocol(self):
class Stuff:
- def __init__(self, flag):
+ def __init__(self):
self.called = False
- self.flag = flag
def _freeze_(self):
self.called = True
- return self.flag
- myobj = Stuff(True)
+ return True
+ myobj = Stuff()
a = self.RPythonAnnotator()
s = a.build_types(lambda: myobj, [])
assert myobj.called
assert isinstance(s, annmodel.SomePBC)
assert s.const == myobj
- myobj = Stuff(False)
+
+ def test_cleanup_protocol(self):
+ class Stuff:
+ def __init__(self):
+ self.called = False
+ def _cleanup_(self):
+ self.called = True
+ myobj = Stuff()
a = self.RPythonAnnotator()
s = a.build_types(lambda: myobj, [])
assert myobj.called
diff --git a/pypy/interpreter/function.py b/pypy/interpreter/function.py
--- a/pypy/interpreter/function.py
+++ b/pypy/interpreter/function.py
@@ -244,7 +244,7 @@
# delicate
_all = {'': None}
- def _freeze_(self):
+ def _cleanup_(self):
from pypy.interpreter.gateway import BuiltinCode
if isinstance(self.code, BuiltinCode):
# we have been seen by other means so rtyping should not choke
diff --git a/pypy/interpreter/mixedmodule.py b/pypy/interpreter/mixedmodule.py
--- a/pypy/interpreter/mixedmodule.py
+++ b/pypy/interpreter/mixedmodule.py
@@ -121,14 +121,11 @@
self.w_initialdict = space.call_method(self.w_dict, 'items')
return self.w_dict
- def _freeze_(self):
+ def _cleanup_(self):
self.getdict(self.space)
self.w_initialdict = None
self.startup_called = False
self._frozen = True
- # hint for the annotator: Modules can hold state, so they are
- # not constant
- return False
def buildloaders(cls):
""" NOT_RPYTHON """
diff --git a/pypy/interpreter/test/test_appinterp.py b/pypy/interpreter/test/test_appinterp.py
--- a/pypy/interpreter/test/test_appinterp.py
+++ b/pypy/interpreter/test/test_appinterp.py
@@ -172,8 +172,8 @@
# Uncomment this line for a workaround
# space.getattr(w_ssl, space.wrap('SSLError'))
- w_socket._freeze_()
+ w_socket._cleanup_()
assert w_socket.startup_called == False
- w_ssl._freeze_() # w_ssl.appleveldefs['SSLError'] imports _socket
+ w_ssl._cleanup_() # w_ssl.appleveldefs['SSLError'] imports _socket
assert w_socket.startup_called == False
diff --git a/pypy/jit/backend/llgraph/runner.py b/pypy/jit/backend/llgraph/runner.py
--- a/pypy/jit/backend/llgraph/runner.py
+++ b/pypy/jit/backend/llgraph/runner.py
@@ -121,9 +121,8 @@
self._future_values = []
self._descrs = {}
- def _freeze_(self):
+ def _cleanup_(self):
assert self.translate_support_code
- return False
def getdescr(self, ofs, typeinfo='?', extrainfo=None, name=None,
arg_types=None, count_fields_if_immut=-1, ffi_flags=0, width=-1):
diff --git a/pypy/jit/metainterp/blackhole.py b/pypy/jit/metainterp/blackhole.py
--- a/pypy/jit/metainterp/blackhole.py
+++ b/pypy/jit/metainterp/blackhole.py
@@ -50,11 +50,10 @@
self.setup_descrs(asm.descrs)
self.metainterp_sd = metainterp_sd
self.num_interpreters = 0
- self._freeze_()
+ self._cleanup_()
- def _freeze_(self):
+ def _cleanup_(self):
self.blackholeinterps = []
- return False
def setup_insns(self, insns):
assert len(insns) <= 256, "too many instructions!"
diff --git a/pypy/module/_codecs/interp_codecs.py b/pypy/module/_codecs/interp_codecs.py
--- a/pypy/module/_codecs/interp_codecs.py
+++ b/pypy/module/_codecs/interp_codecs.py
@@ -69,9 +69,8 @@
self.unicodedata_handler = UnicodeData_Handler(space, w_getcode)
return self.unicodedata_handler
- def _freeze_(self):
+ def _cleanup_(self):
assert not self.codec_search_path
- return False
def register_codec(space, w_search_function):
"""register(search_function)
diff --git a/pypy/module/_file/interp_stream.py b/pypy/module/_file/interp_stream.py
--- a/pypy/module/_file/interp_stream.py
+++ b/pypy/module/_file/interp_stream.py
@@ -58,12 +58,11 @@
self.space.wrap("stream lock is not held"))
self._release_lock()
- def _freeze_(self):
+ def _cleanup_(self):
# remove the lock object, which will be created again as needed at
# run-time.
self.slock = None
assert self.slockowner is None
- return False
def stream_read(self, n):
"""
diff --git a/pypy/module/_multiprocessing/interp_semaphore.py b/pypy/module/_multiprocessing/interp_semaphore.py
--- a/pypy/module/_multiprocessing/interp_semaphore.py
+++ b/pypy/module/_multiprocessing/interp_semaphore.py
@@ -196,7 +196,7 @@
def __init__(self, space):
self.counter = 0
- def _freeze_(self):
+ def _cleanup_(self):
self.counter = 0
def getCount(self):
diff --git a/pypy/module/cpyext/pyobject.py b/pypy/module/cpyext/pyobject.py
--- a/pypy/module/cpyext/pyobject.py
+++ b/pypy/module/cpyext/pyobject.py
@@ -150,10 +150,9 @@
# For tests
self.non_heaptypes_w = []
- def _freeze_(self):
+ def _cleanup_(self):
assert self.borrow_mapping == {None: {}}
self.py_objects_r2w.clear() # is not valid anymore after translation
- return False
def init_r2w_from_w2r(self):
"""Rebuilds the dict py_objects_r2w on startup"""
diff --git a/pypy/module/pypyjit/interp_jit.py b/pypy/module/pypyjit/interp_jit.py
--- a/pypy/module/pypyjit/interp_jit.py
+++ b/pypy/module/pypyjit/interp_jit.py
@@ -121,9 +121,8 @@
PyCode__initialize(self)
self.jit_cells = {}
- def _freeze_(self):
+ def _cleanup_(self):
self.jit_cells = {}
- return False
# ____________________________________________________________
#
diff --git a/pypy/module/rctime/interp_time.py b/pypy/module/rctime/interp_time.py
--- a/pypy/module/rctime/interp_time.py
+++ b/pypy/module/rctime/interp_time.py
@@ -87,7 +87,7 @@
def __init__(self, space):
self.main_thread = 0
- def _freeze_(self):
+ def _cleanup_(self):
self.main_thread = 0
globalState.init()
diff --git a/pypy/module/thread/gil.py b/pypy/module/thread/gil.py
--- a/pypy/module/thread/gil.py
+++ b/pypy/module/thread/gil.py
@@ -62,10 +62,9 @@
class SpaceState:
- def _freeze_(self):
+ def _cleanup_(self):
self.action_after_thread_switch = None
# ^^^ set by AsyncAction.fire_after_thread_switch()
- return False
def after_thread_switch(self):
# this is support logic for the signal module, to help it deliver
@@ -76,7 +75,7 @@
action.fire()
spacestate = SpaceState()
-spacestate._freeze_()
+spacestate._cleanup_()
# Fragile code below. We have to preserve the C-level errno manually...
diff --git a/pypy/module/thread/os_thread.py b/pypy/module/thread/os_thread.py
--- a/pypy/module/thread/os_thread.py
+++ b/pypy/module/thread/os_thread.py
@@ -75,9 +75,8 @@
bootstrapper.w_callable = None
bootstrapper.args = None
- def _freeze_(self):
+ def _cleanup_(self):
self.reinit()
- return False
def bootstrap():
# Note that when this runs, we already hold the GIL. This is ensured
diff --git a/pypy/module/thread/test/test_ll_thread.py b/pypy/module/thread/test/test_ll_thread.py
--- a/pypy/module/thread/test/test_ll_thread.py
+++ b/pypy/module/thread/test/test_ll_thread.py
@@ -9,7 +9,7 @@
# In this module, we assume that ll_thread.start_new_thread() is not
# providing us with a GIL equivalent, except in test_gc_locking
# which installs its own aroundstate.
- rffi.aroundstate._freeze_()
+ rffi.aroundstate._cleanup_()
def test_lock():
l = allocate_lock()
@@ -149,7 +149,7 @@
try:
fn = self.getcompiled(f, [])
finally:
- rffi.aroundstate._freeze_()
+ rffi.aroundstate._cleanup_()
answers = fn()
assert answers == expected
diff --git a/pypy/module/thread/threadlocals.py b/pypy/module/thread/threadlocals.py
--- a/pypy/module/thread/threadlocals.py
+++ b/pypy/module/thread/threadlocals.py
@@ -9,14 +9,13 @@
def __init__(self):
self._valuedict = {} # {thread_ident: ExecutionContext()}
- self._freeze_()
+ self._cleanup_()
- def _freeze_(self):
+ def _cleanup_(self):
self._valuedict.clear()
self._mainthreadident = 0
self._mostrecentkey = 0 # fast minicaching for the common case
self._mostrecentvalue = None # fast minicaching for the common case
- return False
def getvalue(self):
ident = thread.get_ident()
diff --git a/pypy/objspace/flow/objspace.py b/pypy/objspace/flow/objspace.py
--- a/pypy/objspace/flow/objspace.py
+++ b/pypy/objspace/flow/objspace.py
@@ -152,8 +152,10 @@
if (not isinstance(to_check, (type, types.ClassType, types.ModuleType)) and
# classes/types/modules are assumed immutable
hasattr(to_check, '__class__') and to_check.__class__.__module__ != '__builtin__'):
- frozen = hasattr(to_check, '_freeze_') and to_check._freeze_()
- if not frozen:
+ frozen = hasattr(to_check, '_freeze_')
+ if frozen:
+ assert to_check._freeze_() is True
+ else:
# cannot count on it not mutating at runtime!
raise UnwrapException
return obj
diff --git a/pypy/objspace/std/typeobject.py b/pypy/objspace/std/typeobject.py
--- a/pypy/objspace/std/typeobject.py
+++ b/pypy/objspace/std/typeobject.py
@@ -318,7 +318,7 @@
def deldictvalue(w_self, space, key):
if w_self.lazyloaders:
- w_self._freeze_() # force un-lazification
+ w_self._cleanup_() # force un-lazification
if (not space.config.objspace.std.mutable_builtintypes
and not w_self.is_heaptype()):
msg = "can't delete attributes on type object '%s'"
@@ -457,19 +457,18 @@
w_self.name, w_subtype.name, w_subtype.name)
return w_subtype
- def _freeze_(w_self):
+ def _cleanup_(w_self):
"NOT_RPYTHON. Forces the lazy attributes to be computed."
if 'lazyloaders' in w_self.__dict__:
for attr in w_self.lazyloaders.keys():
w_self.getdictvalue(w_self.space, attr)
del w_self.lazyloaders
- return False
def getdict(w_self, space): # returning a dict-proxy!
from pypy.objspace.std.dictproxyobject import DictProxyStrategy
from pypy.objspace.std.dictmultiobject import W_DictMultiObject
if w_self.lazyloaders:
- w_self._freeze_() # force un-lazification
+ w_self._cleanup_() # force un-lazification
strategy = space.fromcache(DictProxyStrategy)
storage = strategy.erase(w_self)
return W_DictMultiObject(space, strategy, storage)
diff --git a/pypy/rlib/jit.py b/pypy/rlib/jit.py
--- a/pypy/rlib/jit.py
+++ b/pypy/rlib/jit.py
@@ -502,7 +502,7 @@
elif (hasattr(value, '__class__')
and value.__class__.__module__ != '__builtin__'):
if hasattr(value, '_freeze_'):
- continue # value._freeze_() is better not called
+ kind = "1:INT"
elif getattr(value, '_alloc_flavor_', 'gc') == 'gc':
kind = '2:REF'
else:
diff --git a/pypy/rlib/rgc.py b/pypy/rlib/rgc.py
--- a/pypy/rlib/rgc.py
+++ b/pypy/rlib/rgc.py
@@ -254,12 +254,9 @@
return False # don't keep any type
if isinstance(x, (list, dict, str)):
return True # keep lists and dicts and strings
- try:
- return not x._freeze_() # don't keep any frozen object
- except AttributeError:
- return type(x).__module__ != '__builtin__' # keep non-builtins
- except Exception:
- return False # don't keep objects whose _freeze_() method explodes
+ if hasattr(x, '_freeze_'):
+ return False
+ return type(x).__module__ != '__builtin__' # keep non-builtins
def add_memory_pressure(estimate):
"""Add memory pressure for OpaquePtrs."""
diff --git a/pypy/rlib/rope.py b/pypy/rlib/rope.py
--- a/pypy/rlib/rope.py
+++ b/pypy/rlib/rope.py
@@ -131,7 +131,7 @@
def __add__(self, other):
return concatenate(self, other)
- def _freeze_(self):
+ def _cleanup_(self):
self.additional_info()
class LiteralNode(StringNode):
diff --git a/pypy/rlib/rstacklet.py b/pypy/rlib/rstacklet.py
--- a/pypy/rlib/rstacklet.py
+++ b/pypy/rlib/rstacklet.py
@@ -100,9 +100,8 @@
def __init__(self):
self.sthread = None
self.active = []
- def _freeze_(self):
+ def _cleanup_(self):
self.__init__()
- return False
def add(self, h):
if not self.sthread.is_empty_handle(h):
if h == self.sthread.get_null_handle():
diff --git a/pypy/rlib/timer.py b/pypy/rlib/timer.py
--- a/pypy/rlib/timer.py
+++ b/pypy/rlib/timer.py
@@ -18,7 +18,7 @@
self.levels = {}
self.timingorder = []
- def _freeze_(self):
+ def _cleanup_(self):
self.reset()
def start(self, timer):
diff --git a/pypy/rpython/lltypesystem/llarena.py b/pypy/rpython/lltypesystem/llarena.py
--- a/pypy/rpython/lltypesystem/llarena.py
+++ b/pypy/rpython/lltypesystem/llarena.py
@@ -433,7 +433,8 @@
class LinuxPageSize:
def __init__(self):
self.pagesize = 0
- _freeze_ = __init__
+ def _cleanup_(self):
+ self.pagesize = 0
linuxpagesize = LinuxPageSize()
def clear_large_memory_chunk(baseaddr, size):
diff --git a/pypy/rpython/lltypesystem/rffi.py b/pypy/rpython/lltypesystem/rffi.py
--- a/pypy/rpython/lltypesystem/rffi.py
+++ b/pypy/rpython/lltypesystem/rffi.py
@@ -319,19 +319,18 @@
AroundFnPtr = lltype.Ptr(lltype.FuncType([], lltype.Void))
class AroundState:
- def _freeze_(self):
+ def _cleanup_(self):
self.before = None # or a regular RPython function
self.after = None # or a regular RPython function
- return False
aroundstate = AroundState()
-aroundstate._freeze_()
+aroundstate._cleanup_()
class StackCounter:
- def _freeze_(self):
+ def _cleanup_(self):
self.stacks_counter = 1 # number of "stack pieces": callbacks
- return False # and threads increase it by one
+ # and threads increase it by one
stackcounter = StackCounter()
-stackcounter._freeze_()
+stackcounter._cleanup_()
def llexternal_use_eci(compilation_info):
"""Return a dummy function that, if called in a RPython program,
More information about the pypy-commit
mailing list