Skip to content

Commit a2b3c26

Browse files
committed
Make repr() on nodes not fail.
… but give up on recursive rerp(). Python 2.x is being difficult with bytes vs. Unicode.
1 parent 49b759a commit a2b3c26

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Version 0.4
77
Not released yet.
88

99
* Fix :class:`HashToken` starting with a non-ASCII character.
10+
* Fix :func:`repr` on AST nodes.
1011

1112

1213
Version 0.3

css_diagram_role.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# coding: utf8
12
"""
23
A Sphinx extension adding a 'css' role creating links to
34
the spec’s railroad diagrams.

tinycss2/ast.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,12 @@ def __init__(self, source_line, source_column):
5757
self.source_line = source_line
5858
self.source_column = source_column
5959

60-
def __repr__(self):
61-
return self.repr_format.format(self=self)
60+
if str is bytes:
61+
def __repr__(self):
62+
return self.repr_format.format(self=self).encode('utf8')
63+
else:
64+
def __repr__(self):
65+
return self.repr_format.format(self=self)
6266

6367
def serialize(self):
6468
"""Serialize this node to CSS syntax and return an Unicode string."""
@@ -132,7 +136,7 @@ class Comment(Node):
132136
"""
133137
__slots__ = ['value']
134138
type = 'comment'
135-
repr_format = '<{self.__class__.__name__} {self.value:r}>'
139+
repr_format = '<{self.__class__.__name__} {self.value}>'
136140

137141
def __init__(self, line, column, value):
138142
Node.__init__(self, line, column)
@@ -319,7 +323,7 @@ class StringToken(Node):
319323
"""
320324
__slots__ = ['value']
321325
type = 'string'
322-
repr_format = '<{self.__class__.__name__} {self.value:r}>'
326+
repr_format = '<{self.__class__.__name__} "{self.value}">'
323327

324328
def __init__(self, line, column, value):
325329
Node.__init__(self, line, column)
@@ -544,7 +548,7 @@ class ParenthesesBlock(Node):
544548
"""
545549
__slots__ = ['content']
546550
type = '() block'
547-
repr_format = '<{self.__class__.__name__} ( {self.content} )>'
551+
repr_format = '<{self.__class__.__name__} ( )>'
548552

549553
def __init__(self, line, column, content):
550554
Node.__init__(self, line, column)
@@ -569,7 +573,7 @@ class SquareBracketsBlock(Node):
569573
"""
570574
__slots__ = ['content']
571575
type = '[] block'
572-
repr_format = '<{self.__class__.__name__} [ {self.content} ]>'
576+
repr_format = '<{self.__class__.__name__} [ ]>'
573577

574578
def __init__(self, line, column, content):
575579
Node.__init__(self, line, column)
@@ -594,7 +598,7 @@ class CurlyBracketsBlock(Node):
594598
"""
595599
__slots__ = ['content']
596600
type = '{} block'
597-
repr_format = '<{self.__class__.__name__} {{ {self.content} }}>'
601+
repr_format = '<{self.__class__.__name__} {{ }}>'
598602

599603
def __init__(self, line, column, content):
600604
Node.__init__(self, line, column)
@@ -631,7 +635,7 @@ class FunctionBlock(Node):
631635
"""
632636
__slots__ = ['name', 'lower_name', 'arguments']
633637
type = 'function'
634-
repr_format = '<{self.__class__.__name__} {self.name}( {self.arguments} )>'
638+
repr_format = '<{self.__class__.__name__} {self.name}( )>'
635639

636640
def __init__(self, line, column, name, arguments):
637641
Node.__init__(self, line, column)
@@ -683,7 +687,7 @@ class Declaration(Node):
683687
"""
684688
__slots__ = ['name', 'lower_name', 'value', 'important']
685689
type = 'declaration'
686-
repr_format = '<{self.__class__.__name__} {self.name}: {self.value}>'
690+
repr_format = '<{self.__class__.__name__} {self.name}: >'
687691

688692
def __init__(self, line, column, name, lower_name, value, important):
689693
Node.__init__(self, line, column)
@@ -724,7 +728,7 @@ class QualifiedRule(Node):
724728
__slots__ = ['prelude', 'content']
725729
type = 'qualified-rule'
726730
repr_format = ('<{self.__class__.__name__} '
727-
'{self.prelude} {{ {self.content} }}>')
731+
' {{ }}>')
728732

729733
def __init__(self, line, column, prelude, content):
730734
Node.__init__(self, line, column)
@@ -780,7 +784,7 @@ class AtRule(Node):
780784
__slots__ = ['at_keyword', 'lower_at_keyword', 'prelude', 'content']
781785
type = 'at-rule'
782786
repr_format = ('<{self.__class__.__name__} '
783-
'@{self.at_keyword} {self.prelude} {{ {self.content} }}>')
787+
'@{self.at_keyword} {{ }}>')
784788

785789
def __init__(self, line, column,
786790
at_keyword, lower_at_keyword, prelude, content):

tinycss2/test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ def json_test(function, filename=None):
8383

8484
@pytest.mark.parametrize(('css', 'expected'), load_json(filename))
8585
def test(css, expected):
86-
value = to_json(function(css))
86+
result = function(css)
87+
repr(result) # Test that these do not raise
88+
value = to_json(result)
8789
if value != expected: # pragma: no cover
8890
pprint.pprint(value)
8991
assert value == expected

0 commit comments

Comments
 (0)