annotate tests/heredoctest.py @ 22564:9599e86159ac

heredoctest: use the same dict for local/global contexts as in doctest In order to mimic module-level evaluation, globals and locals should be the same object, so doctest does not pass separate locals dict. https://docs.python.org/2.7/reference/simple_stmts.html#exec This fixes NameError in the following example: >>> import foo >>> def bar(): ... foo # must exist in globalvars
author Yuya Nishihara <yuya@tcha.org>
date Sun, 28 Sep 2014 14:15:43 +0900
parents 5635a4017061
children 8d45a42b0c0f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15434
5635a4017061 run-tests: replace inline python handling with more native scheme
Matt Mackall <mpm@selenic.com>
parents: 15398
diff changeset
1 import sys
15247
3cd1605e9d8e tests: remove temp doctest file when finished running it
Idan Kamara <idankk86@gmail.com>
parents: 15235
diff changeset
2
15434
5635a4017061 run-tests: replace inline python handling with more native scheme
Matt Mackall <mpm@selenic.com>
parents: 15398
diff changeset
3 globalvars = {}
5635a4017061 run-tests: replace inline python handling with more native scheme
Matt Mackall <mpm@selenic.com>
parents: 15398
diff changeset
4 lines = sys.stdin.readlines()
5635a4017061 run-tests: replace inline python handling with more native scheme
Matt Mackall <mpm@selenic.com>
parents: 15398
diff changeset
5 while lines:
5635a4017061 run-tests: replace inline python handling with more native scheme
Matt Mackall <mpm@selenic.com>
parents: 15398
diff changeset
6 l = lines.pop(0)
5635a4017061 run-tests: replace inline python handling with more native scheme
Matt Mackall <mpm@selenic.com>
parents: 15398
diff changeset
7 if l.startswith('SALT'):
5635a4017061 run-tests: replace inline python handling with more native scheme
Matt Mackall <mpm@selenic.com>
parents: 15398
diff changeset
8 print l[:-1]
5635a4017061 run-tests: replace inline python handling with more native scheme
Matt Mackall <mpm@selenic.com>
parents: 15398
diff changeset
9 elif l.startswith('>>> '):
5635a4017061 run-tests: replace inline python handling with more native scheme
Matt Mackall <mpm@selenic.com>
parents: 15398
diff changeset
10 snippet = l[4:]
5635a4017061 run-tests: replace inline python handling with more native scheme
Matt Mackall <mpm@selenic.com>
parents: 15398
diff changeset
11 while lines and lines[0].startswith('... '):
5635a4017061 run-tests: replace inline python handling with more native scheme
Matt Mackall <mpm@selenic.com>
parents: 15398
diff changeset
12 l = lines.pop(0)
5635a4017061 run-tests: replace inline python handling with more native scheme
Matt Mackall <mpm@selenic.com>
parents: 15398
diff changeset
13 snippet += "\n" + l[4:]
5635a4017061 run-tests: replace inline python handling with more native scheme
Matt Mackall <mpm@selenic.com>
parents: 15398
diff changeset
14 c = compile(snippet, '<heredoc>', 'single')
5635a4017061 run-tests: replace inline python handling with more native scheme
Matt Mackall <mpm@selenic.com>
parents: 15398
diff changeset
15 try:
22564
9599e86159ac heredoctest: use the same dict for local/global contexts as in doctest
Yuya Nishihara <yuya@tcha.org>
parents: 15434
diff changeset
16 exec c in globalvars
15434
5635a4017061 run-tests: replace inline python handling with more native scheme
Matt Mackall <mpm@selenic.com>
parents: 15398
diff changeset
17 except Exception, inst:
5635a4017061 run-tests: replace inline python handling with more native scheme
Matt Mackall <mpm@selenic.com>
parents: 15398
diff changeset
18 print repr(inst)