##// END OF EJS Templates
highlight: fixes garbled text in non-UTF-8 environment...
Yuya Nishihara -
r9424:799373ff default
parent child Browse files
Show More
@@ -1,60 +1,61 b''
1 # highlight.py - highlight extension implementation file
1 # highlight.py - highlight extension implementation file
2 #
2 #
3 # Copyright 2007-2009 Adam Hupp <adam@hupp.org> and others
3 # Copyright 2007-2009 Adam Hupp <adam@hupp.org> and others
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2, incorporated herein by reference.
6 # GNU General Public License version 2, incorporated herein by reference.
7 #
7 #
8 # The original module was split in an interface and an implementation
8 # The original module was split in an interface and an implementation
9 # file to defer pygments loading and speedup extension setup.
9 # file to defer pygments loading and speedup extension setup.
10
10
11 from mercurial import demandimport
11 from mercurial import demandimport
12 demandimport.ignore.extend(['pkgutil', 'pkg_resources', '__main__',])
12 demandimport.ignore.extend(['pkgutil', 'pkg_resources', '__main__',])
13 from mercurial import util, encoding
13 from mercurial import util, encoding
14
14
15 from pygments import highlight
15 from pygments import highlight
16 from pygments.util import ClassNotFound
16 from pygments.util import ClassNotFound
17 from pygments.lexers import guess_lexer, guess_lexer_for_filename, TextLexer
17 from pygments.lexers import guess_lexer, guess_lexer_for_filename, TextLexer
18 from pygments.formatters import HtmlFormatter
18 from pygments.formatters import HtmlFormatter
19
19
20 SYNTAX_CSS = ('\n<link rel="stylesheet" href="{url}highlightcss" '
20 SYNTAX_CSS = ('\n<link rel="stylesheet" href="{url}highlightcss" '
21 'type="text/css" />')
21 'type="text/css" />')
22
22
23 def pygmentize(field, fctx, style, tmpl):
23 def pygmentize(field, fctx, style, tmpl):
24
24
25 # append a <link ...> to the syntax highlighting css
25 # append a <link ...> to the syntax highlighting css
26 old_header = ''.join(tmpl('header'))
26 old_header = ''.join(tmpl('header'))
27 if SYNTAX_CSS not in old_header:
27 if SYNTAX_CSS not in old_header:
28 new_header = old_header + SYNTAX_CSS
28 new_header = old_header + SYNTAX_CSS
29 tmpl.cache['header'] = new_header
29 tmpl.cache['header'] = new_header
30
30
31 text = fctx.data()
31 text = fctx.data()
32 if util.binary(text):
32 if util.binary(text):
33 return
33 return
34
34
35 # avoid UnicodeDecodeError in pygments
35 # Pygments is best used with Unicode strings:
36 text = encoding.tolocal(text)
36 # <http://pygments.org/docs/unicode/>
37 text = text.decode(encoding.encoding, 'replace')
37
38
38 # To get multi-line strings right, we can't format line-by-line
39 # To get multi-line strings right, we can't format line-by-line
39 try:
40 try:
40 lexer = guess_lexer_for_filename(fctx.path(), text[:1024],
41 lexer = guess_lexer_for_filename(fctx.path(), text[:1024])
41 encoding=encoding.encoding)
42 except (ClassNotFound, ValueError):
42 except (ClassNotFound, ValueError):
43 try:
43 try:
44 lexer = guess_lexer(text[:1024], encoding=encoding.encoding)
44 lexer = guess_lexer(text[:1024])
45 except (ClassNotFound, ValueError):
45 except (ClassNotFound, ValueError):
46 lexer = TextLexer(encoding=encoding.encoding)
46 lexer = TextLexer()
47
47
48 formatter = HtmlFormatter(style=style, encoding=encoding.encoding)
48 formatter = HtmlFormatter(style=style)
49
49
50 colorized = highlight(text, lexer, formatter)
50 colorized = highlight(text, lexer, formatter)
51 # strip wrapping div
51 # strip wrapping div
52 colorized = colorized[:colorized.find('\n</pre>')]
52 colorized = colorized[:colorized.find('\n</pre>')]
53 colorized = colorized[colorized.find('<pre>')+5:]
53 colorized = colorized[colorized.find('<pre>')+5:]
54 coloriter = iter(colorized.splitlines())
54 coloriter = (s.encode(encoding.encoding, 'replace')
55 for s in colorized.splitlines())
55
56
56 tmpl.filters['colorize'] = lambda x: coloriter.next()
57 tmpl.filters['colorize'] = lambda x: coloriter.next()
57
58
58 oldl = tmpl.cache[field]
59 oldl = tmpl.cache[field]
59 newl = oldl.replace('line|escape', 'line|colorize')
60 newl = oldl.replace('line|escape', 'line|colorize')
60 tmpl.cache[field] = newl
61 tmpl.cache[field] = newl
@@ -1,123 +1,148 b''
1 #!/bin/sh
1 #!/bin/sh
2
2
3 "$TESTDIR/hghave" pygments || exit 80
3 "$TESTDIR/hghave" pygments || exit 80
4
4
5 cat <<EOF >> $HGRCPATH
5 cat <<EOF >> $HGRCPATH
6 [extensions]
6 [extensions]
7 hgext.highlight =
7 hgext.highlight =
8 [web]
8 [web]
9 pygments_style = friendly
9 pygments_style = friendly
10 EOF
10 EOF
11
11
12 hg init test
12 hg init test
13 cd test
13 cd test
14 # create random Python file to exercise Pygments
14 # create random Python file to exercise Pygments
15 cat <<EOF > primes.py
15 cat <<EOF > primes.py
16 #!/usr/bin/env python
16 #!/usr/bin/env python
17
17
18 """Fun with generators. Corresponding Haskell implementation:
18 """Fun with generators. Corresponding Haskell implementation:
19
19
20 primes = 2 : sieve [3, 5..]
20 primes = 2 : sieve [3, 5..]
21 where sieve (p:ns) = p : sieve [n | n <- ns, mod n p /= 0]
21 where sieve (p:ns) = p : sieve [n | n <- ns, mod n p /= 0]
22 """
22 """
23
23
24 from itertools import dropwhile, ifilter, islice, count, chain
24 from itertools import dropwhile, ifilter, islice, count, chain
25
25
26 def primes():
26 def primes():
27 """Generate all primes."""
27 """Generate all primes."""
28 def sieve(ns):
28 def sieve(ns):
29 p = ns.next()
29 p = ns.next()
30 # It is important to yield *here* in order to stop the
30 # It is important to yield *here* in order to stop the
31 # infinite recursion.
31 # infinite recursion.
32 yield p
32 yield p
33 ns = ifilter(lambda n: n % p != 0, ns)
33 ns = ifilter(lambda n: n % p != 0, ns)
34 for n in sieve(ns):
34 for n in sieve(ns):
35 yield n
35 yield n
36
36
37 odds = ifilter(lambda i: i % 2 == 1, count())
37 odds = ifilter(lambda i: i % 2 == 1, count())
38 return chain([2], sieve(dropwhile(lambda n: n < 3, odds)))
38 return chain([2], sieve(dropwhile(lambda n: n < 3, odds)))
39
39
40 if __name__ == "__main__":
40 if __name__ == "__main__":
41 import sys
41 import sys
42 try:
42 try:
43 n = int(sys.argv[1])
43 n = int(sys.argv[1])
44 except (ValueError, IndexError):
44 except (ValueError, IndexError):
45 n = 10
45 n = 10
46 p = primes()
46 p = primes()
47 print "The first %d primes: %s" % (n, list(islice(p, n)))
47 print "The first %d primes: %s" % (n, list(islice(p, n)))
48 EOF
48 EOF
49
49
50 # check for UnicodeDecodeError with iso-8859-1 file contents
50 # check for UnicodeDecodeError with iso-8859-1 file contents
51 python -c 'fp = open("isolatin.txt", "w"); fp.write("h\xFCbsch\n"); fp.close();'
51 python -c 'fp = open("isolatin.txt", "w"); fp.write("h\xFCbsch\n"); fp.close();'
52
52
53 hg ci -Ama
53 hg ci -Ama
54
54
55 echo % hg serve
55 echo % hg serve
56 hg serve -p $HGPORT -d -n test --pid-file=hg.pid -A access.log -E errors.log
56 hg serve -p $HGPORT -d -n test --pid-file=hg.pid -A access.log -E errors.log
57 cat hg.pid >> $DAEMON_PIDS
57 cat hg.pid >> $DAEMON_PIDS
58
58
59 echo % hgweb filerevision, html
59 echo % hgweb filerevision, html
60 ("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/file/tip/primes.py') \
60 ("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/file/tip/primes.py') \
61 | sed "s/class=\"k\"/class=\"kn\"/g" | sed "s/class=\"mf\"/class=\"mi\"/g"
61 | sed "s/class=\"k\"/class=\"kn\"/g" | sed "s/class=\"mf\"/class=\"mi\"/g"
62
62
63 echo % hgweb filerevision, html
63 echo % hgweb filerevision, html
64 ("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/file/tip/isolatin.txt') \
64 ("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/file/tip/isolatin.txt') \
65 | sed "s/class=\"k\"/class=\"kn\"/g"
65 | sed "s/class=\"k\"/class=\"kn\"/g"
66
66
67 echo % hgweb fileannotate, html
67 echo % hgweb fileannotate, html
68 ("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/annotate/tip/primes.py') \
68 ("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/annotate/tip/primes.py') \
69 | sed "s/class=\"k\"/class=\"kn\"/g" | sed "s/class=\"mi\"/class=\"mf\"/g"
69 | sed "s/class=\"k\"/class=\"kn\"/g" | sed "s/class=\"mi\"/class=\"mf\"/g"
70
70
71 echo % hgweb fileannotate, raw
71 echo % hgweb fileannotate, raw
72 ("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/annotate/tip/primes.py?style=raw') \
72 ("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/annotate/tip/primes.py?style=raw') \
73 | sed "s/test@//" > a
73 | sed "s/test@//" > a
74
74
75 echo "200 Script output follows" > b
75 echo "200 Script output follows" > b
76 echo "" >> b
76 echo "" >> b
77 echo "" >> b
77 echo "" >> b
78 hg annotate "primes.py" >> b
78 hg annotate "primes.py" >> b
79 echo "" >> b
79 echo "" >> b
80 echo "" >> b
80 echo "" >> b
81 echo "" >> b
81 echo "" >> b
82 echo "" >> b
82 echo "" >> b
83
83
84 diff -u b a
84 diff -u b a
85
85
86 echo
86 echo
87 echo % hgweb filerevision, raw
87 echo % hgweb filerevision, raw
88 ("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/file/tip/primes.py?style=raw') \
88 ("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/file/tip/primes.py?style=raw') \
89 > a
89 > a
90
90
91 echo "200 Script output follows" > b
91 echo "200 Script output follows" > b
92 echo "" >> b
92 echo "" >> b
93 hg cat primes.py >> b
93 hg cat primes.py >> b
94
94
95 diff -u b a
95 diff -u b a
96
96
97 echo
97 echo
98 echo % hgweb highlightcss friendly
98 echo % hgweb highlightcss friendly
99 "$TESTDIR/get-with-headers.py" localhost:$HGPORT '/highlightcss' > out
99 "$TESTDIR/get-with-headers.py" localhost:$HGPORT '/highlightcss' > out
100 head -n 4 out
100 head -n 4 out
101 rm out
101 rm out
102
102
103 echo % errors encountered
103 echo % errors encountered
104 cat errors.log
104 cat errors.log
105 "$TESTDIR/killdaemons.py"
105 "$TESTDIR/killdaemons.py"
106
106
107 # Change the pygments style
107 # Change the pygments style
108 cat > .hg/hgrc <<EOF
108 cat > .hg/hgrc <<EOF
109 [web]
109 [web]
110 pygments_style = fruity
110 pygments_style = fruity
111 EOF
111 EOF
112
112
113 echo % hg serve again
113 echo % hg serve again
114 hg serve -p $HGPORT -d -n test --pid-file=hg.pid -A access.log -E errors.log
114 hg serve -p $HGPORT -d -n test --pid-file=hg.pid -A access.log -E errors.log
115 cat hg.pid >> $DAEMON_PIDS
115 cat hg.pid >> $DAEMON_PIDS
116
116
117 echo % hgweb highlightcss fruity
117 echo % hgweb highlightcss fruity
118 "$TESTDIR/get-with-headers.py" localhost:$HGPORT '/highlightcss' > out
118 "$TESTDIR/get-with-headers.py" localhost:$HGPORT '/highlightcss' > out
119 head -n 4 out
119 head -n 4 out
120 rm out
120 rm out
121
121
122 echo % errors encountered
122 echo % errors encountered
123 cat errors.log
123 cat errors.log
124
125 cd ..
126 hg init eucjp
127 cd eucjp
128
129 printf '\265\376\n' >> eucjp.txt # Japanese kanji "Kyo"
130
131 hg ci -Ama
132
133 hgserveget () {
134 "$TESTDIR/killdaemons.py"
135 echo % HGENCODING="$1" hg serve
136 HGENCODING="$1" hg serve -p $HGPORT -d -n test --pid-file=hg.pid -E errors.log
137 cat hg.pid >> $DAEMON_PIDS
138
139 echo % hgweb filerevision, html
140 "$TESTDIR/get-with-headers.py" localhost:$HGPORT "/file/tip/$2" \
141 | grep '<div class="parity0 source">' | $TESTDIR/printrepr.py
142 echo % errors encountered
143 cat errors.log
144 }
145
146 hgserveget euc-jp eucjp.txt
147 hgserveget utf-8 eucjp.txt
148 hgserveget us-ascii eucjp.txt
@@ -1,540 +1,553 b''
1 adding isolatin.txt
1 adding isolatin.txt
2 adding primes.py
2 adding primes.py
3 % hg serve
3 % hg serve
4 % hgweb filerevision, html
4 % hgweb filerevision, html
5 200 Script output follows
5 200 Script output follows
6
6
7 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
7 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
8 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
8 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
9 <head>
9 <head>
10 <link rel="icon" href="/static/hgicon.png" type="image/png" />
10 <link rel="icon" href="/static/hgicon.png" type="image/png" />
11 <meta name="robots" content="index, nofollow" />
11 <meta name="robots" content="index, nofollow" />
12 <link rel="stylesheet" href="/static/style-paper.css" type="text/css" />
12 <link rel="stylesheet" href="/static/style-paper.css" type="text/css" />
13
13
14 <link rel="stylesheet" href="/highlightcss" type="text/css" />
14 <link rel="stylesheet" href="/highlightcss" type="text/css" />
15 <title>test: 3e1445510fe7 primes.py</title>
15 <title>test: 3e1445510fe7 primes.py</title>
16 </head>
16 </head>
17 <body>
17 <body>
18
18
19 <div class="container">
19 <div class="container">
20 <div class="menu">
20 <div class="menu">
21 <div class="logo">
21 <div class="logo">
22 <a href="http://mercurial.selenic.com/">
22 <a href="http://mercurial.selenic.com/">
23 <img src="/static/hglogo.png" alt="mercurial" /></a>
23 <img src="/static/hglogo.png" alt="mercurial" /></a>
24 </div>
24 </div>
25 <ul>
25 <ul>
26 <li><a href="/shortlog/3e1445510fe7">log</a></li>
26 <li><a href="/shortlog/3e1445510fe7">log</a></li>
27 <li><a href="/graph/3e1445510fe7">graph</a></li>
27 <li><a href="/graph/3e1445510fe7">graph</a></li>
28 <li><a href="/tags">tags</a></li>
28 <li><a href="/tags">tags</a></li>
29 <li><a href="/branches">branches</a></li>
29 <li><a href="/branches">branches</a></li>
30 </ul>
30 </ul>
31 <ul>
31 <ul>
32 <li><a href="/rev/3e1445510fe7">changeset</a></li>
32 <li><a href="/rev/3e1445510fe7">changeset</a></li>
33 <li><a href="/file/3e1445510fe7/">browse</a></li>
33 <li><a href="/file/3e1445510fe7/">browse</a></li>
34 </ul>
34 </ul>
35 <ul>
35 <ul>
36 <li class="active">file</li>
36 <li class="active">file</li>
37 <li><a href="/diff/3e1445510fe7/primes.py">diff</a></li>
37 <li><a href="/diff/3e1445510fe7/primes.py">diff</a></li>
38 <li><a href="/annotate/3e1445510fe7/primes.py">annotate</a></li>
38 <li><a href="/annotate/3e1445510fe7/primes.py">annotate</a></li>
39 <li><a href="/log/3e1445510fe7/primes.py">file log</a></li>
39 <li><a href="/log/3e1445510fe7/primes.py">file log</a></li>
40 <li><a href="/raw-file/3e1445510fe7/primes.py">raw</a></li>
40 <li><a href="/raw-file/3e1445510fe7/primes.py">raw</a></li>
41 </ul>
41 </ul>
42 </div>
42 </div>
43
43
44 <div class="main">
44 <div class="main">
45 <h2><a href="/">test</a></h2>
45 <h2><a href="/">test</a></h2>
46 <h3>view primes.py @ 0:3e1445510fe7</h3>
46 <h3>view primes.py @ 0:3e1445510fe7</h3>
47
47
48 <form class="search" action="/log">
48 <form class="search" action="/log">
49
49
50 <p><input name="rev" id="search1" type="text" size="30" /></p>
50 <p><input name="rev" id="search1" type="text" size="30" /></p>
51 <div id="hint">find changesets by author, revision,
51 <div id="hint">find changesets by author, revision,
52 files, or words in the commit message</div>
52 files, or words in the commit message</div>
53 </form>
53 </form>
54
54
55 <div class="description">a</div>
55 <div class="description">a</div>
56
56
57 <table id="changesetEntry">
57 <table id="changesetEntry">
58 <tr>
58 <tr>
59 <th class="author">author</th>
59 <th class="author">author</th>
60 <td class="author">&#116;&#101;&#115;&#116;</td>
60 <td class="author">&#116;&#101;&#115;&#116;</td>
61 </tr>
61 </tr>
62 <tr>
62 <tr>
63 <th class="date">date</th>
63 <th class="date">date</th>
64 <td class="date">Thu Jan 01 00:00:00 1970 +0000 (many years ago)</td>
64 <td class="date">Thu Jan 01 00:00:00 1970 +0000 (many years ago)</td>
65 </tr>
65 </tr>
66 <tr>
66 <tr>
67 <th class="author">parents</th>
67 <th class="author">parents</th>
68 <td class="author"></td>
68 <td class="author"></td>
69 </tr>
69 </tr>
70 <tr>
70 <tr>
71 <th class="author">children</th>
71 <th class="author">children</th>
72 <td class="author"></td>
72 <td class="author"></td>
73 </tr>
73 </tr>
74
74
75 </table>
75 </table>
76
76
77 <div class="overflow">
77 <div class="overflow">
78 <div class="sourcefirst"> line source</div>
78 <div class="sourcefirst"> line source</div>
79
79
80 <div class="parity0 source"><a href="#l1" id="l1"> 1</a> <span class="c">#!/usr/bin/env python</span></div>
80 <div class="parity0 source"><a href="#l1" id="l1"> 1</a> <span class="c">#!/usr/bin/env python</span></div>
81 <div class="parity1 source"><a href="#l2" id="l2"> 2</a> </div>
81 <div class="parity1 source"><a href="#l2" id="l2"> 2</a> </div>
82 <div class="parity0 source"><a href="#l3" id="l3"> 3</a> <span class="sd">&quot;&quot;&quot;Fun with generators. Corresponding Haskell implementation:</span></div>
82 <div class="parity0 source"><a href="#l3" id="l3"> 3</a> <span class="sd">&quot;&quot;&quot;Fun with generators. Corresponding Haskell implementation:</span></div>
83 <div class="parity1 source"><a href="#l4" id="l4"> 4</a> </div>
83 <div class="parity1 source"><a href="#l4" id="l4"> 4</a> </div>
84 <div class="parity0 source"><a href="#l5" id="l5"> 5</a> <span class="sd">primes = 2 : sieve [3, 5..]</span></div>
84 <div class="parity0 source"><a href="#l5" id="l5"> 5</a> <span class="sd">primes = 2 : sieve [3, 5..]</span></div>
85 <div class="parity1 source"><a href="#l6" id="l6"> 6</a> <span class="sd"> where sieve (p:ns) = p : sieve [n | n &lt;- ns, mod n p /= 0]</span></div>
85 <div class="parity1 source"><a href="#l6" id="l6"> 6</a> <span class="sd"> where sieve (p:ns) = p : sieve [n | n &lt;- ns, mod n p /= 0]</span></div>
86 <div class="parity0 source"><a href="#l7" id="l7"> 7</a> <span class="sd">&quot;&quot;&quot;</span></div>
86 <div class="parity0 source"><a href="#l7" id="l7"> 7</a> <span class="sd">&quot;&quot;&quot;</span></div>
87 <div class="parity1 source"><a href="#l8" id="l8"> 8</a> </div>
87 <div class="parity1 source"><a href="#l8" id="l8"> 8</a> </div>
88 <div class="parity0 source"><a href="#l9" id="l9"> 9</a> <span class="kn">from</span> <span class="nn">itertools</span> <span class="kn">import</span> <span class="n">dropwhile</span><span class="p">,</span> <span class="n">ifilter</span><span class="p">,</span> <span class="n">islice</span><span class="p">,</span> <span class="n">count</span><span class="p">,</span> <span class="n">chain</span></div>
88 <div class="parity0 source"><a href="#l9" id="l9"> 9</a> <span class="kn">from</span> <span class="nn">itertools</span> <span class="kn">import</span> <span class="n">dropwhile</span><span class="p">,</span> <span class="n">ifilter</span><span class="p">,</span> <span class="n">islice</span><span class="p">,</span> <span class="n">count</span><span class="p">,</span> <span class="n">chain</span></div>
89 <div class="parity1 source"><a href="#l10" id="l10"> 10</a> </div>
89 <div class="parity1 source"><a href="#l10" id="l10"> 10</a> </div>
90 <div class="parity0 source"><a href="#l11" id="l11"> 11</a> <span class="kn">def</span> <span class="nf">primes</span><span class="p">():</span></div>
90 <div class="parity0 source"><a href="#l11" id="l11"> 11</a> <span class="kn">def</span> <span class="nf">primes</span><span class="p">():</span></div>
91 <div class="parity1 source"><a href="#l12" id="l12"> 12</a> <span class="sd">&quot;&quot;&quot;Generate all primes.&quot;&quot;&quot;</span></div>
91 <div class="parity1 source"><a href="#l12" id="l12"> 12</a> <span class="sd">&quot;&quot;&quot;Generate all primes.&quot;&quot;&quot;</span></div>
92 <div class="parity0 source"><a href="#l13" id="l13"> 13</a> <span class="kn">def</span> <span class="nf">sieve</span><span class="p">(</span><span class="n">ns</span><span class="p">):</span></div>
92 <div class="parity0 source"><a href="#l13" id="l13"> 13</a> <span class="kn">def</span> <span class="nf">sieve</span><span class="p">(</span><span class="n">ns</span><span class="p">):</span></div>
93 <div class="parity1 source"><a href="#l14" id="l14"> 14</a> <span class="n">p</span> <span class="o">=</span> <span class="n">ns</span><span class="o">.</span><span class="n">next</span><span class="p">()</span></div>
93 <div class="parity1 source"><a href="#l14" id="l14"> 14</a> <span class="n">p</span> <span class="o">=</span> <span class="n">ns</span><span class="o">.</span><span class="n">next</span><span class="p">()</span></div>
94 <div class="parity0 source"><a href="#l15" id="l15"> 15</a> <span class="c"># It is important to yield *here* in order to stop the</span></div>
94 <div class="parity0 source"><a href="#l15" id="l15"> 15</a> <span class="c"># It is important to yield *here* in order to stop the</span></div>
95 <div class="parity1 source"><a href="#l16" id="l16"> 16</a> <span class="c"># infinite recursion.</span></div>
95 <div class="parity1 source"><a href="#l16" id="l16"> 16</a> <span class="c"># infinite recursion.</span></div>
96 <div class="parity0 source"><a href="#l17" id="l17"> 17</a> <span class="kn">yield</span> <span class="n">p</span></div>
96 <div class="parity0 source"><a href="#l17" id="l17"> 17</a> <span class="kn">yield</span> <span class="n">p</span></div>
97 <div class="parity1 source"><a href="#l18" id="l18"> 18</a> <span class="n">ns</span> <span class="o">=</span> <span class="n">ifilter</span><span class="p">(</span><span class="kn">lambda</span> <span class="n">n</span><span class="p">:</span> <span class="n">n</span> <span class="o">%</span> <span class="n">p</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">ns</span><span class="p">)</span></div>
97 <div class="parity1 source"><a href="#l18" id="l18"> 18</a> <span class="n">ns</span> <span class="o">=</span> <span class="n">ifilter</span><span class="p">(</span><span class="kn">lambda</span> <span class="n">n</span><span class="p">:</span> <span class="n">n</span> <span class="o">%</span> <span class="n">p</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">ns</span><span class="p">)</span></div>
98 <div class="parity0 source"><a href="#l19" id="l19"> 19</a> <span class="kn">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">sieve</span><span class="p">(</span><span class="n">ns</span><span class="p">):</span></div>
98 <div class="parity0 source"><a href="#l19" id="l19"> 19</a> <span class="kn">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">sieve</span><span class="p">(</span><span class="n">ns</span><span class="p">):</span></div>
99 <div class="parity1 source"><a href="#l20" id="l20"> 20</a> <span class="kn">yield</span> <span class="n">n</span></div>
99 <div class="parity1 source"><a href="#l20" id="l20"> 20</a> <span class="kn">yield</span> <span class="n">n</span></div>
100 <div class="parity0 source"><a href="#l21" id="l21"> 21</a> </div>
100 <div class="parity0 source"><a href="#l21" id="l21"> 21</a> </div>
101 <div class="parity1 source"><a href="#l22" id="l22"> 22</a> <span class="n">odds</span> <span class="o">=</span> <span class="n">ifilter</span><span class="p">(</span><span class="kn">lambda</span> <span class="n">i</span><span class="p">:</span> <span class="n">i</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">1</span><span class="p">,</span> <span class="n">count</span><span class="p">())</span></div>
101 <div class="parity1 source"><a href="#l22" id="l22"> 22</a> <span class="n">odds</span> <span class="o">=</span> <span class="n">ifilter</span><span class="p">(</span><span class="kn">lambda</span> <span class="n">i</span><span class="p">:</span> <span class="n">i</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">1</span><span class="p">,</span> <span class="n">count</span><span class="p">())</span></div>
102 <div class="parity0 source"><a href="#l23" id="l23"> 23</a> <span class="kn">return</span> <span class="n">chain</span><span class="p">([</span><span class="mi">2</span><span class="p">],</span> <span class="n">sieve</span><span class="p">(</span><span class="n">dropwhile</span><span class="p">(</span><span class="kn">lambda</span> <span class="n">n</span><span class="p">:</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">3</span><span class="p">,</span> <span class="n">odds</span><span class="p">)))</span></div>
102 <div class="parity0 source"><a href="#l23" id="l23"> 23</a> <span class="kn">return</span> <span class="n">chain</span><span class="p">([</span><span class="mi">2</span><span class="p">],</span> <span class="n">sieve</span><span class="p">(</span><span class="n">dropwhile</span><span class="p">(</span><span class="kn">lambda</span> <span class="n">n</span><span class="p">:</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">3</span><span class="p">,</span> <span class="n">odds</span><span class="p">)))</span></div>
103 <div class="parity1 source"><a href="#l24" id="l24"> 24</a> </div>
103 <div class="parity1 source"><a href="#l24" id="l24"> 24</a> </div>
104 <div class="parity0 source"><a href="#l25" id="l25"> 25</a> <span class="kn">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span></div>
104 <div class="parity0 source"><a href="#l25" id="l25"> 25</a> <span class="kn">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span></div>
105 <div class="parity1 source"><a href="#l26" id="l26"> 26</a> <span class="kn">import</span> <span class="nn">sys</span></div>
105 <div class="parity1 source"><a href="#l26" id="l26"> 26</a> <span class="kn">import</span> <span class="nn">sys</span></div>
106 <div class="parity0 source"><a href="#l27" id="l27"> 27</a> <span class="kn">try</span><span class="p">:</span></div>
106 <div class="parity0 source"><a href="#l27" id="l27"> 27</a> <span class="kn">try</span><span class="p">:</span></div>
107 <div class="parity1 source"><a href="#l28" id="l28"> 28</a> <span class="n">n</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span></div>
107 <div class="parity1 source"><a href="#l28" id="l28"> 28</a> <span class="n">n</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span></div>
108 <div class="parity0 source"><a href="#l29" id="l29"> 29</a> <span class="kn">except</span> <span class="p">(</span><span class="ne">ValueError</span><span class="p">,</span> <span class="ne">IndexError</span><span class="p">):</span></div>
108 <div class="parity0 source"><a href="#l29" id="l29"> 29</a> <span class="kn">except</span> <span class="p">(</span><span class="ne">ValueError</span><span class="p">,</span> <span class="ne">IndexError</span><span class="p">):</span></div>
109 <div class="parity1 source"><a href="#l30" id="l30"> 30</a> <span class="n">n</span> <span class="o">=</span> <span class="mi">10</span></div>
109 <div class="parity1 source"><a href="#l30" id="l30"> 30</a> <span class="n">n</span> <span class="o">=</span> <span class="mi">10</span></div>
110 <div class="parity0 source"><a href="#l31" id="l31"> 31</a> <span class="n">p</span> <span class="o">=</span> <span class="n">primes</span><span class="p">()</span></div>
110 <div class="parity0 source"><a href="#l31" id="l31"> 31</a> <span class="n">p</span> <span class="o">=</span> <span class="n">primes</span><span class="p">()</span></div>
111 <div class="parity1 source"><a href="#l32" id="l32"> 32</a> <span class="kn">print</span> <span class="s">&quot;The first </span><span class="si">%d</span><span class="s"> primes: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="n">islice</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">n</span><span class="p">)))</span></div>
111 <div class="parity1 source"><a href="#l32" id="l32"> 32</a> <span class="kn">print</span> <span class="s">&quot;The first </span><span class="si">%d</span><span class="s"> primes: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="n">islice</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">n</span><span class="p">)))</span></div>
112 <div class="sourcelast"></div>
112 <div class="sourcelast"></div>
113 </div>
113 </div>
114 </div>
114 </div>
115 </div>
115 </div>
116
116
117
117
118
118
119 </body>
119 </body>
120 </html>
120 </html>
121
121
122 % hgweb filerevision, html
122 % hgweb filerevision, html
123 200 Script output follows
123 200 Script output follows
124
124
125 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
125 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
126 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
126 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
127 <head>
127 <head>
128 <link rel="icon" href="/static/hgicon.png" type="image/png" />
128 <link rel="icon" href="/static/hgicon.png" type="image/png" />
129 <meta name="robots" content="index, nofollow" />
129 <meta name="robots" content="index, nofollow" />
130 <link rel="stylesheet" href="/static/style-paper.css" type="text/css" />
130 <link rel="stylesheet" href="/static/style-paper.css" type="text/css" />
131
131
132 <link rel="stylesheet" href="/highlightcss" type="text/css" />
132 <link rel="stylesheet" href="/highlightcss" type="text/css" />
133 <title>test: 3e1445510fe7 isolatin.txt</title>
133 <title>test: 3e1445510fe7 isolatin.txt</title>
134 </head>
134 </head>
135 <body>
135 <body>
136
136
137 <div class="container">
137 <div class="container">
138 <div class="menu">
138 <div class="menu">
139 <div class="logo">
139 <div class="logo">
140 <a href="http://mercurial.selenic.com/">
140 <a href="http://mercurial.selenic.com/">
141 <img src="/static/hglogo.png" alt="mercurial" /></a>
141 <img src="/static/hglogo.png" alt="mercurial" /></a>
142 </div>
142 </div>
143 <ul>
143 <ul>
144 <li><a href="/shortlog/3e1445510fe7">log</a></li>
144 <li><a href="/shortlog/3e1445510fe7">log</a></li>
145 <li><a href="/graph/3e1445510fe7">graph</a></li>
145 <li><a href="/graph/3e1445510fe7">graph</a></li>
146 <li><a href="/tags">tags</a></li>
146 <li><a href="/tags">tags</a></li>
147 <li><a href="/branches">branches</a></li>
147 <li><a href="/branches">branches</a></li>
148 </ul>
148 </ul>
149 <ul>
149 <ul>
150 <li><a href="/rev/3e1445510fe7">changeset</a></li>
150 <li><a href="/rev/3e1445510fe7">changeset</a></li>
151 <li><a href="/file/3e1445510fe7/">browse</a></li>
151 <li><a href="/file/3e1445510fe7/">browse</a></li>
152 </ul>
152 </ul>
153 <ul>
153 <ul>
154 <li class="active">file</li>
154 <li class="active">file</li>
155 <li><a href="/diff/3e1445510fe7/isolatin.txt">diff</a></li>
155 <li><a href="/diff/3e1445510fe7/isolatin.txt">diff</a></li>
156 <li><a href="/annotate/3e1445510fe7/isolatin.txt">annotate</a></li>
156 <li><a href="/annotate/3e1445510fe7/isolatin.txt">annotate</a></li>
157 <li><a href="/log/3e1445510fe7/isolatin.txt">file log</a></li>
157 <li><a href="/log/3e1445510fe7/isolatin.txt">file log</a></li>
158 <li><a href="/raw-file/3e1445510fe7/isolatin.txt">raw</a></li>
158 <li><a href="/raw-file/3e1445510fe7/isolatin.txt">raw</a></li>
159 </ul>
159 </ul>
160 </div>
160 </div>
161
161
162 <div class="main">
162 <div class="main">
163 <h2><a href="/">test</a></h2>
163 <h2><a href="/">test</a></h2>
164 <h3>view isolatin.txt @ 0:3e1445510fe7</h3>
164 <h3>view isolatin.txt @ 0:3e1445510fe7</h3>
165
165
166 <form class="search" action="/log">
166 <form class="search" action="/log">
167
167
168 <p><input name="rev" id="search1" type="text" size="30" /></p>
168 <p><input name="rev" id="search1" type="text" size="30" /></p>
169 <div id="hint">find changesets by author, revision,
169 <div id="hint">find changesets by author, revision,
170 files, or words in the commit message</div>
170 files, or words in the commit message</div>
171 </form>
171 </form>
172
172
173 <div class="description">a</div>
173 <div class="description">a</div>
174
174
175 <table id="changesetEntry">
175 <table id="changesetEntry">
176 <tr>
176 <tr>
177 <th class="author">author</th>
177 <th class="author">author</th>
178 <td class="author">&#116;&#101;&#115;&#116;</td>
178 <td class="author">&#116;&#101;&#115;&#116;</td>
179 </tr>
179 </tr>
180 <tr>
180 <tr>
181 <th class="date">date</th>
181 <th class="date">date</th>
182 <td class="date">Thu Jan 01 00:00:00 1970 +0000 (many years ago)</td>
182 <td class="date">Thu Jan 01 00:00:00 1970 +0000 (many years ago)</td>
183 </tr>
183 </tr>
184 <tr>
184 <tr>
185 <th class="author">parents</th>
185 <th class="author">parents</th>
186 <td class="author"></td>
186 <td class="author"></td>
187 </tr>
187 </tr>
188 <tr>
188 <tr>
189 <th class="author">children</th>
189 <th class="author">children</th>
190 <td class="author"></td>
190 <td class="author"></td>
191 </tr>
191 </tr>
192
192
193 </table>
193 </table>
194
194
195 <div class="overflow">
195 <div class="overflow">
196 <div class="sourcefirst"> line source</div>
196 <div class="sourcefirst"> line source</div>
197
197
198 <div class="parity0 source"><a href="#l1" id="l1"> 1</a> h?bsch</div>
198 <div class="parity0 source"><a href="#l1" id="l1"> 1</a> h?bsch</div>
199 <div class="sourcelast"></div>
199 <div class="sourcelast"></div>
200 </div>
200 </div>
201 </div>
201 </div>
202 </div>
202 </div>
203
203
204
204
205
205
206 </body>
206 </body>
207 </html>
207 </html>
208
208
209 % hgweb fileannotate, html
209 % hgweb fileannotate, html
210 200 Script output follows
210 200 Script output follows
211
211
212 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
212 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
213 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
213 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
214 <head>
214 <head>
215 <link rel="icon" href="/static/hgicon.png" type="image/png" />
215 <link rel="icon" href="/static/hgicon.png" type="image/png" />
216 <meta name="robots" content="index, nofollow" />
216 <meta name="robots" content="index, nofollow" />
217 <link rel="stylesheet" href="/static/style-paper.css" type="text/css" />
217 <link rel="stylesheet" href="/static/style-paper.css" type="text/css" />
218
218
219 <link rel="stylesheet" href="/highlightcss" type="text/css" />
219 <link rel="stylesheet" href="/highlightcss" type="text/css" />
220 <title>test: primes.py annotate</title>
220 <title>test: primes.py annotate</title>
221 </head>
221 </head>
222 <body>
222 <body>
223
223
224 <div class="container">
224 <div class="container">
225 <div class="menu">
225 <div class="menu">
226 <div class="logo">
226 <div class="logo">
227 <a href="http://mercurial.selenic.com/">
227 <a href="http://mercurial.selenic.com/">
228 <img src="/static/hglogo.png" alt="mercurial" /></a>
228 <img src="/static/hglogo.png" alt="mercurial" /></a>
229 </div>
229 </div>
230 <ul>
230 <ul>
231 <li><a href="/shortlog/3e1445510fe7">log</a></li>
231 <li><a href="/shortlog/3e1445510fe7">log</a></li>
232 <li><a href="/graph/3e1445510fe7">graph</a></li>
232 <li><a href="/graph/3e1445510fe7">graph</a></li>
233 <li><a href="/tags">tags</a></li>
233 <li><a href="/tags">tags</a></li>
234 <li><a href="/branches">branches</a></li>
234 <li><a href="/branches">branches</a></li>
235 </ul>
235 </ul>
236
236
237 <ul>
237 <ul>
238 <li><a href="/rev/3e1445510fe7">changeset</a></li>
238 <li><a href="/rev/3e1445510fe7">changeset</a></li>
239 <li><a href="/file/3e1445510fe7/">browse</a></li>
239 <li><a href="/file/3e1445510fe7/">browse</a></li>
240 </ul>
240 </ul>
241 <ul>
241 <ul>
242 <li><a href="/file/3e1445510fe7/primes.py">file</a></li>
242 <li><a href="/file/3e1445510fe7/primes.py">file</a></li>
243 <li><a href="/diff/3e1445510fe7/primes.py">diff</a></li>
243 <li><a href="/diff/3e1445510fe7/primes.py">diff</a></li>
244 <li class="active">annotate</li>
244 <li class="active">annotate</li>
245 <li><a href="/log/3e1445510fe7/primes.py">file log</a></li>
245 <li><a href="/log/3e1445510fe7/primes.py">file log</a></li>
246 <li><a href="/raw-annotate/3e1445510fe7/primes.py">raw</a></li>
246 <li><a href="/raw-annotate/3e1445510fe7/primes.py">raw</a></li>
247 </ul>
247 </ul>
248 </div>
248 </div>
249
249
250 <div class="main">
250 <div class="main">
251 <h2><a href="/">test</a></h2>
251 <h2><a href="/">test</a></h2>
252 <h3>annotate primes.py @ 0:3e1445510fe7</h3>
252 <h3>annotate primes.py @ 0:3e1445510fe7</h3>
253
253
254 <form class="search" action="/log">
254 <form class="search" action="/log">
255
255
256 <p><input name="rev" id="search1" type="text" size="30" /></p>
256 <p><input name="rev" id="search1" type="text" size="30" /></p>
257 <div id="hint">find changesets by author, revision,
257 <div id="hint">find changesets by author, revision,
258 files, or words in the commit message</div>
258 files, or words in the commit message</div>
259 </form>
259 </form>
260
260
261 <div class="description">a</div>
261 <div class="description">a</div>
262
262
263 <table id="changesetEntry">
263 <table id="changesetEntry">
264 <tr>
264 <tr>
265 <th class="author">author</th>
265 <th class="author">author</th>
266 <td class="author">&#116;&#101;&#115;&#116;</td>
266 <td class="author">&#116;&#101;&#115;&#116;</td>
267 </tr>
267 </tr>
268 <tr>
268 <tr>
269 <th class="date">date</th>
269 <th class="date">date</th>
270 <td class="date">Thu Jan 01 00:00:00 1970 +0000 (many years ago)</td>
270 <td class="date">Thu Jan 01 00:00:00 1970 +0000 (many years ago)</td>
271 </tr>
271 </tr>
272 <tr>
272 <tr>
273 <th class="author">parents</th>
273 <th class="author">parents</th>
274 <td class="author"></td>
274 <td class="author"></td>
275 </tr>
275 </tr>
276 <tr>
276 <tr>
277 <th class="author">children</th>
277 <th class="author">children</th>
278 <td class="author"></td>
278 <td class="author"></td>
279 </tr>
279 </tr>
280
280
281 </table>
281 </table>
282
282
283 <div class="overflow">
283 <div class="overflow">
284 <table class="bigtable">
284 <table class="bigtable">
285 <tr>
285 <tr>
286 <th class="annotate">rev</th>
286 <th class="annotate">rev</th>
287 <th class="line">&nbsp;&nbsp;line source</th>
287 <th class="line">&nbsp;&nbsp;line source</th>
288 </tr>
288 </tr>
289
289
290 <tr class="parity0">
290 <tr class="parity0">
291 <td class="annotate">
291 <td class="annotate">
292 <a href="/annotate/3e1445510fe7/primes.py#1"
292 <a href="/annotate/3e1445510fe7/primes.py#1"
293 title="3e1445510fe7: a">test@0</a>
293 title="3e1445510fe7: a">test@0</a>
294 </td>
294 </td>
295 <td class="source"><a href="#l1" id="l1"> 1</a> <span class="c">#!/usr/bin/env python</span></td>
295 <td class="source"><a href="#l1" id="l1"> 1</a> <span class="c">#!/usr/bin/env python</span></td>
296 </tr>
296 </tr>
297 <tr class="parity1">
297 <tr class="parity1">
298 <td class="annotate">
298 <td class="annotate">
299 <a href="/annotate/3e1445510fe7/primes.py#2"
299 <a href="/annotate/3e1445510fe7/primes.py#2"
300 title="3e1445510fe7: a">test@0</a>
300 title="3e1445510fe7: a">test@0</a>
301 </td>
301 </td>
302 <td class="source"><a href="#l2" id="l2"> 2</a> </td>
302 <td class="source"><a href="#l2" id="l2"> 2</a> </td>
303 </tr>
303 </tr>
304 <tr class="parity0">
304 <tr class="parity0">
305 <td class="annotate">
305 <td class="annotate">
306 <a href="/annotate/3e1445510fe7/primes.py#3"
306 <a href="/annotate/3e1445510fe7/primes.py#3"
307 title="3e1445510fe7: a">test@0</a>
307 title="3e1445510fe7: a">test@0</a>
308 </td>
308 </td>
309 <td class="source"><a href="#l3" id="l3"> 3</a> <span class="sd">&quot;&quot;&quot;Fun with generators. Corresponding Haskell implementation:</span></td>
309 <td class="source"><a href="#l3" id="l3"> 3</a> <span class="sd">&quot;&quot;&quot;Fun with generators. Corresponding Haskell implementation:</span></td>
310 </tr>
310 </tr>
311 <tr class="parity1">
311 <tr class="parity1">
312 <td class="annotate">
312 <td class="annotate">
313 <a href="/annotate/3e1445510fe7/primes.py#4"
313 <a href="/annotate/3e1445510fe7/primes.py#4"
314 title="3e1445510fe7: a">test@0</a>
314 title="3e1445510fe7: a">test@0</a>
315 </td>
315 </td>
316 <td class="source"><a href="#l4" id="l4"> 4</a> </td>
316 <td class="source"><a href="#l4" id="l4"> 4</a> </td>
317 </tr>
317 </tr>
318 <tr class="parity0">
318 <tr class="parity0">
319 <td class="annotate">
319 <td class="annotate">
320 <a href="/annotate/3e1445510fe7/primes.py#5"
320 <a href="/annotate/3e1445510fe7/primes.py#5"
321 title="3e1445510fe7: a">test@0</a>
321 title="3e1445510fe7: a">test@0</a>
322 </td>
322 </td>
323 <td class="source"><a href="#l5" id="l5"> 5</a> <span class="sd">primes = 2 : sieve [3, 5..]</span></td>
323 <td class="source"><a href="#l5" id="l5"> 5</a> <span class="sd">primes = 2 : sieve [3, 5..]</span></td>
324 </tr>
324 </tr>
325 <tr class="parity1">
325 <tr class="parity1">
326 <td class="annotate">
326 <td class="annotate">
327 <a href="/annotate/3e1445510fe7/primes.py#6"
327 <a href="/annotate/3e1445510fe7/primes.py#6"
328 title="3e1445510fe7: a">test@0</a>
328 title="3e1445510fe7: a">test@0</a>
329 </td>
329 </td>
330 <td class="source"><a href="#l6" id="l6"> 6</a> <span class="sd"> where sieve (p:ns) = p : sieve [n | n &lt;- ns, mod n p /= 0]</span></td>
330 <td class="source"><a href="#l6" id="l6"> 6</a> <span class="sd"> where sieve (p:ns) = p : sieve [n | n &lt;- ns, mod n p /= 0]</span></td>
331 </tr>
331 </tr>
332 <tr class="parity0">
332 <tr class="parity0">
333 <td class="annotate">
333 <td class="annotate">
334 <a href="/annotate/3e1445510fe7/primes.py#7"
334 <a href="/annotate/3e1445510fe7/primes.py#7"
335 title="3e1445510fe7: a">test@0</a>
335 title="3e1445510fe7: a">test@0</a>
336 </td>
336 </td>
337 <td class="source"><a href="#l7" id="l7"> 7</a> <span class="sd">&quot;&quot;&quot;</span></td>
337 <td class="source"><a href="#l7" id="l7"> 7</a> <span class="sd">&quot;&quot;&quot;</span></td>
338 </tr>
338 </tr>
339 <tr class="parity1">
339 <tr class="parity1">
340 <td class="annotate">
340 <td class="annotate">
341 <a href="/annotate/3e1445510fe7/primes.py#8"
341 <a href="/annotate/3e1445510fe7/primes.py#8"
342 title="3e1445510fe7: a">test@0</a>
342 title="3e1445510fe7: a">test@0</a>
343 </td>
343 </td>
344 <td class="source"><a href="#l8" id="l8"> 8</a> </td>
344 <td class="source"><a href="#l8" id="l8"> 8</a> </td>
345 </tr>
345 </tr>
346 <tr class="parity0">
346 <tr class="parity0">
347 <td class="annotate">
347 <td class="annotate">
348 <a href="/annotate/3e1445510fe7/primes.py#9"
348 <a href="/annotate/3e1445510fe7/primes.py#9"
349 title="3e1445510fe7: a">test@0</a>
349 title="3e1445510fe7: a">test@0</a>
350 </td>
350 </td>
351 <td class="source"><a href="#l9" id="l9"> 9</a> <span class="kn">from</span> <span class="nn">itertools</span> <span class="kn">import</span> <span class="n">dropwhile</span><span class="p">,</span> <span class="n">ifilter</span><span class="p">,</span> <span class="n">islice</span><span class="p">,</span> <span class="n">count</span><span class="p">,</span> <span class="n">chain</span></td>
351 <td class="source"><a href="#l9" id="l9"> 9</a> <span class="kn">from</span> <span class="nn">itertools</span> <span class="kn">import</span> <span class="n">dropwhile</span><span class="p">,</span> <span class="n">ifilter</span><span class="p">,</span> <span class="n">islice</span><span class="p">,</span> <span class="n">count</span><span class="p">,</span> <span class="n">chain</span></td>
352 </tr>
352 </tr>
353 <tr class="parity1">
353 <tr class="parity1">
354 <td class="annotate">
354 <td class="annotate">
355 <a href="/annotate/3e1445510fe7/primes.py#10"
355 <a href="/annotate/3e1445510fe7/primes.py#10"
356 title="3e1445510fe7: a">test@0</a>
356 title="3e1445510fe7: a">test@0</a>
357 </td>
357 </td>
358 <td class="source"><a href="#l10" id="l10"> 10</a> </td>
358 <td class="source"><a href="#l10" id="l10"> 10</a> </td>
359 </tr>
359 </tr>
360 <tr class="parity0">
360 <tr class="parity0">
361 <td class="annotate">
361 <td class="annotate">
362 <a href="/annotate/3e1445510fe7/primes.py#11"
362 <a href="/annotate/3e1445510fe7/primes.py#11"
363 title="3e1445510fe7: a">test@0</a>
363 title="3e1445510fe7: a">test@0</a>
364 </td>
364 </td>
365 <td class="source"><a href="#l11" id="l11"> 11</a> <span class="kn">def</span> <span class="nf">primes</span><span class="p">():</span></td>
365 <td class="source"><a href="#l11" id="l11"> 11</a> <span class="kn">def</span> <span class="nf">primes</span><span class="p">():</span></td>
366 </tr>
366 </tr>
367 <tr class="parity1">
367 <tr class="parity1">
368 <td class="annotate">
368 <td class="annotate">
369 <a href="/annotate/3e1445510fe7/primes.py#12"
369 <a href="/annotate/3e1445510fe7/primes.py#12"
370 title="3e1445510fe7: a">test@0</a>
370 title="3e1445510fe7: a">test@0</a>
371 </td>
371 </td>
372 <td class="source"><a href="#l12" id="l12"> 12</a> <span class="sd">&quot;&quot;&quot;Generate all primes.&quot;&quot;&quot;</span></td>
372 <td class="source"><a href="#l12" id="l12"> 12</a> <span class="sd">&quot;&quot;&quot;Generate all primes.&quot;&quot;&quot;</span></td>
373 </tr>
373 </tr>
374 <tr class="parity0">
374 <tr class="parity0">
375 <td class="annotate">
375 <td class="annotate">
376 <a href="/annotate/3e1445510fe7/primes.py#13"
376 <a href="/annotate/3e1445510fe7/primes.py#13"
377 title="3e1445510fe7: a">test@0</a>
377 title="3e1445510fe7: a">test@0</a>
378 </td>
378 </td>
379 <td class="source"><a href="#l13" id="l13"> 13</a> <span class="kn">def</span> <span class="nf">sieve</span><span class="p">(</span><span class="n">ns</span><span class="p">):</span></td>
379 <td class="source"><a href="#l13" id="l13"> 13</a> <span class="kn">def</span> <span class="nf">sieve</span><span class="p">(</span><span class="n">ns</span><span class="p">):</span></td>
380 </tr>
380 </tr>
381 <tr class="parity1">
381 <tr class="parity1">
382 <td class="annotate">
382 <td class="annotate">
383 <a href="/annotate/3e1445510fe7/primes.py#14"
383 <a href="/annotate/3e1445510fe7/primes.py#14"
384 title="3e1445510fe7: a">test@0</a>
384 title="3e1445510fe7: a">test@0</a>
385 </td>
385 </td>
386 <td class="source"><a href="#l14" id="l14"> 14</a> <span class="n">p</span> <span class="o">=</span> <span class="n">ns</span><span class="o">.</span><span class="n">next</span><span class="p">()</span></td>
386 <td class="source"><a href="#l14" id="l14"> 14</a> <span class="n">p</span> <span class="o">=</span> <span class="n">ns</span><span class="o">.</span><span class="n">next</span><span class="p">()</span></td>
387 </tr>
387 </tr>
388 <tr class="parity0">
388 <tr class="parity0">
389 <td class="annotate">
389 <td class="annotate">
390 <a href="/annotate/3e1445510fe7/primes.py#15"
390 <a href="/annotate/3e1445510fe7/primes.py#15"
391 title="3e1445510fe7: a">test@0</a>
391 title="3e1445510fe7: a">test@0</a>
392 </td>
392 </td>
393 <td class="source"><a href="#l15" id="l15"> 15</a> <span class="c"># It is important to yield *here* in order to stop the</span></td>
393 <td class="source"><a href="#l15" id="l15"> 15</a> <span class="c"># It is important to yield *here* in order to stop the</span></td>
394 </tr>
394 </tr>
395 <tr class="parity1">
395 <tr class="parity1">
396 <td class="annotate">
396 <td class="annotate">
397 <a href="/annotate/3e1445510fe7/primes.py#16"
397 <a href="/annotate/3e1445510fe7/primes.py#16"
398 title="3e1445510fe7: a">test@0</a>
398 title="3e1445510fe7: a">test@0</a>
399 </td>
399 </td>
400 <td class="source"><a href="#l16" id="l16"> 16</a> <span class="c"># infinite recursion.</span></td>
400 <td class="source"><a href="#l16" id="l16"> 16</a> <span class="c"># infinite recursion.</span></td>
401 </tr>
401 </tr>
402 <tr class="parity0">
402 <tr class="parity0">
403 <td class="annotate">
403 <td class="annotate">
404 <a href="/annotate/3e1445510fe7/primes.py#17"
404 <a href="/annotate/3e1445510fe7/primes.py#17"
405 title="3e1445510fe7: a">test@0</a>
405 title="3e1445510fe7: a">test@0</a>
406 </td>
406 </td>
407 <td class="source"><a href="#l17" id="l17"> 17</a> <span class="kn">yield</span> <span class="n">p</span></td>
407 <td class="source"><a href="#l17" id="l17"> 17</a> <span class="kn">yield</span> <span class="n">p</span></td>
408 </tr>
408 </tr>
409 <tr class="parity1">
409 <tr class="parity1">
410 <td class="annotate">
410 <td class="annotate">
411 <a href="/annotate/3e1445510fe7/primes.py#18"
411 <a href="/annotate/3e1445510fe7/primes.py#18"
412 title="3e1445510fe7: a">test@0</a>
412 title="3e1445510fe7: a">test@0</a>
413 </td>
413 </td>
414 <td class="source"><a href="#l18" id="l18"> 18</a> <span class="n">ns</span> <span class="o">=</span> <span class="n">ifilter</span><span class="p">(</span><span class="kn">lambda</span> <span class="n">n</span><span class="p">:</span> <span class="n">n</span> <span class="o">%</span> <span class="n">p</span> <span class="o">!=</span> <span class="mf">0</span><span class="p">,</span> <span class="n">ns</span><span class="p">)</span></td>
414 <td class="source"><a href="#l18" id="l18"> 18</a> <span class="n">ns</span> <span class="o">=</span> <span class="n">ifilter</span><span class="p">(</span><span class="kn">lambda</span> <span class="n">n</span><span class="p">:</span> <span class="n">n</span> <span class="o">%</span> <span class="n">p</span> <span class="o">!=</span> <span class="mf">0</span><span class="p">,</span> <span class="n">ns</span><span class="p">)</span></td>
415 </tr>
415 </tr>
416 <tr class="parity0">
416 <tr class="parity0">
417 <td class="annotate">
417 <td class="annotate">
418 <a href="/annotate/3e1445510fe7/primes.py#19"
418 <a href="/annotate/3e1445510fe7/primes.py#19"
419 title="3e1445510fe7: a">test@0</a>
419 title="3e1445510fe7: a">test@0</a>
420 </td>
420 </td>
421 <td class="source"><a href="#l19" id="l19"> 19</a> <span class="kn">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">sieve</span><span class="p">(</span><span class="n">ns</span><span class="p">):</span></td>
421 <td class="source"><a href="#l19" id="l19"> 19</a> <span class="kn">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">sieve</span><span class="p">(</span><span class="n">ns</span><span class="p">):</span></td>
422 </tr>
422 </tr>
423 <tr class="parity1">
423 <tr class="parity1">
424 <td class="annotate">
424 <td class="annotate">
425 <a href="/annotate/3e1445510fe7/primes.py#20"
425 <a href="/annotate/3e1445510fe7/primes.py#20"
426 title="3e1445510fe7: a">test@0</a>
426 title="3e1445510fe7: a">test@0</a>
427 </td>
427 </td>
428 <td class="source"><a href="#l20" id="l20"> 20</a> <span class="kn">yield</span> <span class="n">n</span></td>
428 <td class="source"><a href="#l20" id="l20"> 20</a> <span class="kn">yield</span> <span class="n">n</span></td>
429 </tr>
429 </tr>
430 <tr class="parity0">
430 <tr class="parity0">
431 <td class="annotate">
431 <td class="annotate">
432 <a href="/annotate/3e1445510fe7/primes.py#21"
432 <a href="/annotate/3e1445510fe7/primes.py#21"
433 title="3e1445510fe7: a">test@0</a>
433 title="3e1445510fe7: a">test@0</a>
434 </td>
434 </td>
435 <td class="source"><a href="#l21" id="l21"> 21</a> </td>
435 <td class="source"><a href="#l21" id="l21"> 21</a> </td>
436 </tr>
436 </tr>
437 <tr class="parity1">
437 <tr class="parity1">
438 <td class="annotate">
438 <td class="annotate">
439 <a href="/annotate/3e1445510fe7/primes.py#22"
439 <a href="/annotate/3e1445510fe7/primes.py#22"
440 title="3e1445510fe7: a">test@0</a>
440 title="3e1445510fe7: a">test@0</a>
441 </td>
441 </td>
442 <td class="source"><a href="#l22" id="l22"> 22</a> <span class="n">odds</span> <span class="o">=</span> <span class="n">ifilter</span><span class="p">(</span><span class="kn">lambda</span> <span class="n">i</span><span class="p">:</span> <span class="n">i</span> <span class="o">%</span> <span class="mf">2</span> <span class="o">==</span> <span class="mf">1</span><span class="p">,</span> <span class="n">count</span><span class="p">())</span></td>
442 <td class="source"><a href="#l22" id="l22"> 22</a> <span class="n">odds</span> <span class="o">=</span> <span class="n">ifilter</span><span class="p">(</span><span class="kn">lambda</span> <span class="n">i</span><span class="p">:</span> <span class="n">i</span> <span class="o">%</span> <span class="mf">2</span> <span class="o">==</span> <span class="mf">1</span><span class="p">,</span> <span class="n">count</span><span class="p">())</span></td>
443 </tr>
443 </tr>
444 <tr class="parity0">
444 <tr class="parity0">
445 <td class="annotate">
445 <td class="annotate">
446 <a href="/annotate/3e1445510fe7/primes.py#23"
446 <a href="/annotate/3e1445510fe7/primes.py#23"
447 title="3e1445510fe7: a">test@0</a>
447 title="3e1445510fe7: a">test@0</a>
448 </td>
448 </td>
449 <td class="source"><a href="#l23" id="l23"> 23</a> <span class="kn">return</span> <span class="n">chain</span><span class="p">([</span><span class="mf">2</span><span class="p">],</span> <span class="n">sieve</span><span class="p">(</span><span class="n">dropwhile</span><span class="p">(</span><span class="kn">lambda</span> <span class="n">n</span><span class="p">:</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mf">3</span><span class="p">,</span> <span class="n">odds</span><span class="p">)))</span></td>
449 <td class="source"><a href="#l23" id="l23"> 23</a> <span class="kn">return</span> <span class="n">chain</span><span class="p">([</span><span class="mf">2</span><span class="p">],</span> <span class="n">sieve</span><span class="p">(</span><span class="n">dropwhile</span><span class="p">(</span><span class="kn">lambda</span> <span class="n">n</span><span class="p">:</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mf">3</span><span class="p">,</span> <span class="n">odds</span><span class="p">)))</span></td>
450 </tr>
450 </tr>
451 <tr class="parity1">
451 <tr class="parity1">
452 <td class="annotate">
452 <td class="annotate">
453 <a href="/annotate/3e1445510fe7/primes.py#24"
453 <a href="/annotate/3e1445510fe7/primes.py#24"
454 title="3e1445510fe7: a">test@0</a>
454 title="3e1445510fe7: a">test@0</a>
455 </td>
455 </td>
456 <td class="source"><a href="#l24" id="l24"> 24</a> </td>
456 <td class="source"><a href="#l24" id="l24"> 24</a> </td>
457 </tr>
457 </tr>
458 <tr class="parity0">
458 <tr class="parity0">
459 <td class="annotate">
459 <td class="annotate">
460 <a href="/annotate/3e1445510fe7/primes.py#25"
460 <a href="/annotate/3e1445510fe7/primes.py#25"
461 title="3e1445510fe7: a">test@0</a>
461 title="3e1445510fe7: a">test@0</a>
462 </td>
462 </td>
463 <td class="source"><a href="#l25" id="l25"> 25</a> <span class="kn">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span></td>
463 <td class="source"><a href="#l25" id="l25"> 25</a> <span class="kn">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span></td>
464 </tr>
464 </tr>
465 <tr class="parity1">
465 <tr class="parity1">
466 <td class="annotate">
466 <td class="annotate">
467 <a href="/annotate/3e1445510fe7/primes.py#26"
467 <a href="/annotate/3e1445510fe7/primes.py#26"
468 title="3e1445510fe7: a">test@0</a>
468 title="3e1445510fe7: a">test@0</a>
469 </td>
469 </td>
470 <td class="source"><a href="#l26" id="l26"> 26</a> <span class="kn">import</span> <span class="nn">sys</span></td>
470 <td class="source"><a href="#l26" id="l26"> 26</a> <span class="kn">import</span> <span class="nn">sys</span></td>
471 </tr>
471 </tr>
472 <tr class="parity0">
472 <tr class="parity0">
473 <td class="annotate">
473 <td class="annotate">
474 <a href="/annotate/3e1445510fe7/primes.py#27"
474 <a href="/annotate/3e1445510fe7/primes.py#27"
475 title="3e1445510fe7: a">test@0</a>
475 title="3e1445510fe7: a">test@0</a>
476 </td>
476 </td>
477 <td class="source"><a href="#l27" id="l27"> 27</a> <span class="kn">try</span><span class="p">:</span></td>
477 <td class="source"><a href="#l27" id="l27"> 27</a> <span class="kn">try</span><span class="p">:</span></td>
478 </tr>
478 </tr>
479 <tr class="parity1">
479 <tr class="parity1">
480 <td class="annotate">
480 <td class="annotate">
481 <a href="/annotate/3e1445510fe7/primes.py#28"
481 <a href="/annotate/3e1445510fe7/primes.py#28"
482 title="3e1445510fe7: a">test@0</a>
482 title="3e1445510fe7: a">test@0</a>
483 </td>
483 </td>
484 <td class="source"><a href="#l28" id="l28"> 28</a> <span class="n">n</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mf">1</span><span class="p">])</span></td>
484 <td class="source"><a href="#l28" id="l28"> 28</a> <span class="n">n</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mf">1</span><span class="p">])</span></td>
485 </tr>
485 </tr>
486 <tr class="parity0">
486 <tr class="parity0">
487 <td class="annotate">
487 <td class="annotate">
488 <a href="/annotate/3e1445510fe7/primes.py#29"
488 <a href="/annotate/3e1445510fe7/primes.py#29"
489 title="3e1445510fe7: a">test@0</a>
489 title="3e1445510fe7: a">test@0</a>
490 </td>
490 </td>
491 <td class="source"><a href="#l29" id="l29"> 29</a> <span class="kn">except</span> <span class="p">(</span><span class="ne">ValueError</span><span class="p">,</span> <span class="ne">IndexError</span><span class="p">):</span></td>
491 <td class="source"><a href="#l29" id="l29"> 29</a> <span class="kn">except</span> <span class="p">(</span><span class="ne">ValueError</span><span class="p">,</span> <span class="ne">IndexError</span><span class="p">):</span></td>
492 </tr>
492 </tr>
493 <tr class="parity1">
493 <tr class="parity1">
494 <td class="annotate">
494 <td class="annotate">
495 <a href="/annotate/3e1445510fe7/primes.py#30"
495 <a href="/annotate/3e1445510fe7/primes.py#30"
496 title="3e1445510fe7: a">test@0</a>
496 title="3e1445510fe7: a">test@0</a>
497 </td>
497 </td>
498 <td class="source"><a href="#l30" id="l30"> 30</a> <span class="n">n</span> <span class="o">=</span> <span class="mf">10</span></td>
498 <td class="source"><a href="#l30" id="l30"> 30</a> <span class="n">n</span> <span class="o">=</span> <span class="mf">10</span></td>
499 </tr>
499 </tr>
500 <tr class="parity0">
500 <tr class="parity0">
501 <td class="annotate">
501 <td class="annotate">
502 <a href="/annotate/3e1445510fe7/primes.py#31"
502 <a href="/annotate/3e1445510fe7/primes.py#31"
503 title="3e1445510fe7: a">test@0</a>
503 title="3e1445510fe7: a">test@0</a>
504 </td>
504 </td>
505 <td class="source"><a href="#l31" id="l31"> 31</a> <span class="n">p</span> <span class="o">=</span> <span class="n">primes</span><span class="p">()</span></td>
505 <td class="source"><a href="#l31" id="l31"> 31</a> <span class="n">p</span> <span class="o">=</span> <span class="n">primes</span><span class="p">()</span></td>
506 </tr>
506 </tr>
507 <tr class="parity1">
507 <tr class="parity1">
508 <td class="annotate">
508 <td class="annotate">
509 <a href="/annotate/3e1445510fe7/primes.py#32"
509 <a href="/annotate/3e1445510fe7/primes.py#32"
510 title="3e1445510fe7: a">test@0</a>
510 title="3e1445510fe7: a">test@0</a>
511 </td>
511 </td>
512 <td class="source"><a href="#l32" id="l32"> 32</a> <span class="kn">print</span> <span class="s">&quot;The first </span><span class="si">%d</span><span class="s"> primes: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="n">islice</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">n</span><span class="p">)))</span></td>
512 <td class="source"><a href="#l32" id="l32"> 32</a> <span class="kn">print</span> <span class="s">&quot;The first </span><span class="si">%d</span><span class="s"> primes: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="n">islice</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">n</span><span class="p">)))</span></td>
513 </tr>
513 </tr>
514 </table>
514 </table>
515 </div>
515 </div>
516 </div>
516 </div>
517 </div>
517 </div>
518
518
519
519
520
520
521 </body>
521 </body>
522 </html>
522 </html>
523
523
524 % hgweb fileannotate, raw
524 % hgweb fileannotate, raw
525
525
526 % hgweb filerevision, raw
526 % hgweb filerevision, raw
527
527
528 % hgweb highlightcss friendly
528 % hgweb highlightcss friendly
529 200 Script output follows
529 200 Script output follows
530
530
531 /* pygments_style = friendly */
531 /* pygments_style = friendly */
532
532
533 % errors encountered
533 % errors encountered
534 % hg serve again
534 % hg serve again
535 % hgweb highlightcss fruity
535 % hgweb highlightcss fruity
536 200 Script output follows
536 200 Script output follows
537
537
538 /* pygments_style = fruity */
538 /* pygments_style = fruity */
539
539
540 % errors encountered
540 % errors encountered
541 adding eucjp.txt
542 % HGENCODING=euc-jp hg serve
543 % hgweb filerevision, html
544 <div class="parity0 source"><a href="#l1" id="l1"> 1</a> \xb5\xfe</div>
545 % errors encountered
546 % HGENCODING=utf-8 hg serve
547 % hgweb filerevision, html
548 <div class="parity0 source"><a href="#l1" id="l1"> 1</a> \xef\xbf\xbd\xef\xbf\xbd</div>
549 % errors encountered
550 % HGENCODING=us-ascii hg serve
551 % hgweb filerevision, html
552 <div class="parity0 source"><a href="#l1" id="l1"> 1</a> ??</div>
553 % errors encountered
General Comments 0
You need to be logged in to leave comments. Login now