heredoctest: use the same dict for local/global contexts as in doctest
authorYuya Nishihara <yuya@tcha.org>
Sun, 28 Sep 2014 14:15:43 +0900
changeset 22564 9599e86159ac
parent 22563 8cc5e673cac0
child 22565 8d45a42b0c0f
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
tests/heredoctest.py
tests/test-unified-test.t
--- a/tests/heredoctest.py	Sat Sep 27 12:37:53 2014 +0900
+++ b/tests/heredoctest.py	Sun Sep 28 14:15:43 2014 +0900
@@ -1,7 +1,6 @@
 import sys
 
 globalvars = {}
-localvars = {}
 lines = sys.stdin.readlines()
 while lines:
     l = lines.pop(0)
@@ -14,6 +13,6 @@
             snippet += "\n" + l[4:]
         c = compile(snippet, '<heredoc>', 'single')
         try:
-            exec c in globalvars, localvars
+            exec c in globalvars
         except Exception, inst:
             print repr(inst)
--- a/tests/test-unified-test.t	Sat Sep 27 12:37:53 2014 +0900
+++ b/tests/test-unified-test.t	Sun Sep 28 14:15:43 2014 +0900
@@ -37,6 +37,11 @@
   z
   >>> print
   
+  >>> foo = 'global name'
+  >>> def func():
+  ...     print foo, 'should be visible in func()'
+  >>> func()
+  global name should be visible in func()
 
 Regular expressions: