Mercurial > hg
annotate hgext/highlight/highlight.py @ 20742:3681de20b0a7
parsers: fail fast if Python has wrong minor version (issue4110)
This change causes an informative ImportError to be raised when importing
the parsers extension module if the minor version of the currently-running
Python interpreter doesn't match that of the Python used when compiling
the extension module.
This change also exposes a parsers.versionerrortext constant in the
C implementation of the module. Its presence can be used to determine
whether this behavior is present in a version of the module. The value
of the constant is the leading text of the ImportError raised and is set
to "Python minor version mismatch".
Here is an example of what the new error looks like:
Traceback (most recent call last):
File "test.py", line 1, in <module>
import mercurial.parsers
ImportError: Python minor version mismatch: The Mercurial extension
modules were compiled with Python 2.7.6, but Mercurial is currently using
Python with sys.hexversion=33883888: Python 2.5.6
(r256:88840, Nov 18 2012, 05:37:10)
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))]
at: /opt/local/Library/Frameworks/Python.framework/Versions/2.5/Resources/
Python.app/Contents/MacOS/Python
The reason for raising an error in this scenario is that Python's C API
is known not to be compatible from minor version to minor version, even
if sys.api_version is the same. See for example this Python bug report
about incompatibilities between 2.5 and 2.6+:
http://bugs.python.org/issue8118
These incompatibilities can cause Mercurial to break in mysterious,
unforeseen ways. For example, when Mercurial compiled with Python 2.7 was
run with 2.5, the following crash occurred when running "hg status":
http://bz.selenic.com/show_bug.cgi?id=4110
After this crash was fixed, running with Python 2.5 no longer crashes, but
the following puzzling behavior still occurs:
$ hg status
...
File ".../mercurial/changelog.py", line 123, in __init__
revlog.revlog.__init__(self, opener, "00changelog.i")
File ".../mercurial/revlog.py", line 251, in __init__
d = self._io.parseindex(i, self._inline)
File ".../mercurial/revlog.py", line 158, in parseindex
index, cache = parsers.parse_index2(data, inline)
TypeError: data is not a string
which can be reproduced more simply with:
import mercurial.parsers as parsers
parsers.parse_index2("", True)
Both the crash and the TypeError occurred because the Python C API's
PyString_Check() returns the wrong value when the C header files from
Python 2.7 are run with Python 2.5. This is an example of an
incompatibility of the sort mentioned in the Python bug report above.
Failing fast with an informative error message results in a better user
experience in cases like the above. The information in the ImportError
also simplifies troubleshooting for those on Mercurial mailing lists, the
bug tracker, etc.
This patch only adds the version check to parsers.c, which is sufficient
to affect command-line commands like "hg status" and "hg summary".
An idea for a future improvement is to move the version-checking C code
to a more central location, and have it run when importing all
Mercurial extension modules and not just parsers.c.
author | Chris Jerdonek <chris.jerdonek@gmail.com> |
---|---|
date | Wed, 04 Dec 2013 20:38:27 -0800 |
parents | bcdfb6078b9f |
children | 7b8ff3fd11d3 |
rev | line source |
---|---|
8251
7fc30044b514
highlight: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents:
7948
diff
changeset
|
1 # highlight.py - highlight extension implementation file |
7fc30044b514
highlight: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents:
7948
diff
changeset
|
2 # |
7fc30044b514
highlight: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents:
7948
diff
changeset
|
3 # Copyright 2007-2009 Adam Hupp <adam@hupp.org> and others |
7fc30044b514
highlight: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents:
7948
diff
changeset
|
4 # |
7fc30044b514
highlight: add copyright and license header
Martin Geisler <mg@lazybytes.net>
parents:
7948
diff
changeset
|
5 # This software may be used and distributed according to the terms of the |
10263 | 6 # GNU General Public License version 2 or any later version. |
6938
ce94b3236ea4
highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
6666
diff
changeset
|
7 # |
ce94b3236ea4
highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
6666
diff
changeset
|
8 # The original module was split in an interface and an implementation |
ce94b3236ea4
highlight: split code to improve startup times
Patrick Mezard <pmezard@gmail.com>
parents:
6666
diff
changeset
|
9 # file to defer pygments loading and speedup extension setup. |
5532
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
10 |
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
11 from mercurial import demandimport |
10394
4612cded5176
fix coding style (reported by pylint)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10263
diff
changeset
|
12 demandimport.ignore.extend(['pkgutil', 'pkg_resources', '__main__']) |
7948
de377b1a9a84
move encoding bits from util to encoding
Matt Mackall <mpm@selenic.com>
parents:
7120
diff
changeset
|
13 from mercurial import util, encoding |
5532
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
14 |
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
15 from pygments import highlight |
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
16 from pygments.util import ClassNotFound |
6199
0068809347d7
highlight: forgot import
Brendan Cully <brendan@kublai.com>
parents:
6198
diff
changeset
|
17 from pygments.lexers import guess_lexer, guess_lexer_for_filename, TextLexer |
5532
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
18 from pygments.formatters import HtmlFormatter |
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
19 |
6485
938319418d8c
highlight: Generate pygments style sheet dynamically
Isaac Jurado <diptongo@gmail.com>
parents:
6394
diff
changeset
|
20 SYNTAX_CSS = ('\n<link rel="stylesheet" href="{url}highlightcss" ' |
5533
6cf7d7fe7d3d
highlight: clean up coding style a little
Bryan O'Sullivan <bos@serpentine.com>
parents:
5532
diff
changeset
|
21 'type="text/css" />') |
5532
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
22 |
6394
55bc0a035e1f
highlight: some small cleanups
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6393
diff
changeset
|
23 def pygmentize(field, fctx, style, tmpl): |
55bc0a035e1f
highlight: some small cleanups
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
6393
diff
changeset
|
24 |
5532
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
25 # append a <link ...> to the syntax highlighting css |
10959
d1f4657f55e4
highlight: fix to work with caching templater
Matt Mackall <mpm@selenic.com>
parents:
10394
diff
changeset
|
26 old_header = tmpl.load('header') |
5532
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
27 if SYNTAX_CSS not in old_header: |
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
28 new_header = old_header + SYNTAX_CSS |
5616
88ca3e0fb6e5
highlight: adapt to hgweb_mode refactoring
Christian Ebert <blacktrash@gmx.net>
parents:
5533
diff
changeset
|
29 tmpl.cache['header'] = new_header |
5532
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
30 |
6194
fe54e7501de1
highlight: bail out if file is binary
Brendan Cully <brendan@kublai.com>
parents:
6193
diff
changeset
|
31 text = fctx.data() |
fe54e7501de1
highlight: bail out if file is binary
Brendan Cully <brendan@kublai.com>
parents:
6193
diff
changeset
|
32 if util.binary(text): |
fe54e7501de1
highlight: bail out if file is binary
Brendan Cully <brendan@kublai.com>
parents:
6193
diff
changeset
|
33 return |
fe54e7501de1
highlight: bail out if file is binary
Brendan Cully <brendan@kublai.com>
parents:
6193
diff
changeset
|
34 |
9424
799373ff2554
highlight: fixes garbled text in non-UTF-8 environment
Yuya Nishihara <yuya@tcha.org>
parents:
8360
diff
changeset
|
35 # Pygments is best used with Unicode strings: |
799373ff2554
highlight: fixes garbled text in non-UTF-8 environment
Yuya Nishihara <yuya@tcha.org>
parents:
8360
diff
changeset
|
36 # <http://pygments.org/docs/unicode/> |
799373ff2554
highlight: fixes garbled text in non-UTF-8 environment
Yuya Nishihara <yuya@tcha.org>
parents:
8360
diff
changeset
|
37 text = text.decode(encoding.encoding, 'replace') |
7120
db7557359636
highlight: convert text to local before passing to pygmentize (issue1341)
Christian Ebert <blacktrash@gmx.net>
parents:
6938
diff
changeset
|
38 |
6193
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
39 # To get multi-line strings right, we can't format line-by-line |
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
40 try: |
19169
bcdfb6078b9f
highlight: fix page layout with empty first and last lines
Alexander Plavin <me@aplavin.ru>
parents:
18054
diff
changeset
|
41 lexer = guess_lexer_for_filename(fctx.path(), text[:1024], |
bcdfb6078b9f
highlight: fix page layout with empty first and last lines
Alexander Plavin <me@aplavin.ru>
parents:
18054
diff
changeset
|
42 stripnl=False) |
6494
c30849d4c8ba
highlight: backward compatibility with pygments 0.5.1
Benoit Allard <benoit@aeteurope.nl>
parents:
6212
diff
changeset
|
43 except (ClassNotFound, ValueError): |
6198
358cc9cf54db
highlight: guess by text when path name is ambiguous
Brendan Cully <brendan@kublai.com>
parents:
6197
diff
changeset
|
44 try: |
19169
bcdfb6078b9f
highlight: fix page layout with empty first and last lines
Alexander Plavin <me@aplavin.ru>
parents:
18054
diff
changeset
|
45 lexer = guess_lexer(text[:1024], stripnl=False) |
6494
c30849d4c8ba
highlight: backward compatibility with pygments 0.5.1
Benoit Allard <benoit@aeteurope.nl>
parents:
6212
diff
changeset
|
46 except (ClassNotFound, ValueError): |
19169
bcdfb6078b9f
highlight: fix page layout with empty first and last lines
Alexander Plavin <me@aplavin.ru>
parents:
18054
diff
changeset
|
47 lexer = TextLexer(stripnl=False) |
6193
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
48 |
9424
799373ff2554
highlight: fixes garbled text in non-UTF-8 environment
Yuya Nishihara <yuya@tcha.org>
parents:
8360
diff
changeset
|
49 formatter = HtmlFormatter(style=style) |
5532
40a06e39f010
extension for synax highlighting in the hgweb file revision view
Adam Hupp <adam@hupp.org>
parents:
diff
changeset
|
50 |
6193
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
51 colorized = highlight(text, lexer, formatter) |
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
52 # strip wrapping div |
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
53 colorized = colorized[:colorized.find('\n</pre>')] |
18054
b35e3364f94a
check-code: there must also be whitespace between ')' and operator
Mads Kiilerich <madski@unity3d.com>
parents:
10959
diff
changeset
|
54 colorized = colorized[colorized.find('<pre>') + 5:] |
9424
799373ff2554
highlight: fixes garbled text in non-UTF-8 environment
Yuya Nishihara <yuya@tcha.org>
parents:
8360
diff
changeset
|
55 coloriter = (s.encode(encoding.encoding, 'replace') |
799373ff2554
highlight: fixes garbled text in non-UTF-8 environment
Yuya Nishihara <yuya@tcha.org>
parents:
8360
diff
changeset
|
56 for s in colorized.splitlines()) |
6193
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
57 |
8360
acc202b71619
templater: provide the standard template filters by default
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
8251
diff
changeset
|
58 tmpl.filters['colorize'] = lambda x: coloriter.next() |
6193
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
59 |
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
60 oldl = tmpl.cache[field] |
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
61 newl = oldl.replace('line|escape', 'line|colorize') |
2344da8eb9b4
highlight: support annotate, and reduce layering violations.
Brendan Cully <brendan@kublai.com>
parents:
5991
diff
changeset
|
62 tmpl.cache[field] = newl |