python.py 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers.python
  4. ~~~~~~~~~~~~~~~~~~~~~~
  5. Lexers for Python and related languages.
  6. :copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. import re
  10. from pygments.lexer import Lexer, RegexLexer, include, bygroups, using, \
  11. default, words, combined, do_insertions
  12. from pygments.util import get_bool_opt, shebang_matches
  13. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  14. Number, Punctuation, Generic, Other, Error
  15. from pygments import unistring as uni
  16. __all__ = ['PythonLexer', 'PythonConsoleLexer', 'PythonTracebackLexer',
  17. 'Python3Lexer', 'Python3TracebackLexer', 'CythonLexer',
  18. 'DgLexer', 'NumPyLexer']
  19. line_re = re.compile('.*?\n')
  20. class PythonLexer(RegexLexer):
  21. """
  22. For `Python <http://www.python.org>`_ source code.
  23. """
  24. name = 'Python'
  25. aliases = ['python', 'py', 'sage']
  26. filenames = ['*.py', '*.pyw', '*.sc', 'SConstruct', 'SConscript', '*.tac', '*.sage']
  27. mimetypes = ['text/x-python', 'application/x-python']
  28. def innerstring_rules(ttype):
  29. return [
  30. # the old style '%s' % (...) string formatting
  31. (r'%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?'
  32. '[hlL]?[E-GXc-giorsux%]', String.Interpol),
  33. # backslashes, quotes and formatting signs must be parsed one at a time
  34. (r'[^\\\'"%\n]+', ttype),
  35. (r'[\'"\\]', ttype),
  36. # unhandled string formatting sign
  37. (r'%', ttype),
  38. # newlines are an error (use "nl" state)
  39. ]
  40. tokens = {
  41. 'root': [
  42. (r'\n', Text),
  43. (r'^(\s*)([rRuUbB]{,2})("""(?:.|\n)*?""")',
  44. bygroups(Text, String.Affix, String.Doc)),
  45. (r"^(\s*)([rRuUbB]{,2})('''(?:.|\n)*?''')",
  46. bygroups(Text, String.Affix, String.Doc)),
  47. (r'[^\S\n]+', Text),
  48. (r'\A#!.+$', Comment.Hashbang),
  49. (r'#.*$', Comment.Single),
  50. (r'[]{}:(),;[]', Punctuation),
  51. (r'\\\n', Text),
  52. (r'\\', Text),
  53. (r'(in|is|and|or|not)\b', Operator.Word),
  54. (r'!=|==|<<|>>|[-~+/*%=<>&^|.]', Operator),
  55. include('keywords'),
  56. (r'(def)((?:\s|\\\s)+)', bygroups(Keyword, Text), 'funcname'),
  57. (r'(class)((?:\s|\\\s)+)', bygroups(Keyword, Text), 'classname'),
  58. (r'(from)((?:\s|\\\s)+)', bygroups(Keyword.Namespace, Text),
  59. 'fromimport'),
  60. (r'(import)((?:\s|\\\s)+)', bygroups(Keyword.Namespace, Text),
  61. 'import'),
  62. include('builtins'),
  63. include('magicfuncs'),
  64. include('magicvars'),
  65. include('backtick'),
  66. ('([rR]|[uUbB][rR]|[rR][uUbB])(""")',
  67. bygroups(String.Affix, String.Double), 'tdqs'),
  68. ("([rR]|[uUbB][rR]|[rR][uUbB])(''')",
  69. bygroups(String.Affix, String.Single), 'tsqs'),
  70. ('([rR]|[uUbB][rR]|[rR][uUbB])(")',
  71. bygroups(String.Affix, String.Double), 'dqs'),
  72. ("([rR]|[uUbB][rR]|[rR][uUbB])(')",
  73. bygroups(String.Affix, String.Single), 'sqs'),
  74. ('([uUbB]?)(""")', bygroups(String.Affix, String.Double),
  75. combined('stringescape', 'tdqs')),
  76. ("([uUbB]?)(''')", bygroups(String.Affix, String.Single),
  77. combined('stringescape', 'tsqs')),
  78. ('([uUbB]?)(")', bygroups(String.Affix, String.Double),
  79. combined('stringescape', 'dqs')),
  80. ("([uUbB]?)(')", bygroups(String.Affix, String.Single),
  81. combined('stringescape', 'sqs')),
  82. include('name'),
  83. include('numbers'),
  84. ],
  85. 'keywords': [
  86. (words((
  87. 'assert', 'break', 'continue', 'del', 'elif', 'else', 'except',
  88. 'exec', 'finally', 'for', 'global', 'if', 'lambda', 'pass',
  89. 'print', 'raise', 'return', 'try', 'while', 'yield',
  90. 'yield from', 'as', 'with'), suffix=r'\b'),
  91. Keyword),
  92. ],
  93. 'builtins': [
  94. (words((
  95. '__import__', 'abs', 'all', 'any', 'apply', 'basestring', 'bin',
  96. 'bool', 'buffer', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod',
  97. 'cmp', 'coerce', 'compile', 'complex', 'delattr', 'dict', 'dir', 'divmod',
  98. 'enumerate', 'eval', 'execfile', 'exit', 'file', 'filter', 'float',
  99. 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'hex', 'id',
  100. 'input', 'int', 'intern', 'isinstance', 'issubclass', 'iter', 'len',
  101. 'list', 'locals', 'long', 'map', 'max', 'min', 'next', 'object',
  102. 'oct', 'open', 'ord', 'pow', 'property', 'range', 'raw_input', 'reduce',
  103. 'reload', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice',
  104. 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type',
  105. 'unichr', 'unicode', 'vars', 'xrange', 'zip'),
  106. prefix=r'(?<!\.)', suffix=r'\b'),
  107. Name.Builtin),
  108. (r'(?<!\.)(self|None|Ellipsis|NotImplemented|False|True|cls'
  109. r')\b', Name.Builtin.Pseudo),
  110. (words((
  111. 'ArithmeticError', 'AssertionError', 'AttributeError',
  112. 'BaseException', 'DeprecationWarning', 'EOFError', 'EnvironmentError',
  113. 'Exception', 'FloatingPointError', 'FutureWarning', 'GeneratorExit',
  114. 'IOError', 'ImportError', 'ImportWarning', 'IndentationError',
  115. 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError',
  116. 'MemoryError', 'NameError', 'NotImplemented', 'NotImplementedError',
  117. 'OSError', 'OverflowError', 'OverflowWarning', 'PendingDeprecationWarning',
  118. 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError',
  119. 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError',
  120. 'SystemExit', 'TabError', 'TypeError', 'UnboundLocalError',
  121. 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError',
  122. 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning',
  123. 'ValueError', 'VMSError', 'Warning', 'WindowsError',
  124. 'ZeroDivisionError'), prefix=r'(?<!\.)', suffix=r'\b'),
  125. Name.Exception),
  126. ],
  127. 'magicfuncs': [
  128. (words((
  129. '__abs__', '__add__', '__and__', '__call__', '__cmp__', '__coerce__',
  130. '__complex__', '__contains__', '__del__', '__delattr__', '__delete__',
  131. '__delitem__', '__delslice__', '__div__', '__divmod__', '__enter__',
  132. '__eq__', '__exit__', '__float__', '__floordiv__', '__ge__', '__get__',
  133. '__getattr__', '__getattribute__', '__getitem__', '__getslice__', '__gt__',
  134. '__hash__', '__hex__', '__iadd__', '__iand__', '__idiv__', '__ifloordiv__',
  135. '__ilshift__', '__imod__', '__imul__', '__index__', '__init__',
  136. '__instancecheck__', '__int__', '__invert__', '__iop__', '__ior__',
  137. '__ipow__', '__irshift__', '__isub__', '__iter__', '__itruediv__',
  138. '__ixor__', '__le__', '__len__', '__long__', '__lshift__', '__lt__',
  139. '__missing__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__',
  140. '__nonzero__', '__oct__', '__op__', '__or__', '__pos__', '__pow__',
  141. '__radd__', '__rand__', '__rcmp__', '__rdiv__', '__rdivmod__', '__repr__',
  142. '__reversed__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__',
  143. '__rop__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__',
  144. '__rtruediv__', '__rxor__', '__set__', '__setattr__', '__setitem__',
  145. '__setslice__', '__str__', '__sub__', '__subclasscheck__', '__truediv__',
  146. '__unicode__', '__xor__'), suffix=r'\b'),
  147. Name.Function.Magic),
  148. ],
  149. 'magicvars': [
  150. (words((
  151. '__bases__', '__class__', '__closure__', '__code__', '__defaults__',
  152. '__dict__', '__doc__', '__file__', '__func__', '__globals__',
  153. '__metaclass__', '__module__', '__mro__', '__name__', '__self__',
  154. '__slots__', '__weakref__'),
  155. suffix=r'\b'),
  156. Name.Variable.Magic),
  157. ],
  158. 'numbers': [
  159. (r'(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?j?', Number.Float),
  160. (r'\d+[eE][+-]?[0-9]+j?', Number.Float),
  161. (r'0[0-7]+j?', Number.Oct),
  162. (r'0[bB][01]+', Number.Bin),
  163. (r'0[xX][a-fA-F0-9]+', Number.Hex),
  164. (r'\d+L', Number.Integer.Long),
  165. (r'\d+j?', Number.Integer)
  166. ],
  167. 'backtick': [
  168. ('`.*?`', String.Backtick),
  169. ],
  170. 'name': [
  171. (r'@[\w.]+', Name.Decorator),
  172. ('[a-zA-Z_]\w*', Name),
  173. ],
  174. 'funcname': [
  175. include('magicfuncs'),
  176. ('[a-zA-Z_]\w*', Name.Function, '#pop'),
  177. default('#pop'),
  178. ],
  179. 'classname': [
  180. ('[a-zA-Z_]\w*', Name.Class, '#pop')
  181. ],
  182. 'import': [
  183. (r'(?:[ \t]|\\\n)+', Text),
  184. (r'as\b', Keyword.Namespace),
  185. (r',', Operator),
  186. (r'[a-zA-Z_][\w.]*', Name.Namespace),
  187. default('#pop') # all else: go back
  188. ],
  189. 'fromimport': [
  190. (r'(?:[ \t]|\\\n)+', Text),
  191. (r'import\b', Keyword.Namespace, '#pop'),
  192. # if None occurs here, it's "raise x from None", since None can
  193. # never be a module name
  194. (r'None\b', Name.Builtin.Pseudo, '#pop'),
  195. # sadly, in "raise x from y" y will be highlighted as namespace too
  196. (r'[a-zA-Z_.][\w.]*', Name.Namespace),
  197. # anything else here also means "raise x from y" and is therefore
  198. # not an error
  199. default('#pop'),
  200. ],
  201. 'stringescape': [
  202. (r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|'
  203. r'U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})', String.Escape)
  204. ],
  205. 'strings-single': innerstring_rules(String.Single),
  206. 'strings-double': innerstring_rules(String.Double),
  207. 'dqs': [
  208. (r'"', String.Double, '#pop'),
  209. (r'\\\\|\\"|\\\n', String.Escape), # included here for raw strings
  210. include('strings-double')
  211. ],
  212. 'sqs': [
  213. (r"'", String.Single, '#pop'),
  214. (r"\\\\|\\'|\\\n", String.Escape), # included here for raw strings
  215. include('strings-single')
  216. ],
  217. 'tdqs': [
  218. (r'"""', String.Double, '#pop'),
  219. include('strings-double'),
  220. (r'\n', String.Double)
  221. ],
  222. 'tsqs': [
  223. (r"'''", String.Single, '#pop'),
  224. include('strings-single'),
  225. (r'\n', String.Single)
  226. ],
  227. }
  228. def analyse_text(text):
  229. return shebang_matches(text, r'pythonw?(2(\.\d)?)?') or \
  230. 'import ' in text[:1000]
  231. class Python3Lexer(RegexLexer):
  232. """
  233. For `Python <http://www.python.org>`_ source code (version 3.0).
  234. .. versionadded:: 0.10
  235. """
  236. name = 'Python 3'
  237. aliases = ['python3', 'py3']
  238. filenames = [] # Nothing until Python 3 gets widespread
  239. mimetypes = ['text/x-python3', 'application/x-python3']
  240. flags = re.MULTILINE | re.UNICODE
  241. uni_name = "[%s][%s]*" % (uni.xid_start, uni.xid_continue)
  242. def innerstring_rules(ttype):
  243. return [
  244. # the old style '%s' % (...) string formatting (still valid in Py3)
  245. (r'%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?'
  246. '[hlL]?[E-GXc-giorsux%]', String.Interpol),
  247. # the new style '{}'.format(...) string formatting
  248. (r'\{'
  249. '((\w+)((\.\w+)|(\[[^\]]+\]))*)?' # field name
  250. '(\![sra])?' # conversion
  251. '(\:(.?[<>=\^])?[-+ ]?#?0?(\d+)?,?(\.\d+)?[E-GXb-gnosx%]?)?'
  252. '\}', String.Interpol),
  253. # backslashes, quotes and formatting signs must be parsed one at a time
  254. (r'[^\\\'"%{\n]+', ttype),
  255. (r'[\'"\\]', ttype),
  256. # unhandled string formatting sign
  257. (r'%|(\{{1,2})', ttype)
  258. # newlines are an error (use "nl" state)
  259. ]
  260. tokens = PythonLexer.tokens.copy()
  261. tokens['keywords'] = [
  262. (words((
  263. 'assert', 'async', 'await', 'break', 'continue', 'del', 'elif',
  264. 'else', 'except', 'finally', 'for', 'global', 'if', 'lambda', 'pass',
  265. 'raise', 'nonlocal', 'return', 'try', 'while', 'yield', 'yield from',
  266. 'as', 'with'), suffix=r'\b'),
  267. Keyword),
  268. (words((
  269. 'True', 'False', 'None'), suffix=r'\b'),
  270. Keyword.Constant),
  271. ]
  272. tokens['builtins'] = [
  273. (words((
  274. '__import__', 'abs', 'all', 'any', 'bin', 'bool', 'bytearray', 'bytes',
  275. 'chr', 'classmethod', 'cmp', 'compile', 'complex', 'delattr', 'dict',
  276. 'dir', 'divmod', 'enumerate', 'eval', 'filter', 'float', 'format',
  277. 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'hex', 'id',
  278. 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'list',
  279. 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct',
  280. 'open', 'ord', 'pow', 'print', 'property', 'range', 'repr', 'reversed',
  281. 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str',
  282. 'sum', 'super', 'tuple', 'type', 'vars', 'zip'), prefix=r'(?<!\.)',
  283. suffix=r'\b'),
  284. Name.Builtin),
  285. (r'(?<!\.)(self|Ellipsis|NotImplemented|cls)\b', Name.Builtin.Pseudo),
  286. (words((
  287. 'ArithmeticError', 'AssertionError', 'AttributeError',
  288. 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning',
  289. 'EOFError', 'EnvironmentError', 'Exception', 'FloatingPointError',
  290. 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError',
  291. 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError',
  292. 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError',
  293. 'NotImplementedError', 'OSError', 'OverflowError',
  294. 'PendingDeprecationWarning', 'ReferenceError', 'ResourceWarning',
  295. 'RuntimeError', 'RuntimeWarning', 'StopIteration',
  296. 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError',
  297. 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError',
  298. 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError',
  299. 'UnicodeWarning', 'UserWarning', 'ValueError', 'VMSError', 'Warning',
  300. 'WindowsError', 'ZeroDivisionError',
  301. # new builtin exceptions from PEP 3151
  302. 'BlockingIOError', 'ChildProcessError', 'ConnectionError',
  303. 'BrokenPipeError', 'ConnectionAbortedError', 'ConnectionRefusedError',
  304. 'ConnectionResetError', 'FileExistsError', 'FileNotFoundError',
  305. 'InterruptedError', 'IsADirectoryError', 'NotADirectoryError',
  306. 'PermissionError', 'ProcessLookupError', 'TimeoutError'),
  307. prefix=r'(?<!\.)', suffix=r'\b'),
  308. Name.Exception),
  309. ]
  310. tokens['magicfuncs'] = [
  311. (words((
  312. '__abs__', '__add__', '__aenter__', '__aexit__', '__aiter__', '__and__',
  313. '__anext__', '__await__', '__bool__', '__bytes__', '__call__',
  314. '__complex__', '__contains__', '__del__', '__delattr__', '__delete__',
  315. '__delitem__', '__dir__', '__divmod__', '__enter__', '__eq__', '__exit__',
  316. '__float__', '__floordiv__', '__format__', '__ge__', '__get__',
  317. '__getattr__', '__getattribute__', '__getitem__', '__gt__', '__hash__',
  318. '__iadd__', '__iand__', '__ifloordiv__', '__ilshift__', '__imatmul__',
  319. '__imod__', '__import__', '__imul__', '__index__', '__init__',
  320. '__instancecheck__', '__int__', '__invert__', '__ior__', '__ipow__',
  321. '__irshift__', '__isub__', '__iter__', '__itruediv__', '__ixor__',
  322. '__le__', '__len__', '__length_hint__', '__lshift__', '__lt__',
  323. '__matmul__', '__missing__', '__mod__', '__mul__', '__ne__', '__neg__',
  324. '__new__', '__next__', '__or__', '__pos__', '__pow__', '__prepare__',
  325. '__radd__', '__rand__', '__rdivmod__', '__repr__', '__reversed__',
  326. '__rfloordiv__', '__rlshift__', '__rmatmul__', '__rmod__', '__rmul__',
  327. '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__',
  328. '__rsub__', '__rtruediv__', '__rxor__', '__set__', '__setattr__',
  329. '__setitem__', '__str__', '__sub__', '__subclasscheck__', '__truediv__',
  330. '__xor__'), suffix=r'\b'),
  331. Name.Function.Magic),
  332. ]
  333. tokens['magicvars'] = [
  334. (words((
  335. '__annotations__', '__bases__', '__class__', '__closure__', '__code__',
  336. '__defaults__', '__dict__', '__doc__', '__file__', '__func__',
  337. '__globals__', '__kwdefaults__', '__module__', '__mro__', '__name__',
  338. '__objclass__', '__qualname__', '__self__', '__slots__', '__weakref__'),
  339. suffix=r'\b'),
  340. Name.Variable.Magic),
  341. ]
  342. tokens['numbers'] = [
  343. (r'(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?', Number.Float),
  344. (r'\d+[eE][+-]?[0-9]+j?', Number.Float),
  345. (r'0[oO][0-7]+', Number.Oct),
  346. (r'0[bB][01]+', Number.Bin),
  347. (r'0[xX][a-fA-F0-9]+', Number.Hex),
  348. (r'\d+', Number.Integer)
  349. ]
  350. tokens['backtick'] = []
  351. tokens['name'] = [
  352. (r'@\w+', Name.Decorator),
  353. (r'@', Operator), # new matrix multiplication operator
  354. (uni_name, Name),
  355. ]
  356. tokens['funcname'] = [
  357. (uni_name, Name.Function, '#pop')
  358. ]
  359. tokens['classname'] = [
  360. (uni_name, Name.Class, '#pop')
  361. ]
  362. tokens['import'] = [
  363. (r'(\s+)(as)(\s+)', bygroups(Text, Keyword, Text)),
  364. (r'\.', Name.Namespace),
  365. (uni_name, Name.Namespace),
  366. (r'(\s*)(,)(\s*)', bygroups(Text, Operator, Text)),
  367. default('#pop') # all else: go back
  368. ]
  369. tokens['fromimport'] = [
  370. (r'(\s+)(import)\b', bygroups(Text, Keyword), '#pop'),
  371. (r'\.', Name.Namespace),
  372. (uni_name, Name.Namespace),
  373. default('#pop'),
  374. ]
  375. tokens['strings-single'] = innerstring_rules(String.Single)
  376. tokens['strings-double'] = innerstring_rules(String.Double)
  377. def analyse_text(text):
  378. return shebang_matches(text, r'pythonw?3(\.\d)?')
  379. class PythonConsoleLexer(Lexer):
  380. """
  381. For Python console output or doctests, such as:
  382. .. sourcecode:: pycon
  383. >>> a = 'foo'
  384. >>> print a
  385. foo
  386. >>> 1 / 0
  387. Traceback (most recent call last):
  388. File "<stdin>", line 1, in <module>
  389. ZeroDivisionError: integer division or modulo by zero
  390. Additional options:
  391. `python3`
  392. Use Python 3 lexer for code. Default is ``False``.
  393. .. versionadded:: 1.0
  394. """
  395. name = 'Python console session'
  396. aliases = ['pycon']
  397. mimetypes = ['text/x-python-doctest']
  398. def __init__(self, **options):
  399. self.python3 = get_bool_opt(options, 'python3', False)
  400. Lexer.__init__(self, **options)
  401. def get_tokens_unprocessed(self, text):
  402. if self.python3:
  403. pylexer = Python3Lexer(**self.options)
  404. tblexer = Python3TracebackLexer(**self.options)
  405. else:
  406. pylexer = PythonLexer(**self.options)
  407. tblexer = PythonTracebackLexer(**self.options)
  408. curcode = ''
  409. insertions = []
  410. curtb = ''
  411. tbindex = 0
  412. tb = 0
  413. for match in line_re.finditer(text):
  414. line = match.group()
  415. if line.startswith(u'>>> ') or line.startswith(u'... '):
  416. tb = 0
  417. insertions.append((len(curcode),
  418. [(0, Generic.Prompt, line[:4])]))
  419. curcode += line[4:]
  420. elif line.rstrip() == u'...' and not tb:
  421. # only a new >>> prompt can end an exception block
  422. # otherwise an ellipsis in place of the traceback frames
  423. # will be mishandled
  424. insertions.append((len(curcode),
  425. [(0, Generic.Prompt, u'...')]))
  426. curcode += line[3:]
  427. else:
  428. if curcode:
  429. for item in do_insertions(
  430. insertions, pylexer.get_tokens_unprocessed(curcode)):
  431. yield item
  432. curcode = ''
  433. insertions = []
  434. if (line.startswith(u'Traceback (most recent call last):') or
  435. re.match(u' File "[^"]+", line \\d+\\n$', line)):
  436. tb = 1
  437. curtb = line
  438. tbindex = match.start()
  439. elif line == 'KeyboardInterrupt\n':
  440. yield match.start(), Name.Class, line
  441. elif tb:
  442. curtb += line
  443. if not (line.startswith(' ') or line.strip() == u'...'):
  444. tb = 0
  445. for i, t, v in tblexer.get_tokens_unprocessed(curtb):
  446. yield tbindex+i, t, v
  447. curtb = ''
  448. else:
  449. yield match.start(), Generic.Output, line
  450. if curcode:
  451. for item in do_insertions(insertions,
  452. pylexer.get_tokens_unprocessed(curcode)):
  453. yield item
  454. if curtb:
  455. for i, t, v in tblexer.get_tokens_unprocessed(curtb):
  456. yield tbindex+i, t, v
  457. class PythonTracebackLexer(RegexLexer):
  458. """
  459. For Python tracebacks.
  460. .. versionadded:: 0.7
  461. """
  462. name = 'Python Traceback'
  463. aliases = ['pytb']
  464. filenames = ['*.pytb']
  465. mimetypes = ['text/x-python-traceback']
  466. tokens = {
  467. 'root': [
  468. # Cover both (most recent call last) and (innermost last)
  469. # The optional ^C allows us to catch keyboard interrupt signals.
  470. (r'^(\^C)?(Traceback.*\n)',
  471. bygroups(Text, Generic.Traceback), 'intb'),
  472. # SyntaxError starts with this.
  473. (r'^(?= File "[^"]+", line \d+)', Generic.Traceback, 'intb'),
  474. (r'^.*\n', Other),
  475. ],
  476. 'intb': [
  477. (r'^( File )("[^"]+")(, line )(\d+)(, in )(.+)(\n)',
  478. bygroups(Text, Name.Builtin, Text, Number, Text, Name, Text)),
  479. (r'^( File )("[^"]+")(, line )(\d+)(\n)',
  480. bygroups(Text, Name.Builtin, Text, Number, Text)),
  481. (r'^( )(.+)(\n)',
  482. bygroups(Text, using(PythonLexer), Text)),
  483. (r'^([ \t]*)(\.\.\.)(\n)',
  484. bygroups(Text, Comment, Text)), # for doctests...
  485. (r'^([^:]+)(: )(.+)(\n)',
  486. bygroups(Generic.Error, Text, Name, Text), '#pop'),
  487. (r'^([a-zA-Z_]\w*)(:?\n)',
  488. bygroups(Generic.Error, Text), '#pop')
  489. ],
  490. }
  491. class Python3TracebackLexer(RegexLexer):
  492. """
  493. For Python 3.0 tracebacks, with support for chained exceptions.
  494. .. versionadded:: 1.0
  495. """
  496. name = 'Python 3.0 Traceback'
  497. aliases = ['py3tb']
  498. filenames = ['*.py3tb']
  499. mimetypes = ['text/x-python3-traceback']
  500. tokens = {
  501. 'root': [
  502. (r'\n', Text),
  503. (r'^Traceback \(most recent call last\):\n', Generic.Traceback, 'intb'),
  504. (r'^During handling of the above exception, another '
  505. r'exception occurred:\n\n', Generic.Traceback),
  506. (r'^The above exception was the direct cause of the '
  507. r'following exception:\n\n', Generic.Traceback),
  508. (r'^(?= File "[^"]+", line \d+)', Generic.Traceback, 'intb'),
  509. ],
  510. 'intb': [
  511. (r'^( File )("[^"]+")(, line )(\d+)(, in )(.+)(\n)',
  512. bygroups(Text, Name.Builtin, Text, Number, Text, Name, Text)),
  513. (r'^( File )("[^"]+")(, line )(\d+)(\n)',
  514. bygroups(Text, Name.Builtin, Text, Number, Text)),
  515. (r'^( )(.+)(\n)',
  516. bygroups(Text, using(Python3Lexer), Text)),
  517. (r'^([ \t]*)(\.\.\.)(\n)',
  518. bygroups(Text, Comment, Text)), # for doctests...
  519. (r'^([^:]+)(: )(.+)(\n)',
  520. bygroups(Generic.Error, Text, Name, Text), '#pop'),
  521. (r'^([a-zA-Z_]\w*)(:?\n)',
  522. bygroups(Generic.Error, Text), '#pop')
  523. ],
  524. }
  525. class CythonLexer(RegexLexer):
  526. """
  527. For Pyrex and `Cython <http://cython.org>`_ source code.
  528. .. versionadded:: 1.1
  529. """
  530. name = 'Cython'
  531. aliases = ['cython', 'pyx', 'pyrex']
  532. filenames = ['*.pyx', '*.pxd', '*.pxi']
  533. mimetypes = ['text/x-cython', 'application/x-cython']
  534. tokens = {
  535. 'root': [
  536. (r'\n', Text),
  537. (r'^(\s*)("""(?:.|\n)*?""")', bygroups(Text, String.Doc)),
  538. (r"^(\s*)('''(?:.|\n)*?''')", bygroups(Text, String.Doc)),
  539. (r'[^\S\n]+', Text),
  540. (r'#.*$', Comment),
  541. (r'[]{}:(),;[]', Punctuation),
  542. (r'\\\n', Text),
  543. (r'\\', Text),
  544. (r'(in|is|and|or|not)\b', Operator.Word),
  545. (r'(<)([a-zA-Z0-9.?]+)(>)',
  546. bygroups(Punctuation, Keyword.Type, Punctuation)),
  547. (r'!=|==|<<|>>|[-~+/*%=<>&^|.?]', Operator),
  548. (r'(from)(\d+)(<=)(\s+)(<)(\d+)(:)',
  549. bygroups(Keyword, Number.Integer, Operator, Name, Operator,
  550. Name, Punctuation)),
  551. include('keywords'),
  552. (r'(def|property)(\s+)', bygroups(Keyword, Text), 'funcname'),
  553. (r'(cp?def)(\s+)', bygroups(Keyword, Text), 'cdef'),
  554. # (should actually start a block with only cdefs)
  555. (r'(cdef)(:)', bygroups(Keyword, Punctuation)),
  556. (r'(class|struct)(\s+)', bygroups(Keyword, Text), 'classname'),
  557. (r'(from)(\s+)', bygroups(Keyword, Text), 'fromimport'),
  558. (r'(c?import)(\s+)', bygroups(Keyword, Text), 'import'),
  559. include('builtins'),
  560. include('backtick'),
  561. ('(?:[rR]|[uU][rR]|[rR][uU])"""', String, 'tdqs'),
  562. ("(?:[rR]|[uU][rR]|[rR][uU])'''", String, 'tsqs'),
  563. ('(?:[rR]|[uU][rR]|[rR][uU])"', String, 'dqs'),
  564. ("(?:[rR]|[uU][rR]|[rR][uU])'", String, 'sqs'),
  565. ('[uU]?"""', String, combined('stringescape', 'tdqs')),
  566. ("[uU]?'''", String, combined('stringescape', 'tsqs')),
  567. ('[uU]?"', String, combined('stringescape', 'dqs')),
  568. ("[uU]?'", String, combined('stringescape', 'sqs')),
  569. include('name'),
  570. include('numbers'),
  571. ],
  572. 'keywords': [
  573. (words((
  574. 'assert', 'break', 'by', 'continue', 'ctypedef', 'del', 'elif',
  575. 'else', 'except', 'except?', 'exec', 'finally', 'for', 'fused', 'gil',
  576. 'global', 'if', 'include', 'lambda', 'nogil', 'pass', 'print',
  577. 'raise', 'return', 'try', 'while', 'yield', 'as', 'with'), suffix=r'\b'),
  578. Keyword),
  579. (r'(DEF|IF|ELIF|ELSE)\b', Comment.Preproc),
  580. ],
  581. 'builtins': [
  582. (words((
  583. '__import__', 'abs', 'all', 'any', 'apply', 'basestring', 'bin',
  584. 'bool', 'buffer', 'bytearray', 'bytes', 'callable', 'chr',
  585. 'classmethod', 'cmp', 'coerce', 'compile', 'complex', 'delattr',
  586. 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'execfile', 'exit',
  587. 'file', 'filter', 'float', 'frozenset', 'getattr', 'globals',
  588. 'hasattr', 'hash', 'hex', 'id', 'input', 'int', 'intern', 'isinstance',
  589. 'issubclass', 'iter', 'len', 'list', 'locals', 'long', 'map', 'max',
  590. 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'property',
  591. 'range', 'raw_input', 'reduce', 'reload', 'repr', 'reversed',
  592. 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod',
  593. 'str', 'sum', 'super', 'tuple', 'type', 'unichr', 'unicode', 'unsigned',
  594. 'vars', 'xrange', 'zip'), prefix=r'(?<!\.)', suffix=r'\b'),
  595. Name.Builtin),
  596. (r'(?<!\.)(self|None|Ellipsis|NotImplemented|False|True|NULL'
  597. r')\b', Name.Builtin.Pseudo),
  598. (words((
  599. 'ArithmeticError', 'AssertionError', 'AttributeError',
  600. 'BaseException', 'DeprecationWarning', 'EOFError', 'EnvironmentError',
  601. 'Exception', 'FloatingPointError', 'FutureWarning', 'GeneratorExit',
  602. 'IOError', 'ImportError', 'ImportWarning', 'IndentationError',
  603. 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError',
  604. 'MemoryError', 'NameError', 'NotImplemented', 'NotImplementedError',
  605. 'OSError', 'OverflowError', 'OverflowWarning',
  606. 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError',
  607. 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError',
  608. 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError',
  609. 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError',
  610. 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError',
  611. 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning',
  612. 'ZeroDivisionError'), prefix=r'(?<!\.)', suffix=r'\b'),
  613. Name.Exception),
  614. ],
  615. 'numbers': [
  616. (r'(\d+\.?\d*|\d*\.\d+)([eE][+-]?[0-9]+)?', Number.Float),
  617. (r'0\d+', Number.Oct),
  618. (r'0[xX][a-fA-F0-9]+', Number.Hex),
  619. (r'\d+L', Number.Integer.Long),
  620. (r'\d+', Number.Integer)
  621. ],
  622. 'backtick': [
  623. ('`.*?`', String.Backtick),
  624. ],
  625. 'name': [
  626. (r'@\w+', Name.Decorator),
  627. ('[a-zA-Z_]\w*', Name),
  628. ],
  629. 'funcname': [
  630. ('[a-zA-Z_]\w*', Name.Function, '#pop')
  631. ],
  632. 'cdef': [
  633. (r'(public|readonly|extern|api|inline)\b', Keyword.Reserved),
  634. (r'(struct|enum|union|class)\b', Keyword),
  635. (r'([a-zA-Z_]\w*)(\s*)(?=[(:#=]|$)',
  636. bygroups(Name.Function, Text), '#pop'),
  637. (r'([a-zA-Z_]\w*)(\s*)(,)',
  638. bygroups(Name.Function, Text, Punctuation)),
  639. (r'from\b', Keyword, '#pop'),
  640. (r'as\b', Keyword),
  641. (r':', Punctuation, '#pop'),
  642. (r'(?=["\'])', Text, '#pop'),
  643. (r'[a-zA-Z_]\w*', Keyword.Type),
  644. (r'.', Text),
  645. ],
  646. 'classname': [
  647. ('[a-zA-Z_]\w*', Name.Class, '#pop')
  648. ],
  649. 'import': [
  650. (r'(\s+)(as)(\s+)', bygroups(Text, Keyword, Text)),
  651. (r'[a-zA-Z_][\w.]*', Name.Namespace),
  652. (r'(\s*)(,)(\s*)', bygroups(Text, Operator, Text)),
  653. default('#pop') # all else: go back
  654. ],
  655. 'fromimport': [
  656. (r'(\s+)(c?import)\b', bygroups(Text, Keyword), '#pop'),
  657. (r'[a-zA-Z_.][\w.]*', Name.Namespace),
  658. # ``cdef foo from "header"``, or ``for foo from 0 < i < 10``
  659. default('#pop'),
  660. ],
  661. 'stringescape': [
  662. (r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|'
  663. r'U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})', String.Escape)
  664. ],
  665. 'strings': [
  666. (r'%(\([a-zA-Z0-9]+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?'
  667. '[hlL]?[E-GXc-giorsux%]', String.Interpol),
  668. (r'[^\\\'"%\n]+', String),
  669. # quotes, percents and backslashes must be parsed one at a time
  670. (r'[\'"\\]', String),
  671. # unhandled string formatting sign
  672. (r'%', String)
  673. # newlines are an error (use "nl" state)
  674. ],
  675. 'nl': [
  676. (r'\n', String)
  677. ],
  678. 'dqs': [
  679. (r'"', String, '#pop'),
  680. (r'\\\\|\\"|\\\n', String.Escape), # included here again for raw strings
  681. include('strings')
  682. ],
  683. 'sqs': [
  684. (r"'", String, '#pop'),
  685. (r"\\\\|\\'|\\\n", String.Escape), # included here again for raw strings
  686. include('strings')
  687. ],
  688. 'tdqs': [
  689. (r'"""', String, '#pop'),
  690. include('strings'),
  691. include('nl')
  692. ],
  693. 'tsqs': [
  694. (r"'''", String, '#pop'),
  695. include('strings'),
  696. include('nl')
  697. ],
  698. }
  699. class DgLexer(RegexLexer):
  700. """
  701. Lexer for `dg <http://pyos.github.com/dg>`_,
  702. a functional and object-oriented programming language
  703. running on the CPython 3 VM.
  704. .. versionadded:: 1.6
  705. """
  706. name = 'dg'
  707. aliases = ['dg']
  708. filenames = ['*.dg']
  709. mimetypes = ['text/x-dg']
  710. tokens = {
  711. 'root': [
  712. (r'\s+', Text),
  713. (r'#.*?$', Comment.Single),
  714. (r'(?i)0b[01]+', Number.Bin),
  715. (r'(?i)0o[0-7]+', Number.Oct),
  716. (r'(?i)0x[0-9a-f]+', Number.Hex),
  717. (r'(?i)[+-]?[0-9]+\.[0-9]+(e[+-]?[0-9]+)?j?', Number.Float),
  718. (r'(?i)[+-]?[0-9]+e[+-]?\d+j?', Number.Float),
  719. (r'(?i)[+-]?[0-9]+j?', Number.Integer),
  720. (r"(?i)(br|r?b?)'''", String, combined('stringescape', 'tsqs', 'string')),
  721. (r'(?i)(br|r?b?)"""', String, combined('stringescape', 'tdqs', 'string')),
  722. (r"(?i)(br|r?b?)'", String, combined('stringescape', 'sqs', 'string')),
  723. (r'(?i)(br|r?b?)"', String, combined('stringescape', 'dqs', 'string')),
  724. (r"`\w+'*`", Operator),
  725. (r'\b(and|in|is|or|where)\b', Operator.Word),
  726. (r'[!$%&*+\-./:<-@\\^|~;,]+', Operator),
  727. (words((
  728. 'bool', 'bytearray', 'bytes', 'classmethod', 'complex', 'dict', 'dict\'',
  729. 'float', 'frozenset', 'int', 'list', 'list\'', 'memoryview', 'object',
  730. 'property', 'range', 'set', 'set\'', 'slice', 'staticmethod', 'str',
  731. 'super', 'tuple', 'tuple\'', 'type'),
  732. prefix=r'(?<!\.)', suffix=r'(?![\'\w])'),
  733. Name.Builtin),
  734. (words((
  735. '__import__', 'abs', 'all', 'any', 'bin', 'bind', 'chr', 'cmp', 'compile',
  736. 'complex', 'delattr', 'dir', 'divmod', 'drop', 'dropwhile', 'enumerate',
  737. 'eval', 'exhaust', 'filter', 'flip', 'foldl1?', 'format', 'fst',
  738. 'getattr', 'globals', 'hasattr', 'hash', 'head', 'hex', 'id', 'init',
  739. 'input', 'isinstance', 'issubclass', 'iter', 'iterate', 'last', 'len',
  740. 'locals', 'map', 'max', 'min', 'next', 'oct', 'open', 'ord', 'pow',
  741. 'print', 'repr', 'reversed', 'round', 'setattr', 'scanl1?', 'snd',
  742. 'sorted', 'sum', 'tail', 'take', 'takewhile', 'vars', 'zip'),
  743. prefix=r'(?<!\.)', suffix=r'(?![\'\w])'),
  744. Name.Builtin),
  745. (r"(?<!\.)(self|Ellipsis|NotImplemented|None|True|False)(?!['\w])",
  746. Name.Builtin.Pseudo),
  747. (r"(?<!\.)[A-Z]\w*(Error|Exception|Warning)'*(?!['\w])",
  748. Name.Exception),
  749. (r"(?<!\.)(Exception|GeneratorExit|KeyboardInterrupt|StopIteration|"
  750. r"SystemExit)(?!['\w])", Name.Exception),
  751. (r"(?<![\w.])(except|finally|for|if|import|not|otherwise|raise|"
  752. r"subclass|while|with|yield)(?!['\w])", Keyword.Reserved),
  753. (r"[A-Z_]+'*(?!['\w])", Name),
  754. (r"[A-Z]\w+'*(?!['\w])", Keyword.Type),
  755. (r"\w+'*", Name),
  756. (r'[()]', Punctuation),
  757. (r'.', Error),
  758. ],
  759. 'stringescape': [
  760. (r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|'
  761. r'U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})', String.Escape)
  762. ],
  763. 'string': [
  764. (r'%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?'
  765. '[hlL]?[E-GXc-giorsux%]', String.Interpol),
  766. (r'[^\\\'"%\n]+', String),
  767. # quotes, percents and backslashes must be parsed one at a time
  768. (r'[\'"\\]', String),
  769. # unhandled string formatting sign
  770. (r'%', String),
  771. (r'\n', String)
  772. ],
  773. 'dqs': [
  774. (r'"', String, '#pop')
  775. ],
  776. 'sqs': [
  777. (r"'", String, '#pop')
  778. ],
  779. 'tdqs': [
  780. (r'"""', String, '#pop')
  781. ],
  782. 'tsqs': [
  783. (r"'''", String, '#pop')
  784. ],
  785. }
  786. class NumPyLexer(PythonLexer):
  787. """
  788. A Python lexer recognizing Numerical Python builtins.
  789. .. versionadded:: 0.10
  790. """
  791. name = 'NumPy'
  792. aliases = ['numpy']
  793. # override the mimetypes to not inherit them from python
  794. mimetypes = []
  795. filenames = []
  796. EXTRA_KEYWORDS = set((
  797. 'abs', 'absolute', 'accumulate', 'add', 'alen', 'all', 'allclose',
  798. 'alltrue', 'alterdot', 'amax', 'amin', 'angle', 'any', 'append',
  799. 'apply_along_axis', 'apply_over_axes', 'arange', 'arccos', 'arccosh',
  800. 'arcsin', 'arcsinh', 'arctan', 'arctan2', 'arctanh', 'argmax', 'argmin',
  801. 'argsort', 'argwhere', 'around', 'array', 'array2string', 'array_equal',
  802. 'array_equiv', 'array_repr', 'array_split', 'array_str', 'arrayrange',
  803. 'asanyarray', 'asarray', 'asarray_chkfinite', 'ascontiguousarray',
  804. 'asfarray', 'asfortranarray', 'asmatrix', 'asscalar', 'astype',
  805. 'atleast_1d', 'atleast_2d', 'atleast_3d', 'average', 'bartlett',
  806. 'base_repr', 'beta', 'binary_repr', 'bincount', 'binomial',
  807. 'bitwise_and', 'bitwise_not', 'bitwise_or', 'bitwise_xor', 'blackman',
  808. 'bmat', 'broadcast', 'byte_bounds', 'bytes', 'byteswap', 'c_',
  809. 'can_cast', 'ceil', 'choose', 'clip', 'column_stack', 'common_type',
  810. 'compare_chararrays', 'compress', 'concatenate', 'conj', 'conjugate',
  811. 'convolve', 'copy', 'corrcoef', 'correlate', 'cos', 'cosh', 'cov',
  812. 'cross', 'cumprod', 'cumproduct', 'cumsum', 'delete', 'deprecate',
  813. 'diag', 'diagflat', 'diagonal', 'diff', 'digitize', 'disp', 'divide',
  814. 'dot', 'dsplit', 'dstack', 'dtype', 'dump', 'dumps', 'ediff1d', 'empty',
  815. 'empty_like', 'equal', 'exp', 'expand_dims', 'expm1', 'extract', 'eye',
  816. 'fabs', 'fastCopyAndTranspose', 'fft', 'fftfreq', 'fftshift', 'fill',
  817. 'finfo', 'fix', 'flat', 'flatnonzero', 'flatten', 'fliplr', 'flipud',
  818. 'floor', 'floor_divide', 'fmod', 'frexp', 'fromarrays', 'frombuffer',
  819. 'fromfile', 'fromfunction', 'fromiter', 'frompyfunc', 'fromstring',
  820. 'generic', 'get_array_wrap', 'get_include', 'get_numarray_include',
  821. 'get_numpy_include', 'get_printoptions', 'getbuffer', 'getbufsize',
  822. 'geterr', 'geterrcall', 'geterrobj', 'getfield', 'gradient', 'greater',
  823. 'greater_equal', 'gumbel', 'hamming', 'hanning', 'histogram',
  824. 'histogram2d', 'histogramdd', 'hsplit', 'hstack', 'hypot', 'i0',
  825. 'identity', 'ifft', 'imag', 'index_exp', 'indices', 'inf', 'info',
  826. 'inner', 'insert', 'int_asbuffer', 'interp', 'intersect1d',
  827. 'intersect1d_nu', 'inv', 'invert', 'iscomplex', 'iscomplexobj',
  828. 'isfinite', 'isfortran', 'isinf', 'isnan', 'isneginf', 'isposinf',
  829. 'isreal', 'isrealobj', 'isscalar', 'issctype', 'issubclass_',
  830. 'issubdtype', 'issubsctype', 'item', 'itemset', 'iterable', 'ix_',
  831. 'kaiser', 'kron', 'ldexp', 'left_shift', 'less', 'less_equal', 'lexsort',
  832. 'linspace', 'load', 'loads', 'loadtxt', 'log', 'log10', 'log1p', 'log2',
  833. 'logical_and', 'logical_not', 'logical_or', 'logical_xor', 'logspace',
  834. 'lstsq', 'mat', 'matrix', 'max', 'maximum', 'maximum_sctype',
  835. 'may_share_memory', 'mean', 'median', 'meshgrid', 'mgrid', 'min',
  836. 'minimum', 'mintypecode', 'mod', 'modf', 'msort', 'multiply', 'nan',
  837. 'nan_to_num', 'nanargmax', 'nanargmin', 'nanmax', 'nanmin', 'nansum',
  838. 'ndenumerate', 'ndim', 'ndindex', 'negative', 'newaxis', 'newbuffer',
  839. 'newbyteorder', 'nonzero', 'not_equal', 'obj2sctype', 'ogrid', 'ones',
  840. 'ones_like', 'outer', 'permutation', 'piecewise', 'pinv', 'pkgload',
  841. 'place', 'poisson', 'poly', 'poly1d', 'polyadd', 'polyder', 'polydiv',
  842. 'polyfit', 'polyint', 'polymul', 'polysub', 'polyval', 'power', 'prod',
  843. 'product', 'ptp', 'put', 'putmask', 'r_', 'randint', 'random_integers',
  844. 'random_sample', 'ranf', 'rank', 'ravel', 'real', 'real_if_close',
  845. 'recarray', 'reciprocal', 'reduce', 'remainder', 'repeat', 'require',
  846. 'reshape', 'resize', 'restoredot', 'right_shift', 'rint', 'roll',
  847. 'rollaxis', 'roots', 'rot90', 'round', 'round_', 'row_stack', 's_',
  848. 'sample', 'savetxt', 'sctype2char', 'searchsorted', 'seed', 'select',
  849. 'set_numeric_ops', 'set_printoptions', 'set_string_function',
  850. 'setbufsize', 'setdiff1d', 'seterr', 'seterrcall', 'seterrobj',
  851. 'setfield', 'setflags', 'setmember1d', 'setxor1d', 'shape',
  852. 'show_config', 'shuffle', 'sign', 'signbit', 'sin', 'sinc', 'sinh',
  853. 'size', 'slice', 'solve', 'sometrue', 'sort', 'sort_complex', 'source',
  854. 'split', 'sqrt', 'square', 'squeeze', 'standard_normal', 'std',
  855. 'subtract', 'sum', 'svd', 'swapaxes', 'take', 'tan', 'tanh', 'tensordot',
  856. 'test', 'tile', 'tofile', 'tolist', 'tostring', 'trace', 'transpose',
  857. 'trapz', 'tri', 'tril', 'trim_zeros', 'triu', 'true_divide', 'typeDict',
  858. 'typename', 'uniform', 'union1d', 'unique', 'unique1d', 'unravel_index',
  859. 'unwrap', 'vander', 'var', 'vdot', 'vectorize', 'view', 'vonmises',
  860. 'vsplit', 'vstack', 'weibull', 'where', 'who', 'zeros', 'zeros_like'
  861. ))
  862. def get_tokens_unprocessed(self, text):
  863. for index, token, value in \
  864. PythonLexer.get_tokens_unprocessed(self, text):
  865. if token is Name and value in self.EXTRA_KEYWORDS:
  866. yield index, Keyword.Pseudo, value
  867. else:
  868. yield index, token, value
  869. def analyse_text(text):
  870. return (shebang_matches(text, r'pythonw?(2(\.\d)?)?') or
  871. 'import ' in text[:1000]) \
  872. and ('import numpy' in text or 'from numpy import' in text)