pypy / pypy / objspace / std / celldict.py

Carl Friedrich B… 184f4bd 



Carl Friedrich B… 9fab88d 
Carl Friedrich B… 184f4bd 
Armin Rigo 746c2e5 
Carl Friedrich B… 61dae75 
Lukas Diekmann a701aaa 

Carl Friedrich B… 95f5020 
Carl Friedrich B… 184f4bd 



Carl Friedrich B… 95f5020 





Carl Friedrich B… 184f4bd 




Lukas Diekmann a701aaa 

Carl Friedrich B… 1e89bd5 


Lukas Diekmann a701aaa 
Carl Friedrich B… 184f4bd 

Carl Friedrich B… 95f5020 

Carl Friedrich B… 184f4bd 
Carl Friedrich B… 95f5020 
Lukas Diekmann a701aaa 
Alex Gaynor 1917da0 
Lukas Diekmann a701aaa 
Carl Friedrich B… 184f4bd 
Alex Gaynor 1917da0 
Carl Friedrich B… 184f4bd 

Carl Friedrich B… 25523b8 


Carl Friedrich B… 184f4bd 

Carl Friedrich B… 461d904 
Carl Friedrich B… 184f4bd 
Carl Friedrich B… 1e89bd5 
Carl Friedrich B… 9fab88d 
Lukas Diekmann a701aaa 
Carl Friedrich B… 95f5020 

Lukas Diekmann a701aaa 
Carl Friedrich B… 95f5020 
Lukas Diekmann a701aaa 

Carl Friedrich B… 95f5020 
Carl Friedrich B… e7904f8 
Carl Friedrich B… 184f4bd 




Carl Friedrich B… 5424c31 



Carl Friedrich B… 184f4bd 


Carl Friedrich B… 95f5020 
Lukas Diekmann a701aaa 
Lukas Diekmann a0f3390 

Carl Friedrich B… 184f4bd 





Lukas Diekmann a0f3390 
Lukas Diekmann 90dde1a 

Lukas Diekmann a0f3390 
Lukas Diekmann a701aaa 
Carl Friedrich B… 95f5020 



Carl Friedrich B… 184f4bd 






Carl Friedrich B… 61dae75 
Carl Friedrich B… 95f5020 

Lukas Diekmann 90dde1a 

Lukas Diekmann a701aaa 

Carl Friedrich B… 184f4bd 
Carl Friedrich B… 95f5020 
Carl Friedrich B… e7904f8 
Carl Friedrich B… 95f5020 
Carl Friedrich B… e7904f8 
Carl Friedrich B… 95f5020 
Carl Friedrich B… e7904f8 
Carl Friedrich B… 239180d 
Carl Friedrich B… 61dae75 
Carl Friedrich B… 95f5020 

Lukas Diekmann 1d437de 
Carl Friedrich B… e7904f8 
Carl Friedrich B… 95f5020 
Carl Friedrich B… e7904f8 
Carl Friedrich B… 184f4bd 

Carl Friedrich B… 95f5020 
Lukas Diekmann a26b314 
Carl Friedrich B… 95f5020 
Lukas Diekmann a26b314 

Carl Friedrich B… 95f5020 
Lukas Diekmann a701aaa 
Carl Friedrich B… 1e89bd5 
Carl Friedrich B… 184f4bd 
Carl Friedrich B… 95f5020 
Lukas Diekmann a701aaa 
Carl Friedrich B… 95f5020 
Carl Friedrich B… 1e89bd5 
Carl Friedrich B… 184f4bd 

Carl Friedrich B… 95f5020 
Lukas Diekmann a701aaa 
Carl Friedrich B… 184f4bd 

Carl Friedrich B… 95f5020 
Alex Gaynor ac31a98 
Carl Friedrich B… 5d58831 



Alex Gaynor ac31a98 
Lukas Diekmann a701aaa 
Carl Friedrich B… 1e89bd5 
Lukas Diekmann a701aaa 
Carl Friedrich B… 1e89bd5 
Lukas Diekmann a701aaa 
Carl Friedrich B… 184f4bd 
Lukas Diekmann a701aaa 
Carl Friedrich B… 1e89bd5 
Carl Friedrich B… 95f5020 
Maciej Fijalkows… b085853 









Carl Friedrich B… 95f5020 
Armin Rigo 746c2e5 
""" A very simple cell dict implementation using a version tag. The dictionary
maps keys to objects. If a specific key is changed a lot, a level of
indirection is introduced to make the version tag change less often.
"""

from pypy.interpreter.baseobjspace import W_Root
from pypy.objspace.std.dictmultiobject import create_iterator_classes
from pypy.objspace.std.dictmultiobject import DictStrategy, _never_equal_to_string
from pypy.objspace.std.dictmultiobject import ObjectDictStrategy
from pypy.rlib import jit, rerased

class VersionTag(object):
    pass

class ModuleCell(W_Root):
    def __init__(self, w_value=None):
        self.w_value = w_value

    def __repr__(self):
        return "<ModuleCell: %s>" % (self.w_value, )

def unwrap_cell(w_value):
    if isinstance(w_value, ModuleCell):
        return w_value.w_value
    return w_value

