##// END OF EJS Templates
Make IPython work with OpenSSL in FIPS mode #10615...
Srinath -
Show More
@@ -1,143 +1,143 b''
1 """Compiler tools with improved interactive support.
1 """Compiler tools with improved interactive support.
2
2
3 Provides compilation machinery similar to codeop, but with caching support so
3 Provides compilation machinery similar to codeop, but with caching support so
4 we can provide interactive tracebacks.
4 we can provide interactive tracebacks.
5
5
6 Authors
6 Authors
7 -------
7 -------
8 * Robert Kern
8 * Robert Kern
9 * Fernando Perez
9 * Fernando Perez
10 * Thomas Kluyver
10 * Thomas Kluyver
11 """
11 """
12
12
13 # Note: though it might be more natural to name this module 'compiler', that
13 # Note: though it might be more natural to name this module 'compiler', that
14 # name is in the stdlib and name collisions with the stdlib tend to produce
14 # name is in the stdlib and name collisions with the stdlib tend to produce
15 # weird problems (often with third-party tools).
15 # weird problems (often with third-party tools).
16
16
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18 # Copyright (C) 2010-2011 The IPython Development Team.
18 # Copyright (C) 2010-2011 The IPython Development Team.
19 #
19 #
20 # Distributed under the terms of the BSD License.
20 # Distributed under the terms of the BSD License.
21 #
21 #
22 # The full license is in the file COPYING.txt, distributed with this software.
22 # The full license is in the file COPYING.txt, distributed with this software.
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24
24
25 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
26 # Imports
26 # Imports
27 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
28
28
29 # Stdlib imports
29 # Stdlib imports
30 import __future__
30 import __future__
31 from ast import PyCF_ONLY_AST
31 from ast import PyCF_ONLY_AST
32 import codeop
32 import codeop
33 import functools
33 import functools
34 import hashlib
34 import hashlib
35 import linecache
35 import linecache
36 import operator
36 import operator
37 import time
37 import time
38
38
39 #-----------------------------------------------------------------------------
39 #-----------------------------------------------------------------------------
40 # Constants
40 # Constants
41 #-----------------------------------------------------------------------------
41 #-----------------------------------------------------------------------------
42
42
43 # Roughtly equal to PyCF_MASK | PyCF_MASK_OBSOLETE as defined in pythonrun.h,
43 # Roughtly equal to PyCF_MASK | PyCF_MASK_OBSOLETE as defined in pythonrun.h,
44 # this is used as a bitmask to extract future-related code flags.
44 # this is used as a bitmask to extract future-related code flags.
45 PyCF_MASK = functools.reduce(operator.or_,
45 PyCF_MASK = functools.reduce(operator.or_,
46 (getattr(__future__, fname).compiler_flag
46 (getattr(__future__, fname).compiler_flag
47 for fname in __future__.all_feature_names))
47 for fname in __future__.all_feature_names))
48
48
49 #-----------------------------------------------------------------------------
49 #-----------------------------------------------------------------------------
50 # Local utilities
50 # Local utilities
51 #-----------------------------------------------------------------------------
51 #-----------------------------------------------------------------------------
52
52
53 def code_name(code, number=0):
53 def code_name(code, number=0):
54 """ Compute a (probably) unique name for code for caching.
54 """ Compute a (probably) unique name for code for caching.
55
55
56 This now expects code to be unicode.
56 This now expects code to be unicode.
57 """
57 """
58 hash_digest = hashlib.md5(code.encode("utf-8")).hexdigest()
58 hash_digest = hashlib.sha1(code.encode("utf-8")).hexdigest()
59 # Include the number and 12 characters of the hash in the name. It's
59 # Include the number and 12 characters of the hash in the name. It's
60 # pretty much impossible that in a single session we'll have collisions
60 # pretty much impossible that in a single session we'll have collisions
61 # even with truncated hashes, and the full one makes tracebacks too long
61 # even with truncated hashes, and the full one makes tracebacks too long
62 return '<ipython-input-{0}-{1}>'.format(number, hash_digest[:12])
62 return '<ipython-input-{0}-{1}>'.format(number, hash_digest[:12])
63
63
64 #-----------------------------------------------------------------------------
64 #-----------------------------------------------------------------------------
65 # Classes and functions
65 # Classes and functions
66 #-----------------------------------------------------------------------------
66 #-----------------------------------------------------------------------------
67
67
68 class CachingCompiler(codeop.Compile):
68 class CachingCompiler(codeop.Compile):
69 """A compiler that caches code compiled from interactive statements.
69 """A compiler that caches code compiled from interactive statements.
70 """
70 """
71
71
72 def __init__(self):
72 def __init__(self):
73 codeop.Compile.__init__(self)
73 codeop.Compile.__init__(self)
74
74
75 # This is ugly, but it must be done this way to allow multiple
75 # This is ugly, but it must be done this way to allow multiple
76 # simultaneous ipython instances to coexist. Since Python itself
76 # simultaneous ipython instances to coexist. Since Python itself
77 # directly accesses the data structures in the linecache module, and
77 # directly accesses the data structures in the linecache module, and
78 # the cache therein is global, we must work with that data structure.
78 # the cache therein is global, we must work with that data structure.
79 # We must hold a reference to the original checkcache routine and call
79 # We must hold a reference to the original checkcache routine and call
80 # that in our own check_cache() below, but the special IPython cache
80 # that in our own check_cache() below, but the special IPython cache
81 # must also be shared by all IPython instances. If we were to hold
81 # must also be shared by all IPython instances. If we were to hold
82 # separate caches (one in each CachingCompiler instance), any call made
82 # separate caches (one in each CachingCompiler instance), any call made
83 # by Python itself to linecache.checkcache() would obliterate the
83 # by Python itself to linecache.checkcache() would obliterate the
84 # cached data from the other IPython instances.
84 # cached data from the other IPython instances.
85 if not hasattr(linecache, '_ipython_cache'):
85 if not hasattr(linecache, '_ipython_cache'):
86 linecache._ipython_cache = {}
86 linecache._ipython_cache = {}
87 if not hasattr(linecache, '_checkcache_ori'):
87 if not hasattr(linecache, '_checkcache_ori'):
88 linecache._checkcache_ori = linecache.checkcache
88 linecache._checkcache_ori = linecache.checkcache
89 # Now, we must monkeypatch the linecache directly so that parts of the
89 # Now, we must monkeypatch the linecache directly so that parts of the
90 # stdlib that call it outside our control go through our codepath
90 # stdlib that call it outside our control go through our codepath
91 # (otherwise we'd lose our tracebacks).
91 # (otherwise we'd lose our tracebacks).
92 linecache.checkcache = check_linecache_ipython
92 linecache.checkcache = check_linecache_ipython
93
93
94 def ast_parse(self, source, filename='<unknown>', symbol='exec'):
94 def ast_parse(self, source, filename='<unknown>', symbol='exec'):
95 """Parse code to an AST with the current compiler flags active.
95 """Parse code to an AST with the current compiler flags active.
96
96
97 Arguments are exactly the same as ast.parse (in the standard library),
97 Arguments are exactly the same as ast.parse (in the standard library),
98 and are passed to the built-in compile function."""
98 and are passed to the built-in compile function."""
99 return compile(source, filename, symbol, self.flags | PyCF_ONLY_AST, 1)
99 return compile(source, filename, symbol, self.flags | PyCF_ONLY_AST, 1)
100
100
101 def reset_compiler_flags(self):
101 def reset_compiler_flags(self):
102 """Reset compiler flags to default state."""
102 """Reset compiler flags to default state."""
103 # This value is copied from codeop.Compile.__init__, so if that ever
103 # This value is copied from codeop.Compile.__init__, so if that ever
104 # changes, it will need to be updated.
104 # changes, it will need to be updated.
105 self.flags = codeop.PyCF_DONT_IMPLY_DEDENT
105 self.flags = codeop.PyCF_DONT_IMPLY_DEDENT
106
106
107 @property
107 @property
108 def compiler_flags(self):
108 def compiler_flags(self):
109 """Flags currently active in the compilation process.
109 """Flags currently active in the compilation process.
110 """
110 """
111 return self.flags
111 return self.flags
112
112
113 def cache(self, code, number=0):
113 def cache(self, code, number=0):
114 """Make a name for a block of code, and cache the code.
114 """Make a name for a block of code, and cache the code.
115
115
116 Parameters
116 Parameters
117 ----------
117 ----------
118 code : str
118 code : str
119 The Python source code to cache.
119 The Python source code to cache.
120 number : int
120 number : int
121 A number which forms part of the code's name. Used for the execution
121 A number which forms part of the code's name. Used for the execution
122 counter.
122 counter.
123
123
124 Returns
124 Returns
125 -------
125 -------
126 The name of the cached code (as a string). Pass this as the filename
126 The name of the cached code (as a string). Pass this as the filename
127 argument to compilation, so that tracebacks are correctly hooked up.
127 argument to compilation, so that tracebacks are correctly hooked up.
128 """
128 """
129 name = code_name(code, number)
129 name = code_name(code, number)
130 entry = (len(code), time.time(),
130 entry = (len(code), time.time(),
131 [line+'\n' for line in code.splitlines()], name)
131 [line+'\n' for line in code.splitlines()], name)
132 linecache.cache[name] = entry
132 linecache.cache[name] = entry
133 linecache._ipython_cache[name] = entry
133 linecache._ipython_cache[name] = entry
134 return name
134 return name
135
135
136 def check_linecache_ipython(*args):
136 def check_linecache_ipython(*args):
137 """Call linecache.checkcache() safely protecting our cached values.
137 """Call linecache.checkcache() safely protecting our cached values.
138 """
138 """
139 # First call the orignal checkcache as intended
139 # First call the orignal checkcache as intended
140 linecache._checkcache_ori(*args)
140 linecache._checkcache_ori(*args)
141 # Then, update back the cache with our data, so that tracebacks related
141 # Then, update back the cache with our data, so that tracebacks related
142 # to our compiled codes can be produced.
142 # to our compiled codes can be produced.
143 linecache.cache.update(linecache._ipython_cache)
143 linecache.cache.update(linecache._ipython_cache)
General Comments 0
You need to be logged in to leave comments. Login now