Skip to content

Commit

Permalink
Remove what turns out to be a useless __slots__ definition from Token…
Browse files Browse the repository at this point in the history
…Matcher, add a useful one to Token, and confirm with a test that __slots__ does not behave in CPython according to the language docs.
  • Loading branch information
erikrose committed Mar 8, 2015
1 parent d25b80c commit 5c20dcf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 0 additions & 2 deletions parsimonious/expressions.py
Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions parsimonious/tests/test_expressions.py
Expand Up @@ -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.
2 changes: 2 additions & 0 deletions parsimonious/utils.py
Expand Up @@ -37,6 +37,8 @@ class Token(StrAndRepr):
must have a ``type`` attr.
"""
__slots__ = ['type']

def __init__(self, type):
self.type = type

Expand Down

0 comments on commit 5c20dcf

Please sign in to comment.