class ModuleDictStrategy(DictStrategy):

    erase, unerase = rerased.new_erasing_pair("modulecell")
    erase = staticmethod(erase)
    unerase = staticmethod(unerase)

    _immutable_fields_ = ["version?"]

    def __init__(self, space):
        self.space = space
        self.version = VersionTag()

    def get_empty_storage(self):
        return self.erase({})

    def mutated(self):
        self.version = VersionTag()

    def getdictvalue_no_unwrapping(self, w_dict, key):
        # NB: it's important to promote self here, so that self.version is a
        # no-op due to the quasi-immutable field
        self = jit.promote(self)
        return self._getdictvalue_no_unwrapping_pure(self.version, w_dict, key)

    @jit.elidable_promote('0,1,2')
    def _getdictvalue_no_unwrapping_pure(self, version, w_dict, key):
        return self.unerase(w_dict.dstorage).get(key, None)

    def setitem(self, w_dict, w_key, w_value):
        space = self.space
        if space.is_w(space.type(w_key), space.w_str):
            self.setitem_str(w_dict, self.space.str_w(w_key), w_value)
        else:
            self.switch_to_object_strategy(w_dict)
            w_dict.setitem(w_key, w_value)

    def setitem_str(self, w_dict, key, w_value):
        cell = self.getdictvalue_no_unwrapping(w_dict, key)
        if isinstance(cell, ModuleCell):
            cell.w_value = w_value
            return
        if cell is not None:
            # If the new value and the current value are the same, don't create a
            # level of indirection, or mutate the version.
            if self.space.is_w(w_value, cell):
                return
            w_value = ModuleCell(w_value)
        self.mutated()
        self.unerase(w_dict.dstorage)[key] = w_value

    def setdefault(self, w_dict, w_key, w_default):
        space = self.space
        if space.is_w(space.type(w_key), space.w_str):
            key = space.str_w(w_key)
            w_result = self.getitem_str(w_dict, key)
            if w_result is not None:
                return w_result
            self.setitem_str(w_dict, key, w_default)
            return w_default
        else:
            self.switch_to_object_strategy(w_dict)
            return w_dict.setdefault(w_key, w_default)

    def delitem(self, w_dict, w_key):
        space = self.space
        w_key_type = space.type(w_key)
        if space.is_w(w_key_type, space.w_str):
            key = space.str_w(w_key)
            dict_w = self.unerase(w_dict.dstorage)
            try:
                del dict_w[key]
            except KeyError:
                raise
            else:
                self.mutated()
        elif _never_equal_to_string(space, w_key_type):
            raise KeyError
        else:
            self.switch_to_object_strategy(w_dict)
            w_dict.delitem(w_key)

    def length(self, w_dict):
        return len(self.unerase(w_dict.dstorage))

    def getitem(self, w_dict, w_key):
        space = self.space
        w_lookup_type = space.type(w_key)
        if space.is_w(w_lookup_type, space.w_str):
            return self.getitem_str(w_dict, space.str_w(w_key))

        elif _never_equal_to_string(space, w_lookup_type):
            return None
        else:
            self.switch_to_object_strategy(w_dict)
            return w_dict.getitem(w_key)

    def getitem_str(self, w_dict, key):
        w_res = self.getdictvalue_no_unwrapping(w_dict, key)
        return unwrap_cell(w_res)

    def w_keys(self, w_dict):
        space = self.space
        l = self.unerase(w_dict.dstorage).keys()
        return space.newlist_str(l)

    def values(self, w_dict):
        iterator = self.unerase(w_dict.dstorage).itervalues
        return [unwrap_cell(cell) for cell in iterator()]

    def items(self, w_dict):
        space = self.space
        iterator = self.unerase(w_dict.dstorage).iteritems
        return [space.newtuple([space.wrap(key), unwrap_cell(cell)])
                    for key, cell in iterator()]

    def clear(self, w_dict):
        iterator = self.unerase(w_dict.dstorage).clear()
        self.mutated()

    def popitem(self, w_dict):
        d = self.unerase(w_dict.dstorage)
        key, w_value = d.popitem()
        self.mutated()
        return self.space.wrap(key), unwrap_cell(w_value)

    def switch_to_object_strategy(self, w_dict):
        d = self.unerase(w_dict.dstorage)
        strategy = self.space.fromcache(ObjectDictStrategy)
        d_new = strategy.unerase(strategy.get_empty_storage())
        for key, cell in d.iteritems():
            d_new[self.space.wrap(key)] = unwrap_cell(cell)
        w_dict.strategy = strategy
        w_dict.dstorage = strategy.erase(d_new)

    def getiterkeys(self, w_dict):
        return self.unerase(w_dict.dstorage).iterkeys()
    def getitervalues(self, w_dict):
        return self.unerase(w_dict.dstorage).itervalues()
    def getiteritems(self, w_dict):
        return self.unerase(w_dict.dstorage).iteritems()
    def wrapkey(space, key):
        return space.wrap(key)
    def wrapvalue(space, value):
        return unwrap_cell(value)

create_iterator_classes(ModuleDictStrategy)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.