diff options
author | gbrandl <devnull@localhost> | 2007-01-14 15:52:37 +0100 |
---|---|---|
committer | gbrandl <devnull@localhost> | 2007-01-14 15:52:37 +0100 |
commit | 974e1a35f59d29f619dcd0236c3415073ef4e3fb (patch) | |
tree | 9f05f9c303e571cd911ebc899e664c33de92bcf8 | |
parent | b0f8f009b7987f3003819612b2d614322c46bede (diff) | |
download | pygments-974e1a35f59d29f619dcd0236c3415073ef4e3fb.tar.gz |
[svn] Add helpful exception for people doing highlight(code, PythonLexer, ...).
-rw-r--r-- | pygments/__init__.py | 36 | ||||
-rw-r--r-- | pygments/lexers/agile.py | 2 |
2 files changed, 26 insertions, 12 deletions
diff --git a/pygments/__init__.py b/pygments/__init__.py index 6f3fdebc..15413509 100644 --- a/pygments/__init__.py +++ b/pygments/__init__.py @@ -24,10 +24,10 @@ """ __version__ = '0.7' -__docformat__ = 'restructuredtext' -__license__ = 'BSD License' __author__ = 'Georg Brandl <g.brandl@gmx.net>' __url__ = 'http://pygments.pocoo.org/' +__license__ = 'BSD License' +__docformat__ = 'restructuredtext' __all__ = ['lex', 'format', 'highlight'] @@ -41,7 +41,14 @@ def lex(code, lexer): """ Lex ``code`` with ``lexer`` and return an iterable of tokens. """ - return lexer.get_tokens(code) + try: + return lexer.get_tokens(code) + except TypeError, err: + if isinstance(err.args[0], str) and \ + 'unbound method get_tokens' in err.args[0]: + raise TypeError('lex() argument must be a lexer instance, ' + 'not a class') + raise def format(tokens, formatter, outfile=None): @@ -52,14 +59,21 @@ def format(tokens, formatter, outfile=None): with a ``write`` method), the result will be written to it, otherwise it is returned as a string. """ - if not outfile: - # if we want Unicode output, we have to use Python StringIO - realoutfile = formatter.encoding and CStringIO() or StringIO() - formatter.format(tokens, realoutfile) - return realoutfile.getvalue() - else: - formatter.format(tokens, outfile) - + try: + if not outfile: + # if we want Unicode output, we have to use Python StringIO + realoutfile = formatter.encoding and CStringIO() or StringIO() + formatter.format(tokens, realoutfile) + return realoutfile.getvalue() + else: + formatter.format(tokens, outfile) + except TypeError, err: + if isinstance(err.args[0], str) and \ + 'unbound method format' in err.args[0]: + raise TypeError('format() argument must be a formatter instance, ' + 'not a class') + raise + def highlight(code, lexer, formatter, outfile=None): """ diff --git a/pygments/lexers/agile.py b/pygments/lexers/agile.py index b4c33f73..ebe7e90d 100644 --- a/pygments/lexers/agile.py +++ b/pygments/lexers/agile.py @@ -223,7 +223,7 @@ class PythonTracebackLexer(RegexLexer): *New in Pygments 0.7.* """ - name = 'PythonTraceback' + name = 'Python Traceback' aliases = ['pytb'] filenames = ['*.pytb'] mimetypes = ['text/x-python-traceback'] |