From 5c20dcfb32d68a25c32832b8aa7577e08f938e89 Mon Sep 17 00:00:00 2001 From: Erik Rose Date: Sun, 8 Mar 2015 19:36:29 -0400 Subject: [PATCH] Remove what turns out to be a useless __slots__ definition from TokenMatcher, add a useful one to Token, and confirm with a test that __slots__ does not behave in CPython according to the language docs. --- parsimonious/expressions.py | 2 -- parsimonious/tests/test_expressions.py | 27 ++++++++++++++++++++++++++ parsimonious/utils.py | 2 ++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/parsimonious/expressions.py b/parsimonious/expressions.py index 352cb13..be28870 100644 --- a/parsimonious/expressions.py +++ b/parsimonious/expressions.py @@ -233,8 +233,6 @@ class TokenMatcher(Literal): This is for use only with TokenGrammars. """ - __slots__ = [] # so this doesn't grow a __dict__ - def _uncached_match(self, token_list, pos, cache, error): if token_list[pos].type == self.literal: return Node(self.name, token_list, pos, pos + 1) diff --git a/parsimonious/tests/test_expressions.py b/parsimonious/tests/test_expressions.py index 5248e48..c8b2a73 100644 --- a/parsimonious/tests/test_expressions.py +++ b/parsimonious/tests/test_expressions.py @@ -259,3 +259,30 @@ def test_unicode(self): """ unicode(rule_grammar) + + +class SlotsTests(TestCase): + """Tests to do with __slots__""" + + def test_subclassing(self): + """Make sure a subclass of a __slots__-less class can introduce new + slots itself. + + This isn't supposed to work, according to the language docs: + + When inheriting from a class without __slots__, the __dict__ + attribute of that class will always be accessible, so a __slots__ + definition in the subclass is meaningless. + + But it does. + + """ + class Smoo(Optional): + __slots__ = ['smoo'] + + def __init__(self): + self.smoo = 'smoo' + + smoo = Smoo() + eq_(smoo.__dict__, {}) # has a __dict__ but with no smoo in it + eq_(smoo.smoo, 'smoo') # The smoo attr ended up in a slot. diff --git a/parsimonious/utils.py b/parsimonious/utils.py index 4de850a..1bb9317 100644 --- a/parsimonious/utils.py +++ b/parsimonious/utils.py @@ -37,6 +37,8 @@ class Token(StrAndRepr): must have a ``type`` attr. """ + __slots__ = ['type'] + def __init__(self, type): self.type = type