Patchwork [1,of,2,V2] heredoctest: use the same dict for local/global contexts as in doctest

login
register
mail settings
Submitter Yuya Nishihara
Date Sept. 29, 2014, 12:17 p.m.
Message ID <6a5197528c81966ff4c1.1411993047@mimosa>
Download mbox | patch
Permalink /patch/6026/
State Accepted
Headers show

Comments

Yuya Nishihara - Sept. 29, 2014, 12:17 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1411881343 -32400
#      Sun Sep 28 14:15:43 2014 +0900
# Node ID 6a5197528c81966ff4c1417a4c9ae4489cb368a4
# Parent  8cc5e673cac09bdfda15c229bf8165ab4a8a0721
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

Patch

diff --git a/tests/heredoctest.py b/tests/heredoctest.py
--- a/tests/heredoctest.py
+++ b/tests/heredoctest.py
@@ -1,7 +1,6 @@ 
 import sys
 
 globalvars = {}
-localvars = {}
 lines = sys.stdin.readlines()
 while lines:
     l = lines.pop(0)
@@ -14,6 +13,6 @@  while lines:
             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)
diff --git a/tests/test-unified-test.t b/tests/test-unified-test.t
--- a/tests/test-unified-test.t
+++ b/tests/test-unified-test.t
@@ -37,6 +37,11 @@  Doctest commands:
   z
   >>> print
   
+  >>> foo = 'global name'
+  >>> def func():
+  ...     print foo, 'should be visible in func()'
+  >>> func()
+  global name should be visible in func()
 
 Regular expressions: