From b333f9ec11b7ae8d3ce874fdc220d0b460601674 Mon Sep 17 00:00:00 2001 From: ZyX Date: Fri, 19 Sep 2014 22:36:16 +0400 Subject: [PATCH] Forbid most operations for Context instances --- powerline/lint/context.py | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/powerline/lint/context.py b/powerline/lint/context.py index e9a8cef6..986e618e 100644 --- a/powerline/lint/context.py +++ b/powerline/lint/context.py @@ -38,26 +38,36 @@ def list_themes(data, context): class Context(tuple): - def __new__(cls, base, other=None): - if other is not None: - return tuple.__new__(cls, tuple.__add__(base, other)) - else: - return tuple.__new__(cls, ((MarkedUnicode('', base.mark), base),)) + for func in dir(tuple): + if func in ('__getitem__', '__init__', '__getattribute__', '__len__', '__iter__'): + continue + exec(( + 'def {0}(self, *args, **kwargs):\n' + ' raise TypeError("{0} is not allowed for Context")' + ).format(func)) + del func - def __add__(self, arg): - assert(len(arg) == 1) - assert(type(arg) is tuple) - assert(len(arg[0]) == 2) - assert(type(arg[0]) is tuple) - havemarks(arg[0][0], arg[0][1]) - return Context.__new__(Context, self, arg) + __slots__ = () + + def __new__(cls, base, context_key=None, context_value=None): + if context_key is not None: + assert(context_value is not None) + assert(type(base) is Context) + havemarks(context_key, context_value) + return tuple.__new__(cls, tuple.__add__(base, ((context_key, context_value),))) + else: + havemarks(base) + return tuple.__new__(cls, ((MarkedUnicode('', base.mark), base),)) @property def key(self): return key_sep.join((c[0] for c in self)) def enter_key(self, value, key): - return self + ((value.keydict[key], value[key]),) + return self.enter(value.keydict[key], value[key]) def enter_item(self, name, item): - return self + ((MarkedUnicode(name, item.mark), item),) + return self.enter(MarkedUnicode(name, item.mark), item) + + def enter(self, context_key, context_value): + return Context.__new__(Context, self, context_key, context_value)