2
2
Test cases for codeop.py
3
3
Nick Mathewson
4
4
"""
5
- import sys
6
5
import unittest
7
6
import warnings
8
- from test import support
9
7
from test .support import warnings_helper
10
8
11
9
from codeop import compile_command , PyCF_DONT_IMPLY_DEDENT
12
- import io
13
-
14
- if support .is_jython :
15
-
16
- def unify_callables (d ):
17
- for n ,v in d .items ():
18
- if hasattr (v , '__call__' ):
19
- d [n ] = True
20
- return d
21
10
22
11
class CodeopTests (unittest .TestCase ):
23
12
24
13
def assertValid (self , str , symbol = 'single' ):
25
14
'''succeed iff str is a valid piece of code'''
26
- if support .is_jython :
27
- code = compile_command (str , "<input>" , symbol )
28
- self .assertTrue (code )
29
- if symbol == "single" :
30
- d ,r = {},{}
31
- saved_stdout = sys .stdout
32
- sys .stdout = io .StringIO ()
33
- try :
34
- exec (code , d )
35
- exec (compile (str ,"<input>" ,"single" ), r )
36
- finally :
37
- sys .stdout = saved_stdout
38
- elif symbol == 'eval' :
39
- ctx = {'a' : 2 }
40
- d = { 'value' : eval (code ,ctx ) }
41
- r = { 'value' : eval (str ,ctx ) }
42
- self .assertEqual (unify_callables (r ),unify_callables (d ))
43
- else :
44
- expected = compile (str , "<input>" , symbol , PyCF_DONT_IMPLY_DEDENT )
45
- self .assertEqual (compile_command (str , "<input>" , symbol ), expected )
15
+ expected = compile (str , "<input>" , symbol , PyCF_DONT_IMPLY_DEDENT )
16
+ self .assertEqual (compile_command (str , "<input>" , symbol ), expected )
46
17
47
18
def assertIncomplete (self , str , symbol = 'single' ):
48
19
'''succeed iff str is the start of a valid piece of code'''
@@ -62,16 +33,12 @@ def test_valid(self):
62
33
av = self .assertValid
63
34
64
35
# special case
65
- if not support .is_jython :
66
- self .assertEqual (compile_command ("" ),
67
- compile ("pass" , "<input>" , 'single' ,
68
- PyCF_DONT_IMPLY_DEDENT ))
69
- self .assertEqual (compile_command ("\n " ),
70
- compile ("pass" , "<input>" , 'single' ,
71
- PyCF_DONT_IMPLY_DEDENT ))
72
- else :
73
- av ("" )
74
- av ("\n " )
36
+ self .assertEqual (compile_command ("" ),
37
+ compile ("pass" , "<input>" , 'single' ,
38
+ PyCF_DONT_IMPLY_DEDENT ))
39
+ self .assertEqual (compile_command ("\n " ),
40
+ compile ("pass" , "<input>" , 'single' ,
41
+ PyCF_DONT_IMPLY_DEDENT ))
75
42
76
43
av ("a = 1" )
77
44
av ("\n a = 1" )
0 commit comments