##// END OF EJS Templates
Ensure that __builtin__ is always available and compute prompts from it....
Fernando Perez -
Show More
@@ -219,14 +219,21 b' def make_user_namespaces(user_ns=None, user_global_ns=None):'
219 of the interpreter and a dict to be used as the global namespace.
219 of the interpreter and a dict to be used as the global namespace.
220 """
220 """
221
221
222
223 # We must ensure that __builtin__ (without the final 's') is always
224 # available and pointing to the __builtin__ *module*. For more details:
225 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
226
222 if user_ns is None:
227 if user_ns is None:
223 # Set __name__ to __main__ to better match the behavior of the
228 # Set __name__ to __main__ to better match the behavior of the
224 # normal interpreter.
229 # normal interpreter.
225 user_ns = {'__name__' :'__main__',
230 user_ns = {'__name__' :'__main__',
231 '__builtin__' : __builtin__,
226 '__builtins__' : __builtin__,
232 '__builtins__' : __builtin__,
227 }
233 }
228 else:
234 else:
229 user_ns.setdefault('__name__','__main__')
235 user_ns.setdefault('__name__','__main__')
236 user_ns.setdefault('__builtin__',__builtin__)
230 user_ns.setdefault('__builtins__',__builtin__)
237 user_ns.setdefault('__builtins__',__builtin__)
231
238
232 if user_global_ns is None:
239 if user_global_ns is None:
@@ -970,8 +977,20 b' class InteractiveShell(Component, Magic):'
970 # user_ns, and we sync that contents into user_config_ns so that these
977 # user_ns, and we sync that contents into user_config_ns so that these
971 # initial variables aren't shown by %who. After the sync, we add the
978 # initial variables aren't shown by %who. After the sync, we add the
972 # rest of what we *do* want the user to see with %who even on a new
979 # rest of what we *do* want the user to see with %who even on a new
973 # session (probably nothing, so they really only see their own stuff)
980 # session (probably nothing, so theye really only see their own stuff)
974 ns = {}
981
982 # The user dict must *always* have a __builtin__ reference to the
983 # Python standard __builtin__ namespace, which must be imported.
984 # This is so that certain operations in prompt evaluation can be
985 # reliably executed with builtins. Note that we can NOT use
986 # __builtins__ (note the 's'), because that can either be a dict or a
987 # module, and can even mutate at runtime, depending on the context
988 # (Python makes no guarantees on it). In contrast, __builtin__ is
989 # always a module object, though it must be explicitly imported.
990
991 # For more details:
992 # http://mail.python.org/pipermail/python-dev/2001-April/014068.html
993 ns = dict(__builtin__ = __builtin__)
975
994
976 # Put 'help' in the user namespace
995 # Put 'help' in the user namespace
977 try:
996 try:
@@ -131,8 +131,14 b' prompt_specials_color = {'
131 # Prompt/history count, with the actual digits replaced by dots. Used
131 # Prompt/history count, with the actual digits replaced by dots. Used
132 # mainly in continuation prompts (prompt_in2)
132 # mainly in continuation prompts (prompt_in2)
133 #r'\D': '${"."*len(str(self.cache.prompt_count))}',
133 #r'\D': '${"."*len(str(self.cache.prompt_count))}',
134 # More robust form of the above expression, that uses __builtins__
134
135 r'\D': '${"."*__builtins__.len(__builtins__.str(self.cache.prompt_count))}',
135 # More robust form of the above expression, that uses the __builtin__
136 # module. Note that we can NOT use __builtins__ (note the 's'), because
137 # that can either be a dict or a module, and can even mutate at runtime,
138 # depending on the context (Python makes no guarantees on it). In
139 # contrast, __builtin__ is always a module object, though it must be
140 # explicitly imported.
141 r'\D': '${"."*__builtin__.len(__builtin__.str(self.cache.prompt_count))}',
136
142
137 # Current working directory
143 # Current working directory
138 r'\w': '${os.getcwd()}',
144 r'\w': '${os.getcwd()}',
@@ -215,6 +221,7 b' def str_safe(arg):'
215 out = '<ERROR: %s>' % msg
221 out = '<ERROR: %s>' % msg
216 except Exception,msg:
222 except Exception,msg:
217 out = '<ERROR: %s>' % msg
223 out = '<ERROR: %s>' % msg
224 raise # dbg
218 return out
225 return out
219
226
220 class BasePrompt(object):
227 class BasePrompt(object):
General Comments 0
You need to be logged in to leave comments. Login now