summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Hatch <tim@timhatch.com>2016-06-02 13:17:04 -0700
committerTim Hatch <tim@timhatch.com>2016-06-02 13:17:04 -0700
commitc45584d2421d92a2134058e86e54f0bea3f121e5 (patch)
treea2d37098c960e932f1f3653e7ade7d34f9005f3d
parentb7de0a9098ae1189e7211c0705a9cd3ffe106b87 (diff)
parent1b6f130a6d883906da54d4a7cdf80dfc00c1574a (diff)
downloadpygments-c45584d2421d92a2134058e86e54f0bea3f121e5.tar.gz
Merged in jesquik/pygments-main (pull request #600)
Added Python cls builtin word token recognition
-rw-r--r--pygments/lexers/python.py4
-rw-r--r--tests/test_python.py50
2 files changed, 52 insertions, 2 deletions
diff --git a/pygments/lexers/python.py b/pygments/lexers/python.py
index 7601afa8..35635ed1 100644
--- a/pygments/lexers/python.py
+++ b/pygments/lexers/python.py
@@ -116,7 +116,7 @@ class PythonLexer(RegexLexer):
'unichr', 'unicode', 'vars', 'xrange', 'zip'),
prefix=r'(?<!\.)', suffix=r'\b'),
Name.Builtin),
- (r'(?<!\.)(self|None|Ellipsis|NotImplemented|False|True'
+ (r'(?<!\.)(self|None|Ellipsis|NotImplemented|False|True|cls'
r')\b', Name.Builtin.Pseudo),
(words((
'ArithmeticError', 'AssertionError', 'AttributeError',
@@ -303,7 +303,7 @@ class Python3Lexer(RegexLexer):
'sum', 'super', 'tuple', 'type', 'vars', 'zip'), prefix=r'(?<!\.)',
suffix=r'\b'),
Name.Builtin),
- (r'(?<!\.)(self|Ellipsis|NotImplemented)\b', Name.Builtin.Pseudo),
+ (r'(?<!\.)(self|Ellipsis|NotImplemented|cls)\b', Name.Builtin.Pseudo),
(words((
'ArithmeticError', 'AssertionError', 'AttributeError',
'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning',
diff --git a/tests/test_python.py b/tests/test_python.py
new file mode 100644
index 00000000..fc48624e
--- /dev/null
+++ b/tests/test_python.py
@@ -0,0 +1,50 @@
+# -*- coding: utf-8 -*-
+"""
+ Python Tests
+ ~~~~~~~~~~~~
+
+ :copyright: Copyright 2006-2015 by the Pygments team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
+"""
+
+import unittest
+
+from pygments.lexers import PythonLexer
+from pygments.token import Token
+
+
+class PythonTest(unittest.TestCase):
+ def setUp(self):
+ self.lexer = PythonLexer()
+
+ def test_cls_builtin(self):
+ """
+ Tests that a cls token gets interpreted as a Token.Name.Builtin.Pseudo
+
+ """
+ fragment = 'class TestClass():\n @classmethod\n def hello(cls):\n pass\n'
+ tokens = [
+ (Token.Keyword, 'class'),
+ (Token.Text, ' '),
+ (Token.Name.Class, 'TestClass'),
+ (Token.Punctuation, '('),
+ (Token.Punctuation, ')'),
+ (Token.Punctuation, ':'),
+ (Token.Text, '\n'),
+ (Token.Text, ' '),
+ (Token.Name.Decorator, '@classmethod'),
+ (Token.Text, '\n'),
+ (Token.Text, ' '),
+ (Token.Keyword, 'def'),
+ (Token.Text, ' '),
+ (Token.Name.Function, 'hello'),
+ (Token.Punctuation, '('),
+ (Token.Name.Builtin.Pseudo, 'cls'),
+ (Token.Punctuation, ')'),
+ (Token.Punctuation, ':'),
+ (Token.Text, '\n'),
+ (Token.Text, ' '),
+ (Token.Keyword, 'pass'),
+ (Token.Text, '\n'),
+ ]
+ self.assertEqual(tokens, list(self.lexer.get_tokens(fragment)))