##// END OF EJS Templates
Merging with upstream trunk
Fernando Perez -
r2354:d30c181f merge
parent child Browse files
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -0,0 +1,184 b''
1 import os
2
3 c = get_config()
4
5 #-----------------------------------------------------------------------------
6 # Select which launchers to use
7 #-----------------------------------------------------------------------------
8
9 # This allows you to control what method is used to start the controller
10 # and engines. The following methods are currently supported:
11 # - Start as a regular process on localhost.
12 # - Start using mpiexec.
13 # - Start using the Windows HPC Server 2008 scheduler
14 # - Start using PBS
15 # - Start using SSH (currently broken)
16
17
18 # The selected launchers can be configured below.
19
20 # Options are:
21 # - LocalControllerLauncher
22 # - MPIExecControllerLauncher
23 # - PBSControllerLauncher
24 # - WindowsHPCControllerLauncher
25 # c.Global.controller_launcher = 'IPython.kernel.launcher.LocalControllerLauncher'
26
27 # Options are:
28 # - LocalEngineSetLauncher
29 # - MPIExecEngineSetLauncher
30 # - PBSEngineSetLauncher
31 # - WindowsHPCEngineSetLauncher
32 # c.Global.engine_launcher = 'IPython.kernel.launcher.LocalEngineSetLauncher'
33
34 #-----------------------------------------------------------------------------
35 # Global configuration
36 #-----------------------------------------------------------------------------
37
38 # The default number of engines that will be started. This is overridden by
39 # the -n command line option: "ipcluster start -n 4"
40 # c.Global.n = 2
41
42 # Log to a file in cluster_dir/log, otherwise just log to sys.stdout.
43 # c.Global.log_to_file = False
44
45 # Remove old logs from cluster_dir/log before starting.
46 # c.Global.clean_logs = True
47
48 # The working directory for the process. The application will use os.chdir
49 # to change to this directory before starting.
50 # c.Global.work_dir = os.getcwd()
51
52
53 #-----------------------------------------------------------------------------
54 # Local process launchers
55 #-----------------------------------------------------------------------------
56
57 # The command line arguments to call the controller with.
58 # c.LocalControllerLauncher.controller_args = \
59 # ['--log-to-file','--log-level', '40']
60
61 # The working directory for the controller
62 # c.LocalEngineSetLauncher.work_dir = u''
63
64 # Command line argument passed to the engines.
65 # c.LocalEngineSetLauncher.engine_args = ['--log-to-file','--log-level', '40']
66
67 #-----------------------------------------------------------------------------
68 # MPIExec launchers
69 #-----------------------------------------------------------------------------
70
71 # The mpiexec/mpirun command to use in started the controller.
72 # c.MPIExecControllerLauncher.mpi_cmd = ['mpiexec']
73
74 # Additional arguments to pass to the actual mpiexec command.
75 # c.MPIExecControllerLauncher.mpi_args = []
76
77 # The command line argument to call the controller with.
78 # c.MPIExecControllerLauncher.controller_args = \
79 # ['--log-to-file','--log-level', '40']
80
81
82 # The mpiexec/mpirun command to use in started the controller.
83 # c.MPIExecEngineSetLauncher.mpi_cmd = ['mpiexec']
84
85 # Additional arguments to pass to the actual mpiexec command.
86 # c.MPIExecEngineSetLauncher.mpi_args = []
87
88 # Command line argument passed to the engines.
89 # c.MPIExecEngineSetLauncher.engine_args = ['--log-to-file','--log-level', '40']
90
91 # The default number of engines to start if not given elsewhere.
92 # c.MPIExecEngineSetLauncher.n = 1
93
94 #-----------------------------------------------------------------------------
95 # SSH launchers
96 #-----------------------------------------------------------------------------
97
98 # Todo
99
100
101 #-----------------------------------------------------------------------------
102 # Unix batch (PBS) schedulers launchers
103 #-----------------------------------------------------------------------------
104
105 # The command line program to use to submit a PBS job.
106 # c.PBSControllerLauncher.submit_command = 'qsub'
107
108 # The command line program to use to delete a PBS job.
109 # c.PBSControllerLauncher.delete_command = 'qdel'
110
111 # A regular expression that takes the output of qsub and find the job id.
112 # c.PBSControllerLauncher.job_id_regexp = r'\d+'
113
114 # The batch submission script used to start the controller. This is where
115 # environment variables would be setup, etc. This string is interpolated using
116 # the Itpl module in IPython.external. Basically, you can use ${n} for the
117 # number of engine and ${cluster_dir} for the cluster_dir.
118 # c.PBSControllerLauncher.batch_template = """"""
119
120 # The name of the instantiated batch script that will actually be used to
121 # submit the job. This will be written to the cluster directory.
122 # c.PBSControllerLauncher.batch_file_name = u'pbs_batch_script_controller'
123
124
125 # The command line program to use to submit a PBS job.
126 # c.PBSEngineSetLauncher.submit_command = 'qsub'
127
128 # The command line program to use to delete a PBS job.
129 # c.PBSEngineSetLauncher.delete_command = 'qdel'
130
131 # A regular expression that takes the output of qsub and find the job id.
132 # c.PBSEngineSetLauncher.job_id_regexp = r'\d+'
133
134 # The batch submission script used to start the engines. This is where
135 # environment variables would be setup, etc. This string is interpolated using
136 # the Itpl module in IPython.external. Basically, you can use ${n} for the
137 # number of engine and ${cluster_dir} for the cluster_dir.
138 # c.PBSEngineSetLauncher.batch_template = """"""
139
140 # The name of the instantiated batch script that will actually be used to
141 # submit the job. This will be written to the cluster directory.
142 # c.PBSEngineSetLauncher.batch_file_name = u'pbs_batch_script_engines'
143
144 #-----------------------------------------------------------------------------
145 # Windows HPC Server 2008 launcher configuration
146 #-----------------------------------------------------------------------------
147
148 # c.IPControllerJob.job_name = 'IPController'
149 # c.IPControllerJob.is_exclusive = False
150 # c.IPControllerJob.username = r'USERDOMAIN\USERNAME'
151 # c.IPControllerJob.priority = 'Highest'
152 # c.IPControllerJob.requested_nodes = ''
153 # c.IPControllerJob.project = 'MyProject'
154
155 # c.IPControllerTask.task_name = 'IPController'
156 # c.IPControllerTask.controller_cmd = [u'ipcontroller.exe']
157 # c.IPControllerTask.controller_args = ['--log-to-file', '--log-level', '40']
158 # c.IPControllerTask.environment_variables = {}
159
160 # c.WindowsHPCControllerLauncher.scheduler = 'HEADNODE'
161 # c.WindowsHPCControllerLauncher.job_file_name = u'ipcontroller_job.xml'
162
163
164 # c.IPEngineSetJob.job_name = 'IPEngineSet'
165 # c.IPEngineSetJob.is_exclusive = False
166 # c.IPEngineSetJob.username = r'USERDOMAIN\USERNAME'
167 # c.IPEngineSetJob.priority = 'Highest'
168 # c.IPEngineSetJob.requested_nodes = ''
169 # c.IPEngineSetJob.project = 'MyProject'
170
171 # c.IPEngineTask.task_name = 'IPEngine'
172 # c.IPEngineTask.engine_cmd = [u'ipengine.exe']
173 # c.IPEngineTask.engine_args = ['--log-to-file', '--log-level', '40']
174 # c.IPEngineTask.environment_variables = {}
175
176 # c.WindowsHPCEngineSetLauncher.scheduler = 'HEADNODE'
177 # c.WindowsHPCEngineSetLauncher.job_file_name = u'ipengineset_job.xml'
178
179
180
181
182
183
184
@@ -0,0 +1,136 b''
1 from IPython.config.loader import Config
2
3 c = get_config()
4
5 #-----------------------------------------------------------------------------
6 # Global configuration
7 #-----------------------------------------------------------------------------
8
9 # Basic Global config attributes
10
11 # Start up messages are logged to stdout using the logging module.
12 # These all happen before the twisted reactor is started and are
13 # useful for debugging purposes. Can be (10=DEBUG,20=INFO,30=WARN,40=CRITICAL)
14 # and smaller is more verbose.
15 # c.Global.log_level = 20
16
17 # Log to a file in cluster_dir/log, otherwise just log to sys.stdout.
18 # c.Global.log_to_file = False
19
20 # Remove old logs from cluster_dir/log before starting.
21 # c.Global.clean_logs = True
22
23 # A list of Python statements that will be run before starting the
24 # controller. This is provided because occasionally certain things need to
25 # be imported in the controller for pickling to work.
26 # c.Global.import_statements = ['import math']
27
28 # Reuse the controller's FURL files. If False, FURL files are regenerated
29 # each time the controller is run. If True, they will be reused, *but*, you
30 # also must set the network ports by hand. If set, this will override the
31 # values set for the client and engine connections below.
32 # c.Global.reuse_furls = True
33
34 # Enable SSL encryption on all connections to the controller. If set, this
35 # will override the values set for the client and engine connections below.
36 # c.Global.secure = True
37
38 # The working directory for the process. The application will use os.chdir
39 # to change to this directory before starting.
40 # c.Global.work_dir = os.getcwd()
41
42 #-----------------------------------------------------------------------------
43 # Configure the client services
44 #-----------------------------------------------------------------------------
45
46 # Basic client service config attributes
47
48 # The network interface the controller will listen on for client connections.
49 # This should be an IP address or hostname of the controller's host. The empty
50 # string means listen on all interfaces.
51 # c.FCClientServiceFactory.ip = ''
52
53 # The TCP/IP port the controller will listen on for client connections. If 0
54 # a random port will be used. If the controller's host has a firewall running
55 # it must allow incoming traffic on this port.
56 # c.FCClientServiceFactory.port = 0
57
58 # The client learns how to connect to the controller by looking at the
59 # location field embedded in the FURL. If this field is empty, all network
60 # interfaces that the controller is listening on will be listed. To have the
61 # client connect on a particular interface, list it here.
62 # c.FCClientServiceFactory.location = ''
63
64 # Use SSL encryption for the client connection.
65 # c.FCClientServiceFactory.secure = True
66
67 # Reuse the client FURL each time the controller is started. If set, you must
68 # also pick a specific network port above (FCClientServiceFactory.port).
69 # c.FCClientServiceFactory.reuse_furls = False
70
71 #-----------------------------------------------------------------------------
72 # Configure the engine services
73 #-----------------------------------------------------------------------------
74
75 # Basic config attributes for the engine services.
76
77 # The network interface the controller will listen on for engine connections.
78 # This should be an IP address or hostname of the controller's host. The empty
79 # string means listen on all interfaces.
80 # c.FCEngineServiceFactory.ip = ''
81
82 # The TCP/IP port the controller will listen on for engine connections. If 0
83 # a random port will be used. If the controller's host has a firewall running
84 # it must allow incoming traffic on this port.
85 # c.FCEngineServiceFactory.port = 0
86
87 # The engine learns how to connect to the controller by looking at the
88 # location field embedded in the FURL. If this field is empty, all network
89 # interfaces that the controller is listening on will be listed. To have the
90 # client connect on a particular interface, list it here.
91 # c.FCEngineServiceFactory.location = ''
92
93 # Use SSL encryption for the engine connection.
94 # c.FCEngineServiceFactory.secure = True
95
96 # Reuse the client FURL each time the controller is started. If set, you must
97 # also pick a specific network port above (FCClientServiceFactory.port).
98 # c.FCEngineServiceFactory.reuse_furls = False
99
100 #-----------------------------------------------------------------------------
101 # Developer level configuration attributes
102 #-----------------------------------------------------------------------------
103
104 # You shouldn't have to modify anything in this section. These attributes
105 # are more for developers who want to change the behavior of the controller
106 # at a fundamental level.
107
108 # c.FCClientServiceFactory.cert_file = u'ipcontroller-client.pem'
109
110 # default_client_interfaces = Config()
111 # default_client_interfaces.Task.interface_chain = [
112 # 'IPython.kernel.task.ITaskController',
113 # 'IPython.kernel.taskfc.IFCTaskController'
114 # ]
115 #
116 # default_client_interfaces.Task.furl_file = u'ipcontroller-tc.furl'
117 #
118 # default_client_interfaces.MultiEngine.interface_chain = [
119 # 'IPython.kernel.multiengine.IMultiEngine',
120 # 'IPython.kernel.multienginefc.IFCSynchronousMultiEngine'
121 # ]
122 #
123 # default_client_interfaces.MultiEngine.furl_file = u'ipcontroller-mec.furl'
124 #
125 # c.FCEngineServiceFactory.interfaces = default_client_interfaces
126
127 # c.FCEngineServiceFactory.cert_file = u'ipcontroller-engine.pem'
128
129 # default_engine_interfaces = Config()
130 # default_engine_interfaces.Default.interface_chain = [
131 # 'IPython.kernel.enginefc.IFCControllerBase'
132 # ]
133 #
134 # default_engine_interfaces.Default.furl_file = u'ipcontroller-engine.furl'
135 #
136 # c.FCEngineServiceFactory.interfaces = default_engine_interfaces
@@ -0,0 +1,90 b''
1 c = get_config()
2
3 #-----------------------------------------------------------------------------
4 # Global configuration
5 #-----------------------------------------------------------------------------
6
7 # Start up messages are logged to stdout using the logging module.
8 # These all happen before the twisted reactor is started and are
9 # useful for debugging purposes. Can be (10=DEBUG,20=INFO,30=WARN,40=CRITICAL)
10 # and smaller is more verbose.
11 # c.Global.log_level = 20
12
13 # Log to a file in cluster_dir/log, otherwise just log to sys.stdout.
14 # c.Global.log_to_file = False
15
16 # Remove old logs from cluster_dir/log before starting.
17 # c.Global.clean_logs = True
18
19 # A list of strings that will be executed in the users namespace on the engine
20 # before it connects to the controller.
21 # c.Global.exec_lines = ['import numpy']
22
23 # The engine will try to connect to the controller multiple times, to allow
24 # the controller time to startup and write its FURL file. These parameters
25 # control the number of retries (connect_max_tries) and the initial delay
26 # (connect_delay) between attemps. The actual delay between attempts gets
27 # longer each time by a factor of 1.5 (delay[i] = 1.5*delay[i-1])
28 # those attemps.
29 # c.Global.connect_delay = 0.1
30 # c.Global.connect_max_tries = 15
31
32 # By default, the engine will look for the controller's FURL file in its own
33 # cluster directory. Sometimes, the FURL file will be elsewhere and this
34 # attribute can be set to the full path of the FURL file.
35 # c.Global.furl_file = u''
36
37 # The working directory for the process. The application will use os.chdir
38 # to change to this directory before starting.
39 # c.Global.work_dir = os.getcwd()
40
41 #-----------------------------------------------------------------------------
42 # MPI configuration
43 #-----------------------------------------------------------------------------
44
45 # Upon starting the engine can be configured to call MPI_Init. This section
46 # configures that.
47
48 # Select which MPI section to execute to setup MPI. The value of this
49 # attribute must match the name of another attribute in the MPI config
50 # section (mpi4py, pytrilinos, etc.). This can also be set by the --mpi
51 # command line option.
52 # c.MPI.use = ''
53
54 # Initialize MPI using mpi4py. To use this, set c.MPI.use = 'mpi4py' to use
55 # --mpi=mpi4py at the command line.
56 # c.MPI.mpi4py = """from mpi4py import MPI as mpi
57 # mpi.size = mpi.COMM_WORLD.Get_size()
58 # mpi.rank = mpi.COMM_WORLD.Get_rank()
59 # """
60
61 # Initialize MPI using pytrilinos. To use this, set c.MPI.use = 'pytrilinos'
62 # to use --mpi=pytrilinos at the command line.
63 # c.MPI.pytrilinos = """from PyTrilinos import Epetra
64 # class SimpleStruct:
65 # pass
66 # mpi = SimpleStruct()
67 # mpi.rank = 0
68 # mpi.size = 0
69 # """
70
71 #-----------------------------------------------------------------------------
72 # Developer level configuration attributes
73 #-----------------------------------------------------------------------------
74
75 # You shouldn't have to modify anything in this section. These attributes
76 # are more for developers who want to change the behavior of the controller
77 # at a fundamental level.
78
79 # You should not have to change these attributes.
80
81 # c.Global.shell_class = 'IPython.kernel.core.interpreter.Interpreter'
82
83 # c.Global.furl_file_name = u'ipcontroller-engine.furl'
84
85
86
87
88
89
90
@@ -0,0 +1,148 b''
1 # Get the config being loaded so we can set attributes on it
2 c = get_config()
3
4 #-----------------------------------------------------------------------------
5 # Global options
6 #-----------------------------------------------------------------------------
7
8 # c.Global.display_banner = True
9
10 # c.Global.classic = False
11
12 # c.Global.nosep = True
13
14 # Set this to determine the detail of what is logged at startup.
15 # The default is 30 and possible values are 0,10,20,30,40,50.
16 # c.Global.log_level = 20
17
18 # This should be a list of importable Python modules that have an
19 # load_in_ipython(ip) method. This method gets called when the extension
20 # is loaded. You can put your extensions anywhere they can be imported
21 # but we add the extensions subdir of the ipython directory to sys.path
22 # during extension loading, so you can put them there as well.
23 # c.Global.extensions = [
24 # 'myextension'
25 # ]
26
27 # These lines are run in IPython in the user's namespace after extensions
28 # are loaded. They can contain full IPython syntax with magics etc.
29 # c.Global.exec_lines = [
30 # 'import numpy',
31 # 'a = 10; b = 20',
32 # '1/0'
33 # ]
34
35 # These files are run in IPython in the user's namespace. Files with a .py
36 # extension need to be pure Python. Files with a .ipy extension can have
37 # custom IPython syntax (like magics, etc.).
38 # These files need to be in the cwd, the ipython_dir or be absolute paths.
39 # c.Global.exec_files = [
40 # 'mycode.py',
41 # 'fancy.ipy'
42 # ]
43
44 #-----------------------------------------------------------------------------
45 # InteractiveShell options
46 #-----------------------------------------------------------------------------
47
48 # c.InteractiveShell.autocall = 1
49
50 # c.InteractiveShell.autoedit_syntax = False
51
52 # c.InteractiveShell.autoindent = True
53
54 # c.InteractiveShell.automagic = False
55
56 # c.InteractiveShell.banner1 = 'This if for overriding the default IPython banner'
57
58 # c.InteractiveShell.banner2 = "This is for extra banner text"
59
60 # c.InteractiveShell.cache_size = 1000
61
62 # c.InteractiveShell.colors = 'LightBG'
63
64 # c.InteractiveShell.color_info = True
65
66 # c.InteractiveShell.confirm_exit = True
67
68 # c.InteractiveShell.deep_reload = False
69
70 # c.InteractiveShell.editor = 'nano'
71
72 # c.InteractiveShell.logstart = True
73
74 # c.InteractiveShell.logfile = u'ipython_log.py'
75
76 # c.InteractiveShell.logappend = u'mylog.py'
77
78 # c.InteractiveShell.object_info_string_level = 0
79
80 # c.InteractiveShell.pager = 'less'
81
82 # c.InteractiveShell.pdb = False
83
84 # c.InteractiveShell.pprint = True
85
86 # c.InteractiveShell.prompt_in1 = 'In [\#]: '
87 # c.InteractiveShell.prompt_in2 = ' .\D.: '
88 # c.InteractiveShell.prompt_out = 'Out[\#]: '
89 # c.InteractiveShell.prompts_pad_left = True
90
91 # c.InteractiveShell.quiet = False
92
93 # Readline
94 # c.InteractiveShell.readline_use = True
95
96 # c.InteractiveShell.readline_parse_and_bind = [
97 # 'tab: complete',
98 # '"\C-l": possible-completions',
99 # 'set show-all-if-ambiguous on',
100 # '"\C-o": tab-insert',
101 # '"\M-i": " "',
102 # '"\M-o": "\d\d\d\d"',
103 # '"\M-I": "\d\d\d\d"',
104 # '"\C-r": reverse-search-history',
105 # '"\C-s": forward-search-history',
106 # '"\C-p": history-search-backward',
107 # '"\C-n": history-search-forward',
108 # '"\e[A": history-search-backward',
109 # '"\e[B": history-search-forward',
110 # '"\C-k": kill-line',
111 # '"\C-u": unix-line-discard',
112 # ]
113 # c.InteractiveShell.readline_remove_delims = '-/~'
114 # c.InteractiveShell.readline_merge_completions = True
115 # c.InteractiveShell.readline_omit_names = 0
116
117 # c.InteractiveShell.screen_length = 0
118
119 # c.InteractiveShell.separate_in = '\n'
120 # c.InteractiveShell.separate_out = ''
121 # c.InteractiveShell.separate_out2 = ''
122
123 # c.InteractiveShell.system_header = "IPython system call: "
124
125 # c.InteractiveShell.system_verbose = True
126
127 # c.InteractiveShell.term_title = False
128
129 # c.InteractiveShell.wildcards_case_sensitive = True
130
131 # c.InteractiveShell.xmode = 'Context'
132
133 #-----------------------------------------------------------------------------
134 # PrefilterManager options
135 #-----------------------------------------------------------------------------
136
137 # c.PrefilterManager.multi_line_specials = True
138
139 #-----------------------------------------------------------------------------
140 # AliasManager options
141 #-----------------------------------------------------------------------------
142
143 # Do this to disable all defaults
144 # c.AliasManager.default_aliases = []
145
146 # c.AliasManager.user_aliases = [
147 # ('foo', 'echo Hi')
148 # ] No newline at end of file
@@ -0,0 +1,62 b''
1 from os.path import join
2 pjoin = join
3
4 from IPython.utils.genutils import get_ipython_dir, get_security_dir
5 security_dir = get_security_dir()
6
7
8 ENGINE_LOGFILE = ''
9
10 ENGINE_FURL_FILE = 'ipcontroller-engine.furl'
11
12 MPI_CONFIG_MPI4PY = """from mpi4py import MPI as mpi
13 mpi.size = mpi.COMM_WORLD.Get_size()
14 mpi.rank = mpi.COMM_WORLD.Get_rank()
15 """
16
17 MPI_CONFIG_PYTRILINOS = """from PyTrilinos import Epetra
18 class SimpleStruct:
19 pass
20 mpi = SimpleStruct()
21 mpi.rank = 0
22 mpi.size = 0
23 """
24
25 MPI_DEFAULT = ''
26
27 CONTROLLER_LOGFILE = ''
28 CONTROLLER_IMPORT_STATEMENT = ''
29 CONTROLLER_REUSE_FURLS = False
30
31 ENGINE_TUB_IP = ''
32 ENGINE_TUB_PORT = 0
33 ENGINE_TUB_LOCATION = ''
34 ENGINE_TUB_SECURE = True
35 ENGINE_TUB_CERT_FILE = 'ipcontroller-engine.pem'
36 ENGINE_FC_INTERFACE = 'IPython.kernel.enginefc.IFCControllerBase'
37 ENGINE_FURL_FILE = 'ipcontroller-engine.furl'
38
39 CONTROLLER_INTERFACES = dict(
40 TASK = dict(
41 CONTROLLER_INTERFACE = 'IPython.kernel.task.ITaskController',
42 FC_INTERFACE = 'IPython.kernel.taskfc.IFCTaskController',
43 FURL_FILE = pjoin(security_dir, 'ipcontroller-tc.furl')
44 ),
45 MULTIENGINE = dict(
46 CONTROLLER_INTERFACE = 'IPython.kernel.multiengine.IMultiEngine',
47 FC_INTERFACE = 'IPython.kernel.multienginefc.IFCSynchronousMultiEngine',
48 FURL_FILE = pjoin(security_dir, 'ipcontroller-mec.furl')
49 )
50 )
51
52 CLIENT_TUB_IP = ''
53 CLIENT_TUB_PORT = 0
54 CLIENT_TUB_LOCATION = ''
55 CLIENT_TUB_SECURE = True
56 CLIENT_TUB_CERT_FILE = 'ipcontroller-client.pem'
57
58 CLIENT_INTERFACES = dict(
59 TASK = dict(FURL_FILE = 'ipcontroller-tc.furl'),
60 MULTIENGINE = dict(FURLFILE='ipcontroller-mec.furl')
61 )
62
@@ -0,0 +1,336 b''
1 #!/usr/bin/env python
2 # encoding: utf-8
3 """A simple configuration system.
4
5 Authors:
6
7 * Brian Granger
8 """
9
10 #-----------------------------------------------------------------------------
11 # Copyright (C) 2008-2009 The IPython Development Team
12 #
13 # Distributed under the terms of the BSD License. The full license is in
14 # the file COPYING, distributed as part of this software.
15 #-----------------------------------------------------------------------------
16
17 #-----------------------------------------------------------------------------
18 # Imports
19 #-----------------------------------------------------------------------------
20
21 import __builtin__
22 import os
23 import sys
24
25 from IPython.external import argparse
26 from IPython.utils.genutils import filefind
27
28 #-----------------------------------------------------------------------------
29 # Exceptions
30 #-----------------------------------------------------------------------------
31
32
33 class ConfigError(Exception):
34 pass
35
36
37 class ConfigLoaderError(ConfigError):
38 pass
39
40
41 #-----------------------------------------------------------------------------
42 # Config class for holding config information
43 #-----------------------------------------------------------------------------
44
45
46 class Config(dict):
47 """An attribute based dict that can do smart merges."""
48
49 def __init__(self, *args, **kwds):
50 dict.__init__(self, *args, **kwds)
51 # This sets self.__dict__ = self, but it has to be done this way
52 # because we are also overriding __setattr__.
53 dict.__setattr__(self, '__dict__', self)
54
55 def _merge(self, other):
56 to_update = {}
57 for k, v in other.items():
58 if not self.has_key(k):
59 to_update[k] = v
60 else: # I have this key
61 if isinstance(v, Config):
62 # Recursively merge common sub Configs
63 self[k]._merge(v)
64 else:
65 # Plain updates for non-Configs
66 to_update[k] = v
67
68 self.update(to_update)
69
70 def _is_section_key(self, key):
71 if key[0].upper()==key[0] and not key.startswith('_'):
72 return True
73 else:
74 return False
75
76 def has_key(self, key):
77 if self._is_section_key(key):
78 return True
79 else:
80 return dict.has_key(self, key)
81
82 def _has_section(self, key):
83 if self._is_section_key(key):
84 if dict.has_key(self, key):
85 return True
86 return False
87
88 def copy(self):
89 return type(self)(dict.copy(self))
90
91 def __copy__(self):
92 return self.copy()
93
94 def __deepcopy__(self, memo):
95 import copy
96 return type(self)(copy.deepcopy(self.items()))
97
98 def __getitem__(self, key):
99 # Because we use this for an exec namespace, we need to delegate
100 # the lookup of names in __builtin__ to itself. This means
101 # that you can't have section or attribute names that are
102 # builtins.
103 try:
104 return getattr(__builtin__, key)
105 except AttributeError:
106 pass
107 if self._is_section_key(key):
108 try:
109 return dict.__getitem__(self, key)
110 except KeyError:
111 c = Config()
112 dict.__setitem__(self, key, c)
113 return c
114 else:
115 return dict.__getitem__(self, key)
116
117 def __setitem__(self, key, value):
118 # Don't allow names in __builtin__ to be modified.
119 if hasattr(__builtin__, key):
120 raise ConfigError('Config variable names cannot have the same name '
121 'as a Python builtin: %s' % key)
122 if self._is_section_key(key):
123 if not isinstance(value, Config):
124 raise ValueError('values whose keys begin with an uppercase '
125 'char must be Config instances: %r, %r' % (key, value))
126 else:
127 dict.__setitem__(self, key, value)
128
129 def __getattr__(self, key):
130 try:
131 return self.__getitem__(key)
132 except KeyError, e:
133 raise AttributeError(e)
134
135 def __setattr__(self, key, value):
136 try:
137 self.__setitem__(key, value)
138 except KeyError, e:
139 raise AttributeError(e)
140
141 def __delattr__(self, key):
142 try:
143 dict.__delitem__(self, key)
144 except KeyError, e:
145 raise AttributeError(e)
146
147
148 #-----------------------------------------------------------------------------
149 # Config loading classes
150 #-----------------------------------------------------------------------------
151
152
153 class ConfigLoader(object):
154 """A object for loading configurations from just about anywhere.
155
156 The resulting configuration is packaged as a :class:`Struct`.
157
158 Notes
159 -----
160 A :class:`ConfigLoader` does one thing: load a config from a source
161 (file, command line arguments) and returns the data as a :class:`Struct`.
162 There are lots of things that :class:`ConfigLoader` does not do. It does
163 not implement complex logic for finding config files. It does not handle
164 default values or merge multiple configs. These things need to be
165 handled elsewhere.
166 """
167
168 def __init__(self):
169 """A base class for config loaders.
170
171 Examples
172 --------
173
174 >>> cl = ConfigLoader()
175 >>> config = cl.load_config()
176 >>> config
177 {}
178 """
179 self.clear()
180
181 def clear(self):
182 self.config = Config()
183
184 def load_config(self):
185 """Load a config from somewhere, return a Struct.
186
187 Usually, this will cause self.config to be set and then returned.
188 """
189 return self.config
190
191
192 class FileConfigLoader(ConfigLoader):
193 """A base class for file based configurations.
194
195 As we add more file based config loaders, the common logic should go
196 here.
197 """
198 pass
199
200
201 class PyFileConfigLoader(FileConfigLoader):
202 """A config loader for pure python files.
203
204 This calls execfile on a plain python file and looks for attributes
205 that are all caps. These attribute are added to the config Struct.
206 """
207
208 def __init__(self, filename, path=None):
209 """Build a config loader for a filename and path.
210
211 Parameters
212 ----------
213 filename : str
214 The file name of the config file.
215 path : str, list, tuple
216 The path to search for the config file on, or a sequence of
217 paths to try in order.
218 """
219 super(PyFileConfigLoader, self).__init__()
220 self.filename = filename
221 self.path = path
222 self.full_filename = ''
223 self.data = None
224
225 def load_config(self):
226 """Load the config from a file and return it as a Struct."""
227 self._find_file()
228 self._read_file_as_dict()
229 self._convert_to_config()
230 return self.config
231
232 def _find_file(self):
233 """Try to find the file by searching the paths."""
234 self.full_filename = filefind(self.filename, self.path)
235
236 def _read_file_as_dict(self):
237 """Load the config file into self.config, with recursive loading."""
238 # This closure is made available in the namespace that is used
239 # to exec the config file. This allows users to call
240 # load_subconfig('myconfig.py') to load config files recursively.
241 # It needs to be a closure because it has references to self.path
242 # and self.config. The sub-config is loaded with the same path
243 # as the parent, but it uses an empty config which is then merged
244 # with the parents.
245 def load_subconfig(fname):
246 loader = PyFileConfigLoader(fname, self.path)
247 try:
248 sub_config = loader.load_config()
249 except IOError:
250 # Pass silently if the sub config is not there. This happens
251 # when a user us using a profile, but not the default config.
252 pass
253 else:
254 self.config._merge(sub_config)
255
256 # Again, this needs to be a closure and should be used in config
257 # files to get the config being loaded.
258 def get_config():
259 return self.config
260
261 namespace = dict(load_subconfig=load_subconfig, get_config=get_config)
262 execfile(self.full_filename, namespace)
263
264 def _convert_to_config(self):
265 if self.data is None:
266 ConfigLoaderError('self.data does not exist')
267
268
269 class CommandLineConfigLoader(ConfigLoader):
270 """A config loader for command line arguments.
271
272 As we add more command line based loaders, the common logic should go
273 here.
274 """
275
276
277 class NoConfigDefault(object): pass
278 NoConfigDefault = NoConfigDefault()
279
280
281 class ArgParseConfigLoader(CommandLineConfigLoader):
282
283 # arguments = [(('-f','--file'),dict(type=str,dest='file'))]
284 arguments = ()
285
286 def __init__(self, *args, **kw):
287 """Create a config loader for use with argparse.
288
289 The args and kwargs arguments here are passed onto the constructor
290 of :class:`argparse.ArgumentParser`.
291 """
292 super(CommandLineConfigLoader, self).__init__()
293 self.args = args
294 self.kw = kw
295
296 def load_config(self, args=None):
297 """Parse command line arguments and return as a Struct."""
298 self._create_parser()
299 self._parse_args(args)
300 self._convert_to_config()
301 return self.config
302
303 def get_extra_args(self):
304 if hasattr(self, 'extra_args'):
305 return self.extra_args
306 else:
307 return []
308
309 def _create_parser(self):
310 self.parser = argparse.ArgumentParser(*self.args, **self.kw)
311 self._add_arguments()
312 self._add_other_arguments()
313
314 def _add_other_arguments(self):
315 pass
316
317 def _add_arguments(self):
318 for argument in self.arguments:
319 if not argument[1].has_key('default'):
320 argument[1]['default'] = NoConfigDefault
321 self.parser.add_argument(*argument[0],**argument[1])
322
323 def _parse_args(self, args=None):
324 """self.parser->self.parsed_data"""
325 if args is None:
326 self.parsed_data, self.extra_args = self.parser.parse_known_args()
327 else:
328 self.parsed_data, self.extra_args = self.parser.parse_known_args(args)
329
330 def _convert_to_config(self):
331 """self.parsed_data->self.config"""
332 for k, v in vars(self.parsed_data).items():
333 if v is not NoConfigDefault:
334 exec_str = 'self.config.' + k + '= v'
335 exec exec_str in locals(), globals()
336
@@ -0,0 +1,24 b''
1 c = get_config()
2
3 # This can be used at any point in a config file to load a sub config
4 # and merge it into the current one.
5 load_subconfig('ipython_config.py')
6
7 lines = """
8 from IPython.kernel.client import *
9 """
10
11 # You have to make sure that attributes that are containers already
12 # exist before using them. Simple assigning a new list will override
13 # all previous values.
14 if hasattr(c.Global, 'exec_lines'):
15 c.Global.exec_lines.append(lines)
16 else:
17 c.Global.exec_lines = [lines]
18
19 # Load the parallelmagic extension to enable %result, %px, %autopx magics.
20 if hasattr(c.Global, 'extensions'):
21 c.Global.extensions.append('parallelmagic')
22 else:
23 c.Global.extensions = ['parallelmagic']
24
@@ -0,0 +1,19 b''
1 c = get_config()
2
3 # This can be used at any point in a config file to load a sub config
4 # and merge it into the current one.
5 load_subconfig('ipython_config.py')
6
7 lines = """
8 import cmath
9 from math import *
10 """
11
12 # You have to make sure that attributes that are containers already
13 # exist before using them. Simple assigning a new list will override
14 # all previous values.
15 if hasattr(c.Global, 'exec_lines'):
16 c.Global.exec_lines.append(lines)
17 else:
18 c.Global.exec_lines = [lines]
19
@@ -0,0 +1,20 b''
1 c = get_config()
2
3 # This can be used at any point in a config file to load a sub config
4 # and merge it into the current one.
5 load_subconfig('ipython_config.py')
6
7 lines = """
8 import numpy
9 import scipy
10 import numpy as np
11 import scipy as sp
12 """
13
14 # You have to make sure that attributes that are containers already
15 # exist before using them. Simple assigning a new list will override
16 # all previous values.
17 if hasattr(c.Global, 'exec_lines'):
18 c.Global.exec_lines.append(lines)
19 else:
20 c.Global.exec_lines = [lines] No newline at end of file
@@ -0,0 +1,22 b''
1 c = get_config()
2
3 # This can be used at any point in a config file to load a sub config
4 # and merge it into the current one.
5 load_subconfig('ipython_config.py')
6
7 lines = """
8 import matplotlib
9 %gui -a wx
10 matplotlib.use('wxagg')
11 matplotlib.interactive(True)
12 from matplotlib import pyplot as plt
13 from matplotlib.pyplot import *
14 """
15
16 # You have to make sure that attributes that are containers already
17 # exist before using them. Simple assigning a new list will override
18 # all previous values.
19 if hasattr(c.Global, 'exec_lines'):
20 c.Global.exec_lines.append(lines)
21 else:
22 c.Global.exec_lines = [lines] No newline at end of file
@@ -0,0 +1,29 b''
1 c = get_config()
2
3 # This can be used at any point in a config file to load a sub config
4 # and merge it into the current one.
5 load_subconfig('ipython_config.py')
6
7 c.InteractiveShell.prompt_in1 = '\C_LightGreen\u@\h\C_LightBlue[\C_LightCyan\Y1\C_LightBlue]\C_Green|\#> '
8 c.InteractiveShell.prompt_in2 = '\C_Green|\C_LightGreen\D\C_Green> '
9 c.InteractiveShell.prompt_out = '<\#> '
10
11 c.InteractiveShell.prompts_pad_left = True
12
13 c.InteractiveShell.separate_in = ''
14 c.InteractiveShell.separate_out = ''
15 c.InteractiveShell.separate_out2 = ''
16
17 c.PrefilterManager.multi_line_specials = True
18
19 lines = """
20 %rehashx
21 """
22
23 # You have to make sure that attributes that are containers already
24 # exist before using them. Simple assigning a new list will override
25 # all previous values.
26 if hasattr(c.Global, 'exec_lines'):
27 c.Global.exec_lines.append(lines)
28 else:
29 c.Global.exec_lines = [lines] No newline at end of file
@@ -0,0 +1,21 b''
1 c = get_config()
2
3 # This can be used at any point in a config file to load a sub config
4 # and merge it into the current one.
5 load_subconfig('ipython_config.py')
6
7 lines = """
8 from __future__ import division
9 from sympy import *
10 x, y, z = symbols('xyz')
11 k, m, n = symbols('kmn', integer=True)
12 f, g, h = map(Function, 'fgh')
13 """
14
15 # You have to make sure that attributes that are containers already
16 # exist before using them. Simple assigning a new list will override
17 # all previous values.
18 if hasattr(c.Global, 'exec_lines'):
19 c.Global.exec_lines.append(lines)
20 else:
21 c.Global.exec_lines = [lines]
@@ -0,0 +1,5 b''
1 #!/usr/bin/env python
2 # encoding: utf-8
3
4
5
@@ -0,0 +1,163 b''
1 #!/usr/bin/env python
2 # encoding: utf-8
3 """
4 Tests for IPython.config.loader
5
6 Authors:
7
8 * Brian Granger
9 * Fernando Perez (design help)
10 """
11
12 #-----------------------------------------------------------------------------
13 # Copyright (C) 2008-2009 The IPython Development Team
14 #
15 # Distributed under the terms of the BSD License. The full license is in
16 # the file COPYING, distributed as part of this software.
17 #-----------------------------------------------------------------------------
18
19 #-----------------------------------------------------------------------------
20 # Imports
21 #-----------------------------------------------------------------------------
22
23 import os
24 from tempfile import mkstemp
25 from unittest import TestCase
26
27 from IPython.config.loader import (
28 Config,
29 PyFileConfigLoader,
30 ArgParseConfigLoader,
31 ConfigError
32 )
33
34 #-----------------------------------------------------------------------------
35 # Actual tests
36 #-----------------------------------------------------------------------------
37
38
39 pyfile = """
40 a = 10
41 b = 20
42 Foo.Bar.value = 10
43 Foo.Bam.value = range(10)
44 D.C.value = 'hi there'
45 """
46
47 class TestPyFileCL(TestCase):
48
49 def test_basic(self):
50 fd, fname = mkstemp()
51 f = os.fdopen(fd, 'w')
52 f.write(pyfile)
53 f.close()
54 # Unlink the file
55 cl = PyFileConfigLoader(fname)
56 config = cl.load_config()
57 self.assertEquals(config.a, 10)
58 self.assertEquals(config.b, 20)
59 self.assertEquals(config.Foo.Bar.value, 10)
60 self.assertEquals(config.Foo.Bam.value, range(10))
61 self.assertEquals(config.D.C.value, 'hi there')
62
63
64 class TestArgParseCL(TestCase):
65
66 def test_basic(self):
67
68 class MyLoader(ArgParseConfigLoader):
69 arguments = (
70 (('-f','--foo'), dict(dest='Global.foo', type=str)),
71 (('-b',), dict(dest='MyClass.bar', type=int)),
72 (('-n',), dict(dest='n', action='store_true')),
73 (('Global.bam',), dict(type=str))
74 )
75
76 cl = MyLoader()
77 config = cl.load_config('-f hi -b 10 -n wow'.split())
78 self.assertEquals(config.Global.foo, 'hi')
79 self.assertEquals(config.MyClass.bar, 10)
80 self.assertEquals(config.n, True)
81 self.assertEquals(config.Global.bam, 'wow')
82
83 def test_add_arguments(self):
84
85 class MyLoader(ArgParseConfigLoader):
86 def _add_arguments(self):
87 subparsers = self.parser.add_subparsers(dest='subparser_name')
88 subparser1 = subparsers.add_parser('1')
89 subparser1.add_argument('-x',dest='Global.x')
90 subparser2 = subparsers.add_parser('2')
91 subparser2.add_argument('y')
92
93 cl = MyLoader()
94 config = cl.load_config('2 frobble'.split())
95 self.assertEquals(config.subparser_name, '2')
96 self.assertEquals(config.y, 'frobble')
97 config = cl.load_config('1 -x frobble'.split())
98 self.assertEquals(config.subparser_name, '1')
99 self.assertEquals(config.Global.x, 'frobble')
100
101 class TestConfig(TestCase):
102
103 def test_setget(self):
104 c = Config()
105 c.a = 10
106 self.assertEquals(c.a, 10)
107 self.assertEquals(c.has_key('b'), False)
108
109 def test_auto_section(self):
110 c = Config()
111 self.assertEquals(c.has_key('A'), True)
112 self.assertEquals(c._has_section('A'), False)
113 A = c.A
114 A.foo = 'hi there'
115 self.assertEquals(c._has_section('A'), True)
116 self.assertEquals(c.A.foo, 'hi there')
117 del c.A
118 self.assertEquals(len(c.A.keys()),0)
119
120 def test_merge_doesnt_exist(self):
121 c1 = Config()
122 c2 = Config()
123 c2.bar = 10
124 c2.Foo.bar = 10
125 c1._merge(c2)
126 self.assertEquals(c1.Foo.bar, 10)
127 self.assertEquals(c1.bar, 10)
128 c2.Bar.bar = 10
129 c1._merge(c2)
130 self.assertEquals(c1.Bar.bar, 10)
131
132 def test_merge_exists(self):
133 c1 = Config()
134 c2 = Config()
135 c1.Foo.bar = 10
136 c1.Foo.bam = 30
137 c2.Foo.bar = 20
138 c2.Foo.wow = 40
139 c1._merge(c2)
140 self.assertEquals(c1.Foo.bam, 30)
141 self.assertEquals(c1.Foo.bar, 20)
142 self.assertEquals(c1.Foo.wow, 40)
143 c2.Foo.Bam.bam = 10
144 c1._merge(c2)
145 self.assertEquals(c1.Foo.Bam.bam, 10)
146
147 def test_deepcopy(self):
148 c1 = Config()
149 c1.Foo.bar = 10
150 c1.Foo.bam = 30
151 c1.a = 'asdf'
152 c1.b = range(10)
153 import copy
154 c2 = copy.deepcopy(c1)
155 self.assertEquals(c1, c2)
156 self.assert_(c1 is not c2)
157 self.assert_(c1.Foo is not c2.Foo)
158
159 def test_builtin(self):
160 c1 = Config()
161 exec 'foo = True' in c1
162 self.assertEquals(c1.foo, True)
163 self.assertRaises(ConfigError, setattr, c1, 'ValueError', 10)
@@ -0,0 +1,262 b''
1 #!/usr/bin/env python
2 # encoding: utf-8
3 """
4 IPython's alias component
5
6 Authors:
7
8 * Brian Granger
9 """
10
11 #-----------------------------------------------------------------------------
12 # Copyright (C) 2008-2009 The IPython Development Team
13 #
14 # Distributed under the terms of the BSD License. The full license is in
15 # the file COPYING, distributed as part of this software.
16 #-----------------------------------------------------------------------------
17
18 #-----------------------------------------------------------------------------
19 # Imports
20 #-----------------------------------------------------------------------------
21
22 import __builtin__
23 import keyword
24 import os
25 import re
26 import sys
27
28 from IPython.core.component import Component
29 from IPython.core.splitinput import split_user_input
30
31 from IPython.utils.traitlets import CBool, List, Instance
32 from IPython.utils.genutils import error
33 from IPython.utils.autoattr import auto_attr
34
35 #-----------------------------------------------------------------------------
36 # Utilities
37 #-----------------------------------------------------------------------------
38
39 # This is used as the pattern for calls to split_user_input.
40 shell_line_split = re.compile(r'^(\s*)(\S*\s*)(.*$)')
41
42 def default_aliases():
43 # Make some aliases automatically
44 # Prepare list of shell aliases to auto-define
45 if os.name == 'posix':
46 default_aliases = ('mkdir mkdir', 'rmdir rmdir',
47 'mv mv -i','rm rm -i','cp cp -i',
48 'cat cat','less less','clear clear',
49 # a better ls
50 'ls ls -F',
51 # long ls
52 'll ls -lF')
53 # Extra ls aliases with color, which need special treatment on BSD
54 # variants
55 ls_extra = ( # color ls
56 'lc ls -F -o --color',
57 # ls normal files only
58 'lf ls -F -o --color %l | grep ^-',
59 # ls symbolic links
60 'lk ls -F -o --color %l | grep ^l',
61 # directories or links to directories,
62 'ldir ls -F -o --color %l | grep /$',
63 # things which are executable
64 'lx ls -F -o --color %l | grep ^-..x',
65 )
66 # The BSDs don't ship GNU ls, so they don't understand the
67 # --color switch out of the box
68 if 'bsd' in sys.platform:
69 ls_extra = ( # ls normal files only
70 'lf ls -lF | grep ^-',
71 # ls symbolic links
72 'lk ls -lF | grep ^l',
73 # directories or links to directories,
74 'ldir ls -lF | grep /$',
75 # things which are executable
76 'lx ls -lF | grep ^-..x',
77 )
78 default_aliases = default_aliases + ls_extra
79 elif os.name in ['nt','dos']:
80 default_aliases = ('ls dir /on',
81 'ddir dir /ad /on', 'ldir dir /ad /on',
82 'mkdir mkdir','rmdir rmdir','echo echo',
83 'ren ren','cls cls','copy copy')
84 else:
85 default_aliases = ()
86 return [s.split(None,1) for s in default_aliases]
87
88
89 class AliasError(Exception):
90 pass
91
92
93 class InvalidAliasError(AliasError):
94 pass
95
96
97 #-----------------------------------------------------------------------------
98 # Main AliasManager class
99 #-----------------------------------------------------------------------------
100
101
102 class AliasManager(Component):
103
104 default_aliases = List(default_aliases(), config=True)
105 user_aliases = List(default_value=[], config=True)
106
107 def __init__(self, parent, config=None):
108 super(AliasManager, self).__init__(parent, config=config)
109 self.alias_table = {}
110 self.exclude_aliases()
111 self.init_aliases()
112
113 @auto_attr
114 def shell(self):
115 return Component.get_instances(
116 root=self.root,
117 klass='IPython.core.iplib.InteractiveShell')[0]
118
119 def __contains__(self, name):
120 if name in self.alias_table:
121 return True
122 else:
123 return False
124
125 @property
126 def aliases(self):
127 return [(item[0], item[1][1]) for item in self.alias_table.iteritems()]
128
129 def exclude_aliases(self):
130 # set of things NOT to alias (keywords, builtins and some magics)
131 no_alias = set(['cd','popd','pushd','dhist','alias','unalias'])
132 no_alias.update(set(keyword.kwlist))
133 no_alias.update(set(__builtin__.__dict__.keys()))
134 self.no_alias = no_alias
135
136 def init_aliases(self):
137 # Load default aliases
138 for name, cmd in self.default_aliases:
139 self.soft_define_alias(name, cmd)
140
141 # Load user aliases
142 for name, cmd in self.user_aliases:
143 self.soft_define_alias(name, cmd)
144
145 def clear_aliases(self):
146 self.alias_table.clear()
147
148 def soft_define_alias(self, name, cmd):
149 """Define an alias, but don't raise on an AliasError."""
150 try:
151 self.define_alias(name, cmd)
152 except AliasError, e:
153 error("Invalid alias: %s" % e)
154
155 def define_alias(self, name, cmd):
156 """Define a new alias after validating it.
157
158 This will raise an :exc:`AliasError` if there are validation
159 problems.
160 """
161 nargs = self.validate_alias(name, cmd)
162 self.alias_table[name] = (nargs, cmd)
163
164 def undefine_alias(self, name):
165 if self.alias_table.has_key(name):
166 del self.alias_table[name]
167
168 def validate_alias(self, name, cmd):
169 """Validate an alias and return the its number of arguments."""
170 if name in self.no_alias:
171 raise InvalidAliasError("The name %s can't be aliased "
172 "because it is a keyword or builtin." % name)
173 if not (isinstance(cmd, basestring)):
174 raise InvalidAliasError("An alias command must be a string, "
175 "got: %r" % name)
176 nargs = cmd.count('%s')
177 if nargs>0 and cmd.find('%l')>=0:
178 raise InvalidAliasError('The %s and %l specifiers are mutually '
179 'exclusive in alias definitions.')
180 return nargs
181
182 def call_alias(self, alias, rest=''):
183 """Call an alias given its name and the rest of the line."""
184 cmd = self.transform_alias(alias, rest)
185 try:
186 self.shell.system(cmd)
187 except:
188 self.shell.showtraceback()
189
190 def transform_alias(self, alias,rest=''):
191 """Transform alias to system command string."""
192 nargs, cmd = self.alias_table[alias]
193
194 if ' ' in cmd and os.path.isfile(cmd):
195 cmd = '"%s"' % cmd
196
197 # Expand the %l special to be the user's input line
198 if cmd.find('%l') >= 0:
199 cmd = cmd.replace('%l', rest)
200 rest = ''
201 if nargs==0:
202 # Simple, argument-less aliases
203 cmd = '%s %s' % (cmd, rest)
204 else:
205 # Handle aliases with positional arguments
206 args = rest.split(None, nargs)
207 if len(args) < nargs:
208 raise AliasError('Alias <%s> requires %s arguments, %s given.' %
209 (alias, nargs, len(args)))
210 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
211 return cmd
212
213 def expand_alias(self, line):
214 """ Expand an alias in the command line
215
216 Returns the provided command line, possibly with the first word
217 (command) translated according to alias expansion rules.
218
219 [ipython]|16> _ip.expand_aliases("np myfile.txt")
220 <16> 'q:/opt/np/notepad++.exe myfile.txt'
221 """
222
223 pre,fn,rest = split_user_input(line)
224 res = pre + self.expand_aliases(fn, rest)
225 return res
226
227 def expand_aliases(self, fn, rest):
228 """Expand multiple levels of aliases:
229
230 if:
231
232 alias foo bar /tmp
233 alias baz foo
234
235 then:
236
237 baz huhhahhei -> bar /tmp huhhahhei
238
239 """
240 line = fn + " " + rest
241
242 done = set()
243 while 1:
244 pre,fn,rest = split_user_input(line, shell_line_split)
245 if fn in self.alias_table:
246 if fn in done:
247 warn("Cyclic alias definition, repeated '%s'" % fn)
248 return ""
249 done.add(fn)
250
251 l2 = self.transform_alias(fn, rest)
252 if l2 == line:
253 break
254 # ls -> ls -F should not recurse forever
255 if l2.split(None,1)[0] == line.split(None,1)[0]:
256 line = l2
257 break
258 line=l2
259 else:
260 break
261
262 return line
@@ -0,0 +1,364 b''
1 #!/usr/bin/env python
2 # encoding: utf-8
3 """
4 An application for IPython.
5
6 All top-level applications should use the classes in this module for
7 handling configuration and creating componenets.
8
9 The job of an :class:`Application` is to create the master configuration
10 object and then create the components, passing the config to them.
11
12 Authors:
13
14 * Brian Granger
15 * Fernando Perez
16
17 Notes
18 -----
19 """
20
21 #-----------------------------------------------------------------------------
22 # Copyright (C) 2008-2009 The IPython Development Team
23 #
24 # Distributed under the terms of the BSD License. The full license is in
25 # the file COPYING, distributed as part of this software.
26 #-----------------------------------------------------------------------------
27
28 #-----------------------------------------------------------------------------
29 # Imports
30 #-----------------------------------------------------------------------------
31
32 import logging
33 import os
34 import sys
35
36 from IPython.core import release
37 from IPython.utils.genutils import get_ipython_dir
38 from IPython.config.loader import (
39 PyFileConfigLoader,
40 ArgParseConfigLoader,
41 Config,
42 NoConfigDefault
43 )
44
45 #-----------------------------------------------------------------------------
46 # Classes and functions
47 #-----------------------------------------------------------------------------
48
49
50 class BaseAppArgParseConfigLoader(ArgParseConfigLoader):
51 """Default command line options for IPython based applications."""
52
53 def _add_other_arguments(self):
54 self.parser.add_argument('--ipython-dir',
55 dest='Global.ipython_dir',type=unicode,
56 help='Set to override default location of Global.ipython_dir.',
57 default=NoConfigDefault,
58 metavar='Global.ipython_dir')
59 self.parser.add_argument('-p', '--profile',
60 dest='Global.profile',type=unicode,
61 help='The string name of the ipython profile to be used.',
62 default=NoConfigDefault,
63 metavar='Global.profile')
64 self.parser.add_argument('--log-level',
65 dest="Global.log_level",type=int,
66 help='Set the log level (0,10,20,30,40,50). Default is 30.',
67 default=NoConfigDefault,
68 metavar='Global.log_level')
69 self.parser.add_argument('--config-file',
70 dest='Global.config_file',type=unicode,
71 help='Set the config file name to override default.',
72 default=NoConfigDefault,
73 metavar='Global.config_file')
74
75
76 class ApplicationError(Exception):
77 pass
78
79
80 class Application(object):
81 """Load a config, construct components and set them running."""
82
83 name = u'ipython'
84 description = 'IPython: an enhanced interactive Python shell.'
85 config_file_name = u'ipython_config.py'
86 default_log_level = logging.WARN
87
88 def __init__(self):
89 self._exiting = False
90 self.init_logger()
91 # Track the default and actual separately because some messages are
92 # only printed if we aren't using the default.
93 self.default_config_file_name = self.config_file_name
94
95 def init_logger(self):
96 self.log = logging.getLogger(self.__class__.__name__)
97 # This is used as the default until the command line arguments are read.
98 self.log.setLevel(self.default_log_level)
99 self._log_handler = logging.StreamHandler()
100 self._log_formatter = logging.Formatter("[%(name)s] %(message)s")
101 self._log_handler.setFormatter(self._log_formatter)
102 self.log.addHandler(self._log_handler)
103
104 def _set_log_level(self, level):
105 self.log.setLevel(level)
106
107 def _get_log_level(self):
108 return self.log.level
109
110 log_level = property(_get_log_level, _set_log_level)
111
112 def start(self):
113 """Start the application."""
114 self.attempt(self.create_default_config)
115 self.log_default_config()
116 self.set_default_config_log_level()
117 self.attempt(self.pre_load_command_line_config)
118 self.attempt(self.load_command_line_config, action='abort')
119 self.set_command_line_config_log_level()
120 self.attempt(self.post_load_command_line_config)
121 self.log_command_line_config()
122 self.attempt(self.find_ipython_dir)
123 self.attempt(self.find_resources)
124 self.attempt(self.find_config_file_name)
125 self.attempt(self.find_config_file_paths)
126 self.attempt(self.pre_load_file_config)
127 self.attempt(self.load_file_config)
128 self.set_file_config_log_level()
129 self.attempt(self.post_load_file_config)
130 self.log_file_config()
131 self.attempt(self.merge_configs)
132 self.log_master_config()
133 self.attempt(self.pre_construct)
134 self.attempt(self.construct)
135 self.attempt(self.post_construct)
136 self.attempt(self.start_app)
137
138 #-------------------------------------------------------------------------
139 # Various stages of Application creation
140 #-------------------------------------------------------------------------
141
142 def create_default_config(self):
143 """Create defaults that can't be set elsewhere.
144
145 For the most part, we try to set default in the class attributes
146 of Components. But, defaults the top-level Application (which is
147 not a HasTraitlets or Component) are not set in this way. Instead
148 we set them here. The Global section is for variables like this that
149 don't belong to a particular component.
150 """
151 self.default_config = Config()
152 self.default_config.Global.ipython_dir = get_ipython_dir()
153 self.default_config.Global.log_level = self.log_level
154
155 def log_default_config(self):
156 self.log.debug('Default config loaded:')
157 self.log.debug(repr(self.default_config))
158
159 def set_default_config_log_level(self):
160 try:
161 self.log_level = self.default_config.Global.log_level
162 except AttributeError:
163 # Fallback to the default_log_level class attribute
164 pass
165
166 def create_command_line_config(self):
167 """Create and return a command line config loader."""
168 return BaseAppArgParseConfigLoader(
169 description=self.description,
170 version=release.version
171 )
172
173 def pre_load_command_line_config(self):
174 """Do actions just before loading the command line config."""
175 pass
176
177 def load_command_line_config(self):
178 """Load the command line config."""
179 loader = self.create_command_line_config()
180 self.command_line_config = loader.load_config()
181 self.extra_args = loader.get_extra_args()
182
183 def set_command_line_config_log_level(self):
184 try:
185 self.log_level = self.command_line_config.Global.log_level
186 except AttributeError:
187 pass
188
189 def post_load_command_line_config(self):
190 """Do actions just after loading the command line config."""
191 pass
192
193 def log_command_line_config(self):
194 self.log.debug("Command line config loaded:")
195 self.log.debug(repr(self.command_line_config))
196
197 def find_ipython_dir(self):
198 """Set the IPython directory.
199
200 This sets ``self.ipython_dir``, but the actual value that is passed
201 to the application is kept in either ``self.default_config`` or
202 ``self.command_line_config``. This also adds ``self.ipython_dir`` to
203 ``sys.path`` so config files there can be references by other config
204 files.
205 """
206
207 try:
208 self.ipython_dir = self.command_line_config.Global.ipython_dir
209 except AttributeError:
210 self.ipython_dir = self.default_config.Global.ipython_dir
211 sys.path.append(os.path.abspath(self.ipython_dir))
212 if not os.path.isdir(self.ipython_dir):
213 os.makedirs(self.ipython_dir, mode=0777)
214 self.log.debug("IPYTHON_DIR set to: %s" % self.ipython_dir)
215
216 def find_resources(self):
217 """Find other resources that need to be in place.
218
219 Things like cluster directories need to be in place to find the
220 config file. These happen right after the IPython directory has
221 been set.
222 """
223 pass
224
225 def find_config_file_name(self):
226 """Find the config file name for this application.
227
228 This must set ``self.config_file_name`` to the filename of the
229 config file to use (just the filename). The search paths for the
230 config file are set in :meth:`find_config_file_paths` and then passed
231 to the config file loader where they are resolved to an absolute path.
232
233 If a profile has been set at the command line, this will resolve
234 it.
235 """
236
237 try:
238 self.config_file_name = self.command_line_config.Global.config_file
239 except AttributeError:
240 pass
241
242 try:
243 self.profile_name = self.command_line_config.Global.profile
244 name_parts = self.config_file_name.split('.')
245 name_parts.insert(1, u'_' + self.profile_name + u'.')
246 self.config_file_name = ''.join(name_parts)
247 except AttributeError:
248 pass
249
250 def find_config_file_paths(self):
251 """Set the search paths for resolving the config file.
252
253 This must set ``self.config_file_paths`` to a sequence of search
254 paths to pass to the config file loader.
255 """
256 self.config_file_paths = (os.getcwd(), self.ipython_dir)
257
258 def pre_load_file_config(self):
259 """Do actions before the config file is loaded."""
260 pass
261
262 def load_file_config(self):
263 """Load the config file.
264
265 This tries to load the config file from disk. If successful, the
266 ``CONFIG_FILE`` config variable is set to the resolved config file
267 location. If not successful, an empty config is used.
268 """
269 self.log.debug("Attempting to load config file: %s" % self.config_file_name)
270 loader = PyFileConfigLoader(self.config_file_name,
271 path=self.config_file_paths)
272 try:
273 self.file_config = loader.load_config()
274 self.file_config.Global.config_file = loader.full_filename
275 except IOError:
276 # Only warn if the default config file was NOT being used.
277 if not self.config_file_name==self.default_config_file_name:
278 self.log.warn("Config file not found, skipping: %s" % \
279 self.config_file_name, exc_info=True)
280 self.file_config = Config()
281 except:
282 self.log.warn("Error loading config file: %s" % \
283 self.config_file_name, exc_info=True)
284 self.file_config = Config()
285
286 def set_file_config_log_level(self):
287 # We need to keeep self.log_level updated. But we only use the value
288 # of the file_config if a value was not specified at the command
289 # line, because the command line overrides everything.
290 if not hasattr(self.command_line_config.Global, 'log_level'):
291 try:
292 self.log_level = self.file_config.Global.log_level
293 except AttributeError:
294 pass # Use existing value
295
296 def post_load_file_config(self):
297 """Do actions after the config file is loaded."""
298 pass
299
300 def log_file_config(self):
301 if hasattr(self.file_config.Global, 'config_file'):
302 self.log.debug("Config file loaded: %s" % self.file_config.Global.config_file)
303 self.log.debug(repr(self.file_config))
304
305 def merge_configs(self):
306 """Merge the default, command line and file config objects."""
307 config = Config()
308 config._merge(self.default_config)
309 config._merge(self.file_config)
310 config._merge(self.command_line_config)
311 self.master_config = config
312
313 def log_master_config(self):
314 self.log.debug("Master config created:")
315 self.log.debug(repr(self.master_config))
316
317 def pre_construct(self):
318 """Do actions after the config has been built, but before construct."""
319 pass
320
321 def construct(self):
322 """Construct the main components that make up this app."""
323 self.log.debug("Constructing components for application")
324
325 def post_construct(self):
326 """Do actions after construct, but before starting the app."""
327 pass
328
329 def start_app(self):
330 """Actually start the app."""
331 self.log.debug("Starting application")
332
333 #-------------------------------------------------------------------------
334 # Utility methods
335 #-------------------------------------------------------------------------
336
337 def abort(self):
338 """Abort the starting of the application."""
339 if self._exiting:
340 pass
341 else:
342 self.log.critical("Aborting application: %s" % self.name, exc_info=True)
343 self._exiting = True
344 sys.exit(1)
345
346 def exit(self, exit_status=0):
347 if self._exiting:
348 pass
349 else:
350 self.log.debug("Exiting application: %s" % self.name)
351 self._exiting = True
352 sys.exit(exit_status)
353
354 def attempt(self, func, action='abort'):
355 try:
356 func()
357 except SystemExit:
358 raise
359 except:
360 if action == 'abort':
361 self.abort()
362 elif action == 'exit':
363 self.exit(0)
364
@@ -0,0 +1,45 b''
1 #!/usr/bin/env python
2 # encoding: utf-8
3 """
4 Autocall capabilities for IPython.core.
5
6 Authors:
7
8 * Brian Granger
9 * Fernando Perez
10
11 Notes
12 -----
13 """
14
15 #-----------------------------------------------------------------------------
16 # Copyright (C) 2008-2009 The IPython Development Team
17 #
18 # Distributed under the terms of the BSD License. The full license is in
19 # the file COPYING, distributed as part of this software.
20 #-----------------------------------------------------------------------------
21
22 #-----------------------------------------------------------------------------
23 # Imports
24 #-----------------------------------------------------------------------------
25
26
27 #-----------------------------------------------------------------------------
28 # Code
29 #-----------------------------------------------------------------------------
30
31 class IPyAutocall(object):
32 """ Instances of this class are always autocalled
33
34 This happens regardless of 'autocall' variable state. Use this to
35 develop macro-like mechanisms.
36 """
37
38 def set_ip(self,ip):
39 """ Will be used to set _ip point to current ipython instance b/f call
40
41 Override this method if you don't want this to happen.
42
43 """
44 self._ip = ip
45
@@ -0,0 +1,118 b''
1 #!/usr/bin/env python
2 # encoding: utf-8
3 """
4 A context manager for managing things injected into :mod:`__builtin__`.
5
6 Authors:
7
8 * Brian Granger
9 """
10
11 #-----------------------------------------------------------------------------
12 # Copyright (C) 2008-2009 The IPython Development Team
13 #
14 # Distributed under the terms of the BSD License. The full license is in
15 # the file COPYING, distributed as part of this software.
16 #-----------------------------------------------------------------------------
17
18 #-----------------------------------------------------------------------------
19 # Imports
20 #-----------------------------------------------------------------------------
21
22 import __builtin__
23
24 from IPython.core.component import Component
25 from IPython.core.quitter import Quitter
26
27 from IPython.utils.autoattr import auto_attr
28
29 #-----------------------------------------------------------------------------
30 # Classes and functions
31 #-----------------------------------------------------------------------------
32
33
34 class BuiltinUndefined(object): pass
35 BuiltinUndefined = BuiltinUndefined()
36
37
38 class BuiltinTrap(Component):
39
40 def __init__(self, parent):
41 super(BuiltinTrap, self).__init__(parent, None, None)
42 self._orig_builtins = {}
43 # We define this to track if a single BuiltinTrap is nested.
44 # Only turn off the trap when the outermost call to __exit__ is made.
45 self._nested_level = 0
46
47 @auto_attr
48 def shell(self):
49 return Component.get_instances(
50 root=self.root,
51 klass='IPython.core.iplib.InteractiveShell')[0]
52
53 def __enter__(self):
54 if self._nested_level == 0:
55 self.set()
56 self._nested_level += 1
57 # I return self, so callers can use add_builtin in a with clause.
58 return self
59
60 def __exit__(self, type, value, traceback):
61 if self._nested_level == 1:
62 self.unset()
63 self._nested_level -= 1
64 # Returning False will cause exceptions to propagate
65 return False
66
67 def add_builtin(self, key, value):
68 """Add a builtin and save the original."""
69 orig = __builtin__.__dict__.get(key, BuiltinUndefined)
70 self._orig_builtins[key] = orig
71 __builtin__.__dict__[key] = value
72
73 def remove_builtin(self, key):
74 """Remove an added builtin and re-set the original."""
75 try:
76 orig = self._orig_builtins.pop(key)
77 except KeyError:
78 pass
79 else:
80 if orig is BuiltinUndefined:
81 del __builtin__.__dict__[key]
82 else:
83 __builtin__.__dict__[key] = orig
84
85 def set(self):
86 """Store ipython references in the __builtin__ namespace."""
87 self.add_builtin('exit', Quitter(self.shell, 'exit'))
88 self.add_builtin('quit', Quitter(self.shell, 'quit'))
89 self.add_builtin('get_ipython', self.shell.get_ipython)
90
91 # Recursive reload function
92 try:
93 from IPython.lib import deepreload
94 if self.shell.deep_reload:
95 self.add_builtin('reload', deepreload.reload)
96 else:
97 self.add_builtin('dreload', deepreload.reload)
98 del deepreload
99 except ImportError:
100 pass
101
102 # Keep in the builtins a flag for when IPython is active. We set it
103 # with setdefault so that multiple nested IPythons don't clobber one
104 # another. Each will increase its value by one upon being activated,
105 # which also gives us a way to determine the nesting level.
106 __builtin__.__dict__.setdefault('__IPYTHON__active',0)
107
108 def unset(self):
109 """Remove any builtins which might have been added by add_builtins, or
110 restore overwritten ones to their previous values."""
111 for key in self._orig_builtins.keys():
112 self.remove_builtin(key)
113 self._orig_builtins.clear()
114 self._builtins_added = False
115 try:
116 del __builtin__.__dict__['__IPYTHON__active']
117 except KeyError:
118 pass
@@ -0,0 +1,346 b''
1 #!/usr/bin/env python
2 # encoding: utf-8
3 """
4 A lightweight component system for IPython.
5
6 Authors:
7
8 * Brian Granger
9 * Fernando Perez
10 """
11
12 #-----------------------------------------------------------------------------
13 # Copyright (C) 2008-2009 The IPython Development Team
14 #
15 # Distributed under the terms of the BSD License. The full license is in
16 # the file COPYING, distributed as part of this software.
17 #-----------------------------------------------------------------------------
18
19 #-----------------------------------------------------------------------------
20 # Imports
21 #-----------------------------------------------------------------------------
22
23 from copy import deepcopy
24 import datetime
25 from weakref import WeakValueDictionary
26
27 from IPython.utils.importstring import import_item
28 from IPython.config.loader import Config
29 from IPython.utils.traitlets import (
30 HasTraitlets, TraitletError, MetaHasTraitlets, Instance, This
31 )
32
33
34 #-----------------------------------------------------------------------------
35 # Helper classes for Components
36 #-----------------------------------------------------------------------------
37
38
39 class ComponentError(Exception):
40 pass
41
42 class MetaComponentTracker(type):
43 """A metaclass that tracks instances of Components and its subclasses."""
44
45 def __init__(cls, name, bases, d):
46 super(MetaComponentTracker, cls).__init__(name, bases, d)
47 cls.__instance_refs = WeakValueDictionary()
48 cls.__numcreated = 0
49
50 def __call__(cls, *args, **kw):
51 """Called when a class is called (instantiated)!!!
52
53 When a Component or subclass is instantiated, this is called and
54 the instance is saved in a WeakValueDictionary for tracking.
55 """
56 instance = cls.__new__(cls, *args, **kw)
57
58 # Register the instance before __init__ is called so get_instances
59 # works inside __init__ methods!
60 indices = cls.register_instance(instance)
61
62 # This is in a try/except because of the __init__ method fails, the
63 # instance is discarded and shouldn't be tracked.
64 try:
65 if isinstance(instance, cls):
66 cls.__init__(instance, *args, **kw)
67 except:
68 # Unregister the instance because __init__ failed!
69 cls.unregister_instances(indices)
70 raise
71 else:
72 return instance
73
74 def register_instance(cls, instance):
75 """Register instance with cls and its subclasses."""
76 # indices is a list of the keys used to register the instance
77 # with. This list is needed if the instance needs to be unregistered.
78 indices = []
79 for c in cls.__mro__:
80 if issubclass(cls, c) and issubclass(c, Component):
81 c.__numcreated += 1
82 indices.append(c.__numcreated)
83 c.__instance_refs[c.__numcreated] = instance
84 else:
85 break
86 return indices
87
88 def unregister_instances(cls, indices):
89 """Unregister instance with cls and its subclasses."""
90 for c, index in zip(cls.__mro__, indices):
91 try:
92 del c.__instance_refs[index]
93 except KeyError:
94 pass
95
96 def clear_instances(cls):
97 """Clear all instances tracked by cls."""
98 cls.__instance_refs.clear()
99 cls.__numcreated = 0
100
101 def get_instances(cls, name=None, root=None, klass=None):
102 """Get all instances of cls and its subclasses.
103
104 Parameters
105 ----------
106 name : str
107 Limit to components with this name.
108 root : Component or subclass
109 Limit to components having this root.
110 klass : class or str
111 Limits to instances of the class or its subclasses. If a str
112 is given ut must be in the form 'foo.bar.MyClass'. The str
113 form of this argument is useful for forward declarations.
114 """
115 if klass is not None:
116 if isinstance(klass, basestring):
117 klass = import_item(klass)
118 # Limit search to instances of klass for performance
119 if issubclass(klass, Component):
120 return klass.get_instances(name=name, root=root)
121 instances = cls.__instance_refs.values()
122 if name is not None:
123 instances = [i for i in instances if i.name == name]
124 if klass is not None:
125 instances = [i for i in instances if isinstance(i, klass)]
126 if root is not None:
127 instances = [i for i in instances if i.root == root]
128 return instances
129
130 def get_instances_by_condition(cls, call, name=None, root=None,
131 klass=None):
132 """Get all instances of cls, i such that call(i)==True.
133
134 This also takes the ``name`` and ``root`` and ``classname``
135 arguments of :meth:`get_instance`
136 """
137 return [i for i in cls.get_instances(name, root, klass) if call(i)]
138
139
140 def masquerade_as(instance, cls):
141 """Let instance masquerade as an instance of cls.
142
143 Sometimes, such as in testing code, it is useful to let a class
144 masquerade as another. Python, being duck typed, allows this by
145 default. But, instances of components are tracked by their class type.
146
147 After calling this, ``cls.get_instances()`` will return ``instance``. This
148 does not, however, cause ``isinstance(instance, cls)`` to return ``True``.
149
150 Parameters
151 ----------
152 instance : an instance of a Component or Component subclass
153 The instance that will pretend to be a cls.
154 cls : subclass of Component
155 The Component subclass that instance will pretend to be.
156 """
157 cls.register_instance(instance)
158
159
160 class ComponentNameGenerator(object):
161 """A Singleton to generate unique component names."""
162
163 def __init__(self, prefix):
164 self.prefix = prefix
165 self.i = 0
166
167 def __call__(self):
168 count = self.i
169 self.i += 1
170 return "%s%s" % (self.prefix, count)
171
172
173 ComponentNameGenerator = ComponentNameGenerator('ipython.component')
174
175
176 class MetaComponent(MetaHasTraitlets, MetaComponentTracker):
177 pass
178
179
180 #-----------------------------------------------------------------------------
181 # Component implementation
182 #-----------------------------------------------------------------------------
183
184
185 class Component(HasTraitlets):
186
187 __metaclass__ = MetaComponent
188
189 # Traitlets are fun!
190 config = Instance(Config,(),{})
191 parent = This()
192 root = This()
193 created = None
194
195 def __init__(self, parent, name=None, config=None):
196 """Create a component given a parent and possibly and name and config.
197
198 Parameters
199 ----------
200 parent : Component subclass
201 The parent in the component graph. The parent is used
202 to get the root of the component graph.
203 name : str
204 The unique name of the component. If empty, then a unique
205 one will be autogenerated.
206 config : Config
207 If this is empty, self.config = parent.config, otherwise
208 self.config = config and root.config is ignored. This argument
209 should only be used to *override* the automatic inheritance of
210 parent.config. If a caller wants to modify parent.config
211 (not override), the caller should make a copy and change
212 attributes and then pass the copy to this argument.
213
214 Notes
215 -----
216 Subclasses of Component must call the :meth:`__init__` method of
217 :class:`Component` *before* doing anything else and using
218 :func:`super`::
219
220 class MyComponent(Component):
221 def __init__(self, parent, name=None, config=None):
222 super(MyComponent, self).__init__(parent, name, config)
223 # Then any other code you need to finish initialization.
224
225 This ensures that the :attr:`parent`, :attr:`name` and :attr:`config`
226 attributes are handled properly.
227 """
228 super(Component, self).__init__()
229 self._children = []
230 if name is None:
231 self.name = ComponentNameGenerator()
232 else:
233 self.name = name
234 self.root = self # This is the default, it is set when parent is set
235 self.parent = parent
236 if config is not None:
237 self.config = config
238 # We used to deepcopy, but for now we are trying to just save
239 # by reference. This *could* have side effects as all components
240 # will share config. In fact, I did find such a side effect in
241 # _config_changed below. If a config attribute value was a mutable type
242 # all instances of a component were getting the same copy, effectively
243 # making that a class attribute.
244 # self.config = deepcopy(config)
245 else:
246 if self.parent is not None:
247 self.config = self.parent.config
248 # We used to deepcopy, but for now we are trying to just save
249 # by reference. This *could* have side effects as all components
250 # will share config. In fact, I did find such a side effect in
251 # _config_changed below. If a config attribute value was a mutable type
252 # all instances of a component were getting the same copy, effectively
253 # making that a class attribute.
254 # self.config = deepcopy(self.parent.config)
255
256 self.created = datetime.datetime.now()
257
258 #-------------------------------------------------------------------------
259 # Static traitlet notifiations
260 #-------------------------------------------------------------------------
261
262 def _parent_changed(self, name, old, new):
263 if old is not None:
264 old._remove_child(self)
265 if new is not None:
266 new._add_child(self)
267
268 if new is None:
269 self.root = self
270 else:
271 self.root = new.root
272
273 def _root_changed(self, name, old, new):
274 if self.parent is None:
275 if not (new is self):
276 raise ComponentError("Root not self, but parent is None.")
277 else:
278 if not self.parent.root is new:
279 raise ComponentError("Error in setting the root attribute: "
280 "root != parent.root")
281
282 def _config_changed(self, name, old, new):
283 """Update all the class traits having ``config=True`` as metadata.
284
285 For any class traitlet with a ``config`` metadata attribute that is
286 ``True``, we update the traitlet with the value of the corresponding
287 config entry.
288 """
289 # Get all traitlets with a config metadata entry that is True
290 traitlets = self.traitlets(config=True)
291
292 # We auto-load config section for this class as well as any parent
293 # classes that are Component subclasses. This starts with Component
294 # and works down the mro loading the config for each section.
295 section_names = [cls.__name__ for cls in \
296 reversed(self.__class__.__mro__) if
297 issubclass(cls, Component) and issubclass(self.__class__, cls)]
298
299 for sname in section_names:
300 # Don't do a blind getattr as that would cause the config to
301 # dynamically create the section with name self.__class__.__name__.
302 if new._has_section(sname):
303 my_config = new[sname]
304 for k, v in traitlets.items():
305 # Don't allow traitlets with config=True to start with
306 # uppercase. Otherwise, they are confused with Config
307 # subsections. But, developers shouldn't have uppercase
308 # attributes anyways! (PEP 6)
309 if k[0].upper()==k[0] and not k.startswith('_'):
310 raise ComponentError('Component traitlets with '
311 'config=True must start with a lowercase so they are '
312 'not confused with Config subsections: %s.%s' % \
313 (self.__class__.__name__, k))
314 try:
315 # Here we grab the value from the config
316 # If k has the naming convention of a config
317 # section, it will be auto created.
318 config_value = my_config[k]
319 except KeyError:
320 pass
321 else:
322 # print "Setting %s.%s from %s.%s=%r" % \
323 # (self.__class__.__name__,k,sname,k,config_value)
324 # We have to do a deepcopy here if we don't deepcopy the entire
325 # config object. If we don't, a mutable config_value will be
326 # shared by all instances, effectively making it a class attribute.
327 setattr(self, k, deepcopy(config_value))
328
329 @property
330 def children(self):
331 """A list of all my child components."""
332 return self._children
333
334 def _remove_child(self, child):
335 """A private method for removing children components."""
336 if child in self._children:
337 index = self._children.index(child)
338 del self._children[index]
339
340 def _add_child(self, child):
341 """A private method for adding children components."""
342 if child not in self._children:
343 self._children.append(child)
344
345 def __repr__(self):
346 return "<%s('%s')>" % (self.__class__.__name__, self.name)
@@ -0,0 +1,77 b''
1 #!/usr/bin/env python
2 # encoding: utf-8
3 """
4 A context manager for handling sys.displayhook.
5
6 Authors:
7
8 * Robert Kern
9 * Brian Granger
10 """
11
12 #-----------------------------------------------------------------------------
13 # Copyright (C) 2008-2009 The IPython Development Team
14 #
15 # Distributed under the terms of the BSD License. The full license is in
16 # the file COPYING, distributed as part of this software.
17 #-----------------------------------------------------------------------------
18
19 #-----------------------------------------------------------------------------
20 # Imports
21 #-----------------------------------------------------------------------------
22
23 import sys
24
25 from IPython.core.component import Component
26
27 from IPython.utils.autoattr import auto_attr
28
29 #-----------------------------------------------------------------------------
30 # Classes and functions
31 #-----------------------------------------------------------------------------
32
33
34 class DisplayTrap(Component):
35 """Object to manage sys.displayhook.
36
37 This came from IPython.core.kernel.display_hook, but is simplified
38 (no callbacks or formatters) until more of the core is refactored.
39 """
40
41 def __init__(self, parent, hook):
42 super(DisplayTrap, self).__init__(parent, None, None)
43 self.hook = hook
44 self.old_hook = None
45 # We define this to track if a single BuiltinTrap is nested.
46 # Only turn off the trap when the outermost call to __exit__ is made.
47 self._nested_level = 0
48
49 # @auto_attr
50 # def shell(self):
51 # return Component.get_instances(
52 # root=self.root,
53 # klass='IPython.core.iplib.InteractiveShell')[0]
54
55 def __enter__(self):
56 if self._nested_level == 0:
57 self.set()
58 self._nested_level += 1
59 return self
60
61 def __exit__(self, type, value, traceback):
62 if self._nested_level == 1:
63 self.unset()
64 self._nested_level -= 1
65 # Returning False will cause exceptions to propagate
66 return False
67
68 def set(self):
69 """Set the hook."""
70 if sys.displayhook is not self.hook:
71 self.old_hook = sys.displayhook
72 sys.displayhook = self.hook
73
74 def unset(self):
75 """Unset the hook."""
76 sys.displayhook = self.old_hook
77
@@ -0,0 +1,272 b''
1 #!/usr/bin/env python
2 # encoding: utf-8
3 """
4 An embedded IPython shell.
5
6 Authors:
7
8 * Brian Granger
9 * Fernando Perez
10
11 Notes
12 -----
13 """
14
15 #-----------------------------------------------------------------------------
16 # Copyright (C) 2008-2009 The IPython Development Team
17 #
18 # Distributed under the terms of the BSD License. The full license is in
19 # the file COPYING, distributed as part of this software.
20 #-----------------------------------------------------------------------------
21
22 #-----------------------------------------------------------------------------
23 # Imports
24 #-----------------------------------------------------------------------------
25
26 from __future__ import with_statement
27
28 import sys
29 from contextlib import nested
30
31 from IPython.core import ultratb
32 from IPython.core.iplib import InteractiveShell
33 from IPython.core.ipapp import load_default_config
34
35 from IPython.utils.traitlets import Bool, Str, CBool
36 from IPython.utils.genutils import ask_yes_no
37
38
39 #-----------------------------------------------------------------------------
40 # Classes and functions
41 #-----------------------------------------------------------------------------
42
43 # This is an additional magic that is exposed in embedded shells.
44 def kill_embedded(self,parameter_s=''):
45 """%kill_embedded : deactivate for good the current embedded IPython.
46
47 This function (after asking for confirmation) sets an internal flag so that
48 an embedded IPython will never activate again. This is useful to
49 permanently disable a shell that is being called inside a loop: once you've
50 figured out what you needed from it, you may then kill it and the program
51 will then continue to run without the interactive shell interfering again.
52 """
53
54 kill = ask_yes_no("Are you sure you want to kill this embedded instance "
55 "(y/n)? [y/N] ",'n')
56 if kill:
57 self.embedded_active = False
58 print "This embedded IPython will not reactivate anymore once you exit."
59
60
61 class InteractiveShellEmbed(InteractiveShell):
62
63 dummy_mode = Bool(False)
64 exit_msg = Str('')
65 embedded = CBool(True)
66 embedded_active = CBool(True)
67 # Like the base class display_banner is not configurable, but here it
68 # is True by default.
69 display_banner = CBool(True)
70
71 def __init__(self, parent=None, config=None, ipython_dir=None, usage=None,
72 user_ns=None, user_global_ns=None,
73 banner1=None, banner2=None, display_banner=None,
74 custom_exceptions=((),None), exit_msg=''):
75
76 self.save_sys_ipcompleter()
77
78 super(InteractiveShellEmbed,self).__init__(
79 parent=parent, config=config, ipython_dir=ipython_dir, usage=usage,
80 user_ns=user_ns, user_global_ns=user_global_ns,
81 banner1=banner1, banner2=banner2, display_banner=display_banner,
82 custom_exceptions=custom_exceptions)
83
84 self.exit_msg = exit_msg
85 self.define_magic("kill_embedded", kill_embedded)
86
87 # don't use the ipython crash handler so that user exceptions aren't
88 # trapped
89 sys.excepthook = ultratb.FormattedTB(color_scheme=self.colors,
90 mode=self.xmode,
91 call_pdb=self.pdb)
92
93 self.restore_sys_ipcompleter()
94
95 def init_sys_modules(self):
96 pass
97
98 def save_sys_ipcompleter(self):
99 """Save readline completer status."""
100 try:
101 #print 'Save completer',sys.ipcompleter # dbg
102 self.sys_ipcompleter_orig = sys.ipcompleter
103 except:
104 pass # not nested with IPython
105
106 def restore_sys_ipcompleter(self):
107 """Restores the readline completer which was in place.
108
109 This allows embedded IPython within IPython not to disrupt the
110 parent's completion.
111 """
112 try:
113 self.readline.set_completer(self.sys_ipcompleter_orig)
114 sys.ipcompleter = self.sys_ipcompleter_orig
115 except:
116 pass
117
118 def __call__(self, header='', local_ns=None, global_ns=None, dummy=None,
119 stack_depth=1):
120 """Activate the interactive interpreter.
121
122 __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start
123 the interpreter shell with the given local and global namespaces, and
124 optionally print a header string at startup.
125
126 The shell can be globally activated/deactivated using the
127 set/get_dummy_mode methods. This allows you to turn off a shell used
128 for debugging globally.
129
130 However, *each* time you call the shell you can override the current
131 state of dummy_mode with the optional keyword parameter 'dummy'. For
132 example, if you set dummy mode on with IPShell.set_dummy_mode(1), you
133 can still have a specific call work by making it as IPShell(dummy=0).
134
135 The optional keyword parameter dummy controls whether the call
136 actually does anything.
137 """
138
139 # If the user has turned it off, go away
140 if not self.embedded_active:
141 return
142
143 # Normal exits from interactive mode set this flag, so the shell can't
144 # re-enter (it checks this variable at the start of interactive mode).
145 self.exit_now = False
146
147 # Allow the dummy parameter to override the global __dummy_mode
148 if dummy or (dummy != 0 and self.dummy_mode):
149 return
150
151 if self.has_readline:
152 self.set_completer()
153
154 # self.banner is auto computed
155 if header:
156 self.old_banner2 = self.banner2
157 self.banner2 = self.banner2 + '\n' + header + '\n'
158
159 # Call the embedding code with a stack depth of 1 so it can skip over
160 # our call and get the original caller's namespaces.
161 self.mainloop(local_ns, global_ns, stack_depth=stack_depth)
162
163 self.banner2 = self.old_banner2
164
165 if self.exit_msg is not None:
166 print self.exit_msg
167
168 self.restore_sys_ipcompleter()
169
170 def mainloop(self, local_ns=None, global_ns=None, stack_depth=0,
171 display_banner=None):
172 """Embeds IPython into a running python program.
173
174 Input:
175
176 - header: An optional header message can be specified.
177
178 - local_ns, global_ns: working namespaces. If given as None, the
179 IPython-initialized one is updated with __main__.__dict__, so that
180 program variables become visible but user-specific configuration
181 remains possible.
182
183 - stack_depth: specifies how many levels in the stack to go to
184 looking for namespaces (when local_ns and global_ns are None). This
185 allows an intermediate caller to make sure that this function gets
186 the namespace from the intended level in the stack. By default (0)
187 it will get its locals and globals from the immediate caller.
188
189 Warning: it's possible to use this in a program which is being run by
190 IPython itself (via %run), but some funny things will happen (a few
191 globals get overwritten). In the future this will be cleaned up, as
192 there is no fundamental reason why it can't work perfectly."""
193
194 # Get locals and globals from caller
195 if local_ns is None or global_ns is None:
196 call_frame = sys._getframe(stack_depth).f_back
197
198 if local_ns is None:
199 local_ns = call_frame.f_locals
200 if global_ns is None:
201 global_ns = call_frame.f_globals
202
203 # Update namespaces and fire up interpreter
204
205 # The global one is easy, we can just throw it in
206 self.user_global_ns = global_ns
207
208 # but the user/local one is tricky: ipython needs it to store internal
209 # data, but we also need the locals. We'll copy locals in the user
210 # one, but will track what got copied so we can delete them at exit.
211 # This is so that a later embedded call doesn't see locals from a
212 # previous call (which most likely existed in a separate scope).
213 local_varnames = local_ns.keys()
214 self.user_ns.update(local_ns)
215 #self.user_ns['local_ns'] = local_ns # dbg
216
217 # Patch for global embedding to make sure that things don't overwrite
218 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
219 # FIXME. Test this a bit more carefully (the if.. is new)
220 if local_ns is None and global_ns is None:
221 self.user_global_ns.update(__main__.__dict__)
222
223 # make sure the tab-completer has the correct frame information, so it
224 # actually completes using the frame's locals/globals
225 self.set_completer_frame()
226
227 with nested(self.builtin_trap, self.display_trap):
228 self.interact(display_banner=display_banner)
229
230 # now, purge out the user namespace from anything we might have added
231 # from the caller's local namespace
232 delvar = self.user_ns.pop
233 for var in local_varnames:
234 delvar(var,None)
235
236
237 _embedded_shell = None
238
239
240 def embed(header='', config=None, usage=None, banner1=None, banner2=None,
241 display_banner=True, exit_msg=''):
242 """Call this to embed IPython at the current point in your program.
243
244 The first invocation of this will create an :class:`InteractiveShellEmbed`
245 instance and then call it. Consecutive calls just call the already
246 created instance.
247
248 Here is a simple example::
249
250 from IPython import embed
251 a = 10
252 b = 20
253 embed('First time')
254 c = 30
255 d = 40
256 embed
257
258 Full customization can be done by passing a :class:`Struct` in as the
259 config argument.
260 """
261 if config is None:
262 config = load_default_config()
263 config.InteractiveShellEmbed = config.InteractiveShell
264 global _embedded_shell
265 if _embedded_shell is None:
266 _embedded_shell = InteractiveShellEmbed(
267 config=config, usage=usage,
268 banner1=banner1, banner2=banner2,
269 display_banner=display_banner, exit_msg=exit_msg
270 )
271 _embedded_shell(header=header, stack_depth=2)
272
@@ -0,0 +1,52 b''
1 #!/usr/bin/env python
2 # encoding: utf-8
3 """
4 Global exception classes for IPython.core.
5
6 Authors:
7
8 * Brian Granger
9 * Fernando Perez
10
11 Notes
12 -----
13 """
14
15 #-----------------------------------------------------------------------------
16 # Copyright (C) 2008-2009 The IPython Development Team
17 #
18 # Distributed under the terms of the BSD License. The full license is in
19 # the file COPYING, distributed as part of this software.
20 #-----------------------------------------------------------------------------
21
22 #-----------------------------------------------------------------------------
23 # Imports
24 #-----------------------------------------------------------------------------
25
26 #-----------------------------------------------------------------------------
27 # Exception classes
28 #-----------------------------------------------------------------------------
29
30 class IPythonCoreError(Exception):
31 pass
32
33
34 class TryNext(IPythonCoreError):
35 """Try next hook exception.
36
37 Raise this in your hook function to indicate that the next hook handler
38 should be used to handle the operation. If you pass arguments to the
39 constructor those arguments will be used by the next hook instead of the
40 original ones.
41 """
42
43 def __init__(self, *args, **kwargs):
44 self.args = args
45 self.kwargs = kwargs
46
47 class UsageError(IPythonCoreError):
48 """Error in magic function arguments, etc.
49
50 Something that probably won't warrant a full traceback, but should
51 nevertheless interrupt a macro / batch file.
52 """ No newline at end of file
@@ -0,0 +1,38 b''
1 #!/usr/bin/env python
2 # encoding: utf-8
3 """
4 This module is *completely* deprecated and should no longer be used for
5 any purpose. Currently, we have a few parts of the core that have
6 not been componentized and thus, still rely on this module. When everything
7 has been made into a component, this module will be sent to deathrow.
8 """
9
10 #-----------------------------------------------------------------------------
11 # Copyright (C) 2008-2009 The IPython Development Team
12 #
13 # Distributed under the terms of the BSD License. The full license is in
14 # the file COPYING, distributed as part of this software.
15 #-----------------------------------------------------------------------------
16
17 #-----------------------------------------------------------------------------
18 # Imports
19 #-----------------------------------------------------------------------------
20
21 from IPython.core.error import TryNext, UsageError, IPythonCoreError
22
23 #-----------------------------------------------------------------------------
24 # Classes and functions
25 #-----------------------------------------------------------------------------
26
27
28 def get():
29 """Get the most recently created InteractiveShell instance."""
30 from IPython.core.iplib import InteractiveShell
31 insts = InteractiveShell.get_instances()
32 if len(insts)==0:
33 return None
34 most_recent = insts[0]
35 for inst in insts[1:]:
36 if inst.created > most_recent.created:
37 most_recent = inst
38 return most_recent
This diff has been collapsed as it changes many lines, (544 lines changed) Show them Hide them
@@ -0,0 +1,544 b''
1 #!/usr/bin/env python
2 # encoding: utf-8
3 """
4 The :class:`~IPython.core.application.Application` object for the command
5 line :command:`ipython` program.
6
7 Authors:
8
9 * Brian Granger
10 * Fernando Perez
11
12 Notes
13 -----
14 """
15
16 #-----------------------------------------------------------------------------
17 # Copyright (C) 2008-2009 The IPython Development Team
18 #
19 # Distributed under the terms of the BSD License. The full license is in
20 # the file COPYING, distributed as part of this software.
21 #-----------------------------------------------------------------------------
22
23 #-----------------------------------------------------------------------------
24 # Imports
25 #-----------------------------------------------------------------------------
26
27 import logging
28 import os
29 import sys
30 import warnings
31
32 from IPython.core.application import Application, BaseAppArgParseConfigLoader
33 from IPython.core import release
34 from IPython.core.iplib import InteractiveShell
35 from IPython.config.loader import (
36 NoConfigDefault,
37 Config,
38 PyFileConfigLoader
39 )
40
41 from IPython.lib import inputhook
42
43 from IPython.utils.genutils import filefind, get_ipython_dir
44
45 #-----------------------------------------------------------------------------
46 # Utilities and helpers
47 #-----------------------------------------------------------------------------
48
49
50 ipython_desc = """
51 A Python shell with automatic history (input and output), dynamic object
52 introspection, easier configuration, command completion, access to the system
53 shell and more.
54 """
55
56 def pylab_warning():
57 msg = """
58
59 IPython's -pylab mode has been disabled until matplotlib supports this version
60 of IPython. This version of IPython has greatly improved GUI integration that
61 matplotlib will soon be able to take advantage of. This will eventually
62 result in greater stability and a richer API for matplotlib under IPython.
63 However during this transition, you will either need to use an older version
64 of IPython, or do the following to use matplotlib interactively::
65
66 import matplotlib
67 matplotlib.interactive(True)
68 matplotlib.use('wxagg') # adjust for your backend
69 %gui -a wx # adjust for your GUI
70 from matplotlib import pyplot as plt
71
72 See the %gui magic for information on the new interface.
73 """
74 warnings.warn(msg, category=DeprecationWarning, stacklevel=1)
75
76
77 #-----------------------------------------------------------------------------
78 # Main classes and functions
79 #-----------------------------------------------------------------------------
80
81 cl_args = (
82 (('--autocall',), dict(
83 type=int, dest='InteractiveShell.autocall', default=NoConfigDefault,
84 help='Set the autocall value (0,1,2).',
85 metavar='InteractiveShell.autocall')
86 ),
87 (('--autoindent',), dict(
88 action='store_true', dest='InteractiveShell.autoindent', default=NoConfigDefault,
89 help='Turn on autoindenting.')
90 ),
91 (('--no-autoindent',), dict(
92 action='store_false', dest='InteractiveShell.autoindent', default=NoConfigDefault,
93 help='Turn off autoindenting.')
94 ),
95 (('--automagic',), dict(
96 action='store_true', dest='InteractiveShell.automagic', default=NoConfigDefault,
97 help='Turn on the auto calling of magic commands.')
98 ),
99 (('--no-automagic',), dict(
100 action='store_false', dest='InteractiveShell.automagic', default=NoConfigDefault,
101 help='Turn off the auto calling of magic commands.')
102 ),
103 (('--autoedit-syntax',), dict(
104 action='store_true', dest='InteractiveShell.autoedit_syntax', default=NoConfigDefault,
105 help='Turn on auto editing of files with syntax errors.')
106 ),
107 (('--no-autoedit-syntax',), dict(
108 action='store_false', dest='InteractiveShell.autoedit_syntax', default=NoConfigDefault,
109 help='Turn off auto editing of files with syntax errors.')
110 ),
111 (('--banner',), dict(
112 action='store_true', dest='Global.display_banner', default=NoConfigDefault,
113 help='Display a banner upon starting IPython.')
114 ),
115 (('--no-banner',), dict(
116 action='store_false', dest='Global.display_banner', default=NoConfigDefault,
117 help="Don't display a banner upon starting IPython.")
118 ),
119 (('--cache-size',), dict(
120 type=int, dest='InteractiveShell.cache_size', default=NoConfigDefault,
121 help="Set the size of the output cache.",
122 metavar='InteractiveShell.cache_size')
123 ),
124 (('--classic',), dict(
125 action='store_true', dest='Global.classic', default=NoConfigDefault,
126 help="Gives IPython a similar feel to the classic Python prompt.")
127 ),
128 (('--colors',), dict(
129 type=str, dest='InteractiveShell.colors', default=NoConfigDefault,
130 help="Set the color scheme (NoColor, Linux, and LightBG).",
131 metavar='InteractiveShell.colors')
132 ),
133 (('--color-info',), dict(
134 action='store_true', dest='InteractiveShell.color_info', default=NoConfigDefault,
135 help="Enable using colors for info related things.")
136 ),
137 (('--no-color-info',), dict(
138 action='store_false', dest='InteractiveShell.color_info', default=NoConfigDefault,
139 help="Disable using colors for info related things.")
140 ),
141 (('--confirm-exit',), dict(
142 action='store_true', dest='InteractiveShell.confirm_exit', default=NoConfigDefault,
143 help="Prompt the user when existing.")
144 ),
145 (('--no-confirm-exit',), dict(
146 action='store_false', dest='InteractiveShell.confirm_exit', default=NoConfigDefault,
147 help="Don't prompt the user when existing.")
148 ),
149 (('--deep-reload',), dict(
150 action='store_true', dest='InteractiveShell.deep_reload', default=NoConfigDefault,
151 help="Enable deep (recursive) reloading by default.")
152 ),
153 (('--no-deep-reload',), dict(
154 action='store_false', dest='InteractiveShell.deep_reload', default=NoConfigDefault,
155 help="Disable deep (recursive) reloading by default.")
156 ),
157 (('--editor',), dict(
158 type=str, dest='InteractiveShell.editor', default=NoConfigDefault,
159 help="Set the editor used by IPython (default to $EDITOR/vi/notepad).",
160 metavar='InteractiveShell.editor')
161 ),
162 (('--log','-l'), dict(
163 action='store_true', dest='InteractiveShell.logstart', default=NoConfigDefault,
164 help="Start logging to the default file (./ipython_log.py).")
165 ),
166 (('--logfile','-lf'), dict(
167 type=unicode, dest='InteractiveShell.logfile', default=NoConfigDefault,
168 help="Start logging to logfile.",
169 metavar='InteractiveShell.logfile')
170 ),
171 (('--log-append','-la'), dict(
172 type=unicode, dest='InteractiveShell.logappend', default=NoConfigDefault,
173 help="Start logging to the give file in append mode.",
174 metavar='InteractiveShell.logfile')
175 ),
176 (('--pdb',), dict(
177 action='store_true', dest='InteractiveShell.pdb', default=NoConfigDefault,
178 help="Enable auto calling the pdb debugger after every exception.")
179 ),
180 (('--no-pdb',), dict(
181 action='store_false', dest='InteractiveShell.pdb', default=NoConfigDefault,
182 help="Disable auto calling the pdb debugger after every exception.")
183 ),
184 (('--pprint',), dict(
185 action='store_true', dest='InteractiveShell.pprint', default=NoConfigDefault,
186 help="Enable auto pretty printing of results.")
187 ),
188 (('--no-pprint',), dict(
189 action='store_false', dest='InteractiveShell.pprint', default=NoConfigDefault,
190 help="Disable auto auto pretty printing of results.")
191 ),
192 (('--prompt-in1','-pi1'), dict(
193 type=str, dest='InteractiveShell.prompt_in1', default=NoConfigDefault,
194 help="Set the main input prompt ('In [\#]: ')",
195 metavar='InteractiveShell.prompt_in1')
196 ),
197 (('--prompt-in2','-pi2'), dict(
198 type=str, dest='InteractiveShell.prompt_in2', default=NoConfigDefault,
199 help="Set the secondary input prompt (' .\D.: ')",
200 metavar='InteractiveShell.prompt_in2')
201 ),
202 (('--prompt-out','-po'), dict(
203 type=str, dest='InteractiveShell.prompt_out', default=NoConfigDefault,
204 help="Set the output prompt ('Out[\#]:')",
205 metavar='InteractiveShell.prompt_out')
206 ),
207 (('--quick',), dict(
208 action='store_true', dest='Global.quick', default=NoConfigDefault,
209 help="Enable quick startup with no config files.")
210 ),
211 (('--readline',), dict(
212 action='store_true', dest='InteractiveShell.readline_use', default=NoConfigDefault,
213 help="Enable readline for command line usage.")
214 ),
215 (('--no-readline',), dict(
216 action='store_false', dest='InteractiveShell.readline_use', default=NoConfigDefault,
217 help="Disable readline for command line usage.")
218 ),
219 (('--screen-length','-sl'), dict(
220 type=int, dest='InteractiveShell.screen_length', default=NoConfigDefault,
221 help='Number of lines on screen, used to control printing of long strings.',
222 metavar='InteractiveShell.screen_length')
223 ),
224 (('--separate-in','-si'), dict(
225 type=str, dest='InteractiveShell.separate_in', default=NoConfigDefault,
226 help="Separator before input prompts. Default '\n'.",
227 metavar='InteractiveShell.separate_in')
228 ),
229 (('--separate-out','-so'), dict(
230 type=str, dest='InteractiveShell.separate_out', default=NoConfigDefault,
231 help="Separator before output prompts. Default 0 (nothing).",
232 metavar='InteractiveShell.separate_out')
233 ),
234 (('--separate-out2','-so2'), dict(
235 type=str, dest='InteractiveShell.separate_out2', default=NoConfigDefault,
236 help="Separator after output prompts. Default 0 (nonight).",
237 metavar='InteractiveShell.separate_out2')
238 ),
239 (('-no-sep',), dict(
240 action='store_true', dest='Global.nosep', default=NoConfigDefault,
241 help="Eliminate all spacing between prompts.")
242 ),
243 (('--term-title',), dict(
244 action='store_true', dest='InteractiveShell.term_title', default=NoConfigDefault,
245 help="Enable auto setting the terminal title.")
246 ),
247 (('--no-term-title',), dict(
248 action='store_false', dest='InteractiveShell.term_title', default=NoConfigDefault,
249 help="Disable auto setting the terminal title.")
250 ),
251 (('--xmode',), dict(
252 type=str, dest='InteractiveShell.xmode', default=NoConfigDefault,
253 help="Exception mode ('Plain','Context','Verbose')",
254 metavar='InteractiveShell.xmode')
255 ),
256 (('--ext',), dict(
257 type=str, dest='Global.extra_extension', default=NoConfigDefault,
258 help="The dotted module name of an IPython extension to load.",
259 metavar='Global.extra_extension')
260 ),
261 (('-c',), dict(
262 type=str, dest='Global.code_to_run', default=NoConfigDefault,
263 help="Execute the given command string.",
264 metavar='Global.code_to_run')
265 ),
266 (('-i',), dict(
267 action='store_true', dest='Global.force_interact', default=NoConfigDefault,
268 help="If running code from the command line, become interactive afterwards.")
269 ),
270 (('--wthread',), dict(
271 action='store_true', dest='Global.wthread', default=NoConfigDefault,
272 help="Enable wxPython event loop integration.")
273 ),
274 (('--q4thread','--qthread'), dict(
275 action='store_true', dest='Global.q4thread', default=NoConfigDefault,
276 help="Enable Qt4 event loop integration. Qt3 is no longer supported.")
277 ),
278 (('--gthread',), dict(
279 action='store_true', dest='Global.gthread', default=NoConfigDefault,
280 help="Enable GTK event loop integration.")
281 ),
282 # # These are only here to get the proper deprecation warnings
283 (('--pylab',), dict(
284 action='store_true', dest='Global.pylab', default=NoConfigDefault,
285 help="Disabled. Pylab has been disabled until matplotlib "
286 "supports this version of IPython.")
287 )
288 )
289
290
291 class IPythonAppCLConfigLoader(BaseAppArgParseConfigLoader):
292
293 arguments = cl_args
294
295
296 default_config_file_name = u'ipython_config.py'
297
298
299 class IPythonApp(Application):
300 name = u'ipython'
301 description = 'IPython: an enhanced interactive Python shell.'
302 config_file_name = default_config_file_name
303
304 def create_default_config(self):
305 super(IPythonApp, self).create_default_config()
306 self.default_config.Global.display_banner = True
307
308 # If the -c flag is given or a file is given to run at the cmd line
309 # like "ipython foo.py", normally we exit without starting the main
310 # loop. The force_interact config variable allows a user to override
311 # this and interact. It is also set by the -i cmd line flag, just
312 # like Python.
313 self.default_config.Global.force_interact = False
314
315 # By default always interact by starting the IPython mainloop.
316 self.default_config.Global.interact = True
317
318 # No GUI integration by default
319 self.default_config.Global.wthread = False
320 self.default_config.Global.q4thread = False
321 self.default_config.Global.gthread = False
322
323 def create_command_line_config(self):
324 """Create and return a command line config loader."""
325 return IPythonAppCLConfigLoader(
326 description=self.description,
327 version=release.version
328 )
329
330 def post_load_command_line_config(self):
331 """Do actions after loading cl config."""
332 clc = self.command_line_config
333
334 # Display the deprecation warnings about threaded shells
335 if hasattr(clc.Global, 'pylab'):
336 pylab_warning()
337 del clc.Global['pylab']
338
339 def load_file_config(self):
340 if hasattr(self.command_line_config.Global, 'quick'):
341 if self.command_line_config.Global.quick:
342 self.file_config = Config()
343 return
344 super(IPythonApp, self).load_file_config()
345
346 def post_load_file_config(self):
347 if hasattr(self.command_line_config.Global, 'extra_extension'):
348 if not hasattr(self.file_config.Global, 'extensions'):
349 self.file_config.Global.extensions = []
350 self.file_config.Global.extensions.append(
351 self.command_line_config.Global.extra_extension)
352 del self.command_line_config.Global.extra_extension
353
354 def pre_construct(self):
355 config = self.master_config
356
357 if hasattr(config.Global, 'classic'):
358 if config.Global.classic:
359 config.InteractiveShell.cache_size = 0
360 config.InteractiveShell.pprint = 0
361 config.InteractiveShell.prompt_in1 = '>>> '
362 config.InteractiveShell.prompt_in2 = '... '
363 config.InteractiveShell.prompt_out = ''
364 config.InteractiveShell.separate_in = \
365 config.InteractiveShell.separate_out = \
366 config.InteractiveShell.separate_out2 = ''
367 config.InteractiveShell.colors = 'NoColor'
368 config.InteractiveShell.xmode = 'Plain'
369
370 if hasattr(config.Global, 'nosep'):
371 if config.Global.nosep:
372 config.InteractiveShell.separate_in = \
373 config.InteractiveShell.separate_out = \
374 config.InteractiveShell.separate_out2 = ''
375
376 # if there is code of files to run from the cmd line, don't interact
377 # unless the -i flag (Global.force_interact) is true.
378 code_to_run = config.Global.get('code_to_run','')
379 file_to_run = False
380 if len(self.extra_args)>=1:
381 if self.extra_args[0]:
382 file_to_run = True
383 if file_to_run or code_to_run:
384 if not config.Global.force_interact:
385 config.Global.interact = False
386
387 def construct(self):
388 # I am a little hesitant to put these into InteractiveShell itself.
389 # But that might be the place for them
390 sys.path.insert(0, '')
391
392 # Create an InteractiveShell instance
393 self.shell = InteractiveShell(
394 parent=None,
395 config=self.master_config
396 )
397
398 def post_construct(self):
399 """Do actions after construct, but before starting the app."""
400 config = self.master_config
401
402 # shell.display_banner should always be False for the terminal
403 # based app, because we call shell.show_banner() by hand below
404 # so the banner shows *before* all extension loading stuff.
405 self.shell.display_banner = False
406
407 if config.Global.display_banner and \
408 config.Global.interact:
409 self.shell.show_banner()
410
411 # Make sure there is a space below the banner.
412 if self.log_level <= logging.INFO: print
413
414 # Now a variety of things that happen after the banner is printed.
415 self._enable_gui()
416 self._load_extensions()
417 self._run_exec_lines()
418 self._run_exec_files()
419 self._run_cmd_line_code()
420
421 def _enable_gui(self):
422 """Enable GUI event loop integration."""
423 config = self.master_config
424 try:
425 # Enable GUI integration
426 if config.Global.wthread:
427 self.log.info("Enabling wx GUI event loop integration")
428 inputhook.enable_wx(app=True)
429 elif config.Global.q4thread:
430 self.log.info("Enabling Qt4 GUI event loop integration")
431 inputhook.enable_qt4(app=True)
432 elif config.Global.gthread:
433 self.log.info("Enabling GTK GUI event loop integration")
434 inputhook.enable_gtk(app=True)
435 except:
436 self.log.warn("Error in enabling GUI event loop integration:")
437 self.shell.showtraceback()
438
439 def _load_extensions(self):
440 """Load all IPython extensions in Global.extensions.
441
442 This uses the :meth:`InteractiveShell.load_extensions` to load all
443 the extensions listed in ``self.master_config.Global.extensions``.
444 """
445 try:
446 if hasattr(self.master_config.Global, 'extensions'):
447 self.log.debug("Loading IPython extensions...")
448 extensions = self.master_config.Global.extensions
449 for ext in extensions:
450 try:
451 self.log.info("Loading IPython extension: %s" % ext)
452 self.shell.load_extension(ext)
453 except:
454 self.log.warn("Error in loading extension: %s" % ext)
455 self.shell.showtraceback()
456 except:
457 self.log.warn("Unknown error in loading extensions:")
458 self.shell.showtraceback()
459
460 def _run_exec_lines(self):
461 """Run lines of code in Global.exec_lines in the user's namespace."""
462 try:
463 if hasattr(self.master_config.Global, 'exec_lines'):
464 self.log.debug("Running code from Global.exec_lines...")
465 exec_lines = self.master_config.Global.exec_lines
466 for line in exec_lines:
467 try:
468 self.log.info("Running code in user namespace: %s" % line)
469 self.shell.runlines(line)
470 except:
471 self.log.warn("Error in executing line in user namespace: %s" % line)
472 self.shell.showtraceback()
473 except:
474 self.log.warn("Unknown error in handling Global.exec_lines:")
475 self.shell.showtraceback()
476
477 def _exec_file(self, fname):
478 full_filename = filefind(fname, [u'.', self.ipython_dir])
479 if os.path.isfile(full_filename):
480 if full_filename.endswith(u'.py'):
481 self.log.info("Running file in user namespace: %s" % full_filename)
482 self.shell.safe_execfile(full_filename, self.shell.user_ns)
483 elif full_filename.endswith('.ipy'):
484 self.log.info("Running file in user namespace: %s" % full_filename)
485 self.shell.safe_execfile_ipy(full_filename)
486 else:
487 self.log.warn("File does not have a .py or .ipy extension: <%s>" % full_filename)
488
489 def _run_exec_files(self):
490 try:
491 if hasattr(self.master_config.Global, 'exec_files'):
492 self.log.debug("Running files in Global.exec_files...")
493 exec_files = self.master_config.Global.exec_files
494 for fname in exec_files:
495 self._exec_file(fname)
496 except:
497 self.log.warn("Unknown error in handling Global.exec_files:")
498 self.shell.showtraceback()
499
500 def _run_cmd_line_code(self):
501 if hasattr(self.master_config.Global, 'code_to_run'):
502 line = self.master_config.Global.code_to_run
503 try:
504 self.log.info("Running code given at command line (-c): %s" % line)
505 self.shell.runlines(line)
506 except:
507 self.log.warn("Error in executing line in user namespace: %s" % line)
508 self.shell.showtraceback()
509 return
510 # Like Python itself, ignore the second if the first of these is present
511 try:
512 fname = self.extra_args[0]
513 except:
514 pass
515 else:
516 try:
517 self._exec_file(fname)
518 except:
519 self.log.warn("Error in executing file in user namespace: %s" % fname)
520 self.shell.showtraceback()
521
522 def start_app(self):
523 if self.master_config.Global.interact:
524 self.log.debug("Starting IPython's mainloop...")
525 self.shell.mainloop()
526
527
528 def load_default_config(ipython_dir=None):
529 """Load the default config file from the default ipython_dir.
530
531 This is useful for embedded shells.
532 """
533 if ipython_dir is None:
534 ipython_dir = get_ipython_dir()
535 cl = PyFileConfigLoader(default_config_file_name, ipython_dir)
536 config = cl.load_config()
537 return config
538
539
540 def launch_new_instance():
541 """Create and run a full blown IPython instance"""
542 app = IPythonApp()
543 app.start()
544
This diff has been collapsed as it changes many lines, (2488 lines changed) Show them Hide them
@@ -0,0 +1,2488 b''
1 # -*- coding: utf-8 -*-
2 """
3 Main IPython Component
4 """
5
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
8 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
9 # Copyright (C) 2008-2009 The IPython Development Team
10 #
11 # Distributed under the terms of the BSD License. The full license is in
12 # the file COPYING, distributed as part of this software.
13 #-----------------------------------------------------------------------------
14
15 #-----------------------------------------------------------------------------
16 # Imports
17 #-----------------------------------------------------------------------------
18
19 from __future__ import with_statement
20
21 import __builtin__
22 import StringIO
23 import bdb
24 import codeop
25 import exceptions
26 import new
27 import os
28 import re
29 import string
30 import sys
31 import tempfile
32 from contextlib import nested
33
34 from IPython.core import ultratb
35 from IPython.core import debugger, oinspect
36 from IPython.core import shadowns
37 from IPython.core import history as ipcorehist
38 from IPython.core import prefilter
39 from IPython.core.alias import AliasManager
40 from IPython.core.builtin_trap import BuiltinTrap
41 from IPython.core.display_trap import DisplayTrap
42 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
43 from IPython.core.logger import Logger
44 from IPython.core.magic import Magic
45 from IPython.core.prompts import CachedOutput
46 from IPython.core.prefilter import PrefilterManager
47 from IPython.core.component import Component
48 from IPython.core.usage import interactive_usage, default_banner
49 from IPython.core.error import TryNext, UsageError
50
51 from IPython.utils import pickleshare
52 from IPython.external.Itpl import ItplNS
53 from IPython.lib.backgroundjobs import BackgroundJobManager
54 from IPython.utils.ipstruct import Struct
55 from IPython.utils import PyColorize
56 from IPython.utils.genutils import *
57 from IPython.utils.genutils import get_ipython_dir
58 from IPython.utils.platutils import toggle_set_term_title, set_term_title
59 from IPython.utils.strdispatch import StrDispatch
60 from IPython.utils.syspathcontext import prepended_to_syspath
61
62 # from IPython.utils import growl
63 # growl.start("IPython")
64
65 from IPython.utils.traitlets import (
66 Int, Str, CBool, CaselessStrEnum, Enum, List, Unicode
67 )
68
69 #-----------------------------------------------------------------------------
70 # Globals
71 #-----------------------------------------------------------------------------
72
73
74 # store the builtin raw_input globally, and use this always, in case user code
75 # overwrites it (like wx.py.PyShell does)
76 raw_input_original = raw_input
77
78 # compiled regexps for autoindent management
79 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
80
81
82 #-----------------------------------------------------------------------------
83 # Utilities
84 #-----------------------------------------------------------------------------
85
86
87 ini_spaces_re = re.compile(r'^(\s+)')
88
89
90 def num_ini_spaces(strng):
91 """Return the number of initial spaces in a string"""
92
93 ini_spaces = ini_spaces_re.match(strng)
94 if ini_spaces:
95 return ini_spaces.end()
96 else:
97 return 0
98
99
100 def softspace(file, newvalue):
101 """Copied from code.py, to remove the dependency"""
102
103 oldvalue = 0
104 try:
105 oldvalue = file.softspace
106 except AttributeError:
107 pass
108 try:
109 file.softspace = newvalue
110 except (AttributeError, TypeError):
111 # "attribute-less object" or "read-only attributes"
112 pass
113 return oldvalue
114
115
116 class SpaceInInput(exceptions.Exception): pass
117
118 class Bunch: pass
119
120 class InputList(list):
121 """Class to store user input.
122
123 It's basically a list, but slices return a string instead of a list, thus
124 allowing things like (assuming 'In' is an instance):
125
126 exec In[4:7]
127
128 or
129
130 exec In[5:9] + In[14] + In[21:25]"""
131
132 def __getslice__(self,i,j):
133 return ''.join(list.__getslice__(self,i,j))
134
135
136 class SyntaxTB(ultratb.ListTB):
137 """Extension which holds some state: the last exception value"""
138
139 def __init__(self,color_scheme = 'NoColor'):
140 ultratb.ListTB.__init__(self,color_scheme)
141 self.last_syntax_error = None
142
143 def __call__(self, etype, value, elist):
144 self.last_syntax_error = value
145 ultratb.ListTB.__call__(self,etype,value,elist)
146
147 def clear_err_state(self):
148 """Return the current error state and clear it"""
149 e = self.last_syntax_error
150 self.last_syntax_error = None
151 return e
152
153
154 def get_default_editor():
155 try:
156 ed = os.environ['EDITOR']
157 except KeyError:
158 if os.name == 'posix':
159 ed = 'vi' # the only one guaranteed to be there!
160 else:
161 ed = 'notepad' # same in Windows!
162 return ed
163
164
165 def get_default_colors():
166 if sys.platform=='darwin':
167 return "LightBG"
168 elif os.name=='nt':
169 return 'Linux'
170 else:
171 return 'Linux'
172
173
174 class SeparateStr(Str):
175 """A Str subclass to validate separate_in, separate_out, etc.
176
177 This is a Str based traitlet that converts '0'->'' and '\\n'->'\n'.
178 """
179
180 def validate(self, obj, value):
181 if value == '0': value = ''
182 value = value.replace('\\n','\n')
183 return super(SeparateStr, self).validate(obj, value)
184
185
186 #-----------------------------------------------------------------------------
187 # Main IPython class
188 #-----------------------------------------------------------------------------
189
190
191 class InteractiveShell(Component, Magic):
192 """An enhanced, interactive shell for Python."""
193
194 autocall = Enum((0,1,2), default_value=1, config=True)
195 autoedit_syntax = CBool(False, config=True)
196 autoindent = CBool(True, config=True)
197 automagic = CBool(True, config=True)
198 banner = Str('')
199 banner1 = Str(default_banner, config=True)
200 banner2 = Str('', config=True)
201 cache_size = Int(1000, config=True)
202 color_info = CBool(True, config=True)
203 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
204 default_value=get_default_colors(), config=True)
205 confirm_exit = CBool(True, config=True)
206 debug = CBool(False, config=True)
207 deep_reload = CBool(False, config=True)
208 # This display_banner only controls whether or not self.show_banner()
209 # is called when mainloop/interact are called. The default is False
210 # because for the terminal based application, the banner behavior
211 # is controlled by Global.display_banner, which IPythonApp looks at
212 # to determine if *it* should call show_banner() by hand or not.
213 display_banner = CBool(False) # This isn't configurable!
214 embedded = CBool(False)
215 embedded_active = CBool(False)
216 editor = Str(get_default_editor(), config=True)
217 filename = Str("<ipython console>")
218 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
219 logstart = CBool(False, config=True)
220 logfile = Str('', config=True)
221 logappend = Str('', config=True)
222 object_info_string_level = Enum((0,1,2), default_value=0,
223 config=True)
224 pager = Str('less', config=True)
225 pdb = CBool(False, config=True)
226 pprint = CBool(True, config=True)
227 profile = Str('', config=True)
228 prompt_in1 = Str('In [\\#]: ', config=True)
229 prompt_in2 = Str(' .\\D.: ', config=True)
230 prompt_out = Str('Out[\\#]: ', config=True)
231 prompts_pad_left = CBool(True, config=True)
232 quiet = CBool(False, config=True)
233
234 readline_use = CBool(True, config=True)
235 readline_merge_completions = CBool(True, config=True)
236 readline_omit__names = Enum((0,1,2), default_value=0, config=True)
237 readline_remove_delims = Str('-/~', config=True)
238 readline_parse_and_bind = List([
239 'tab: complete',
240 '"\C-l": possible-completions',
241 'set show-all-if-ambiguous on',
242 '"\C-o": tab-insert',
243 '"\M-i": " "',
244 '"\M-o": "\d\d\d\d"',
245 '"\M-I": "\d\d\d\d"',
246 '"\C-r": reverse-search-history',
247 '"\C-s": forward-search-history',
248 '"\C-p": history-search-backward',
249 '"\C-n": history-search-forward',
250 '"\e[A": history-search-backward',
251 '"\e[B": history-search-forward',
252 '"\C-k": kill-line',
253 '"\C-u": unix-line-discard',
254 ], allow_none=False, config=True)
255
256 screen_length = Int(0, config=True)
257
258 # Use custom TraitletTypes that convert '0'->'' and '\\n'->'\n'
259 separate_in = SeparateStr('\n', config=True)
260 separate_out = SeparateStr('', config=True)
261 separate_out2 = SeparateStr('', config=True)
262
263 system_header = Str('IPython system call: ', config=True)
264 system_verbose = CBool(False, config=True)
265 term_title = CBool(False, config=True)
266 wildcards_case_sensitive = CBool(True, config=True)
267 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
268 default_value='Context', config=True)
269
270 autoexec = List(allow_none=False)
271
272 # class attribute to indicate whether the class supports threads or not.
273 # Subclasses with thread support should override this as needed.
274 isthreaded = False
275
276 def __init__(self, parent=None, config=None, ipython_dir=None, usage=None,
277 user_ns=None, user_global_ns=None,
278 banner1=None, banner2=None, display_banner=None,
279 custom_exceptions=((),None)):
280
281 # This is where traitlets with a config_key argument are updated
282 # from the values on config.
283 super(InteractiveShell, self).__init__(parent, config=config)
284
285 # These are relatively independent and stateless
286 self.init_ipython_dir(ipython_dir)
287 self.init_instance_attrs()
288 self.init_term_title()
289 self.init_usage(usage)
290 self.init_banner(banner1, banner2, display_banner)
291
292 # Create namespaces (user_ns, user_global_ns, etc.)
293 self.init_create_namespaces(user_ns, user_global_ns)
294 # This has to be done after init_create_namespaces because it uses
295 # something in self.user_ns, but before init_sys_modules, which
296 # is the first thing to modify sys.
297 self.save_sys_module_state()
298 self.init_sys_modules()
299
300 self.init_history()
301 self.init_encoding()
302 self.init_prefilter()
303
304 Magic.__init__(self, self)
305
306 self.init_syntax_highlighting()
307 self.init_hooks()
308 self.init_pushd_popd_magic()
309 self.init_traceback_handlers(custom_exceptions)
310 self.init_user_ns()
311 self.init_logger()
312 self.init_alias()
313 self.init_builtins()
314
315 # pre_config_initialization
316 self.init_shadow_hist()
317
318 # The next section should contain averything that was in ipmaker.
319 self.init_logstart()
320
321 # The following was in post_config_initialization
322 self.init_inspector()
323 self.init_readline()
324 self.init_prompts()
325 self.init_displayhook()
326 self.init_reload_doctest()
327 self.init_magics()
328 self.init_pdb()
329 self.hooks.late_startup_hook()
330
331 def get_ipython(self):
332 return self
333
334 #-------------------------------------------------------------------------
335 # Traitlet changed handlers
336 #-------------------------------------------------------------------------
337
338 def _banner1_changed(self):
339 self.compute_banner()
340
341 def _banner2_changed(self):
342 self.compute_banner()
343
344 def _ipython_dir_changed(self, name, new):
345 if not os.path.isdir(new):
346 os.makedirs(new, mode = 0777)
347 if not os.path.isdir(self.ipython_extension_dir):
348 os.makedirs(self.ipython_extension_dir, mode = 0777)
349
350 @property
351 def ipython_extension_dir(self):
352 return os.path.join(self.ipython_dir, 'extensions')
353
354 @property
355 def usable_screen_length(self):
356 if self.screen_length == 0:
357 return 0
358 else:
359 num_lines_bot = self.separate_in.count('\n')+1
360 return self.screen_length - num_lines_bot
361
362 def _term_title_changed(self, name, new_value):
363 self.init_term_title()
364
365 def set_autoindent(self,value=None):
366 """Set the autoindent flag, checking for readline support.
367
368 If called with no arguments, it acts as a toggle."""
369
370 if not self.has_readline:
371 if os.name == 'posix':
372 warn("The auto-indent feature requires the readline library")
373 self.autoindent = 0
374 return
375 if value is None:
376 self.autoindent = not self.autoindent
377 else:
378 self.autoindent = value
379
380 #-------------------------------------------------------------------------
381 # init_* methods called by __init__
382 #-------------------------------------------------------------------------
383
384 def init_ipython_dir(self, ipython_dir):
385 if ipython_dir is not None:
386 self.ipython_dir = ipython_dir
387 self.config.Global.ipython_dir = self.ipython_dir
388 return
389
390 if hasattr(self.config.Global, 'ipython_dir'):
391 self.ipython_dir = self.config.Global.ipython_dir
392 else:
393 self.ipython_dir = get_ipython_dir()
394
395 # All children can just read this
396 self.config.Global.ipython_dir = self.ipython_dir
397
398 def init_instance_attrs(self):
399 self.jobs = BackgroundJobManager()
400 self.more = False
401
402 # command compiler
403 self.compile = codeop.CommandCompiler()
404
405 # User input buffer
406 self.buffer = []
407
408 # Make an empty namespace, which extension writers can rely on both
409 # existing and NEVER being used by ipython itself. This gives them a
410 # convenient location for storing additional information and state
411 # their extensions may require, without fear of collisions with other
412 # ipython names that may develop later.
413 self.meta = Struct()
414
415 # Object variable to store code object waiting execution. This is
416 # used mainly by the multithreaded shells, but it can come in handy in
417 # other situations. No need to use a Queue here, since it's a single
418 # item which gets cleared once run.
419 self.code_to_run = None
420
421 # Flag to mark unconditional exit
422 self.exit_now = False
423
424 # Temporary files used for various purposes. Deleted at exit.
425 self.tempfiles = []
426
427 # Keep track of readline usage (later set by init_readline)
428 self.has_readline = False
429
430 # keep track of where we started running (mainly for crash post-mortem)
431 # This is not being used anywhere currently.
432 self.starting_dir = os.getcwd()
433
434 # Indentation management
435 self.indent_current_nsp = 0
436
437 def init_term_title(self):
438 # Enable or disable the terminal title.
439 if self.term_title:
440 toggle_set_term_title(True)
441 set_term_title('IPython: ' + abbrev_cwd())
442 else:
443 toggle_set_term_title(False)
444
445 def init_usage(self, usage=None):
446 if usage is None:
447 self.usage = interactive_usage
448 else:
449 self.usage = usage
450
451 def init_encoding(self):
452 # Get system encoding at startup time. Certain terminals (like Emacs
453 # under Win32 have it set to None, and we need to have a known valid
454 # encoding to use in the raw_input() method
455 try:
456 self.stdin_encoding = sys.stdin.encoding or 'ascii'
457 except AttributeError:
458 self.stdin_encoding = 'ascii'
459
460 def init_syntax_highlighting(self):
461 # Python source parser/formatter for syntax highlighting
462 pyformat = PyColorize.Parser().format
463 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
464
465 def init_pushd_popd_magic(self):
466 # for pushd/popd management
467 try:
468 self.home_dir = get_home_dir()
469 except HomeDirError, msg:
470 fatal(msg)
471
472 self.dir_stack = []
473
474 def init_logger(self):
475 self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate')
476 # local shortcut, this is used a LOT
477 self.log = self.logger.log
478
479 def init_logstart(self):
480 if self.logappend:
481 self.magic_logstart(self.logappend + ' append')
482 elif self.logfile:
483 self.magic_logstart(self.logfile)
484 elif self.logstart:
485 self.magic_logstart()
486
487 def init_builtins(self):
488 self.builtin_trap = BuiltinTrap(self)
489
490 def init_inspector(self):
491 # Object inspector
492 self.inspector = oinspect.Inspector(oinspect.InspectColors,
493 PyColorize.ANSICodeColors,
494 'NoColor',
495 self.object_info_string_level)
496
497 def init_prompts(self):
498 # Initialize cache, set in/out prompts and printing system
499 self.outputcache = CachedOutput(self,
500 self.cache_size,
501 self.pprint,
502 input_sep = self.separate_in,
503 output_sep = self.separate_out,
504 output_sep2 = self.separate_out2,
505 ps1 = self.prompt_in1,
506 ps2 = self.prompt_in2,
507 ps_out = self.prompt_out,
508 pad_left = self.prompts_pad_left)
509
510 # user may have over-ridden the default print hook:
511 try:
512 self.outputcache.__class__.display = self.hooks.display
513 except AttributeError:
514 pass
515
516 def init_displayhook(self):
517 self.display_trap = DisplayTrap(self, self.outputcache)
518
519 def init_reload_doctest(self):
520 # Do a proper resetting of doctest, including the necessary displayhook
521 # monkeypatching
522 try:
523 doctest_reload()
524 except ImportError:
525 warn("doctest module does not exist.")
526
527 #-------------------------------------------------------------------------
528 # Things related to the banner
529 #-------------------------------------------------------------------------
530
531 def init_banner(self, banner1, banner2, display_banner):
532 if banner1 is not None:
533 self.banner1 = banner1
534 if banner2 is not None:
535 self.banner2 = banner2
536 if display_banner is not None:
537 self.display_banner = display_banner
538 self.compute_banner()
539
540 def show_banner(self, banner=None):
541 if banner is None:
542 banner = self.banner
543 self.write(banner)
544
545 def compute_banner(self):
546 self.banner = self.banner1 + '\n'
547 if self.profile:
548 self.banner += '\nIPython profile: %s\n' % self.profile
549 if self.banner2:
550 self.banner += '\n' + self.banner2 + '\n'
551
552 #-------------------------------------------------------------------------
553 # Things related to injections into the sys module
554 #-------------------------------------------------------------------------
555
556 def save_sys_module_state(self):
557 """Save the state of hooks in the sys module.
558
559 This has to be called after self.user_ns is created.
560 """
561 self._orig_sys_module_state = {}
562 self._orig_sys_module_state['stdin'] = sys.stdin
563 self._orig_sys_module_state['stdout'] = sys.stdout
564 self._orig_sys_module_state['stderr'] = sys.stderr
565 self._orig_sys_module_state['excepthook'] = sys.excepthook
566 try:
567 self._orig_sys_modules_main_name = self.user_ns['__name__']
568 except KeyError:
569 pass
570
571 def restore_sys_module_state(self):
572 """Restore the state of the sys module."""
573 try:
574 for k, v in self._orig_sys_module_state.items():
575 setattr(sys, k, v)
576 except AttributeError:
577 pass
578 try:
579 delattr(sys, 'ipcompleter')
580 except AttributeError:
581 pass
582 # Reset what what done in self.init_sys_modules
583 try:
584 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
585 except (AttributeError, KeyError):
586 pass
587
588 #-------------------------------------------------------------------------
589 # Things related to hooks
590 #-------------------------------------------------------------------------
591
592 def init_hooks(self):
593 # hooks holds pointers used for user-side customizations
594 self.hooks = Struct()
595
596 self.strdispatchers = {}
597
598 # Set all default hooks, defined in the IPython.hooks module.
599 import IPython.core.hooks
600 hooks = IPython.core.hooks
601 for hook_name in hooks.__all__:
602 # default hooks have priority 100, i.e. low; user hooks should have
603 # 0-100 priority
604 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
605
606 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
607 """set_hook(name,hook) -> sets an internal IPython hook.
608
609 IPython exposes some of its internal API as user-modifiable hooks. By
610 adding your function to one of these hooks, you can modify IPython's
611 behavior to call at runtime your own routines."""
612
613 # At some point in the future, this should validate the hook before it
614 # accepts it. Probably at least check that the hook takes the number
615 # of args it's supposed to.
616
617 f = new.instancemethod(hook,self,self.__class__)
618
619 # check if the hook is for strdispatcher first
620 if str_key is not None:
621 sdp = self.strdispatchers.get(name, StrDispatch())
622 sdp.add_s(str_key, f, priority )
623 self.strdispatchers[name] = sdp
624 return
625 if re_key is not None:
626 sdp = self.strdispatchers.get(name, StrDispatch())
627 sdp.add_re(re.compile(re_key), f, priority )
628 self.strdispatchers[name] = sdp
629 return
630
631 dp = getattr(self.hooks, name, None)
632 if name not in IPython.core.hooks.__all__:
633 print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ )
634 if not dp:
635 dp = IPython.core.hooks.CommandChainDispatcher()
636
637 try:
638 dp.add(f,priority)
639 except AttributeError:
640 # it was not commandchain, plain old func - replace
641 dp = f
642
643 setattr(self.hooks,name, dp)
644
645 #-------------------------------------------------------------------------
646 # Things related to the "main" module
647 #-------------------------------------------------------------------------
648
649 def new_main_mod(self,ns=None):
650 """Return a new 'main' module object for user code execution.
651 """
652 main_mod = self._user_main_module
653 init_fakemod_dict(main_mod,ns)
654 return main_mod
655
656 def cache_main_mod(self,ns,fname):
657 """Cache a main module's namespace.
658
659 When scripts are executed via %run, we must keep a reference to the
660 namespace of their __main__ module (a FakeModule instance) around so
661 that Python doesn't clear it, rendering objects defined therein
662 useless.
663
664 This method keeps said reference in a private dict, keyed by the
665 absolute path of the module object (which corresponds to the script
666 path). This way, for multiple executions of the same script we only
667 keep one copy of the namespace (the last one), thus preventing memory
668 leaks from old references while allowing the objects from the last
669 execution to be accessible.
670
671 Note: we can not allow the actual FakeModule instances to be deleted,
672 because of how Python tears down modules (it hard-sets all their
673 references to None without regard for reference counts). This method
674 must therefore make a *copy* of the given namespace, to allow the
675 original module's __dict__ to be cleared and reused.
676
677
678 Parameters
679 ----------
680 ns : a namespace (a dict, typically)
681
682 fname : str
683 Filename associated with the namespace.
684
685 Examples
686 --------
687
688 In [10]: import IPython
689
690 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
691
692 In [12]: IPython.__file__ in _ip._main_ns_cache
693 Out[12]: True
694 """
695 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
696
697 def clear_main_mod_cache(self):
698 """Clear the cache of main modules.
699
700 Mainly for use by utilities like %reset.
701
702 Examples
703 --------
704
705 In [15]: import IPython
706
707 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
708
709 In [17]: len(_ip._main_ns_cache) > 0
710 Out[17]: True
711
712 In [18]: _ip.clear_main_mod_cache()
713
714 In [19]: len(_ip._main_ns_cache) == 0
715 Out[19]: True
716 """
717 self._main_ns_cache.clear()
718
719 #-------------------------------------------------------------------------
720 # Things related to debugging
721 #-------------------------------------------------------------------------
722
723 def init_pdb(self):
724 # Set calling of pdb on exceptions
725 # self.call_pdb is a property
726 self.call_pdb = self.pdb
727
728 def _get_call_pdb(self):
729 return self._call_pdb
730
731 def _set_call_pdb(self,val):
732
733 if val not in (0,1,False,True):
734 raise ValueError,'new call_pdb value must be boolean'
735
736 # store value in instance
737 self._call_pdb = val
738
739 # notify the actual exception handlers
740 self.InteractiveTB.call_pdb = val
741 if self.isthreaded:
742 try:
743 self.sys_excepthook.call_pdb = val
744 except:
745 warn('Failed to activate pdb for threaded exception handler')
746
747 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
748 'Control auto-activation of pdb at exceptions')
749
750 def debugger(self,force=False):
751 """Call the pydb/pdb debugger.
752
753 Keywords:
754
755 - force(False): by default, this routine checks the instance call_pdb
756 flag and does not actually invoke the debugger if the flag is false.
757 The 'force' option forces the debugger to activate even if the flag
758 is false.
759 """
760
761 if not (force or self.call_pdb):
762 return
763
764 if not hasattr(sys,'last_traceback'):
765 error('No traceback has been produced, nothing to debug.')
766 return
767
768 # use pydb if available
769 if debugger.has_pydb:
770 from pydb import pm
771 else:
772 # fallback to our internal debugger
773 pm = lambda : self.InteractiveTB.debugger(force=True)
774 self.history_saving_wrapper(pm)()
775
776 #-------------------------------------------------------------------------
777 # Things related to IPython's various namespaces
778 #-------------------------------------------------------------------------
779
780 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
781 # Create the namespace where the user will operate. user_ns is
782 # normally the only one used, and it is passed to the exec calls as
783 # the locals argument. But we do carry a user_global_ns namespace
784 # given as the exec 'globals' argument, This is useful in embedding
785 # situations where the ipython shell opens in a context where the
786 # distinction between locals and globals is meaningful. For
787 # non-embedded contexts, it is just the same object as the user_ns dict.
788
789 # FIXME. For some strange reason, __builtins__ is showing up at user
790 # level as a dict instead of a module. This is a manual fix, but I
791 # should really track down where the problem is coming from. Alex
792 # Schmolck reported this problem first.
793
794 # A useful post by Alex Martelli on this topic:
795 # Re: inconsistent value from __builtins__
796 # Von: Alex Martelli <aleaxit@yahoo.com>
797 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
798 # Gruppen: comp.lang.python
799
800 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
801 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
802 # > <type 'dict'>
803 # > >>> print type(__builtins__)
804 # > <type 'module'>
805 # > Is this difference in return value intentional?
806
807 # Well, it's documented that '__builtins__' can be either a dictionary
808 # or a module, and it's been that way for a long time. Whether it's
809 # intentional (or sensible), I don't know. In any case, the idea is
810 # that if you need to access the built-in namespace directly, you
811 # should start with "import __builtin__" (note, no 's') which will
812 # definitely give you a module. Yeah, it's somewhat confusing:-(.
813
814 # These routines return properly built dicts as needed by the rest of
815 # the code, and can also be used by extension writers to generate
816 # properly initialized namespaces.
817 user_ns, user_global_ns = self.make_user_namespaces(user_ns,
818 user_global_ns)
819
820 # Assign namespaces
821 # This is the namespace where all normal user variables live
822 self.user_ns = user_ns
823 self.user_global_ns = user_global_ns
824
825 # An auxiliary namespace that checks what parts of the user_ns were
826 # loaded at startup, so we can list later only variables defined in
827 # actual interactive use. Since it is always a subset of user_ns, it
828 # doesn't need to be seaparately tracked in the ns_table
829 self.user_config_ns = {}
830
831 # A namespace to keep track of internal data structures to prevent
832 # them from cluttering user-visible stuff. Will be updated later
833 self.internal_ns = {}
834
835 # Now that FakeModule produces a real module, we've run into a nasty
836 # problem: after script execution (via %run), the module where the user
837 # code ran is deleted. Now that this object is a true module (needed
838 # so docetst and other tools work correctly), the Python module
839 # teardown mechanism runs over it, and sets to None every variable
840 # present in that module. Top-level references to objects from the
841 # script survive, because the user_ns is updated with them. However,
842 # calling functions defined in the script that use other things from
843 # the script will fail, because the function's closure had references
844 # to the original objects, which are now all None. So we must protect
845 # these modules from deletion by keeping a cache.
846 #
847 # To avoid keeping stale modules around (we only need the one from the
848 # last run), we use a dict keyed with the full path to the script, so
849 # only the last version of the module is held in the cache. Note,
850 # however, that we must cache the module *namespace contents* (their
851 # __dict__). Because if we try to cache the actual modules, old ones
852 # (uncached) could be destroyed while still holding references (such as
853 # those held by GUI objects that tend to be long-lived)>
854 #
855 # The %reset command will flush this cache. See the cache_main_mod()
856 # and clear_main_mod_cache() methods for details on use.
857
858 # This is the cache used for 'main' namespaces
859 self._main_ns_cache = {}
860 # And this is the single instance of FakeModule whose __dict__ we keep
861 # copying and clearing for reuse on each %run
862 self._user_main_module = FakeModule()
863
864 # A table holding all the namespaces IPython deals with, so that
865 # introspection facilities can search easily.
866 self.ns_table = {'user':user_ns,
867 'user_global':user_global_ns,
868 'internal':self.internal_ns,
869 'builtin':__builtin__.__dict__
870 }
871
872 # Similarly, track all namespaces where references can be held and that
873 # we can safely clear (so it can NOT include builtin). This one can be
874 # a simple list.
875 self.ns_refs_table = [ user_ns, user_global_ns, self.user_config_ns,
876 self.internal_ns, self._main_ns_cache ]
877
878 def init_sys_modules(self):
879 # We need to insert into sys.modules something that looks like a
880 # module but which accesses the IPython namespace, for shelve and
881 # pickle to work interactively. Normally they rely on getting
882 # everything out of __main__, but for embedding purposes each IPython
883 # instance has its own private namespace, so we can't go shoving
884 # everything into __main__.
885
886 # note, however, that we should only do this for non-embedded
887 # ipythons, which really mimic the __main__.__dict__ with their own
888 # namespace. Embedded instances, on the other hand, should not do
889 # this because they need to manage the user local/global namespaces
890 # only, but they live within a 'normal' __main__ (meaning, they
891 # shouldn't overtake the execution environment of the script they're
892 # embedded in).
893
894 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
895
896 try:
897 main_name = self.user_ns['__name__']
898 except KeyError:
899 raise KeyError('user_ns dictionary MUST have a "__name__" key')
900 else:
901 sys.modules[main_name] = FakeModule(self.user_ns)
902
903 def make_user_namespaces(self, user_ns=None, user_global_ns=None):
904 """Return a valid local and global user interactive namespaces.
905
906 This builds a dict with the minimal information needed to operate as a
907 valid IPython user namespace, which you can pass to the various
908 embedding classes in ipython. The default implementation returns the
909 same dict for both the locals and the globals to allow functions to
910 refer to variables in the namespace. Customized implementations can
911 return different dicts. The locals dictionary can actually be anything
912 following the basic mapping protocol of a dict, but the globals dict
913 must be a true dict, not even a subclass. It is recommended that any
914 custom object for the locals namespace synchronize with the globals
915 dict somehow.
916
917 Raises TypeError if the provided globals namespace is not a true dict.
918
919 :Parameters:
920 user_ns : dict-like, optional
921 The current user namespace. The items in this namespace should
922 be included in the output. If None, an appropriate blank
923 namespace should be created.
924 user_global_ns : dict, optional
925 The current user global namespace. The items in this namespace
926 should be included in the output. If None, an appropriate
927 blank namespace should be created.
928
929 :Returns:
930 A tuple pair of dictionary-like object to be used as the local namespace
931 of the interpreter and a dict to be used as the global namespace.
932 """
933
934 if user_ns is None:
935 # Set __name__ to __main__ to better match the behavior of the
936 # normal interpreter.
937 user_ns = {'__name__' :'__main__',
938 '__builtins__' : __builtin__,
939 }
940 else:
941 user_ns.setdefault('__name__','__main__')
942 user_ns.setdefault('__builtins__',__builtin__)
943
944 if user_global_ns is None:
945 user_global_ns = user_ns
946 if type(user_global_ns) is not dict:
947 raise TypeError("user_global_ns must be a true dict; got %r"
948 % type(user_global_ns))
949
950 return user_ns, user_global_ns
951
952 def init_user_ns(self):
953 """Initialize all user-visible namespaces to their minimum defaults.
954
955 Certain history lists are also initialized here, as they effectively
956 act as user namespaces.
957
958 Notes
959 -----
960 All data structures here are only filled in, they are NOT reset by this
961 method. If they were not empty before, data will simply be added to
962 therm.
963 """
964 # Store myself as the public api!!!
965 self.user_ns['get_ipython'] = self.get_ipython
966
967 # make global variables for user access to the histories
968 self.user_ns['_ih'] = self.input_hist
969 self.user_ns['_oh'] = self.output_hist
970 self.user_ns['_dh'] = self.dir_hist
971
972 # user aliases to input and output histories
973 self.user_ns['In'] = self.input_hist
974 self.user_ns['Out'] = self.output_hist
975
976 self.user_ns['_sh'] = shadowns
977
978 # Put 'help' in the user namespace
979 try:
980 from site import _Helper
981 self.user_ns['help'] = _Helper()
982 except ImportError:
983 warn('help() not available - check site.py')
984
985 def reset(self):
986 """Clear all internal namespaces.
987
988 Note that this is much more aggressive than %reset, since it clears
989 fully all namespaces, as well as all input/output lists.
990 """
991 for ns in self.ns_refs_table:
992 ns.clear()
993
994 self.alias_manager.clear_aliases()
995
996 # Clear input and output histories
997 self.input_hist[:] = []
998 self.input_hist_raw[:] = []
999 self.output_hist.clear()
1000
1001 # Restore the user namespaces to minimal usability
1002 self.init_user_ns()
1003
1004 # Restore the default and user aliases
1005 self.alias_manager.init_aliases()
1006
1007 def push(self, variables, interactive=True):
1008 """Inject a group of variables into the IPython user namespace.
1009
1010 Parameters
1011 ----------
1012 variables : dict, str or list/tuple of str
1013 The variables to inject into the user's namespace. If a dict,
1014 a simple update is done. If a str, the string is assumed to
1015 have variable names separated by spaces. A list/tuple of str
1016 can also be used to give the variable names. If just the variable
1017 names are give (list/tuple/str) then the variable values looked
1018 up in the callers frame.
1019 interactive : bool
1020 If True (default), the variables will be listed with the ``who``
1021 magic.
1022 """
1023 vdict = None
1024
1025 # We need a dict of name/value pairs to do namespace updates.
1026 if isinstance(variables, dict):
1027 vdict = variables
1028 elif isinstance(variables, (basestring, list, tuple)):
1029 if isinstance(variables, basestring):
1030 vlist = variables.split()
1031 else:
1032 vlist = variables
1033 vdict = {}
1034 cf = sys._getframe(1)
1035 for name in vlist:
1036 try:
1037 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1038 except:
1039 print ('Could not get variable %s from %s' %
1040 (name,cf.f_code.co_name))
1041 else:
1042 raise ValueError('variables must be a dict/str/list/tuple')
1043
1044 # Propagate variables to user namespace
1045 self.user_ns.update(vdict)
1046
1047 # And configure interactive visibility
1048 config_ns = self.user_config_ns
1049 if interactive:
1050 for name, val in vdict.iteritems():
1051 config_ns.pop(name, None)
1052 else:
1053 for name,val in vdict.iteritems():
1054 config_ns[name] = val
1055
1056 #-------------------------------------------------------------------------
1057 # Things related to history management
1058 #-------------------------------------------------------------------------
1059
1060 def init_history(self):
1061 # List of input with multi-line handling.
1062 self.input_hist = InputList()
1063 # This one will hold the 'raw' input history, without any
1064 # pre-processing. This will allow users to retrieve the input just as
1065 # it was exactly typed in by the user, with %hist -r.
1066 self.input_hist_raw = InputList()
1067
1068 # list of visited directories
1069 try:
1070 self.dir_hist = [os.getcwd()]
1071 except OSError:
1072 self.dir_hist = []
1073
1074 # dict of output history
1075 self.output_hist = {}
1076
1077 # Now the history file
1078 if self.profile:
1079 histfname = 'history-%s' % self.profile
1080 else:
1081 histfname = 'history'
1082 self.histfile = os.path.join(self.ipython_dir, histfname)
1083
1084 # Fill the history zero entry, user counter starts at 1
1085 self.input_hist.append('\n')
1086 self.input_hist_raw.append('\n')
1087
1088 def init_shadow_hist(self):
1089 try:
1090 self.db = pickleshare.PickleShareDB(self.ipython_dir + "/db")
1091 except exceptions.UnicodeDecodeError:
1092 print "Your ipython_dir can't be decoded to unicode!"
1093 print "Please set HOME environment variable to something that"
1094 print r"only has ASCII characters, e.g. c:\home"
1095 print "Now it is", self.ipython_dir
1096 sys.exit()
1097 self.shadowhist = ipcorehist.ShadowHist(self.db)
1098
1099 def savehist(self):
1100 """Save input history to a file (via readline library)."""
1101
1102 if not self.has_readline:
1103 return
1104
1105 try:
1106 self.readline.write_history_file(self.histfile)
1107 except:
1108 print 'Unable to save IPython command history to file: ' + \
1109 `self.histfile`
1110
1111 def reloadhist(self):
1112 """Reload the input history from disk file."""
1113
1114 if self.has_readline:
1115 try:
1116 self.readline.clear_history()
1117 self.readline.read_history_file(self.shell.histfile)
1118 except AttributeError:
1119 pass
1120
1121 def history_saving_wrapper(self, func):
1122 """ Wrap func for readline history saving
1123
1124 Convert func into callable that saves & restores
1125 history around the call """
1126
1127 if not self.has_readline:
1128 return func
1129
1130 def wrapper():
1131 self.savehist()
1132 try:
1133 func()
1134 finally:
1135 readline.read_history_file(self.histfile)
1136 return wrapper
1137
1138 #-------------------------------------------------------------------------
1139 # Things related to exception handling and tracebacks (not debugging)
1140 #-------------------------------------------------------------------------
1141
1142 def init_traceback_handlers(self, custom_exceptions):
1143 # Syntax error handler.
1144 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
1145
1146 # The interactive one is initialized with an offset, meaning we always
1147 # want to remove the topmost item in the traceback, which is our own
1148 # internal code. Valid modes: ['Plain','Context','Verbose']
1149 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1150 color_scheme='NoColor',
1151 tb_offset = 1)
1152
1153 # IPython itself shouldn't crash. This will produce a detailed
1154 # post-mortem if it does. But we only install the crash handler for
1155 # non-threaded shells, the threaded ones use a normal verbose reporter
1156 # and lose the crash handler. This is because exceptions in the main
1157 # thread (such as in GUI code) propagate directly to sys.excepthook,
1158 # and there's no point in printing crash dumps for every user exception.
1159 if self.isthreaded:
1160 ipCrashHandler = ultratb.FormattedTB()
1161 else:
1162 from IPython.core import crashhandler
1163 ipCrashHandler = crashhandler.IPythonCrashHandler(self)
1164 self.set_crash_handler(ipCrashHandler)
1165
1166 # and add any custom exception handlers the user may have specified
1167 self.set_custom_exc(*custom_exceptions)
1168
1169 def set_crash_handler(self, crashHandler):
1170 """Set the IPython crash handler.
1171
1172 This must be a callable with a signature suitable for use as
1173 sys.excepthook."""
1174
1175 # Install the given crash handler as the Python exception hook
1176 sys.excepthook = crashHandler
1177
1178 # The instance will store a pointer to this, so that runtime code
1179 # (such as magics) can access it. This is because during the
1180 # read-eval loop, it gets temporarily overwritten (to deal with GUI
1181 # frameworks).
1182 self.sys_excepthook = sys.excepthook
1183
1184 def set_custom_exc(self,exc_tuple,handler):
1185 """set_custom_exc(exc_tuple,handler)
1186
1187 Set a custom exception handler, which will be called if any of the
1188 exceptions in exc_tuple occur in the mainloop (specifically, in the
1189 runcode() method.
1190
1191 Inputs:
1192
1193 - exc_tuple: a *tuple* of valid exceptions to call the defined
1194 handler for. It is very important that you use a tuple, and NOT A
1195 LIST here, because of the way Python's except statement works. If
1196 you only want to trap a single exception, use a singleton tuple:
1197
1198 exc_tuple == (MyCustomException,)
1199
1200 - handler: this must be defined as a function with the following
1201 basic interface: def my_handler(self,etype,value,tb).
1202
1203 This will be made into an instance method (via new.instancemethod)
1204 of IPython itself, and it will be called if any of the exceptions
1205 listed in the exc_tuple are caught. If the handler is None, an
1206 internal basic one is used, which just prints basic info.
1207
1208 WARNING: by putting in your own exception handler into IPython's main
1209 execution loop, you run a very good chance of nasty crashes. This
1210 facility should only be used if you really know what you are doing."""
1211
1212 assert type(exc_tuple)==type(()) , \
1213 "The custom exceptions must be given AS A TUPLE."
1214
1215 def dummy_handler(self,etype,value,tb):
1216 print '*** Simple custom exception handler ***'
1217 print 'Exception type :',etype
1218 print 'Exception value:',value
1219 print 'Traceback :',tb
1220 print 'Source code :','\n'.join(self.buffer)
1221
1222 if handler is None: handler = dummy_handler
1223
1224 self.CustomTB = new.instancemethod(handler,self,self.__class__)
1225 self.custom_exceptions = exc_tuple
1226
1227 def excepthook(self, etype, value, tb):
1228 """One more defense for GUI apps that call sys.excepthook.
1229
1230 GUI frameworks like wxPython trap exceptions and call
1231 sys.excepthook themselves. I guess this is a feature that
1232 enables them to keep running after exceptions that would
1233 otherwise kill their mainloop. This is a bother for IPython
1234 which excepts to catch all of the program exceptions with a try:
1235 except: statement.
1236
1237 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1238 any app directly invokes sys.excepthook, it will look to the user like
1239 IPython crashed. In order to work around this, we can disable the
1240 CrashHandler and replace it with this excepthook instead, which prints a
1241 regular traceback using our InteractiveTB. In this fashion, apps which
1242 call sys.excepthook will generate a regular-looking exception from
1243 IPython, and the CrashHandler will only be triggered by real IPython
1244 crashes.
1245
1246 This hook should be used sparingly, only in places which are not likely
1247 to be true IPython errors.
1248 """
1249 self.showtraceback((etype,value,tb),tb_offset=0)
1250
1251 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None):
1252 """Display the exception that just occurred.
1253
1254 If nothing is known about the exception, this is the method which
1255 should be used throughout the code for presenting user tracebacks,
1256 rather than directly invoking the InteractiveTB object.
1257
1258 A specific showsyntaxerror() also exists, but this method can take
1259 care of calling it if needed, so unless you are explicitly catching a
1260 SyntaxError exception, don't try to analyze the stack manually and
1261 simply call this method."""
1262
1263
1264 # Though this won't be called by syntax errors in the input line,
1265 # there may be SyntaxError cases whith imported code.
1266
1267 try:
1268 if exc_tuple is None:
1269 etype, value, tb = sys.exc_info()
1270 else:
1271 etype, value, tb = exc_tuple
1272
1273 if etype is SyntaxError:
1274 self.showsyntaxerror(filename)
1275 elif etype is UsageError:
1276 print "UsageError:", value
1277 else:
1278 # WARNING: these variables are somewhat deprecated and not
1279 # necessarily safe to use in a threaded environment, but tools
1280 # like pdb depend on their existence, so let's set them. If we
1281 # find problems in the field, we'll need to revisit their use.
1282 sys.last_type = etype
1283 sys.last_value = value
1284 sys.last_traceback = tb
1285
1286 if etype in self.custom_exceptions:
1287 self.CustomTB(etype,value,tb)
1288 else:
1289 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1290 if self.InteractiveTB.call_pdb and self.has_readline:
1291 # pdb mucks up readline, fix it back
1292 self.set_completer()
1293 except KeyboardInterrupt:
1294 self.write("\nKeyboardInterrupt\n")
1295
1296 def showsyntaxerror(self, filename=None):
1297 """Display the syntax error that just occurred.
1298
1299 This doesn't display a stack trace because there isn't one.
1300
1301 If a filename is given, it is stuffed in the exception instead
1302 of what was there before (because Python's parser always uses
1303 "<string>" when reading from a string).
1304 """
1305 etype, value, last_traceback = sys.exc_info()
1306
1307 # See note about these variables in showtraceback() below
1308 sys.last_type = etype
1309 sys.last_value = value
1310 sys.last_traceback = last_traceback
1311
1312 if filename and etype is SyntaxError:
1313 # Work hard to stuff the correct filename in the exception
1314 try:
1315 msg, (dummy_filename, lineno, offset, line) = value
1316 except:
1317 # Not the format we expect; leave it alone
1318 pass
1319 else:
1320 # Stuff in the right filename
1321 try:
1322 # Assume SyntaxError is a class exception
1323 value = SyntaxError(msg, (filename, lineno, offset, line))
1324 except:
1325 # If that failed, assume SyntaxError is a string
1326 value = msg, (filename, lineno, offset, line)
1327 self.SyntaxTB(etype,value,[])
1328
1329 def edit_syntax_error(self):
1330 """The bottom half of the syntax error handler called in the main loop.
1331
1332 Loop until syntax error is fixed or user cancels.
1333 """
1334
1335 while self.SyntaxTB.last_syntax_error:
1336 # copy and clear last_syntax_error
1337 err = self.SyntaxTB.clear_err_state()
1338 if not self._should_recompile(err):
1339 return
1340 try:
1341 # may set last_syntax_error again if a SyntaxError is raised
1342 self.safe_execfile(err.filename,self.user_ns)
1343 except:
1344 self.showtraceback()
1345 else:
1346 try:
1347 f = file(err.filename)
1348 try:
1349 # This should be inside a display_trap block and I
1350 # think it is.
1351 sys.displayhook(f.read())
1352 finally:
1353 f.close()
1354 except:
1355 self.showtraceback()
1356
1357 def _should_recompile(self,e):
1358 """Utility routine for edit_syntax_error"""
1359
1360 if e.filename in ('<ipython console>','<input>','<string>',
1361 '<console>','<BackgroundJob compilation>',
1362 None):
1363
1364 return False
1365 try:
1366 if (self.autoedit_syntax and
1367 not self.ask_yes_no('Return to editor to correct syntax error? '
1368 '[Y/n] ','y')):
1369 return False
1370 except EOFError:
1371 return False
1372
1373 def int0(x):
1374 try:
1375 return int(x)
1376 except TypeError:
1377 return 0
1378 # always pass integer line and offset values to editor hook
1379 try:
1380 self.hooks.fix_error_editor(e.filename,
1381 int0(e.lineno),int0(e.offset),e.msg)
1382 except TryNext:
1383 warn('Could not open editor')
1384 return False
1385 return True
1386
1387 #-------------------------------------------------------------------------
1388 # Things related to tab completion
1389 #-------------------------------------------------------------------------
1390
1391 def complete(self, text):
1392 """Return a sorted list of all possible completions on text.
1393
1394 Inputs:
1395
1396 - text: a string of text to be completed on.
1397
1398 This is a wrapper around the completion mechanism, similar to what
1399 readline does at the command line when the TAB key is hit. By
1400 exposing it as a method, it can be used by other non-readline
1401 environments (such as GUIs) for text completion.
1402
1403 Simple usage example:
1404
1405 In [7]: x = 'hello'
1406
1407 In [8]: x
1408 Out[8]: 'hello'
1409
1410 In [9]: print x
1411 hello
1412
1413 In [10]: _ip.complete('x.l')
1414 Out[10]: ['x.ljust', 'x.lower', 'x.lstrip']
1415 """
1416
1417 # Inject names into __builtin__ so we can complete on the added names.
1418 with self.builtin_trap:
1419 complete = self.Completer.complete
1420 state = 0
1421 # use a dict so we get unique keys, since ipyhton's multiple
1422 # completers can return duplicates. When we make 2.4 a requirement,
1423 # start using sets instead, which are faster.
1424 comps = {}
1425 while True:
1426 newcomp = complete(text,state,line_buffer=text)
1427 if newcomp is None:
1428 break
1429 comps[newcomp] = 1
1430 state += 1
1431 outcomps = comps.keys()
1432 outcomps.sort()
1433 #print "T:",text,"OC:",outcomps # dbg
1434 #print "vars:",self.user_ns.keys()
1435 return outcomps
1436
1437 def set_custom_completer(self,completer,pos=0):
1438 """Adds a new custom completer function.
1439
1440 The position argument (defaults to 0) is the index in the completers
1441 list where you want the completer to be inserted."""
1442
1443 newcomp = new.instancemethod(completer,self.Completer,
1444 self.Completer.__class__)
1445 self.Completer.matchers.insert(pos,newcomp)
1446
1447 def set_completer(self):
1448 """Reset readline's completer to be our own."""
1449 self.readline.set_completer(self.Completer.complete)
1450
1451 def set_completer_frame(self, frame=None):
1452 """Set the frame of the completer."""
1453 if frame:
1454 self.Completer.namespace = frame.f_locals
1455 self.Completer.global_namespace = frame.f_globals
1456 else:
1457 self.Completer.namespace = self.user_ns
1458 self.Completer.global_namespace = self.user_global_ns
1459
1460 #-------------------------------------------------------------------------
1461 # Things related to readline
1462 #-------------------------------------------------------------------------
1463
1464 def init_readline(self):
1465 """Command history completion/saving/reloading."""
1466
1467 self.rl_next_input = None
1468 self.rl_do_indent = False
1469
1470 if not self.readline_use:
1471 return
1472
1473 import IPython.utils.rlineimpl as readline
1474
1475 if not readline.have_readline:
1476 self.has_readline = 0
1477 self.readline = None
1478 # no point in bugging windows users with this every time:
1479 warn('Readline services not available on this platform.')
1480 else:
1481 sys.modules['readline'] = readline
1482 import atexit
1483 from IPython.core.completer import IPCompleter
1484 self.Completer = IPCompleter(self,
1485 self.user_ns,
1486 self.user_global_ns,
1487 self.readline_omit__names,
1488 self.alias_manager.alias_table)
1489 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1490 self.strdispatchers['complete_command'] = sdisp
1491 self.Completer.custom_completers = sdisp
1492 # Platform-specific configuration
1493 if os.name == 'nt':
1494 self.readline_startup_hook = readline.set_pre_input_hook
1495 else:
1496 self.readline_startup_hook = readline.set_startup_hook
1497
1498 # Load user's initrc file (readline config)
1499 # Or if libedit is used, load editrc.
1500 inputrc_name = os.environ.get('INPUTRC')
1501 if inputrc_name is None:
1502 home_dir = get_home_dir()
1503 if home_dir is not None:
1504 inputrc_name = '.inputrc'
1505 if readline.uses_libedit:
1506 inputrc_name = '.editrc'
1507 inputrc_name = os.path.join(home_dir, inputrc_name)
1508 if os.path.isfile(inputrc_name):
1509 try:
1510 readline.read_init_file(inputrc_name)
1511 except:
1512 warn('Problems reading readline initialization file <%s>'
1513 % inputrc_name)
1514
1515 self.has_readline = 1
1516 self.readline = readline
1517 # save this in sys so embedded copies can restore it properly
1518 sys.ipcompleter = self.Completer.complete
1519 self.set_completer()
1520
1521 # Configure readline according to user's prefs
1522 # This is only done if GNU readline is being used. If libedit
1523 # is being used (as on Leopard) the readline config is
1524 # not run as the syntax for libedit is different.
1525 if not readline.uses_libedit:
1526 for rlcommand in self.readline_parse_and_bind:
1527 #print "loading rl:",rlcommand # dbg
1528 readline.parse_and_bind(rlcommand)
1529
1530 # Remove some chars from the delimiters list. If we encounter
1531 # unicode chars, discard them.
1532 delims = readline.get_completer_delims().encode("ascii", "ignore")
1533 delims = delims.translate(string._idmap,
1534 self.readline_remove_delims)
1535 readline.set_completer_delims(delims)
1536 # otherwise we end up with a monster history after a while:
1537 readline.set_history_length(1000)
1538 try:
1539 #print '*** Reading readline history' # dbg
1540 readline.read_history_file(self.histfile)
1541 except IOError:
1542 pass # It doesn't exist yet.
1543
1544 atexit.register(self.atexit_operations)
1545 del atexit
1546
1547 # Configure auto-indent for all platforms
1548 self.set_autoindent(self.autoindent)
1549
1550 def set_next_input(self, s):
1551 """ Sets the 'default' input string for the next command line.
1552
1553 Requires readline.
1554
1555 Example:
1556
1557 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1558 [D:\ipython]|2> Hello Word_ # cursor is here
1559 """
1560
1561 self.rl_next_input = s
1562
1563 def pre_readline(self):
1564 """readline hook to be used at the start of each line.
1565
1566 Currently it handles auto-indent only."""
1567
1568 #debugx('self.indent_current_nsp','pre_readline:')
1569
1570 if self.rl_do_indent:
1571 self.readline.insert_text(self._indent_current_str())
1572 if self.rl_next_input is not None:
1573 self.readline.insert_text(self.rl_next_input)
1574 self.rl_next_input = None
1575
1576 def _indent_current_str(self):
1577 """return the current level of indentation as a string"""
1578 return self.indent_current_nsp * ' '
1579
1580 #-------------------------------------------------------------------------
1581 # Things related to magics
1582 #-------------------------------------------------------------------------
1583
1584 def init_magics(self):
1585 # Set user colors (don't do it in the constructor above so that it
1586 # doesn't crash if colors option is invalid)
1587 self.magic_colors(self.colors)
1588
1589 def magic(self,arg_s):
1590 """Call a magic function by name.
1591
1592 Input: a string containing the name of the magic function to call and any
1593 additional arguments to be passed to the magic.
1594
1595 magic('name -opt foo bar') is equivalent to typing at the ipython
1596 prompt:
1597
1598 In[1]: %name -opt foo bar
1599
1600 To call a magic without arguments, simply use magic('name').
1601
1602 This provides a proper Python function to call IPython's magics in any
1603 valid Python code you can type at the interpreter, including loops and
1604 compound statements.
1605 """
1606
1607 args = arg_s.split(' ',1)
1608 magic_name = args[0]
1609 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1610
1611 try:
1612 magic_args = args[1]
1613 except IndexError:
1614 magic_args = ''
1615 fn = getattr(self,'magic_'+magic_name,None)
1616 if fn is None:
1617 error("Magic function `%s` not found." % magic_name)
1618 else:
1619 magic_args = self.var_expand(magic_args,1)
1620 with nested(self.builtin_trap,):
1621 result = fn(magic_args)
1622 return result
1623
1624 def define_magic(self, magicname, func):
1625 """Expose own function as magic function for ipython
1626
1627 def foo_impl(self,parameter_s=''):
1628 'My very own magic!. (Use docstrings, IPython reads them).'
1629 print 'Magic function. Passed parameter is between < >:'
1630 print '<%s>' % parameter_s
1631 print 'The self object is:',self
1632
1633 self.define_magic('foo',foo_impl)
1634 """
1635
1636 import new
1637 im = new.instancemethod(func,self, self.__class__)
1638 old = getattr(self, "magic_" + magicname, None)
1639 setattr(self, "magic_" + magicname, im)
1640 return old
1641
1642 #-------------------------------------------------------------------------
1643 # Things related to macros
1644 #-------------------------------------------------------------------------
1645
1646 def define_macro(self, name, themacro):
1647 """Define a new macro
1648
1649 Parameters
1650 ----------
1651 name : str
1652 The name of the macro.
1653 themacro : str or Macro
1654 The action to do upon invoking the macro. If a string, a new
1655 Macro object is created by passing the string to it.
1656 """
1657
1658 from IPython.core import macro
1659
1660 if isinstance(themacro, basestring):
1661 themacro = macro.Macro(themacro)
1662 if not isinstance(themacro, macro.Macro):
1663 raise ValueError('A macro must be a string or a Macro instance.')
1664 self.user_ns[name] = themacro
1665
1666 #-------------------------------------------------------------------------
1667 # Things related to the running of system commands
1668 #-------------------------------------------------------------------------
1669
1670 def system(self, cmd):
1671 """Make a system call, using IPython."""
1672 return self.hooks.shell_hook(self.var_expand(cmd, depth=2))
1673
1674 #-------------------------------------------------------------------------
1675 # Things related to aliases
1676 #-------------------------------------------------------------------------
1677
1678 def init_alias(self):
1679 self.alias_manager = AliasManager(self, config=self.config)
1680 self.ns_table['alias'] = self.alias_manager.alias_table,
1681
1682 #-------------------------------------------------------------------------
1683 # Things related to the running of code
1684 #-------------------------------------------------------------------------
1685
1686 def ex(self, cmd):
1687 """Execute a normal python statement in user namespace."""
1688 with nested(self.builtin_trap,):
1689 exec cmd in self.user_global_ns, self.user_ns
1690
1691 def ev(self, expr):
1692 """Evaluate python expression expr in user namespace.
1693
1694 Returns the result of evaluation
1695 """
1696 with nested(self.builtin_trap,):
1697 return eval(expr, self.user_global_ns, self.user_ns)
1698
1699 def mainloop(self, display_banner=None):
1700 """Start the mainloop.
1701
1702 If an optional banner argument is given, it will override the
1703 internally created default banner.
1704 """
1705
1706 with nested(self.builtin_trap, self.display_trap):
1707
1708 # if you run stuff with -c <cmd>, raw hist is not updated
1709 # ensure that it's in sync
1710 if len(self.input_hist) != len (self.input_hist_raw):
1711 self.input_hist_raw = InputList(self.input_hist)
1712
1713 while 1:
1714 try:
1715 self.interact(display_banner=display_banner)
1716 #self.interact_with_readline()
1717 # XXX for testing of a readline-decoupled repl loop, call
1718 # interact_with_readline above
1719 break
1720 except KeyboardInterrupt:
1721 # this should not be necessary, but KeyboardInterrupt
1722 # handling seems rather unpredictable...
1723 self.write("\nKeyboardInterrupt in interact()\n")
1724
1725 def interact_prompt(self):
1726 """ Print the prompt (in read-eval-print loop)
1727
1728 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1729 used in standard IPython flow.
1730 """
1731 if self.more:
1732 try:
1733 prompt = self.hooks.generate_prompt(True)
1734 except:
1735 self.showtraceback()
1736 if self.autoindent:
1737 self.rl_do_indent = True
1738
1739 else:
1740 try:
1741 prompt = self.hooks.generate_prompt(False)
1742 except:
1743 self.showtraceback()
1744 self.write(prompt)
1745
1746 def interact_handle_input(self,line):
1747 """ Handle the input line (in read-eval-print loop)
1748
1749 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1750 used in standard IPython flow.
1751 """
1752 if line.lstrip() == line:
1753 self.shadowhist.add(line.strip())
1754 lineout = self.prefilter_manager.prefilter_lines(line,self.more)
1755
1756 if line.strip():
1757 if self.more:
1758 self.input_hist_raw[-1] += '%s\n' % line
1759 else:
1760 self.input_hist_raw.append('%s\n' % line)
1761
1762
1763 self.more = self.push_line(lineout)
1764 if (self.SyntaxTB.last_syntax_error and
1765 self.autoedit_syntax):
1766 self.edit_syntax_error()
1767
1768 def interact_with_readline(self):
1769 """ Demo of using interact_handle_input, interact_prompt
1770
1771 This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI),
1772 it should work like this.
1773 """
1774 self.readline_startup_hook(self.pre_readline)
1775 while not self.exit_now:
1776 self.interact_prompt()
1777 if self.more:
1778 self.rl_do_indent = True
1779 else:
1780 self.rl_do_indent = False
1781 line = raw_input_original().decode(self.stdin_encoding)
1782 self.interact_handle_input(line)
1783
1784 def interact(self, display_banner=None):
1785 """Closely emulate the interactive Python console."""
1786
1787 # batch run -> do not interact
1788 if self.exit_now:
1789 return
1790
1791 if display_banner is None:
1792 display_banner = self.display_banner
1793 if display_banner:
1794 self.show_banner()
1795
1796 more = 0
1797
1798 # Mark activity in the builtins
1799 __builtin__.__dict__['__IPYTHON__active'] += 1
1800
1801 if self.has_readline:
1802 self.readline_startup_hook(self.pre_readline)
1803 # exit_now is set by a call to %Exit or %Quit, through the
1804 # ask_exit callback.
1805
1806 while not self.exit_now:
1807 self.hooks.pre_prompt_hook()
1808 if more:
1809 try:
1810 prompt = self.hooks.generate_prompt(True)
1811 except:
1812 self.showtraceback()
1813 if self.autoindent:
1814 self.rl_do_indent = True
1815
1816 else:
1817 try:
1818 prompt = self.hooks.generate_prompt(False)
1819 except:
1820 self.showtraceback()
1821 try:
1822 line = self.raw_input(prompt, more)
1823 if self.exit_now:
1824 # quick exit on sys.std[in|out] close
1825 break
1826 if self.autoindent:
1827 self.rl_do_indent = False
1828
1829 except KeyboardInterrupt:
1830 #double-guard against keyboardinterrupts during kbdint handling
1831 try:
1832 self.write('\nKeyboardInterrupt\n')
1833 self.resetbuffer()
1834 # keep cache in sync with the prompt counter:
1835 self.outputcache.prompt_count -= 1
1836
1837 if self.autoindent:
1838 self.indent_current_nsp = 0
1839 more = 0
1840 except KeyboardInterrupt:
1841 pass
1842 except EOFError:
1843 if self.autoindent:
1844 self.rl_do_indent = False
1845 self.readline_startup_hook(None)
1846 self.write('\n')
1847 self.exit()
1848 except bdb.BdbQuit:
1849 warn('The Python debugger has exited with a BdbQuit exception.\n'
1850 'Because of how pdb handles the stack, it is impossible\n'
1851 'for IPython to properly format this particular exception.\n'
1852 'IPython will resume normal operation.')
1853 except:
1854 # exceptions here are VERY RARE, but they can be triggered
1855 # asynchronously by signal handlers, for example.
1856 self.showtraceback()
1857 else:
1858 more = self.push_line(line)
1859 if (self.SyntaxTB.last_syntax_error and
1860 self.autoedit_syntax):
1861 self.edit_syntax_error()
1862
1863 # We are off again...
1864 __builtin__.__dict__['__IPYTHON__active'] -= 1
1865
1866 def safe_execfile(self, fname, *where, **kw):
1867 """A safe version of the builtin execfile().
1868
1869 This version will never throw an exception, but instead print
1870 helpful error messages to the screen. This only works on pure
1871 Python files with the .py extension.
1872
1873 Parameters
1874 ----------
1875 fname : string
1876 The name of the file to be executed.
1877 where : tuple
1878 One or two namespaces, passed to execfile() as (globals,locals).
1879 If only one is given, it is passed as both.
1880 exit_ignore : bool (False)
1881 If True, then don't print errors for non-zero exit statuses.
1882 """
1883 kw.setdefault('exit_ignore', False)
1884
1885 fname = os.path.abspath(os.path.expanduser(fname))
1886
1887 # Make sure we have a .py file
1888 if not fname.endswith('.py'):
1889 warn('File must end with .py to be run using execfile: <%s>' % fname)
1890
1891 # Make sure we can open the file
1892 try:
1893 with open(fname) as thefile:
1894 pass
1895 except:
1896 warn('Could not open file <%s> for safe execution.' % fname)
1897 return
1898
1899 # Find things also in current directory. This is needed to mimic the
1900 # behavior of running a script from the system command line, where
1901 # Python inserts the script's directory into sys.path
1902 dname = os.path.dirname(fname)
1903
1904 with prepended_to_syspath(dname):
1905 try:
1906 if sys.platform == 'win32' and sys.version_info < (2,5,1):
1907 # Work around a bug in Python for Windows. The bug was
1908 # fixed in in Python 2.5 r54159 and 54158, but that's still
1909 # SVN Python as of March/07. For details, see:
1910 # http://projects.scipy.org/ipython/ipython/ticket/123
1911 try:
1912 globs,locs = where[0:2]
1913 except:
1914 try:
1915 globs = locs = where[0]
1916 except:
1917 globs = locs = globals()
1918 exec file(fname) in globs,locs
1919 else:
1920 execfile(fname,*where)
1921 except SyntaxError:
1922 self.showsyntaxerror()
1923 warn('Failure executing file: <%s>' % fname)
1924 except SystemExit, status:
1925 # Code that correctly sets the exit status flag to success (0)
1926 # shouldn't be bothered with a traceback. Note that a plain
1927 # sys.exit() does NOT set the message to 0 (it's empty) so that
1928 # will still get a traceback. Note that the structure of the
1929 # SystemExit exception changed between Python 2.4 and 2.5, so
1930 # the checks must be done in a version-dependent way.
1931 show = False
1932 if status.args[0]==0 and not kw['exit_ignore']:
1933 show = True
1934 if show:
1935 self.showtraceback()
1936 warn('Failure executing file: <%s>' % fname)
1937 except:
1938 self.showtraceback()
1939 warn('Failure executing file: <%s>' % fname)
1940
1941 def safe_execfile_ipy(self, fname):
1942 """Like safe_execfile, but for .ipy files with IPython syntax.
1943
1944 Parameters
1945 ----------
1946 fname : str
1947 The name of the file to execute. The filename must have a
1948 .ipy extension.
1949 """
1950 fname = os.path.abspath(os.path.expanduser(fname))
1951
1952 # Make sure we have a .py file
1953 if not fname.endswith('.ipy'):
1954 warn('File must end with .py to be run using execfile: <%s>' % fname)
1955
1956 # Make sure we can open the file
1957 try:
1958 with open(fname) as thefile:
1959 pass
1960 except:
1961 warn('Could not open file <%s> for safe execution.' % fname)
1962 return
1963
1964 # Find things also in current directory. This is needed to mimic the
1965 # behavior of running a script from the system command line, where
1966 # Python inserts the script's directory into sys.path
1967 dname = os.path.dirname(fname)
1968
1969 with prepended_to_syspath(dname):
1970 try:
1971 with open(fname) as thefile:
1972 script = thefile.read()
1973 # self.runlines currently captures all exceptions
1974 # raise in user code. It would be nice if there were
1975 # versions of runlines, execfile that did raise, so
1976 # we could catch the errors.
1977 self.runlines(script, clean=True)
1978 except:
1979 self.showtraceback()
1980 warn('Unknown failure executing file: <%s>' % fname)
1981
1982 def _is_secondary_block_start(self, s):
1983 if not s.endswith(':'):
1984 return False
1985 if (s.startswith('elif') or
1986 s.startswith('else') or
1987 s.startswith('except') or
1988 s.startswith('finally')):
1989 return True
1990
1991 def cleanup_ipy_script(self, script):
1992 """Make a script safe for self.runlines()
1993
1994 Currently, IPython is lines based, with blocks being detected by
1995 empty lines. This is a problem for block based scripts that may
1996 not have empty lines after blocks. This script adds those empty
1997 lines to make scripts safe for running in the current line based
1998 IPython.
1999 """
2000 res = []
2001 lines = script.splitlines()
2002 level = 0
2003
2004 for l in lines:
2005 lstripped = l.lstrip()
2006 stripped = l.strip()
2007 if not stripped:
2008 continue
2009 newlevel = len(l) - len(lstripped)
2010 if level > 0 and newlevel == 0 and \
2011 not self._is_secondary_block_start(stripped):
2012 # add empty line
2013 res.append('')
2014 res.append(l)
2015 level = newlevel
2016
2017 return '\n'.join(res) + '\n'
2018
2019 def runlines(self, lines, clean=False):
2020 """Run a string of one or more lines of source.
2021
2022 This method is capable of running a string containing multiple source
2023 lines, as if they had been entered at the IPython prompt. Since it
2024 exposes IPython's processing machinery, the given strings can contain
2025 magic calls (%magic), special shell access (!cmd), etc.
2026 """
2027
2028 if isinstance(lines, (list, tuple)):
2029 lines = '\n'.join(lines)
2030
2031 if clean:
2032 lines = self.cleanup_ipy_script(lines)
2033
2034 # We must start with a clean buffer, in case this is run from an
2035 # interactive IPython session (via a magic, for example).
2036 self.resetbuffer()
2037 lines = lines.splitlines()
2038 more = 0
2039
2040 with nested(self.builtin_trap, self.display_trap):
2041 for line in lines:
2042 # skip blank lines so we don't mess up the prompt counter, but do
2043 # NOT skip even a blank line if we are in a code block (more is
2044 # true)
2045
2046 if line or more:
2047 # push to raw history, so hist line numbers stay in sync
2048 self.input_hist_raw.append("# " + line + "\n")
2049 prefiltered = self.prefilter_manager.prefilter_lines(line,more)
2050 more = self.push_line(prefiltered)
2051 # IPython's runsource returns None if there was an error
2052 # compiling the code. This allows us to stop processing right
2053 # away, so the user gets the error message at the right place.
2054 if more is None:
2055 break
2056 else:
2057 self.input_hist_raw.append("\n")
2058 # final newline in case the input didn't have it, so that the code
2059 # actually does get executed
2060 if more:
2061 self.push_line('\n')
2062
2063 def runsource(self, source, filename='<input>', symbol='single'):
2064 """Compile and run some source in the interpreter.
2065
2066 Arguments are as for compile_command().
2067
2068 One several things can happen:
2069
2070 1) The input is incorrect; compile_command() raised an
2071 exception (SyntaxError or OverflowError). A syntax traceback
2072 will be printed by calling the showsyntaxerror() method.
2073
2074 2) The input is incomplete, and more input is required;
2075 compile_command() returned None. Nothing happens.
2076
2077 3) The input is complete; compile_command() returned a code
2078 object. The code is executed by calling self.runcode() (which
2079 also handles run-time exceptions, except for SystemExit).
2080
2081 The return value is:
2082
2083 - True in case 2
2084
2085 - False in the other cases, unless an exception is raised, where
2086 None is returned instead. This can be used by external callers to
2087 know whether to continue feeding input or not.
2088
2089 The return value can be used to decide whether to use sys.ps1 or
2090 sys.ps2 to prompt the next line."""
2091
2092 # if the source code has leading blanks, add 'if 1:\n' to it
2093 # this allows execution of indented pasted code. It is tempting
2094 # to add '\n' at the end of source to run commands like ' a=1'
2095 # directly, but this fails for more complicated scenarios
2096 source=source.encode(self.stdin_encoding)
2097 if source[:1] in [' ', '\t']:
2098 source = 'if 1:\n%s' % source
2099
2100 try:
2101 code = self.compile(source,filename,symbol)
2102 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
2103 # Case 1
2104 self.showsyntaxerror(filename)
2105 return None
2106
2107 if code is None:
2108 # Case 2
2109 return True
2110
2111 # Case 3
2112 # We store the code object so that threaded shells and
2113 # custom exception handlers can access all this info if needed.
2114 # The source corresponding to this can be obtained from the
2115 # buffer attribute as '\n'.join(self.buffer).
2116 self.code_to_run = code
2117 # now actually execute the code object
2118 if self.runcode(code) == 0:
2119 return False
2120 else:
2121 return None
2122
2123 def runcode(self,code_obj):
2124 """Execute a code object.
2125
2126 When an exception occurs, self.showtraceback() is called to display a
2127 traceback.
2128
2129 Return value: a flag indicating whether the code to be run completed
2130 successfully:
2131
2132 - 0: successful execution.
2133 - 1: an error occurred.
2134 """
2135
2136 # Set our own excepthook in case the user code tries to call it
2137 # directly, so that the IPython crash handler doesn't get triggered
2138 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2139
2140 # we save the original sys.excepthook in the instance, in case config
2141 # code (such as magics) needs access to it.
2142 self.sys_excepthook = old_excepthook
2143 outflag = 1 # happens in more places, so it's easier as default
2144 try:
2145 try:
2146 self.hooks.pre_runcode_hook()
2147 exec code_obj in self.user_global_ns, self.user_ns
2148 finally:
2149 # Reset our crash handler in place
2150 sys.excepthook = old_excepthook
2151 except SystemExit:
2152 self.resetbuffer()
2153 self.showtraceback()
2154 warn("Type %exit or %quit to exit IPython "
2155 "(%Exit or %Quit do so unconditionally).",level=1)
2156 except self.custom_exceptions:
2157 etype,value,tb = sys.exc_info()
2158 self.CustomTB(etype,value,tb)
2159 except:
2160 self.showtraceback()
2161 else:
2162 outflag = 0
2163 if softspace(sys.stdout, 0):
2164 print
2165 # Flush out code object which has been run (and source)
2166 self.code_to_run = None
2167 return outflag
2168
2169 def push_line(self, line):
2170 """Push a line to the interpreter.
2171
2172 The line should not have a trailing newline; it may have
2173 internal newlines. The line is appended to a buffer and the
2174 interpreter's runsource() method is called with the
2175 concatenated contents of the buffer as source. If this
2176 indicates that the command was executed or invalid, the buffer
2177 is reset; otherwise, the command is incomplete, and the buffer
2178 is left as it was after the line was appended. The return
2179 value is 1 if more input is required, 0 if the line was dealt
2180 with in some way (this is the same as runsource()).
2181 """
2182
2183 # autoindent management should be done here, and not in the
2184 # interactive loop, since that one is only seen by keyboard input. We
2185 # need this done correctly even for code run via runlines (which uses
2186 # push).
2187
2188 #print 'push line: <%s>' % line # dbg
2189 for subline in line.splitlines():
2190 self._autoindent_update(subline)
2191 self.buffer.append(line)
2192 more = self.runsource('\n'.join(self.buffer), self.filename)
2193 if not more:
2194 self.resetbuffer()
2195 return more
2196
2197 def _autoindent_update(self,line):
2198 """Keep track of the indent level."""
2199
2200 #debugx('line')
2201 #debugx('self.indent_current_nsp')
2202 if self.autoindent:
2203 if line:
2204 inisp = num_ini_spaces(line)
2205 if inisp < self.indent_current_nsp:
2206 self.indent_current_nsp = inisp
2207
2208 if line[-1] == ':':
2209 self.indent_current_nsp += 4
2210 elif dedent_re.match(line):
2211 self.indent_current_nsp -= 4
2212 else:
2213 self.indent_current_nsp = 0
2214
2215 def resetbuffer(self):
2216 """Reset the input buffer."""
2217 self.buffer[:] = []
2218
2219 def raw_input(self,prompt='',continue_prompt=False):
2220 """Write a prompt and read a line.
2221
2222 The returned line does not include the trailing newline.
2223 When the user enters the EOF key sequence, EOFError is raised.
2224
2225 Optional inputs:
2226
2227 - prompt(''): a string to be printed to prompt the user.
2228
2229 - continue_prompt(False): whether this line is the first one or a
2230 continuation in a sequence of inputs.
2231 """
2232 # growl.notify("raw_input: ", "prompt = %r\ncontinue_prompt = %s" % (prompt, continue_prompt))
2233
2234 # Code run by the user may have modified the readline completer state.
2235 # We must ensure that our completer is back in place.
2236
2237 if self.has_readline:
2238 self.set_completer()
2239
2240 try:
2241 line = raw_input_original(prompt).decode(self.stdin_encoding)
2242 except ValueError:
2243 warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
2244 " or sys.stdout.close()!\nExiting IPython!")
2245 self.ask_exit()
2246 return ""
2247
2248 # Try to be reasonably smart about not re-indenting pasted input more
2249 # than necessary. We do this by trimming out the auto-indent initial
2250 # spaces, if the user's actual input started itself with whitespace.
2251 #debugx('self.buffer[-1]')
2252
2253 if self.autoindent:
2254 if num_ini_spaces(line) > self.indent_current_nsp:
2255 line = line[self.indent_current_nsp:]
2256 self.indent_current_nsp = 0
2257
2258 # store the unfiltered input before the user has any chance to modify
2259 # it.
2260 if line.strip():
2261 if continue_prompt:
2262 self.input_hist_raw[-1] += '%s\n' % line
2263 if self.has_readline and self.readline_use:
2264 try:
2265 histlen = self.readline.get_current_history_length()
2266 if histlen > 1:
2267 newhist = self.input_hist_raw[-1].rstrip()
2268 self.readline.remove_history_item(histlen-1)
2269 self.readline.replace_history_item(histlen-2,
2270 newhist.encode(self.stdin_encoding))
2271 except AttributeError:
2272 pass # re{move,place}_history_item are new in 2.4.
2273 else:
2274 self.input_hist_raw.append('%s\n' % line)
2275 # only entries starting at first column go to shadow history
2276 if line.lstrip() == line:
2277 self.shadowhist.add(line.strip())
2278 elif not continue_prompt:
2279 self.input_hist_raw.append('\n')
2280 try:
2281 lineout = self.prefilter_manager.prefilter_lines(line,continue_prompt)
2282 except:
2283 # blanket except, in case a user-defined prefilter crashes, so it
2284 # can't take all of ipython with it.
2285 self.showtraceback()
2286 return ''
2287 else:
2288 return lineout
2289
2290 #-------------------------------------------------------------------------
2291 # Working with components
2292 #-------------------------------------------------------------------------
2293
2294 def get_component(self, name=None, klass=None):
2295 """Fetch a component by name and klass in my tree."""
2296 c = Component.get_instances(root=self, name=name, klass=klass)
2297 if len(c) == 0:
2298 return None
2299 if len(c) == 1:
2300 return c[0]
2301 else:
2302 return c
2303
2304 #-------------------------------------------------------------------------
2305 # IPython extensions
2306 #-------------------------------------------------------------------------
2307
2308 def load_extension(self, module_str):
2309 """Load an IPython extension by its module name.
2310
2311 An IPython extension is an importable Python module that has
2312 a function with the signature::
2313
2314 def load_ipython_extension(ipython):
2315 # Do things with ipython
2316
2317 This function is called after your extension is imported and the
2318 currently active :class:`InteractiveShell` instance is passed as
2319 the only argument. You can do anything you want with IPython at
2320 that point, including defining new magic and aliases, adding new
2321 components, etc.
2322
2323 The :func:`load_ipython_extension` will be called again is you
2324 load or reload the extension again. It is up to the extension
2325 author to add code to manage that.
2326
2327 You can put your extension modules anywhere you want, as long as
2328 they can be imported by Python's standard import mechanism. However,
2329 to make it easy to write extensions, you can also put your extensions
2330 in ``os.path.join(self.ipython_dir, 'extensions')``. This directory
2331 is added to ``sys.path`` automatically.
2332 """
2333 from IPython.utils.syspathcontext import prepended_to_syspath
2334
2335 if module_str not in sys.modules:
2336 with prepended_to_syspath(self.ipython_extension_dir):
2337 __import__(module_str)
2338 mod = sys.modules[module_str]
2339 self._call_load_ipython_extension(mod)
2340
2341 def unload_extension(self, module_str):
2342 """Unload an IPython extension by its module name.
2343
2344 This function looks up the extension's name in ``sys.modules`` and
2345 simply calls ``mod.unload_ipython_extension(self)``.
2346 """
2347 if module_str in sys.modules:
2348 mod = sys.modules[module_str]
2349 self._call_unload_ipython_extension(mod)
2350
2351 def reload_extension(self, module_str):
2352 """Reload an IPython extension by calling reload.
2353
2354 If the module has not been loaded before,
2355 :meth:`InteractiveShell.load_extension` is called. Otherwise
2356 :func:`reload` is called and then the :func:`load_ipython_extension`
2357 function of the module, if it exists is called.
2358 """
2359 from IPython.utils.syspathcontext import prepended_to_syspath
2360
2361 with prepended_to_syspath(self.ipython_extension_dir):
2362 if module_str in sys.modules:
2363 mod = sys.modules[module_str]
2364 reload(mod)
2365 self._call_load_ipython_extension(mod)
2366 else:
2367 self.load_extension(module_str)
2368
2369 def _call_load_ipython_extension(self, mod):
2370 if hasattr(mod, 'load_ipython_extension'):
2371 mod.load_ipython_extension(self)
2372
2373 def _call_unload_ipython_extension(self, mod):
2374 if hasattr(mod, 'unload_ipython_extension'):
2375 mod.unload_ipython_extension(self)
2376
2377 #-------------------------------------------------------------------------
2378 # Things related to the prefilter
2379 #-------------------------------------------------------------------------
2380
2381 def init_prefilter(self):
2382 self.prefilter_manager = PrefilterManager(self, config=self.config)
2383
2384 #-------------------------------------------------------------------------
2385 # Utilities
2386 #-------------------------------------------------------------------------
2387
2388 def getoutput(self, cmd):
2389 return getoutput(self.var_expand(cmd,depth=2),
2390 header=self.system_header,
2391 verbose=self.system_verbose)
2392
2393 def getoutputerror(self, cmd):
2394 return getoutputerror(self.var_expand(cmd,depth=2),
2395 header=self.system_header,
2396 verbose=self.system_verbose)
2397
2398 def var_expand(self,cmd,depth=0):
2399 """Expand python variables in a string.
2400
2401 The depth argument indicates how many frames above the caller should
2402 be walked to look for the local namespace where to expand variables.
2403
2404 The global namespace for expansion is always the user's interactive
2405 namespace.
2406 """
2407
2408 return str(ItplNS(cmd,
2409 self.user_ns, # globals
2410 # Skip our own frame in searching for locals:
2411 sys._getframe(depth+1).f_locals # locals
2412 ))
2413
2414 def mktempfile(self,data=None):
2415 """Make a new tempfile and return its filename.
2416
2417 This makes a call to tempfile.mktemp, but it registers the created
2418 filename internally so ipython cleans it up at exit time.
2419
2420 Optional inputs:
2421
2422 - data(None): if data is given, it gets written out to the temp file
2423 immediately, and the file is closed again."""
2424
2425 filename = tempfile.mktemp('.py','ipython_edit_')
2426 self.tempfiles.append(filename)
2427
2428 if data:
2429 tmp_file = open(filename,'w')
2430 tmp_file.write(data)
2431 tmp_file.close()
2432 return filename
2433
2434 def write(self,data):
2435 """Write a string to the default output"""
2436 Term.cout.write(data)
2437
2438 def write_err(self,data):
2439 """Write a string to the default error output"""
2440 Term.cerr.write(data)
2441
2442 def ask_yes_no(self,prompt,default=True):
2443 if self.quiet:
2444 return True
2445 return ask_yes_no(prompt,default)
2446
2447 #-------------------------------------------------------------------------
2448 # Things related to IPython exiting
2449 #-------------------------------------------------------------------------
2450
2451 def ask_exit(self):
2452 """ Call for exiting. Can be overiden and used as a callback. """
2453 self.exit_now = True
2454
2455 def exit(self):
2456 """Handle interactive exit.
2457
2458 This method calls the ask_exit callback."""
2459 if self.confirm_exit:
2460 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2461 self.ask_exit()
2462 else:
2463 self.ask_exit()
2464
2465 def atexit_operations(self):
2466 """This will be executed at the time of exit.
2467
2468 Saving of persistent data should be performed here.
2469 """
2470 self.savehist()
2471
2472 # Cleanup all tempfiles left around
2473 for tfile in self.tempfiles:
2474 try:
2475 os.unlink(tfile)
2476 except OSError:
2477 pass
2478
2479 # Clear all user namespaces to release all references cleanly.
2480 self.reset()
2481
2482 # Run user hooks
2483 self.hooks.shutdown_hook()
2484
2485 def cleanup(self):
2486 self.restore_sys_module_state()
2487
2488
@@ -0,0 +1,308 b''
1 #!/usr/bin/env python
2 # encoding: utf-8
3 """
4 Paging capabilities for IPython.core
5
6 Authors:
7
8 * Brian Granger
9 * Fernando Perez
10
11 Notes
12 -----
13
14 For now this uses ipapi, so it can't be in IPython.utils. If we can get
15 rid of that dependency, we could move it there.
16 -----
17 """
18
19 #-----------------------------------------------------------------------------
20 # Copyright (C) 2008-2009 The IPython Development Team
21 #
22 # Distributed under the terms of the BSD License. The full license is in
23 # the file COPYING, distributed as part of this software.
24 #-----------------------------------------------------------------------------
25
26 #-----------------------------------------------------------------------------
27 # Imports
28 #-----------------------------------------------------------------------------
29
30 import os
31 import re
32 import sys
33
34 from IPython.core import ipapi
35 from IPython.core.error import TryNext
36 from IPython.utils.genutils import (
37 chop, Term, USE_CURSES
38 )
39
40 if os.name == "nt":
41 from IPython.utils.winconsole import get_console_size
42
43
44 #-----------------------------------------------------------------------------
45 # Classes and functions
46 #-----------------------------------------------------------------------------
47
48 esc_re = re.compile(r"(\x1b[^m]+m)")
49
50 def page_dumb(strng,start=0,screen_lines=25):
51 """Very dumb 'pager' in Python, for when nothing else works.
52
53 Only moves forward, same interface as page(), except for pager_cmd and
54 mode."""
55
56 out_ln = strng.splitlines()[start:]
57 screens = chop(out_ln,screen_lines-1)
58 if len(screens) == 1:
59 print >>Term.cout, os.linesep.join(screens[0])
60 else:
61 last_escape = ""
62 for scr in screens[0:-1]:
63 hunk = os.linesep.join(scr)
64 print >>Term.cout, last_escape + hunk
65 if not page_more():
66 return
67 esc_list = esc_re.findall(hunk)
68 if len(esc_list) > 0:
69 last_escape = esc_list[-1]
70 print >>Term.cout, last_escape + os.linesep.join(screens[-1])
71
72 #----------------------------------------------------------------------------
73 def page(strng,start=0,screen_lines=0,pager_cmd = None):
74 """Print a string, piping through a pager after a certain length.
75
76 The screen_lines parameter specifies the number of *usable* lines of your
77 terminal screen (total lines minus lines you need to reserve to show other
78 information).
79
80 If you set screen_lines to a number <=0, page() will try to auto-determine
81 your screen size and will only use up to (screen_size+screen_lines) for
82 printing, paging after that. That is, if you want auto-detection but need
83 to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for
84 auto-detection without any lines reserved simply use screen_lines = 0.
85
86 If a string won't fit in the allowed lines, it is sent through the
87 specified pager command. If none given, look for PAGER in the environment,
88 and ultimately default to less.
89
90 If no system pager works, the string is sent through a 'dumb pager'
91 written in python, very simplistic.
92 """
93
94 # Some routines may auto-compute start offsets incorrectly and pass a
95 # negative value. Offset to 0 for robustness.
96 start = max(0,start)
97
98 # first, try the hook
99 ip = ipapi.get()
100 if ip:
101 try:
102 ip.hooks.show_in_pager(strng)
103 return
104 except TryNext:
105 pass
106
107 # Ugly kludge, but calling curses.initscr() flat out crashes in emacs
108 TERM = os.environ.get('TERM','dumb')
109 if TERM in ['dumb','emacs'] and os.name != 'nt':
110 print strng
111 return
112 # chop off the topmost part of the string we don't want to see
113 str_lines = strng.split(os.linesep)[start:]
114 str_toprint = os.linesep.join(str_lines)
115 num_newlines = len(str_lines)
116 len_str = len(str_toprint)
117
118 # Dumb heuristics to guesstimate number of on-screen lines the string
119 # takes. Very basic, but good enough for docstrings in reasonable
120 # terminals. If someone later feels like refining it, it's not hard.
121 numlines = max(num_newlines,int(len_str/80)+1)
122
123 if os.name == "nt":
124 screen_lines_def = get_console_size(defaulty=25)[1]
125 else:
126 screen_lines_def = 25 # default value if we can't auto-determine
127
128 # auto-determine screen size
129 if screen_lines <= 0:
130 if TERM=='xterm' or TERM=='xterm-color':
131 use_curses = USE_CURSES
132 else:
133 # curses causes problems on many terminals other than xterm.
134 use_curses = False
135 if use_curses:
136 import termios
137 import curses
138 # There is a bug in curses, where *sometimes* it fails to properly
139 # initialize, and then after the endwin() call is made, the
140 # terminal is left in an unusable state. Rather than trying to
141 # check everytime for this (by requesting and comparing termios
142 # flags each time), we just save the initial terminal state and
143 # unconditionally reset it every time. It's cheaper than making
144 # the checks.
145 term_flags = termios.tcgetattr(sys.stdout)
146 scr = curses.initscr()
147 screen_lines_real,screen_cols = scr.getmaxyx()
148 curses.endwin()
149 # Restore terminal state in case endwin() didn't.
150 termios.tcsetattr(sys.stdout,termios.TCSANOW,term_flags)
151 # Now we have what we needed: the screen size in rows/columns
152 screen_lines += screen_lines_real
153 #print '***Screen size:',screen_lines_real,'lines x',\
154 #screen_cols,'columns.' # dbg
155 else:
156 screen_lines += screen_lines_def
157
158 #print 'numlines',numlines,'screenlines',screen_lines # dbg
159 if numlines <= screen_lines :
160 #print '*** normal print' # dbg
161 print >>Term.cout, str_toprint
162 else:
163 # Try to open pager and default to internal one if that fails.
164 # All failure modes are tagged as 'retval=1', to match the return
165 # value of a failed system command. If any intermediate attempt
166 # sets retval to 1, at the end we resort to our own page_dumb() pager.
167 pager_cmd = get_pager_cmd(pager_cmd)
168 pager_cmd += ' ' + get_pager_start(pager_cmd,start)
169 if os.name == 'nt':
170 if pager_cmd.startswith('type'):
171 # The default WinXP 'type' command is failing on complex strings.
172 retval = 1
173 else:
174 tmpname = tempfile.mktemp('.txt')
175 tmpfile = file(tmpname,'wt')
176 tmpfile.write(strng)
177 tmpfile.close()
178 cmd = "%s < %s" % (pager_cmd,tmpname)
179 if os.system(cmd):
180 retval = 1
181 else:
182 retval = None
183 os.remove(tmpname)
184 else:
185 try:
186 retval = None
187 # if I use popen4, things hang. No idea why.
188 #pager,shell_out = os.popen4(pager_cmd)
189 pager = os.popen(pager_cmd,'w')
190 pager.write(strng)
191 pager.close()
192 retval = pager.close() # success returns None
193 except IOError,msg: # broken pipe when user quits
194 if msg.args == (32,'Broken pipe'):
195 retval = None
196 else:
197 retval = 1
198 except OSError:
199 # Other strange problems, sometimes seen in Win2k/cygwin
200 retval = 1
201 if retval is not None:
202 page_dumb(strng,screen_lines=screen_lines)
203
204 #----------------------------------------------------------------------------
205 def page_file(fname,start = 0, pager_cmd = None):
206 """Page a file, using an optional pager command and starting line.
207 """
208
209 pager_cmd = get_pager_cmd(pager_cmd)
210 pager_cmd += ' ' + get_pager_start(pager_cmd,start)
211
212 try:
213 if os.environ['TERM'] in ['emacs','dumb']:
214 raise EnvironmentError
215 xsys(pager_cmd + ' ' + fname)
216 except:
217 try:
218 if start > 0:
219 start -= 1
220 page(open(fname).read(),start)
221 except:
222 print 'Unable to show file',`fname`
223
224 #----------------------------------------------------------------------------
225 def get_pager_cmd(pager_cmd = None):
226 """Return a pager command.
227
228 Makes some attempts at finding an OS-correct one."""
229
230 if os.name == 'posix':
231 default_pager_cmd = 'less -r' # -r for color control sequences
232 elif os.name in ['nt','dos']:
233 default_pager_cmd = 'type'
234
235 if pager_cmd is None:
236 try:
237 pager_cmd = os.environ['PAGER']
238 except:
239 pager_cmd = default_pager_cmd
240 return pager_cmd
241
242 #-----------------------------------------------------------------------------
243 def get_pager_start(pager,start):
244 """Return the string for paging files with an offset.
245
246 This is the '+N' argument which less and more (under Unix) accept.
247 """
248
249 if pager in ['less','more']:
250 if start:
251 start_string = '+' + str(start)
252 else:
253 start_string = ''
254 else:
255 start_string = ''
256 return start_string
257
258 #----------------------------------------------------------------------------
259 # (X)emacs on W32 doesn't like to be bypassed with msvcrt.getch()
260 if os.name == 'nt' and os.environ.get('TERM','dumb') != 'emacs':
261 import msvcrt
262 def page_more():
263 """ Smart pausing between pages
264
265 @return: True if need print more lines, False if quit
266 """
267 Term.cout.write('---Return to continue, q to quit--- ')
268 ans = msvcrt.getch()
269 if ans in ("q", "Q"):
270 result = False
271 else:
272 result = True
273 Term.cout.write("\b"*37 + " "*37 + "\b"*37)
274 return result
275 else:
276 def page_more():
277 ans = raw_input('---Return to continue, q to quit--- ')
278 if ans.lower().startswith('q'):
279 return False
280 else:
281 return True
282
283 #----------------------------------------------------------------------------
284 def snip_print(str,width = 75,print_full = 0,header = ''):
285 """Print a string snipping the midsection to fit in width.
286
287 print_full: mode control:
288 - 0: only snip long strings
289 - 1: send to page() directly.
290 - 2: snip long strings and ask for full length viewing with page()
291 Return 1 if snipping was necessary, 0 otherwise."""
292
293 if print_full == 1:
294 page(header+str)
295 return 0
296
297 print header,
298 if len(str) < width:
299 print str
300 snip = 0
301 else:
302 whalf = int((width -5)/2)
303 print str[:whalf] + ' <...> ' + str[-whalf:]
304 snip = 1
305 if snip and print_full == 2:
306 if raw_input(header+' Snipped. View (y/n)? [N]').lower() == 'y':
307 page(str)
308 return snip No newline at end of file
This diff has been collapsed as it changes many lines, (995 lines changed) Show them Hide them
@@ -0,0 +1,995 b''
1 #!/usr/bin/env python
2 # encoding: utf-8
3 """
4 Prefiltering components.
5
6 Prefilters transform user input before it is exec'd by Python. These
7 transforms are used to implement additional syntax such as !ls and %magic.
8
9 Authors:
10
11 * Brian Granger
12 * Fernando Perez
13 * Dan Milstein
14 * Ville Vainio
15 """
16
17 #-----------------------------------------------------------------------------
18 # Copyright (C) 2008-2009 The IPython Development Team
19 #
20 # Distributed under the terms of the BSD License. The full license is in
21 # the file COPYING, distributed as part of this software.
22 #-----------------------------------------------------------------------------
23
24 #-----------------------------------------------------------------------------
25 # Imports
26 #-----------------------------------------------------------------------------
27
28 import __builtin__
29 import codeop
30 import keyword
31 import os
32 import re
33 import sys
34
35 from IPython.core.alias import AliasManager
36 from IPython.core.autocall import IPyAutocall
37 from IPython.core.component import Component
38 from IPython.core.splitinput import split_user_input
39 from IPython.core.page import page
40
41 from IPython.utils.traitlets import List, Int, Any, Str, CBool, Bool
42 from IPython.utils.genutils import make_quoted_expr, Term
43 from IPython.utils.autoattr import auto_attr
44
45 #-----------------------------------------------------------------------------
46 # Global utilities, errors and constants
47 #-----------------------------------------------------------------------------
48
49 # Warning, these cannot be changed unless various regular expressions
50 # are updated in a number of places. Not great, but at least we told you.
51 ESC_SHELL = '!'
52 ESC_SH_CAP = '!!'
53 ESC_HELP = '?'
54 ESC_MAGIC = '%'
55 ESC_QUOTE = ','
56 ESC_QUOTE2 = ';'
57 ESC_PAREN = '/'
58
59
60 class PrefilterError(Exception):
61 pass
62
63
64 # RegExp to identify potential function names
65 re_fun_name = re.compile(r'[a-zA-Z_]([a-zA-Z0-9_.]*) *$')
66
67 # RegExp to exclude strings with this start from autocalling. In
68 # particular, all binary operators should be excluded, so that if foo is
69 # callable, foo OP bar doesn't become foo(OP bar), which is invalid. The
70 # characters '!=()' don't need to be checked for, as the checkPythonChars
71 # routine explicitely does so, to catch direct calls and rebindings of
72 # existing names.
73
74 # Warning: the '-' HAS TO BE AT THE END of the first group, otherwise
75 # it affects the rest of the group in square brackets.
76 re_exclude_auto = re.compile(r'^[,&^\|\*/\+-]'
77 r'|^is |^not |^in |^and |^or ')
78
79 # try to catch also methods for stuff in lists/tuples/dicts: off
80 # (experimental). For this to work, the line_split regexp would need
81 # to be modified so it wouldn't break things at '['. That line is
82 # nasty enough that I shouldn't change it until I can test it _well_.
83 #self.re_fun_name = re.compile (r'[a-zA-Z_]([a-zA-Z0-9_.\[\]]*) ?$')
84
85
86 # Handler Check Utilities
87 def is_shadowed(identifier, ip):
88 """Is the given identifier defined in one of the namespaces which shadow
89 the alias and magic namespaces? Note that an identifier is different
90 than ifun, because it can not contain a '.' character."""
91 # This is much safer than calling ofind, which can change state
92 return (identifier in ip.user_ns \
93 or identifier in ip.internal_ns \
94 or identifier in ip.ns_table['builtin'])
95
96
97 #-----------------------------------------------------------------------------
98 # The LineInfo class used throughout
99 #-----------------------------------------------------------------------------
100
101
102 class LineInfo(object):
103 """A single line of input and associated info.
104
105 Includes the following as properties:
106
107 line
108 The original, raw line
109
110 continue_prompt
111 Is this line a continuation in a sequence of multiline input?
112
113 pre
114 The initial esc character or whitespace.
115
116 pre_char
117 The escape character(s) in pre or the empty string if there isn't one.
118 Note that '!!' is a possible value for pre_char. Otherwise it will
119 always be a single character.
120
121 pre_whitespace
122 The leading whitespace from pre if it exists. If there is a pre_char,
123 this is just ''.
124
125 ifun
126 The 'function part', which is basically the maximal initial sequence
127 of valid python identifiers and the '.' character. This is what is
128 checked for alias and magic transformations, used for auto-calling,
129 etc.
130
131 the_rest
132 Everything else on the line.
133 """
134 def __init__(self, line, continue_prompt):
135 self.line = line
136 self.continue_prompt = continue_prompt
137 self.pre, self.ifun, self.the_rest = split_user_input(line)
138
139 self.pre_char = self.pre.strip()
140 if self.pre_char:
141 self.pre_whitespace = '' # No whitespace allowd before esc chars
142 else:
143 self.pre_whitespace = self.pre
144
145 self._oinfo = None
146
147 def ofind(self, ip):
148 """Do a full, attribute-walking lookup of the ifun in the various
149 namespaces for the given IPython InteractiveShell instance.
150
151 Return a dict with keys: found,obj,ospace,ismagic
152
153 Note: can cause state changes because of calling getattr, but should
154 only be run if autocall is on and if the line hasn't matched any
155 other, less dangerous handlers.
156
157 Does cache the results of the call, so can be called multiple times
158 without worrying about *further* damaging state.
159 """
160 if not self._oinfo:
161 self._oinfo = ip._ofind(self.ifun)
162 return self._oinfo
163
164 def __str__(self):
165 return "Lineinfo [%s|%s|%s]" %(self.pre,self.ifun,self.the_rest)
166
167
168 #-----------------------------------------------------------------------------
169 # Main Prefilter manager
170 #-----------------------------------------------------------------------------
171
172
173 class PrefilterManager(Component):
174 """Main prefilter component.
175
176 The IPython prefilter is run on all user input before it is run. The
177 prefilter consumes lines of input and produces transformed lines of
178 input.
179
180 The iplementation consists of two phases:
181
182 1. Transformers
183 2. Checkers and handlers
184
185 Over time, we plan on deprecating the checkers and handlers and doing
186 everything in the transformers.
187
188 The transformers are instances of :class:`PrefilterTransformer` and have
189 a single method :meth:`transform` that takes a line and returns a
190 transformed line. The transformation can be accomplished using any
191 tool, but our current ones use regular expressions for speed. We also
192 ship :mod:`pyparsing` in :mod:`IPython.external` for use in transformers.
193
194 After all the transformers have been run, the line is fed to the checkers,
195 which are instances of :class:`PrefilterChecker`. The line is passed to
196 the :meth:`check` method, which either returns `None` or a
197 :class:`PrefilterHandler` instance. If `None` is returned, the other
198 checkers are tried. If an :class:`PrefilterHandler` instance is returned,
199 the line is passed to the :meth:`handle` method of the returned
200 handler and no further checkers are tried.
201
202 Both transformers and checkers have a `priority` attribute, that determines
203 the order in which they are called. Smaller priorities are tried first.
204
205 Both transformers and checkers also have `enabled` attribute, which is
206 a boolean that determines if the instance is used.
207
208 Users or developers can change the priority or enabled attribute of
209 transformers or checkers, but they must call the :meth:`sort_checkers`
210 or :meth:`sort_transformers` method after changing the priority.
211 """
212
213 multi_line_specials = CBool(True, config=True)
214
215 def __init__(self, parent, config=None):
216 super(PrefilterManager, self).__init__(parent, config=config)
217 self.init_transformers()
218 self.init_handlers()
219 self.init_checkers()
220
221 @auto_attr
222 def shell(self):
223 return Component.get_instances(
224 root=self.root,
225 klass='IPython.core.iplib.InteractiveShell')[0]
226
227 #-------------------------------------------------------------------------
228 # API for managing transformers
229 #-------------------------------------------------------------------------
230
231 def init_transformers(self):
232 """Create the default transformers."""
233 self._transformers = []
234 for transformer_cls in _default_transformers:
235 transformer_cls(self, config=self.config)
236
237 def sort_transformers(self):
238 """Sort the transformers by priority.
239
240 This must be called after the priority of a transformer is changed.
241 The :meth:`register_transformer` method calls this automatically.
242 """
243 self._transformers.sort(cmp=lambda x,y: x.priority-y.priority)
244
245 @property
246 def transformers(self):
247 """Return a list of checkers, sorted by priority."""
248 return self._transformers
249
250 def register_transformer(self, transformer):
251 """Register a transformer instance."""
252 if transformer not in self._transformers:
253 self._transformers.append(transformer)
254 self.sort_transformers()
255
256 def unregister_transformer(self, transformer):
257 """Unregister a transformer instance."""
258 if transformer in self._transformers:
259 self._transformers.remove(transformer)
260
261 #-------------------------------------------------------------------------
262 # API for managing checkers
263 #-------------------------------------------------------------------------
264
265 def init_checkers(self):
266 """Create the default checkers."""
267 self._checkers = []
268 for checker in _default_checkers:
269 checker(self, config=self.config)
270
271 def sort_checkers(self):
272 """Sort the checkers by priority.
273
274 This must be called after the priority of a checker is changed.
275 The :meth:`register_checker` method calls this automatically.
276 """
277 self._checkers.sort(cmp=lambda x,y: x.priority-y.priority)
278
279 @property
280 def checkers(self):
281 """Return a list of checkers, sorted by priority."""
282 return self._checkers
283
284 def register_checker(self, checker):
285 """Register a checker instance."""
286 if checker not in self._checkers:
287 self._checkers.append(checker)
288 self.sort_checkers()
289
290 def unregister_checker(self, checker):
291 """Unregister a checker instance."""
292 if checker in self._checkers:
293 self._checkers.remove(checker)
294
295 #-------------------------------------------------------------------------
296 # API for managing checkers
297 #-------------------------------------------------------------------------
298
299 def init_handlers(self):
300 """Create the default handlers."""
301 self._handlers = {}
302 self._esc_handlers = {}
303 for handler in _default_handlers:
304 handler(self, config=self.config)
305
306 @property
307 def handlers(self):
308 """Return a dict of all the handlers."""
309 return self._handlers
310
311 def register_handler(self, name, handler, esc_strings):
312 """Register a handler instance by name with esc_strings."""
313 self._handlers[name] = handler
314 for esc_str in esc_strings:
315 self._esc_handlers[esc_str] = handler
316
317 def unregister_handler(self, name, handler, esc_strings):
318 """Unregister a handler instance by name with esc_strings."""
319 try:
320 del self._handlers[name]
321 except KeyError:
322 pass
323 for esc_str in esc_strings:
324 h = self._esc_handlers.get(esc_str)
325 if h is handler:
326 del self._esc_handlers[esc_str]
327
328 def get_handler_by_name(self, name):
329 """Get a handler by its name."""
330 return self._handlers.get(name)
331
332 def get_handler_by_esc(self, esc_str):
333 """Get a handler by its escape string."""
334 return self._esc_handlers.get(esc_str)
335
336 #-------------------------------------------------------------------------
337 # Main prefiltering API
338 #-------------------------------------------------------------------------
339
340 def prefilter_line_info(self, line_info):
341 """Prefilter a line that has been converted to a LineInfo object.
342
343 This implements the checker/handler part of the prefilter pipe.
344 """
345 # print "prefilter_line_info: ", line_info
346 handler = self.find_handler(line_info)
347 return handler.handle(line_info)
348
349 def find_handler(self, line_info):
350 """Find a handler for the line_info by trying checkers."""
351 for checker in self.checkers:
352 if checker.enabled:
353 handler = checker.check(line_info)
354 if handler:
355 return handler
356 return self.get_handler_by_name('normal')
357
358 def transform_line(self, line, continue_prompt):
359 """Calls the enabled transformers in order of increasing priority."""
360 for transformer in self.transformers:
361 if transformer.enabled:
362 line = transformer.transform(line, continue_prompt)
363 return line
364
365 def prefilter_line(self, line, continue_prompt):
366 """Prefilter a single input line as text.
367
368 This method prefilters a single line of text by calling the
369 transformers and then the checkers/handlers.
370 """
371
372 # print "prefilter_line: ", line, continue_prompt
373 # All handlers *must* return a value, even if it's blank ('').
374
375 # Lines are NOT logged here. Handlers should process the line as
376 # needed, update the cache AND log it (so that the input cache array
377 # stays synced).
378
379 # save the line away in case we crash, so the post-mortem handler can
380 # record it
381 self.shell._last_input_line = line
382
383 if not line:
384 # Return immediately on purely empty lines, so that if the user
385 # previously typed some whitespace that started a continuation
386 # prompt, he can break out of that loop with just an empty line.
387 # This is how the default python prompt works.
388
389 # Only return if the accumulated input buffer was just whitespace!
390 if ''.join(self.shell.buffer).isspace():
391 self.shell.buffer[:] = []
392 return ''
393
394 # At this point, we invoke our transformers.
395 if not continue_prompt or (continue_prompt and self.multi_line_specials):
396 line = self.transform_line(line, continue_prompt)
397
398 # Now we compute line_info for the checkers and handlers
399 line_info = LineInfo(line, continue_prompt)
400
401 # the input history needs to track even empty lines
402 stripped = line.strip()
403
404 normal_handler = self.get_handler_by_name('normal')
405 if not stripped:
406 if not continue_prompt:
407 self.shell.outputcache.prompt_count -= 1
408
409 return normal_handler.handle(line_info)
410
411 # special handlers are only allowed for single line statements
412 if continue_prompt and not self.multi_line_specials:
413 return normal_handler.handle(line_info)
414
415 prefiltered = self.prefilter_line_info(line_info)
416 # print "prefiltered line: %r" % prefiltered
417 return prefiltered
418
419 def prefilter_lines(self, lines, continue_prompt):
420 """Prefilter multiple input lines of text.
421
422 This is the main entry point for prefiltering multiple lines of
423 input. This simply calls :meth:`prefilter_line` for each line of
424 input.
425
426 This covers cases where there are multiple lines in the user entry,
427 which is the case when the user goes back to a multiline history
428 entry and presses enter.
429 """
430 out = []
431 for line in lines.rstrip('\n').split('\n'):
432 out.append(self.prefilter_line(line, continue_prompt))
433 return '\n'.join(out)
434
435
436 #-----------------------------------------------------------------------------
437 # Prefilter transformers
438 #-----------------------------------------------------------------------------
439
440
441 class PrefilterTransformer(Component):
442 """Transform a line of user input."""
443
444 priority = Int(100, config=True)
445 shell = Any
446 prefilter_manager = Any
447 enabled = Bool(True, config=True)
448
449 def __init__(self, parent, config=None):
450 super(PrefilterTransformer, self).__init__(parent, config=config)
451 self.prefilter_manager.register_transformer(self)
452
453 @auto_attr
454 def shell(self):
455 return Component.get_instances(
456 root=self.root,
457 klass='IPython.core.iplib.InteractiveShell')[0]
458
459 @auto_attr
460 def prefilter_manager(self):
461 return PrefilterManager.get_instances(root=self.root)[0]
462
463 def transform(self, line, continue_prompt):
464 """Transform a line, returning the new one."""
465 return None
466
467 def __repr__(self):
468 return "<%s(priority=%r, enabled=%r)>" % (
469 self.__class__.__name__, self.priority, self.enabled)
470
471
472 _assign_system_re = re.compile(r'(?P<lhs>(\s*)([\w\.]+)((\s*,\s*[\w\.]+)*))'
473 r'\s*=\s*!(?P<cmd>.*)')
474
475
476 class AssignSystemTransformer(PrefilterTransformer):
477 """Handle the `files = !ls` syntax."""
478
479 priority = Int(100, config=True)
480
481 def transform(self, line, continue_prompt):
482 m = _assign_system_re.match(line)
483 if m is not None:
484 cmd = m.group('cmd')
485 lhs = m.group('lhs')
486 expr = make_quoted_expr("sc -l =%s" % cmd)
487 new_line = '%s = get_ipython().magic(%s)' % (lhs, expr)
488 return new_line
489 return line
490
491
492 _assign_magic_re = re.compile(r'(?P<lhs>(\s*)([\w\.]+)((\s*,\s*[\w\.]+)*))'
493 r'\s*=\s*%(?P<cmd>.*)')
494
495 class AssignMagicTransformer(PrefilterTransformer):
496 """Handle the `a = %who` syntax."""
497
498 priority = Int(200, config=True)
499
500 def transform(self, line, continue_prompt):
501 m = _assign_magic_re.match(line)
502 if m is not None:
503 cmd = m.group('cmd')
504 lhs = m.group('lhs')
505 expr = make_quoted_expr(cmd)
506 new_line = '%s = get_ipython().magic(%s)' % (lhs, expr)
507 return new_line
508 return line
509
510
511 #-----------------------------------------------------------------------------
512 # Prefilter checkers
513 #-----------------------------------------------------------------------------
514
515
516 class PrefilterChecker(Component):
517 """Inspect an input line and return a handler for that line."""
518
519 priority = Int(100, config=True)
520 shell = Any
521 prefilter_manager = Any
522 enabled = Bool(True, config=True)
523
524 def __init__(self, parent, config=None):
525 super(PrefilterChecker, self).__init__(parent, config=config)
526 self.prefilter_manager.register_checker(self)
527
528 @auto_attr
529 def shell(self):
530 return Component.get_instances(
531 root=self.root,
532 klass='IPython.core.iplib.InteractiveShell')[0]
533
534 @auto_attr
535 def prefilter_manager(self):
536 return PrefilterManager.get_instances(root=self.root)[0]
537
538 def check(self, line_info):
539 """Inspect line_info and return a handler instance or None."""
540 return None
541
542 def __repr__(self):
543 return "<%s(priority=%r, enabled=%r)>" % (
544 self.__class__.__name__, self.priority, self.enabled)
545
546
547 class EmacsChecker(PrefilterChecker):
548
549 priority = Int(100, config=True)
550 enabled = Bool(False, config=True)
551
552 def check(self, line_info):
553 "Emacs ipython-mode tags certain input lines."
554 if line_info.line.endswith('# PYTHON-MODE'):
555 return self.prefilter_manager.get_handler_by_name('emacs')
556 else:
557 return None
558
559
560 class ShellEscapeChecker(PrefilterChecker):
561
562 priority = Int(200, config=True)
563
564 def check(self, line_info):
565 if line_info.line.lstrip().startswith(ESC_SHELL):
566 return self.prefilter_manager.get_handler_by_name('shell')
567
568
569 class IPyAutocallChecker(PrefilterChecker):
570
571 priority = Int(300, config=True)
572
573 def check(self, line_info):
574 "Instances of IPyAutocall in user_ns get autocalled immediately"
575 obj = self.shell.user_ns.get(line_info.ifun, None)
576 if isinstance(obj, IPyAutocall):
577 obj.set_ip(self.shell)
578 return self.prefilter_manager.get_handler_by_name('auto')
579 else:
580 return None
581
582
583 class MultiLineMagicChecker(PrefilterChecker):
584
585 priority = Int(400, config=True)
586
587 def check(self, line_info):
588 "Allow ! and !! in multi-line statements if multi_line_specials is on"
589 # Note that this one of the only places we check the first character of
590 # ifun and *not* the pre_char. Also note that the below test matches
591 # both ! and !!.
592 if line_info.continue_prompt \
593 and self.prefilter_manager.multi_line_specials:
594 if line_info.ifun.startswith(ESC_MAGIC):
595 return self.prefilter_manager.get_handler_by_name('magic')
596 else:
597 return None
598
599
600 class EscCharsChecker(PrefilterChecker):
601
602 priority = Int(500, config=True)
603
604 def check(self, line_info):
605 """Check for escape character and return either a handler to handle it,
606 or None if there is no escape char."""
607 if line_info.line[-1] == ESC_HELP \
608 and line_info.pre_char != ESC_SHELL \
609 and line_info.pre_char != ESC_SH_CAP:
610 # the ? can be at the end, but *not* for either kind of shell escape,
611 # because a ? can be a vaild final char in a shell cmd
612 return self.prefilter_manager.get_handler_by_name('help')
613 else:
614 # This returns None like it should if no handler exists
615 return self.prefilter_manager.get_handler_by_esc(line_info.pre_char)
616
617
618 class AssignmentChecker(PrefilterChecker):
619
620 priority = Int(600, config=True)
621
622 def check(self, line_info):
623 """Check to see if user is assigning to a var for the first time, in
624 which case we want to avoid any sort of automagic / autocall games.
625
626 This allows users to assign to either alias or magic names true python
627 variables (the magic/alias systems always take second seat to true
628 python code). E.g. ls='hi', or ls,that=1,2"""
629 if line_info.the_rest:
630 if line_info.the_rest[0] in '=,':
631 return self.prefilter_manager.get_handler_by_name('normal')
632 else:
633 return None
634
635
636 class AutoMagicChecker(PrefilterChecker):
637
638 priority = Int(700, config=True)
639
640 def check(self, line_info):
641 """If the ifun is magic, and automagic is on, run it. Note: normal,
642 non-auto magic would already have been triggered via '%' in
643 check_esc_chars. This just checks for automagic. Also, before
644 triggering the magic handler, make sure that there is nothing in the
645 user namespace which could shadow it."""
646 if not self.shell.automagic or not hasattr(self.shell,'magic_'+line_info.ifun):
647 return None
648
649 # We have a likely magic method. Make sure we should actually call it.
650 if line_info.continue_prompt and not self.shell.multi_line_specials:
651 return None
652
653 head = line_info.ifun.split('.',1)[0]
654 if is_shadowed(head, self.shell):
655 return None
656
657 return self.prefilter_manager.get_handler_by_name('magic')
658
659
660 class AliasChecker(PrefilterChecker):
661
662 priority = Int(800, config=True)
663
664 @auto_attr
665 def alias_manager(self):
666 return AliasManager.get_instances(root=self.root)[0]
667
668 def check(self, line_info):
669 "Check if the initital identifier on the line is an alias."
670 # Note: aliases can not contain '.'
671 head = line_info.ifun.split('.',1)[0]
672 if line_info.ifun not in self.alias_manager \
673 or head not in self.alias_manager \
674 or is_shadowed(head, self.shell):
675 return None
676
677 return self.prefilter_manager.get_handler_by_name('alias')
678
679
680 class PythonOpsChecker(PrefilterChecker):
681
682 priority = Int(900, config=True)
683
684 def check(self, line_info):
685 """If the 'rest' of the line begins with a function call or pretty much
686 any python operator, we should simply execute the line (regardless of
687 whether or not there's a possible autocall expansion). This avoids
688 spurious (and very confusing) geattr() accesses."""
689 if line_info.the_rest and line_info.the_rest[0] in '!=()<>,+*/%^&|':
690 return self.prefilter_manager.get_handler_by_name('normal')
691 else:
692 return None
693
694
695 class AutocallChecker(PrefilterChecker):
696
697 priority = Int(1000, config=True)
698
699 def check(self, line_info):
700 "Check if the initial word/function is callable and autocall is on."
701 if not self.shell.autocall:
702 return None
703
704 oinfo = line_info.ofind(self.shell) # This can mutate state via getattr
705 if not oinfo['found']:
706 return None
707
708 if callable(oinfo['obj']) \
709 and (not re_exclude_auto.match(line_info.the_rest)) \
710 and re_fun_name.match(line_info.ifun):
711 return self.prefilter_manager.get_handler_by_name('auto')
712 else:
713 return None
714
715
716 #-----------------------------------------------------------------------------
717 # Prefilter handlers
718 #-----------------------------------------------------------------------------
719
720
721 class PrefilterHandler(Component):
722
723 handler_name = Str('normal')
724 esc_strings = List([])
725 shell = Any
726 prefilter_manager = Any
727
728 def __init__(self, parent, config=None):
729 super(PrefilterHandler, self).__init__(parent, config=config)
730 self.prefilter_manager.register_handler(
731 self.handler_name,
732 self,
733 self.esc_strings
734 )
735
736 @auto_attr
737 def shell(self):
738 return Component.get_instances(
739 root=self.root,
740 klass='IPython.core.iplib.InteractiveShell')[0]
741
742 @auto_attr
743 def prefilter_manager(self):
744 return PrefilterManager.get_instances(root=self.root)[0]
745
746 def handle(self, line_info):
747 # print "normal: ", line_info
748 """Handle normal input lines. Use as a template for handlers."""
749
750 # With autoindent on, we need some way to exit the input loop, and I
751 # don't want to force the user to have to backspace all the way to
752 # clear the line. The rule will be in this case, that either two
753 # lines of pure whitespace in a row, or a line of pure whitespace but
754 # of a size different to the indent level, will exit the input loop.
755 line = line_info.line
756 continue_prompt = line_info.continue_prompt
757
758 if (continue_prompt and self.shell.autoindent and line.isspace() and
759 (0 < abs(len(line) - self.shell.indent_current_nsp) <= 2 or
760 (self.shell.buffer[-1]).isspace() )):
761 line = ''
762
763 self.shell.log(line, line, continue_prompt)
764 return line
765
766 def __str__(self):
767 return "<%s(name=%s)>" % (self.__class__.__name__, self.handler_name)
768
769
770 class AliasHandler(PrefilterHandler):
771
772 handler_name = Str('alias')
773
774 @auto_attr
775 def alias_manager(self):
776 return AliasManager.get_instances(root=self.root)[0]
777
778 def handle(self, line_info):
779 """Handle alias input lines. """
780 transformed = self.alias_manager.expand_aliases(line_info.ifun,line_info.the_rest)
781 # pre is needed, because it carries the leading whitespace. Otherwise
782 # aliases won't work in indented sections.
783 line_out = '%sget_ipython().system(%s)' % (line_info.pre_whitespace,
784 make_quoted_expr(transformed))
785
786 self.shell.log(line_info.line, line_out, line_info.continue_prompt)
787 return line_out
788
789
790 class ShellEscapeHandler(PrefilterHandler):
791
792 handler_name = Str('shell')
793 esc_strings = List([ESC_SHELL, ESC_SH_CAP])
794
795 def handle(self, line_info):
796 """Execute the line in a shell, empty return value"""
797 magic_handler = self.prefilter_manager.get_handler_by_name('magic')
798
799 line = line_info.line
800 if line.lstrip().startswith(ESC_SH_CAP):
801 # rewrite LineInfo's line, ifun and the_rest to properly hold the
802 # call to %sx and the actual command to be executed, so
803 # handle_magic can work correctly. Note that this works even if
804 # the line is indented, so it handles multi_line_specials
805 # properly.
806 new_rest = line.lstrip()[2:]
807 line_info.line = '%ssx %s' % (ESC_MAGIC, new_rest)
808 line_info.ifun = 'sx'
809 line_info.the_rest = new_rest
810 return magic_handler.handle(line_info)
811 else:
812 cmd = line.lstrip().lstrip(ESC_SHELL)
813 line_out = '%sget_ipython().system(%s)' % (line_info.pre_whitespace,
814 make_quoted_expr(cmd))
815 # update cache/log and return
816 self.shell.log(line, line_out, line_info.continue_prompt)
817 return line_out
818
819
820 class MagicHandler(PrefilterHandler):
821
822 handler_name = Str('magic')
823 esc_strings = List([ESC_MAGIC])
824
825 def handle(self, line_info):
826 """Execute magic functions."""
827 ifun = line_info.ifun
828 the_rest = line_info.the_rest
829 cmd = '%sget_ipython().magic(%s)' % (line_info.pre_whitespace,
830 make_quoted_expr(ifun + " " + the_rest))
831 self.shell.log(line_info.line, cmd, line_info.continue_prompt)
832 return cmd
833
834
835 class AutoHandler(PrefilterHandler):
836
837 handler_name = Str('auto')
838 esc_strings = List([ESC_PAREN, ESC_QUOTE, ESC_QUOTE2])
839
840 def handle(self, line_info):
841 """Hande lines which can be auto-executed, quoting if requested."""
842 line = line_info.line
843 ifun = line_info.ifun
844 the_rest = line_info.the_rest
845 pre = line_info.pre
846 continue_prompt = line_info.continue_prompt
847 obj = line_info.ofind(self)['obj']
848
849 #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun,the_rest) # dbg
850
851 # This should only be active for single-line input!
852 if continue_prompt:
853 self.log(line,line,continue_prompt)
854 return line
855
856 force_auto = isinstance(obj, IPyAutocall)
857 auto_rewrite = True
858
859 if pre == ESC_QUOTE:
860 # Auto-quote splitting on whitespace
861 newcmd = '%s("%s")' % (ifun,'", "'.join(the_rest.split()) )
862 elif pre == ESC_QUOTE2:
863 # Auto-quote whole string
864 newcmd = '%s("%s")' % (ifun,the_rest)
865 elif pre == ESC_PAREN:
866 newcmd = '%s(%s)' % (ifun,",".join(the_rest.split()))
867 else:
868 # Auto-paren.
869 # We only apply it to argument-less calls if the autocall
870 # parameter is set to 2. We only need to check that autocall is <
871 # 2, since this function isn't called unless it's at least 1.
872 if not the_rest and (self.shell.autocall < 2) and not force_auto:
873 newcmd = '%s %s' % (ifun,the_rest)
874 auto_rewrite = False
875 else:
876 if not force_auto and the_rest.startswith('['):
877 if hasattr(obj,'__getitem__'):
878 # Don't autocall in this case: item access for an object
879 # which is BOTH callable and implements __getitem__.
880 newcmd = '%s %s' % (ifun,the_rest)
881 auto_rewrite = False
882 else:
883 # if the object doesn't support [] access, go ahead and
884 # autocall
885 newcmd = '%s(%s)' % (ifun.rstrip(),the_rest)
886 elif the_rest.endswith(';'):
887 newcmd = '%s(%s);' % (ifun.rstrip(),the_rest[:-1])
888 else:
889 newcmd = '%s(%s)' % (ifun.rstrip(), the_rest)
890
891 if auto_rewrite:
892 rw = self.shell.outputcache.prompt1.auto_rewrite() + newcmd
893
894 try:
895 # plain ascii works better w/ pyreadline, on some machines, so
896 # we use it and only print uncolored rewrite if we have unicode
897 rw = str(rw)
898 print >>Term.cout, rw
899 except UnicodeEncodeError:
900 print "-------------->" + newcmd
901
902 # log what is now valid Python, not the actual user input (without the
903 # final newline)
904 self.shell.log(line,newcmd,continue_prompt)
905 return newcmd
906
907
908 class HelpHandler(PrefilterHandler):
909
910 handler_name = Str('help')
911 esc_strings = List([ESC_HELP])
912
913 def handle(self, line_info):
914 """Try to get some help for the object.
915
916 obj? or ?obj -> basic information.
917 obj?? or ??obj -> more details.
918 """
919 normal_handler = self.prefilter_manager.get_handler_by_name('normal')
920 line = line_info.line
921 # We need to make sure that we don't process lines which would be
922 # otherwise valid python, such as "x=1 # what?"
923 try:
924 codeop.compile_command(line)
925 except SyntaxError:
926 # We should only handle as help stuff which is NOT valid syntax
927 if line[0]==ESC_HELP:
928 line = line[1:]
929 elif line[-1]==ESC_HELP:
930 line = line[:-1]
931 self.shell.log(line, '#?'+line, line_info.continue_prompt)
932 if line:
933 #print 'line:<%r>' % line # dbg
934 self.shell.magic_pinfo(line)
935 else:
936 page(self.shell.usage, screen_lines=self.shell.usable_screen_length)
937 return '' # Empty string is needed here!
938 except:
939 raise
940 # Pass any other exceptions through to the normal handler
941 return normal_handler.handle(line_info)
942 else:
943 raise
944 # If the code compiles ok, we should handle it normally
945 return normal_handler.handle(line_info)
946
947
948 class EmacsHandler(PrefilterHandler):
949
950 handler_name = Str('emacs')
951 esc_strings = List([])
952
953 def handle(self, line_info):
954 """Handle input lines marked by python-mode."""
955
956 # Currently, nothing is done. Later more functionality can be added
957 # here if needed.
958
959 # The input cache shouldn't be updated
960 return line_info.line
961
962
963 #-----------------------------------------------------------------------------
964 # Defaults
965 #-----------------------------------------------------------------------------
966
967
968 _default_transformers = [
969 AssignSystemTransformer,
970 AssignMagicTransformer
971 ]
972
973 _default_checkers = [
974 EmacsChecker,
975 ShellEscapeChecker,
976 IPyAutocallChecker,
977 MultiLineMagicChecker,
978 EscCharsChecker,
979 AssignmentChecker,
980 AutoMagicChecker,
981 AliasChecker,
982 PythonOpsChecker,
983 AutocallChecker
984 ]
985
986 _default_handlers = [
987 PrefilterHandler,
988 AliasHandler,
989 ShellEscapeHandler,
990 MagicHandler,
991 AutoHandler,
992 HelpHandler,
993 EmacsHandler
994 ]
995
@@ -0,0 +1,38 b''
1 #!/usr/bin/env python
2 # encoding: utf-8
3 """
4 A simple class for quitting IPython.
5
6 Authors:
7
8 * Brian Granger
9 """
10
11 #-----------------------------------------------------------------------------
12 # Copyright (C) 2008-2009 The IPython Development Team
13 #
14 # Distributed under the terms of the BSD License. The full license is in
15 # the file COPYING, distributed as part of this software.
16 #-----------------------------------------------------------------------------
17
18 #-----------------------------------------------------------------------------
19 # Imports
20 #-----------------------------------------------------------------------------
21
22
23 class Quitter(object):
24 """Simple class to handle exit, similar to Python 2.5's.
25
26 It handles exiting in an ipython-safe manner, which the one in Python 2.5
27 doesn't do (obviously, since it doesn't know about ipython)."""
28
29 def __init__(self, shell, name):
30 self.shell = shell
31 self.name = name
32
33 def __repr__(self):
34 return 'Type %s() to exit.' % self.name
35 __str__ = __repr__
36
37 def __call__(self):
38 self.shell.exit() No newline at end of file
@@ -0,0 +1,83 b''
1 #!/usr/bin/env python
2 # encoding: utf-8
3 """
4 Simple utility for splitting user input.
5
6 Authors:
7
8 * Brian Granger
9 * Fernando Perez
10 """
11
12 #-----------------------------------------------------------------------------
13 # Copyright (C) 2008-2009 The IPython Development Team
14 #
15 # Distributed under the terms of the BSD License. The full license is in
16 # the file COPYING, distributed as part of this software.
17 #-----------------------------------------------------------------------------
18
19 #-----------------------------------------------------------------------------
20 # Imports
21 #-----------------------------------------------------------------------------
22
23 import re
24
25 #-----------------------------------------------------------------------------
26 # Main function
27 #-----------------------------------------------------------------------------
28
29
30 # RegExp for splitting line contents into pre-char//first word-method//rest.
31 # For clarity, each group in on one line.
32
33 # WARNING: update the regexp if the escapes in iplib are changed, as they
34 # are hardwired in.
35
36 # Although it's not solely driven by the regex, note that:
37 # ,;/% only trigger if they are the first character on the line
38 # ! and !! trigger if they are first char(s) *or* follow an indent
39 # ? triggers as first or last char.
40
41 # The three parts of the regex are:
42 # 1) pre: pre_char *or* initial whitespace
43 # 2) ifun: first word/method (mix of \w and '.')
44 # 3) the_rest: rest of line (separated from ifun by space if non-empty)
45 line_split = re.compile(r'^([,;/%?]|!!?|\s*)'
46 r'\s*([\w\.]+)'
47 r'(\s+.*$|$)')
48
49 # r'[\w\.]+'
50 # r'\s*=\s*%.*'
51
52 def split_user_input(line, pattern=None):
53 """Split user input into pre-char/whitespace, function part and rest.
54
55 This is currently handles lines with '=' in them in a very inconsistent
56 manner.
57 """
58
59 if pattern is None:
60 pattern = line_split
61 match = pattern.match(line)
62 if not match:
63 # print "match failed for line '%s'" % line
64 try:
65 ifun, the_rest = line.split(None,1)
66 except ValueError:
67 # print "split failed for line '%s'" % line
68 ifun, the_rest = line,''
69 pre = re.match('^(\s*)(.*)',line).groups()[0]
70 else:
71 pre,ifun,the_rest = match.groups()
72
73 # ifun has to be a valid python identifier, so it better be only pure
74 # ascii, no unicode:
75 try:
76 ifun = ifun.encode('ascii')
77 except UnicodeEncodeError:
78 the_rest = ifun + u' ' + the_rest
79 ifun = u''
80
81 #print 'line:<%s>' % line # dbg
82 #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun.strip(),the_rest) # dbg
83 return pre, ifun.strip(), the_rest.lstrip()
@@ -0,0 +1,214 b''
1 #!/usr/bin/env python
2 # encoding: utf-8
3 """
4 Tests for IPython.core.component
5
6 Authors:
7
8 * Brian Granger
9 * Fernando Perez (design help)
10 """
11
12 #-----------------------------------------------------------------------------
13 # Copyright (C) 2008-2009 The IPython Development Team
14 #
15 # Distributed under the terms of the BSD License. The full license is in
16 # the file COPYING, distributed as part of this software.
17 #-----------------------------------------------------------------------------
18
19 #-----------------------------------------------------------------------------
20 # Imports
21 #-----------------------------------------------------------------------------
22
23 from unittest import TestCase
24
25 from IPython.core.component import Component, ComponentError
26 from IPython.utils.traitlets import (
27 TraitletError, Int, Float, Str
28 )
29 from IPython.config.loader import Config
30
31
32 #-----------------------------------------------------------------------------
33 # Test cases
34 #-----------------------------------------------------------------------------
35
36
37 class TestComponentMeta(TestCase):
38
39 def test_get_instances(self):
40 class BaseComponent(Component):
41 pass
42 c1 = BaseComponent(None)
43 c2 = BaseComponent(c1)
44 self.assertEquals(BaseComponent.get_instances(),[c1,c2])
45
46 def test_get_instances_subclass(self):
47 class MyComponent(Component):
48 pass
49 class MyOtherComponent(MyComponent):
50 pass
51 c1 = MyComponent(None)
52 c2 = MyOtherComponent(c1)
53 c3 = MyOtherComponent(c2)
54 self.assertEquals(MyComponent.get_instances(), [c1, c2, c3])
55 self.assertEquals(MyOtherComponent.get_instances(), [c2, c3])
56
57 def test_get_instances_root(self):
58 class MyComponent(Component):
59 pass
60 class MyOtherComponent(MyComponent):
61 pass
62 c1 = MyComponent(None)
63 c2 = MyOtherComponent(c1)
64 c3 = MyOtherComponent(c2)
65 c4 = MyComponent(None)
66 c5 = MyComponent(c4)
67 self.assertEquals(MyComponent.get_instances(root=c1), [c1, c2, c3])
68 self.assertEquals(MyComponent.get_instances(root=c4), [c4, c5])
69
70
71 class TestComponent(TestCase):
72
73 def test_parent_child(self):
74 c1 = Component(None)
75 c2 = Component(c1)
76 c3 = Component(c1)
77 c4 = Component(c3)
78 self.assertEquals(c1.parent, None)
79 self.assertEquals(c2.parent, c1)
80 self.assertEquals(c3.parent, c1)
81 self.assertEquals(c4.parent, c3)
82 self.assertEquals(c1.children, [c2, c3])
83 self.assertEquals(c2.children, [])
84 self.assertEquals(c3.children, [c4])
85 self.assertEquals(c4.children, [])
86
87 def test_root(self):
88 c1 = Component(None)
89 c2 = Component(c1)
90 c3 = Component(c1)
91 c4 = Component(c3)
92 self.assertEquals(c1.root, c1.root)
93 self.assertEquals(c2.root, c1)
94 self.assertEquals(c3.root, c1)
95 self.assertEquals(c4.root, c1)
96
97 def test_change_parent(self):
98 c1 = Component(None)
99 c2 = Component(None)
100 c3 = Component(c1)
101 self.assertEquals(c3.root, c1)
102 self.assertEquals(c3.parent, c1)
103 self.assertEquals(c1.children,[c3])
104 c3.parent = c2
105 self.assertEquals(c3.root, c2)
106 self.assertEquals(c3.parent, c2)
107 self.assertEquals(c2.children,[c3])
108 self.assertEquals(c1.children,[])
109
110 def test_subclass_parent(self):
111 c1 = Component(None)
112 self.assertRaises(TraitletError, setattr, c1, 'parent', 10)
113
114 class MyComponent(Component):
115 pass
116 c1 = Component(None)
117 c2 = MyComponent(c1)
118 self.assertEquals(MyComponent.parent.this_class, Component)
119 self.assertEquals(c2.parent, c1)
120
121 def test_bad_root(self):
122 c1 = Component(None)
123 c2 = Component(None)
124 c3 = Component(None)
125 self.assertRaises(ComponentError, setattr, c1, 'root', c2)
126 c1.parent = c2
127 self.assertEquals(c1.root, c2)
128 self.assertRaises(ComponentError, setattr, c1, 'root', c3)
129
130
131 class TestComponentConfig(TestCase):
132
133 def test_default(self):
134 c1 = Component(None)
135 c2 = Component(c1)
136 c3 = Component(c2)
137 self.assertEquals(c1.config, c2.config)
138 self.assertEquals(c2.config, c3.config)
139
140 def test_custom(self):
141 config = Config()
142 config.foo = 'foo'
143 config.bar = 'bar'
144 c1 = Component(None, config=config)
145 c2 = Component(c1)
146 c3 = Component(c2)
147 self.assertEquals(c1.config, config)
148 self.assertEquals(c2.config, config)
149 self.assertEquals(c3.config, config)
150 # Test that copies are not made
151 self.assert_(c1.config is config)
152 self.assert_(c2.config is config)
153 self.assert_(c3.config is config)
154 self.assert_(c1.config is c2.config)
155 self.assert_(c2.config is c3.config)
156
157 def test_inheritance(self):
158 class MyComponent(Component):
159 a = Int(1, config=True)
160 b = Float(1.0, config=True)
161 c = Str('no config')
162 config = Config()
163 config.MyComponent.a = 2
164 config.MyComponent.b = 2.0
165 c1 = MyComponent(None, config=config)
166 c2 = MyComponent(c1)
167 self.assertEquals(c1.a, config.MyComponent.a)
168 self.assertEquals(c1.b, config.MyComponent.b)
169 self.assertEquals(c2.a, config.MyComponent.a)
170 self.assertEquals(c2.b, config.MyComponent.b)
171 c4 = MyComponent(c2, config=Config())
172 self.assertEquals(c4.a, 1)
173 self.assertEquals(c4.b, 1.0)
174
175 def test_parent(self):
176 class Foo(Component):
177 a = Int(0, config=True)
178 b = Str('nope', config=True)
179 class Bar(Foo):
180 b = Str('gotit', config=False)
181 c = Float(config=True)
182 config = Config()
183 config.Foo.a = 10
184 config.Foo.b = "wow"
185 config.Bar.b = 'later'
186 config.Bar.c = 100.0
187 f = Foo(None, config=config)
188 b = Bar(f)
189 self.assertEquals(f.a, 10)
190 self.assertEquals(f.b, 'wow')
191 self.assertEquals(b.b, 'gotit')
192 self.assertEquals(b.c, 100.0)
193
194
195 class TestComponentName(TestCase):
196
197 def test_default(self):
198 class MyComponent(Component):
199 pass
200 c1 = Component(None)
201 c2 = MyComponent(None)
202 c3 = Component(c2)
203 self.assertNotEquals(c1.name, c2.name)
204 self.assertNotEquals(c1.name, c3.name)
205
206 def test_manual(self):
207 class MyComponent(Component):
208 pass
209 c1 = Component(None, name='foo')
210 c2 = MyComponent(None, name='bar')
211 c3 = Component(c2, name='bah')
212 self.assertEquals(c1.name, 'foo')
213 self.assertEquals(c2.name, 'bar')
214 self.assertEquals(c3.name, 'bah')
@@ -0,0 +1,62 b''
1 #!/usr/bin/env python
2 # encoding: utf-8
3
4 def test_import_completer():
5 from IPython.core import completer
6
7 def test_import_crashhandler():
8 from IPython.core import crashhandler
9
10 def test_import_debugger():
11 from IPython.core import debugger
12
13 def test_import_fakemodule():
14 from IPython.core import fakemodule
15
16 def test_import_excolors():
17 from IPython.core import excolors
18
19 def test_import_history():
20 from IPython.core import history
21
22 def test_import_hooks():
23 from IPython.core import hooks
24
25 def test_import_ipapi():
26 from IPython.core import ipapi
27
28 def test_import_iplib():
29 from IPython.core import iplib
30
31 def test_import_logger():
32 from IPython.core import logger
33
34 def test_import_macro():
35 from IPython.core import macro
36
37 def test_import_magic():
38 from IPython.core import magic
39
40 def test_import_oinspect():
41 from IPython.core import oinspect
42
43 def test_import_outputtrap():
44 from IPython.core import outputtrap
45
46 def test_import_prefilter():
47 from IPython.core import prefilter
48
49 def test_import_prompts():
50 from IPython.core import prompts
51
52 def test_import_release():
53 from IPython.core import release
54
55 def test_import_shadowns():
56 from IPython.core import shadowns
57
58 def test_import_ultratb():
59 from IPython.core import ultratb
60
61 def test_import_usage():
62 from IPython.core import usage
1 NO CONTENT: new file 100644
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100755
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100755
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100755
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100755
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100755
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: new file 100644
The requested commit or file is too big and content was truncated. Show full diff
This diff has been collapsed as it changes many lines, (1254 lines changed) Show them Hide them
@@ -1,1246 +1,42 b''
1 # -*- coding: utf-8 -*-
2 """IPython Shell classes.
1 #!/usr/bin/env python
2 # encoding: utf-8
3 """
4 A backwards compatibility layer for IPython.Shell.
3 5
4 All the matplotlib support code was co-developed with John Hunter,
5 matplotlib's author.
6 Previously, IPython had an IPython.Shell module. IPython.Shell has been moved
7 to IPython.core.shell and is being refactored. This new module is provided
8 for backwards compatability. We strongly encourage everyone to start using
9 the new code in IPython.core.shell.
6 10 """
7 11
8 #*****************************************************************************
9 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
12 #-----------------------------------------------------------------------------
13 # Copyright (C) 2008-2009 The IPython Development Team
10 14 #
11 15 # Distributed under the terms of the BSD License. The full license is in
12 16 # the file COPYING, distributed as part of this software.
13 #*****************************************************************************
14
15 # Code begins
16 # Stdlib imports
17 import __builtin__
18 import __main__
19 import Queue
20 import inspect
21 import os
22 import sys
23 import thread
24 import threading
25 import time
26
27 from signal import signal, SIGINT
28
29 try:
30 import ctypes
31 HAS_CTYPES = True
32 except ImportError:
33 HAS_CTYPES = False
34
35 # IPython imports
36 import IPython
37 from IPython import ultraTB, ipapi
38 from IPython.Magic import Magic
39 from IPython.genutils import Term,warn,error,flag_calls, ask_yes_no
40 from IPython.iplib import InteractiveShell
41 from IPython.ipmaker import make_IPython
42 from IPython.ipstruct import Struct
43 from IPython.testing import decorators as testdec
44
45 # Globals
46 # global flag to pass around information about Ctrl-C without exceptions
47 KBINT = False
48
49 # global flag to turn on/off Tk support.
50 USE_TK = False
51
52 # ID for the main thread, used for cross-thread exceptions
53 MAIN_THREAD_ID = thread.get_ident()
54
55 # Tag when runcode() is active, for exception handling
56 CODE_RUN = None
57
58 # Default timeout for waiting for multithreaded shells (in seconds)
59 GUI_TIMEOUT = 10
60
61 #-----------------------------------------------------------------------------
62 # This class is trivial now, but I want to have it in to publish a clean
63 # interface. Later when the internals are reorganized, code that uses this
64 # shouldn't have to change.
65
66 class IPShell:
67 """Create an IPython instance."""
68
69 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
70 debug=1,shell_class=InteractiveShell):
71 self.IP = make_IPython(argv,user_ns=user_ns,
72 user_global_ns=user_global_ns,
73 debug=debug,shell_class=shell_class)
74
75 def mainloop(self,sys_exit=0,banner=None):
76 self.IP.mainloop(banner)
77 if sys_exit:
78 sys.exit()
79
80 #-----------------------------------------------------------------------------
81 def kill_embedded(self,parameter_s=''):
82 """%kill_embedded : deactivate for good the current embedded IPython.
83
84 This function (after asking for confirmation) sets an internal flag so that
85 an embedded IPython will never activate again. This is useful to
86 permanently disable a shell that is being called inside a loop: once you've
87 figured out what you needed from it, you may then kill it and the program
88 will then continue to run without the interactive shell interfering again.
89 """
90
91 kill = ask_yes_no("Are you sure you want to kill this embedded instance "
92 "(y/n)? [y/N] ",'n')
93 if kill:
94 self.shell.embedded_active = False
95 print "This embedded IPython will not reactivate anymore once you exit."
96
97 class IPShellEmbed:
98 """Allow embedding an IPython shell into a running program.
99
100 Instances of this class are callable, with the __call__ method being an
101 alias to the embed() method of an InteractiveShell instance.
102
103 Usage (see also the example-embed.py file for a running example):
104
105 ipshell = IPShellEmbed([argv,banner,exit_msg,rc_override])
106
107 - argv: list containing valid command-line options for IPython, as they
108 would appear in sys.argv[1:].
109
110 For example, the following command-line options:
111
112 $ ipython -prompt_in1 'Input <\\#>' -colors LightBG
113
114 would be passed in the argv list as:
115
116 ['-prompt_in1','Input <\\#>','-colors','LightBG']
117
118 - banner: string which gets printed every time the interpreter starts.
119
120 - exit_msg: string which gets printed every time the interpreter exits.
121
122 - rc_override: a dict or Struct of configuration options such as those
123 used by IPython. These options are read from your ~/.ipython/ipythonrc
124 file when the Shell object is created. Passing an explicit rc_override
125 dict with any options you want allows you to override those values at
126 creation time without having to modify the file. This way you can create
127 embeddable instances configured in any way you want without editing any
128 global files (thus keeping your interactive IPython configuration
129 unchanged).
130
131 Then the ipshell instance can be called anywhere inside your code:
132
133 ipshell(header='') -> Opens up an IPython shell.
134
135 - header: string printed by the IPython shell upon startup. This can let
136 you know where in your code you are when dropping into the shell. Note
137 that 'banner' gets prepended to all calls, so header is used for
138 location-specific information.
139
140 For more details, see the __call__ method below.
141
142 When the IPython shell is exited with Ctrl-D, normal program execution
143 resumes.
144
145 This functionality was inspired by a posting on comp.lang.python by cmkl
146 <cmkleffner@gmx.de> on Dec. 06/01 concerning similar uses of pyrepl, and
147 by the IDL stop/continue commands."""
148
149 def __init__(self,argv=None,banner='',exit_msg=None,rc_override=None,
150 user_ns=None):
151 """Note that argv here is a string, NOT a list."""
152 self.set_banner(banner)
153 self.set_exit_msg(exit_msg)
154 self.set_dummy_mode(0)
155
156 # sys.displayhook is a global, we need to save the user's original
157 # Don't rely on __displayhook__, as the user may have changed that.
158 self.sys_displayhook_ori = sys.displayhook
159
160 # save readline completer status
161 try:
162 #print 'Save completer',sys.ipcompleter # dbg
163 self.sys_ipcompleter_ori = sys.ipcompleter
164 except:
165 pass # not nested with IPython
166
167 self.IP = make_IPython(argv,rc_override=rc_override,
168 embedded=True,
169 user_ns=user_ns)
170
171 ip = ipapi.IPApi(self.IP)
172 ip.expose_magic("kill_embedded",kill_embedded)
173
174 # copy our own displayhook also
175 self.sys_displayhook_embed = sys.displayhook
176 # and leave the system's display hook clean
177 sys.displayhook = self.sys_displayhook_ori
178 # don't use the ipython crash handler so that user exceptions aren't
179 # trapped
180 sys.excepthook = ultraTB.FormattedTB(color_scheme = self.IP.rc.colors,
181 mode = self.IP.rc.xmode,
182 call_pdb = self.IP.rc.pdb)
183 self.restore_system_completer()
184
185 def restore_system_completer(self):
186 """Restores the readline completer which was in place.
187
188 This allows embedded IPython within IPython not to disrupt the
189 parent's completion.
190 """
191
192 try:
193 self.IP.readline.set_completer(self.sys_ipcompleter_ori)
194 sys.ipcompleter = self.sys_ipcompleter_ori
195 except:
196 pass
197
198 def __call__(self,header='',local_ns=None,global_ns=None,dummy=None):
199 """Activate the interactive interpreter.
200
201 __call__(self,header='',local_ns=None,global_ns,dummy=None) -> Start
202 the interpreter shell with the given local and global namespaces, and
203 optionally print a header string at startup.
204
205 The shell can be globally activated/deactivated using the
206 set/get_dummy_mode methods. This allows you to turn off a shell used
207 for debugging globally.
208
209 However, *each* time you call the shell you can override the current
210 state of dummy_mode with the optional keyword parameter 'dummy'. For
211 example, if you set dummy mode on with IPShell.set_dummy_mode(1), you
212 can still have a specific call work by making it as IPShell(dummy=0).
213
214 The optional keyword parameter dummy controls whether the call
215 actually does anything. """
216
217 # If the user has turned it off, go away
218 if not self.IP.embedded_active:
219 return
220
221 # Normal exits from interactive mode set this flag, so the shell can't
222 # re-enter (it checks this variable at the start of interactive mode).
223 self.IP.exit_now = False
224
225 # Allow the dummy parameter to override the global __dummy_mode
226 if dummy or (dummy != 0 and self.__dummy_mode):
227 return
228
229 # Set global subsystems (display,completions) to our values
230 sys.displayhook = self.sys_displayhook_embed
231 if self.IP.has_readline:
232 self.IP.set_completer()
233
234 if self.banner and header:
235 format = '%s\n%s\n'
236 else:
237 format = '%s%s\n'
238 banner = format % (self.banner,header)
239
240 # Call the embedding code with a stack depth of 1 so it can skip over
241 # our call and get the original caller's namespaces.
242 self.IP.embed_mainloop(banner,local_ns,global_ns,stack_depth=1)
243
244 if self.exit_msg:
245 print self.exit_msg
246
247 # Restore global systems (display, completion)
248 sys.displayhook = self.sys_displayhook_ori
249 self.restore_system_completer()
250
251 def set_dummy_mode(self,dummy):
252 """Sets the embeddable shell's dummy mode parameter.
253
254 set_dummy_mode(dummy): dummy = 0 or 1.
255
256 This parameter is persistent and makes calls to the embeddable shell
257 silently return without performing any action. This allows you to
258 globally activate or deactivate a shell you're using with a single call.
259
260 If you need to manually"""
261
262 if dummy not in [0,1,False,True]:
263 raise ValueError,'dummy parameter must be boolean'
264 self.__dummy_mode = dummy
265
266 def get_dummy_mode(self):
267 """Return the current value of the dummy mode parameter.
268 """
269 return self.__dummy_mode
270
271 def set_banner(self,banner):
272 """Sets the global banner.
273
274 This banner gets prepended to every header printed when the shell
275 instance is called."""
276
277 self.banner = banner
278
279 def set_exit_msg(self,exit_msg):
280 """Sets the global exit_msg.
281
282 This exit message gets printed upon exiting every time the embedded
283 shell is called. It is None by default. """
284
285 self.exit_msg = exit_msg
286
287 #-----------------------------------------------------------------------------
288 if HAS_CTYPES:
289 # Add async exception support. Trick taken from:
290 # http://sebulba.wikispaces.com/recipe+thread2
291 def _async_raise(tid, exctype):
292 """raises the exception, performs cleanup if needed"""
293 if not inspect.isclass(exctype):
294 raise TypeError("Only types can be raised (not instances)")
295 # Explicit cast to c_long is necessary for 64-bit support:
296 # See https://bugs.launchpad.net/ipython/+bug/237073
297 res = ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(tid),
298 ctypes.py_object(exctype))
299 if res == 0:
300 raise ValueError("invalid thread id")
301 elif res != 1:
302 # If it returns a number greater than one, you're in trouble,
303 # and you should call it again with exc=NULL to revert the effect
304 ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, 0)
305 raise SystemError("PyThreadState_SetAsyncExc failed")
306
307 def sigint_handler(signum,stack_frame):
308 """Sigint handler for threaded apps.
309
310 This is a horrible hack to pass information about SIGINT _without_
311 using exceptions, since I haven't been able to properly manage
312 cross-thread exceptions in GTK/WX. In fact, I don't think it can be
313 done (or at least that's my understanding from a c.l.py thread where
314 this was discussed)."""
315
316 global KBINT
317
318 if CODE_RUN:
319 _async_raise(MAIN_THREAD_ID,KeyboardInterrupt)
320 else:
321 KBINT = True
322 print '\nKeyboardInterrupt - Press <Enter> to continue.',
323 Term.cout.flush()
324
325 else:
326 def sigint_handler(signum,stack_frame):
327 """Sigint handler for threaded apps.
328
329 This is a horrible hack to pass information about SIGINT _without_
330 using exceptions, since I haven't been able to properly manage
331 cross-thread exceptions in GTK/WX. In fact, I don't think it can be
332 done (or at least that's my understanding from a c.l.py thread where
333 this was discussed)."""
334
335 global KBINT
336
337 print '\nKeyboardInterrupt - Press <Enter> to continue.',
338 Term.cout.flush()
339 # Set global flag so that runsource can know that Ctrl-C was hit
340 KBINT = True
341
342
343 class MTInteractiveShell(InteractiveShell):
344 """Simple multi-threaded shell."""
345
346 # Threading strategy taken from:
347 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65109, by Brian
348 # McErlean and John Finlay. Modified with corrections by Antoon Pardon,
349 # from the pygtk mailing list, to avoid lockups with system calls.
350
351 # class attribute to indicate whether the class supports threads or not.
352 # Subclasses with thread support should override this as needed.
353 isthreaded = True
354
355 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
356 user_ns=None,user_global_ns=None,banner2='',
357 gui_timeout=GUI_TIMEOUT,**kw):
358 """Similar to the normal InteractiveShell, but with threading control"""
359
360 InteractiveShell.__init__(self,name,usage,rc,user_ns,
361 user_global_ns,banner2)
362
363 # Timeout we wait for GUI thread
364 self.gui_timeout = gui_timeout
365
366 # A queue to hold the code to be executed.
367 self.code_queue = Queue.Queue()
368
369 # Stuff to do at closing time
370 self._kill = None
371 on_kill = kw.get('on_kill', [])
372 # Check that all things to kill are callable:
373 for t in on_kill:
374 if not callable(t):
375 raise TypeError,'on_kill must be a list of callables'
376 self.on_kill = on_kill
377 # thread identity of the "worker thread" (that may execute code directly)
378 self.worker_ident = None
379
380 def runsource(self, source, filename="<input>", symbol="single"):
381 """Compile and run some source in the interpreter.
382
383 Modified version of code.py's runsource(), to handle threading issues.
384 See the original for full docstring details."""
385
386 global KBINT
387
388 # If Ctrl-C was typed, we reset the flag and return right away
389 if KBINT:
390 KBINT = False
391 return False
392
393 if self._kill:
394 # can't queue new code if we are being killed
395 return True
396
397 try:
398 code = self.compile(source, filename, symbol)
399 except (OverflowError, SyntaxError, ValueError):
400 # Case 1
401 self.showsyntaxerror(filename)
402 return False
403
404 if code is None:
405 # Case 2
406 return True
407
408 # shortcut - if we are in worker thread, or the worker thread is not
409 # running, execute directly (to allow recursion and prevent deadlock if
410 # code is run early in IPython construction)
411
412 if (self.worker_ident is None
413 or self.worker_ident == thread.get_ident() ):
414 InteractiveShell.runcode(self,code)
415 return False
416
417 # Case 3
418 # Store code in queue, so the execution thread can handle it.
419
420 completed_ev, received_ev = threading.Event(), threading.Event()
421
422 self.code_queue.put((code,completed_ev, received_ev))
423 # first make sure the message was received, with timeout
424 received_ev.wait(self.gui_timeout)
425 if not received_ev.isSet():
426 # the mainloop is dead, start executing code directly
427 print "Warning: Timeout for mainloop thread exceeded"
428 print "switching to nonthreaded mode (until mainloop wakes up again)"
429 self.worker_ident = None
430 else:
431 completed_ev.wait()
432 return False
433
434 def runcode(self):
435 """Execute a code object.
436
437 Multithreaded wrapper around IPython's runcode()."""
438
439 global CODE_RUN
440
441 # we are in worker thread, stash out the id for runsource()
442 self.worker_ident = thread.get_ident()
443
444 if self._kill:
445 print >>Term.cout, 'Closing threads...',
446 Term.cout.flush()
447 for tokill in self.on_kill:
448 tokill()
449 print >>Term.cout, 'Done.'
450 # allow kill() to return
451 self._kill.set()
452 return True
453
454 # Install sigint handler. We do it every time to ensure that if user
455 # code modifies it, we restore our own handling.
456 try:
457 signal(SIGINT,sigint_handler)
458 except SystemError:
459 # This happens under Windows, which seems to have all sorts
460 # of problems with signal handling. Oh well...
461 pass
462
463 # Flush queue of pending code by calling the run methood of the parent
464 # class with all items which may be in the queue.
465 code_to_run = None
466 while 1:
467 try:
468 code_to_run, completed_ev, received_ev = self.code_queue.get_nowait()
469 except Queue.Empty:
470 break
471 received_ev.set()
472
473 # Exceptions need to be raised differently depending on which
474 # thread is active. This convoluted try/except is only there to
475 # protect against asynchronous exceptions, to ensure that a KBINT
476 # at the wrong time doesn't deadlock everything. The global
477 # CODE_TO_RUN is set to true/false as close as possible to the
478 # runcode() call, so that the KBINT handler is correctly informed.
479 try:
480 try:
481 CODE_RUN = True
482 InteractiveShell.runcode(self,code_to_run)
483 except KeyboardInterrupt:
484 print "Keyboard interrupted in mainloop"
485 while not self.code_queue.empty():
486 code, ev1,ev2 = self.code_queue.get_nowait()
487 ev1.set()
488 ev2.set()
489 break
490 finally:
491 CODE_RUN = False
492 # allow runsource() return from wait
493 completed_ev.set()
494
495
496 # This MUST return true for gtk threading to work
497 return True
498
499 def kill(self):
500 """Kill the thread, returning when it has been shut down."""
501 self._kill = threading.Event()
502 self._kill.wait()
503
504 class MatplotlibShellBase:
505 """Mixin class to provide the necessary modifications to regular IPython
506 shell classes for matplotlib support.
507
508 Given Python's MRO, this should be used as the FIRST class in the
509 inheritance hierarchy, so that it overrides the relevant methods."""
510
511 def _matplotlib_config(self,name,user_ns,user_global_ns=None):
512 """Return items needed to setup the user's shell with matplotlib"""
513
514 # Initialize matplotlib to interactive mode always
515 import matplotlib
516 from matplotlib import backends
517 matplotlib.interactive(True)
518
519 def use(arg):
520 """IPython wrapper for matplotlib's backend switcher.
521
522 In interactive use, we can not allow switching to a different
523 interactive backend, since thread conflicts will most likely crash
524 the python interpreter. This routine does a safety check first,
525 and refuses to perform a dangerous switch. It still allows
526 switching to non-interactive backends."""
527
528 if arg in backends.interactive_bk and arg != self.mpl_backend:
529 m=('invalid matplotlib backend switch.\n'
530 'This script attempted to switch to the interactive '
531 'backend: `%s`\n'
532 'Your current choice of interactive backend is: `%s`\n\n'
533 'Switching interactive matplotlib backends at runtime\n'
534 'would crash the python interpreter, '
535 'and IPython has blocked it.\n\n'
536 'You need to either change your choice of matplotlib backend\n'
537 'by editing your .matplotlibrc file, or run this script as a \n'
538 'standalone file from the command line, not using IPython.\n' %
539 (arg,self.mpl_backend) )
540 raise RuntimeError, m
541 else:
542 self.mpl_use(arg)
543 self.mpl_use._called = True
544
545 self.matplotlib = matplotlib
546 self.mpl_backend = matplotlib.rcParams['backend']
547
548 # we also need to block switching of interactive backends by use()
549 self.mpl_use = matplotlib.use
550 self.mpl_use._called = False
551 # overwrite the original matplotlib.use with our wrapper
552 matplotlib.use = use
553
554 # This must be imported last in the matplotlib series, after
555 # backend/interactivity choices have been made
556 import matplotlib.pylab as pylab
557 self.pylab = pylab
558
559 self.pylab.show._needmain = False
560 # We need to detect at runtime whether show() is called by the user.
561 # For this, we wrap it into a decorator which adds a 'called' flag.
562 self.pylab.draw_if_interactive = flag_calls(self.pylab.draw_if_interactive)
563
564 # Build a user namespace initialized with matplotlib/matlab features.
565 user_ns, user_global_ns = IPython.ipapi.make_user_namespaces(user_ns,
566 user_global_ns)
567
568 # Import numpy as np/pyplot as plt are conventions we're trying to
569 # somewhat standardize on. Making them available to users by default
570 # will greatly help this.
571 exec ("import numpy\n"
572 "import numpy as np\n"
573 "import matplotlib\n"
574 "import matplotlib.pylab as pylab\n"
575 "try:\n"
576 " import matplotlib.pyplot as plt\n"
577 "except ImportError:\n"
578 " pass\n"
579 ) in user_ns
580
581 # Build matplotlib info banner
582 b="""
583 Welcome to pylab, a matplotlib-based Python environment.
584 For more information, type 'help(pylab)'.
585 """
586 return user_ns,user_global_ns,b
587
588 def mplot_exec(self,fname,*where,**kw):
589 """Execute a matplotlib script.
590
591 This is a call to execfile(), but wrapped in safeties to properly
592 handle interactive rendering and backend switching."""
593
594 #print '*** Matplotlib runner ***' # dbg
595 # turn off rendering until end of script
596 isInteractive = self.matplotlib.rcParams['interactive']
597 self.matplotlib.interactive(False)
598 self.safe_execfile(fname,*where,**kw)
599 self.matplotlib.interactive(isInteractive)
600 # make rendering call now, if the user tried to do it
601 if self.pylab.draw_if_interactive.called:
602 self.pylab.draw()
603 self.pylab.draw_if_interactive.called = False
604
605 # if a backend switch was performed, reverse it now
606 if self.mpl_use._called:
607 self.matplotlib.rcParams['backend'] = self.mpl_backend
608
609 @testdec.skip_doctest
610 def magic_run(self,parameter_s=''):
611 Magic.magic_run(self,parameter_s,runner=self.mplot_exec)
612
613 # Fix the docstring so users see the original as well
614 magic_run.__doc__ = "%s\n%s" % (Magic.magic_run.__doc__,
615 "\n *** Modified %run for Matplotlib,"
616 " with proper interactive handling ***")
617
618 # Now we provide 2 versions of a matplotlib-aware IPython base shells, single
619 # and multithreaded. Note that these are meant for internal use, the IPShell*
620 # classes below are the ones meant for public consumption.
621
622 class MatplotlibShell(MatplotlibShellBase,InteractiveShell):
623 """Single-threaded shell with matplotlib support."""
624
625 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
626 user_ns=None,user_global_ns=None,**kw):
627 user_ns,user_global_ns,b2 = self._matplotlib_config(name,user_ns,user_global_ns)
628 InteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns,
629 banner2=b2,**kw)
630
631 class MatplotlibMTShell(MatplotlibShellBase,MTInteractiveShell):
632 """Multi-threaded shell with matplotlib support."""
633
634 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
635 user_ns=None,user_global_ns=None, **kw):
636 user_ns,user_global_ns,b2 = self._matplotlib_config(name,user_ns,user_global_ns)
637 MTInteractiveShell.__init__(self,name,usage,rc,user_ns,user_global_ns,
638 banner2=b2,**kw)
639
640 #-----------------------------------------------------------------------------
641 # Utility functions for the different GUI enabled IPShell* classes.
642
643 def get_tk():
644 """Tries to import Tkinter and returns a withdrawn Tkinter root
645 window. If Tkinter is already imported or not available, this
646 returns None. This function calls `hijack_tk` underneath.
647 """
648 if not USE_TK or sys.modules.has_key('Tkinter'):
649 return None
650 else:
651 try:
652 import Tkinter
653 except ImportError:
654 return None
655 else:
656 hijack_tk()
657 r = Tkinter.Tk()
658 r.withdraw()
659 return r
660
661 def hijack_tk():
662 """Modifies Tkinter's mainloop with a dummy so when a module calls
663 mainloop, it does not block.
664
665 """
666 def misc_mainloop(self, n=0):
667 pass
668 def tkinter_mainloop(n=0):
669 pass
670
671 import Tkinter
672 Tkinter.Misc.mainloop = misc_mainloop
673 Tkinter.mainloop = tkinter_mainloop
674
675 def update_tk(tk):
676 """Updates the Tkinter event loop. This is typically called from
677 the respective WX or GTK mainloops.
678 """
679 if tk:
680 tk.update()
681
682 def hijack_wx():
683 """Modifies wxPython's MainLoop with a dummy so user code does not
684 block IPython. The hijacked mainloop function is returned.
685 """
686 def dummy_mainloop(*args, **kw):
687 pass
688
689 try:
690 import wx
691 except ImportError:
692 # For very old versions of WX
693 import wxPython as wx
694
695 ver = wx.__version__
696 orig_mainloop = None
697 if ver[:3] >= '2.5':
698 import wx
699 if hasattr(wx, '_core_'): core = getattr(wx, '_core_')
700 elif hasattr(wx, '_core'): core = getattr(wx, '_core')
701 else: raise AttributeError('Could not find wx core module')
702 orig_mainloop = core.PyApp_MainLoop
703 core.PyApp_MainLoop = dummy_mainloop
704 elif ver[:3] == '2.4':
705 orig_mainloop = wx.wxc.wxPyApp_MainLoop
706 wx.wxc.wxPyApp_MainLoop = dummy_mainloop
707 else:
708 warn("Unable to find either wxPython version 2.4 or >= 2.5.")
709 return orig_mainloop
710
711 def hijack_gtk():
712 """Modifies pyGTK's mainloop with a dummy so user code does not
713 block IPython. This function returns the original `gtk.mainloop`
714 function that has been hijacked.
715 """
716 def dummy_mainloop(*args, **kw):
717 pass
718 import gtk
719 if gtk.pygtk_version >= (2,4,0): orig_mainloop = gtk.main
720 else: orig_mainloop = gtk.mainloop
721 gtk.mainloop = dummy_mainloop
722 gtk.main = dummy_mainloop
723 return orig_mainloop
724
725 def hijack_qt():
726 """Modifies PyQt's mainloop with a dummy so user code does not
727 block IPython. This function returns the original
728 `qt.qApp.exec_loop` function that has been hijacked.
729 """
730 def dummy_mainloop(*args, **kw):
731 pass
732 import qt
733 orig_mainloop = qt.qApp.exec_loop
734 qt.qApp.exec_loop = dummy_mainloop
735 qt.QApplication.exec_loop = dummy_mainloop
736 return orig_mainloop
737
738 def hijack_qt4():
739 """Modifies PyQt4's mainloop with a dummy so user code does not
740 block IPython. This function returns the original
741 `QtGui.qApp.exec_` function that has been hijacked.
742 """
743 def dummy_mainloop(*args, **kw):
744 pass
745 from PyQt4 import QtGui, QtCore
746 orig_mainloop = QtGui.qApp.exec_
747 QtGui.qApp.exec_ = dummy_mainloop
748 QtGui.QApplication.exec_ = dummy_mainloop
749 QtCore.QCoreApplication.exec_ = dummy_mainloop
750 return orig_mainloop
751
752 17 #-----------------------------------------------------------------------------
753 # The IPShell* classes below are the ones meant to be run by external code as
754 # IPython instances. Note that unless a specific threading strategy is
755 # desired, the factory function start() below should be used instead (it
756 # selects the proper threaded class).
757
758 class IPThread(threading.Thread):
759 def run(self):
760 self.IP.mainloop(self._banner)
761 self.IP.kill()
762
763 class IPShellGTK(IPThread):
764 """Run a gtk mainloop() in a separate thread.
765
766 Python commands can be passed to the thread where they will be executed.
767 This is implemented by periodically checking for passed code using a
768 GTK timeout callback."""
769
770 TIMEOUT = 100 # Millisecond interval between timeouts.
771
772 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
773 debug=1,shell_class=MTInteractiveShell):
774
775 import gtk
776 # Check for set_interactive, coming up in new pygtk.
777 # Disable it so that this code works, but notify
778 # the user that he has a better option as well.
779 # XXX TODO better support when set_interactive is released
780 try:
781 gtk.set_interactive(False)
782 print "Your PyGtk has set_interactive(), so you can use the"
783 print "more stable single-threaded Gtk mode."
784 print "See https://bugs.launchpad.net/ipython/+bug/270856"
785 except AttributeError:
786 pass
787
788 self.gtk = gtk
789 self.gtk_mainloop = hijack_gtk()
790
791 # Allows us to use both Tk and GTK.
792 self.tk = get_tk()
793
794 if gtk.pygtk_version >= (2,4,0): mainquit = self.gtk.main_quit
795 else: mainquit = self.gtk.mainquit
796
797 self.IP = make_IPython(argv,user_ns=user_ns,
798 user_global_ns=user_global_ns,
799 debug=debug,
800 shell_class=shell_class,
801 on_kill=[mainquit])
802
803 # HACK: slot for banner in self; it will be passed to the mainloop
804 # method only and .run() needs it. The actual value will be set by
805 # .mainloop().
806 self._banner = None
807
808 threading.Thread.__init__(self)
809
810 def mainloop(self,sys_exit=0,banner=None):
811
812 self._banner = banner
813
814 if self.gtk.pygtk_version >= (2,4,0):
815 import gobject
816 gobject.idle_add(self.on_timer)
817 else:
818 self.gtk.idle_add(self.on_timer)
819
820 if sys.platform != 'win32':
821 try:
822 if self.gtk.gtk_version[0] >= 2:
823 self.gtk.gdk.threads_init()
824 except AttributeError:
825 pass
826 except RuntimeError:
827 error('Your pyGTK likely has not been compiled with '
828 'threading support.\n'
829 'The exception printout is below.\n'
830 'You can either rebuild pyGTK with threads, or '
831 'try using \n'
832 'matplotlib with a different backend (like Tk or WX).\n'
833 'Note that matplotlib will most likely not work in its '
834 'current state!')
835 self.IP.InteractiveTB()
836
837 self.start()
838 self.gtk.gdk.threads_enter()
839 self.gtk_mainloop()
840 self.gtk.gdk.threads_leave()
841 self.join()
842
843 def on_timer(self):
844 """Called when GTK is idle.
845
846 Must return True always, otherwise GTK stops calling it"""
847
848 update_tk(self.tk)
849 self.IP.runcode()
850 time.sleep(0.01)
851 return True
852
853
854 class IPShellWX(IPThread):
855 """Run a wx mainloop() in a separate thread.
856
857 Python commands can be passed to the thread where they will be executed.
858 This is implemented by periodically checking for passed code using a
859 GTK timeout callback."""
860
861 TIMEOUT = 100 # Millisecond interval between timeouts.
862
863 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
864 debug=1,shell_class=MTInteractiveShell):
865
866 self.IP = make_IPython(argv,user_ns=user_ns,
867 user_global_ns=user_global_ns,
868 debug=debug,
869 shell_class=shell_class,
870 on_kill=[self.wxexit])
871
872 wantedwxversion=self.IP.rc.wxversion
873 if wantedwxversion!="0":
874 try:
875 import wxversion
876 except ImportError:
877 error('The wxversion module is needed for WX version selection')
878 else:
879 try:
880 wxversion.select(wantedwxversion)
881 except:
882 self.IP.InteractiveTB()
883 error('Requested wxPython version %s could not be loaded' %
884 wantedwxversion)
885
886 import wx
887
888 threading.Thread.__init__(self)
889 self.wx = wx
890 self.wx_mainloop = hijack_wx()
891
892 # Allows us to use both Tk and GTK.
893 self.tk = get_tk()
894
895 # HACK: slot for banner in self; it will be passed to the mainloop
896 # method only and .run() needs it. The actual value will be set by
897 # .mainloop().
898 self._banner = None
899
900 self.app = None
901
902 def wxexit(self, *args):
903 if self.app is not None:
904 self.app.agent.timer.Stop()
905 self.app.ExitMainLoop()
906
907 def mainloop(self,sys_exit=0,banner=None):
908
909 self._banner = banner
910
911 self.start()
912
913 class TimerAgent(self.wx.MiniFrame):
914 wx = self.wx
915 IP = self.IP
916 tk = self.tk
917 def __init__(self, parent, interval):
918 style = self.wx.DEFAULT_FRAME_STYLE | self.wx.TINY_CAPTION_HORIZ
919 self.wx.MiniFrame.__init__(self, parent, -1, ' ', pos=(200, 200),
920 size=(100, 100),style=style)
921 self.Show(False)
922 self.interval = interval
923 self.timerId = self.wx.NewId()
924
925 def StartWork(self):
926 self.timer = self.wx.Timer(self, self.timerId)
927 self.wx.EVT_TIMER(self, self.timerId, self.OnTimer)
928 self.timer.Start(self.interval)
929
930 def OnTimer(self, event):
931 update_tk(self.tk)
932 self.IP.runcode()
933
934 class App(self.wx.App):
935 wx = self.wx
936 TIMEOUT = self.TIMEOUT
937 def OnInit(self):
938 'Create the main window and insert the custom frame'
939 self.agent = TimerAgent(None, self.TIMEOUT)
940 self.agent.Show(False)
941 self.agent.StartWork()
942 return True
943
944 self.app = App(redirect=False)
945 self.wx_mainloop(self.app)
946 self.join()
947
948 18
949 class IPShellQt(IPThread):
950 """Run a Qt event loop in a separate thread.
19 from warnings import warn
951 20
952 Python commands can be passed to the thread where they will be executed.
953 This is implemented by periodically checking for passed code using a
954 Qt timer / slot."""
21 msg = """
22 This module (IPython.Shell) is deprecated. The classes that were in this
23 module have been replaced by:
955 24
956 TIMEOUT = 100 # Millisecond interval between timeouts.
25 IPShell->IPython.core.iplib.InteractiveShell
26 IPShellEmbed->IPython.core.embed.InteractiveShellEmbed
957 27
958 def __init__(self, argv=None, user_ns=None, user_global_ns=None,
959 debug=0, shell_class=MTInteractiveShell):
960
961 import qt
962
963 self.exec_loop = hijack_qt()
964
965 # Allows us to use both Tk and QT.
966 self.tk = get_tk()
967
968 self.IP = make_IPython(argv,
969 user_ns=user_ns,
970 user_global_ns=user_global_ns,
971 debug=debug,
972 shell_class=shell_class,
973 on_kill=[qt.qApp.exit])
974
975 # HACK: slot for banner in self; it will be passed to the mainloop
976 # method only and .run() needs it. The actual value will be set by
977 # .mainloop().
978 self._banner = None
979
980 threading.Thread.__init__(self)
981
982 def mainloop(self, sys_exit=0, banner=None):
983
984 import qt
985
986 self._banner = banner
987
988 if qt.QApplication.startingUp():
989 a = qt.QApplication(sys.argv)
990
991 self.timer = qt.QTimer()
992 qt.QObject.connect(self.timer,
993 qt.SIGNAL('timeout()'),
994 self.on_timer)
995
996 self.start()
997 self.timer.start(self.TIMEOUT, True)
998 while True:
999 if self.IP._kill: break
1000 self.exec_loop()
1001 self.join()
1002
1003 def on_timer(self):
1004 update_tk(self.tk)
1005 result = self.IP.runcode()
1006 self.timer.start(self.TIMEOUT, True)
1007 return result
1008
1009
1010 class IPShellQt4(IPThread):
1011 """Run a Qt event loop in a separate thread.
1012
1013 Python commands can be passed to the thread where they will be executed.
1014 This is implemented by periodically checking for passed code using a
1015 Qt timer / slot."""
1016
1017 TIMEOUT = 100 # Millisecond interval between timeouts.
1018
1019 def __init__(self, argv=None, user_ns=None, user_global_ns=None,
1020 debug=0, shell_class=MTInteractiveShell):
1021
1022 from PyQt4 import QtCore, QtGui
1023
1024 try:
1025 # present in PyQt4-4.2.1 or later
1026 QtCore.pyqtRemoveInputHook()
1027 except AttributeError:
1028 pass
1029
1030 if QtCore.PYQT_VERSION_STR == '4.3':
1031 warn('''PyQt4 version 4.3 detected.
1032 If you experience repeated threading warnings, please update PyQt4.
1033 ''')
1034
1035 self.exec_ = hijack_qt4()
1036
1037 # Allows us to use both Tk and QT.
1038 self.tk = get_tk()
1039
1040 self.IP = make_IPython(argv,
1041 user_ns=user_ns,
1042 user_global_ns=user_global_ns,
1043 debug=debug,
1044 shell_class=shell_class,
1045 on_kill=[QtGui.qApp.exit])
1046
1047 # HACK: slot for banner in self; it will be passed to the mainloop
1048 # method only and .run() needs it. The actual value will be set by
1049 # .mainloop().
1050 self._banner = None
1051
1052 threading.Thread.__init__(self)
1053
1054 def mainloop(self, sys_exit=0, banner=None):
1055
1056 from PyQt4 import QtCore, QtGui
1057
1058 self._banner = banner
1059
1060 if QtGui.QApplication.startingUp():
1061 a = QtGui.QApplication(sys.argv)
1062
1063 self.timer = QtCore.QTimer()
1064 QtCore.QObject.connect(self.timer,
1065 QtCore.SIGNAL('timeout()'),
1066 self.on_timer)
1067
1068 self.start()
1069 self.timer.start(self.TIMEOUT)
1070 while True:
1071 if self.IP._kill: break
1072 self.exec_()
1073 self.join()
1074
1075 def on_timer(self):
1076 update_tk(self.tk)
1077 result = self.IP.runcode()
1078 self.timer.start(self.TIMEOUT)
1079 return result
1080
1081
1082 # A set of matplotlib public IPython shell classes, for single-threaded (Tk*
1083 # and FLTK*) and multithreaded (GTK*, WX* and Qt*) backends to use.
1084 def _load_pylab(user_ns):
1085 """Allow users to disable pulling all of pylab into the top-level
1086 namespace.
1087
1088 This little utility must be called AFTER the actual ipython instance is
1089 running, since only then will the options file have been fully parsed."""
1090
1091 ip = IPython.ipapi.get()
1092 if ip.options.pylab_import_all:
1093 ip.ex("from matplotlib.pylab import *")
1094 ip.IP.user_config_ns.update(ip.user_ns)
1095
1096
1097 class IPShellMatplotlib(IPShell):
1098 """Subclass IPShell with MatplotlibShell as the internal shell.
1099
1100 Single-threaded class, meant for the Tk* and FLTK* backends.
1101
1102 Having this on a separate class simplifies the external driver code."""
1103
1104 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
1105 IPShell.__init__(self,argv,user_ns,user_global_ns,debug,
1106 shell_class=MatplotlibShell)
1107 _load_pylab(self.IP.user_ns)
1108
1109 class IPShellMatplotlibGTK(IPShellGTK):
1110 """Subclass IPShellGTK with MatplotlibMTShell as the internal shell.
1111
1112 Multi-threaded class, meant for the GTK* backends."""
1113
1114 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
1115 IPShellGTK.__init__(self,argv,user_ns,user_global_ns,debug,
1116 shell_class=MatplotlibMTShell)
1117 _load_pylab(self.IP.user_ns)
1118
1119 class IPShellMatplotlibWX(IPShellWX):
1120 """Subclass IPShellWX with MatplotlibMTShell as the internal shell.
1121
1122 Multi-threaded class, meant for the WX* backends."""
1123
1124 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
1125 IPShellWX.__init__(self,argv,user_ns,user_global_ns,debug,
1126 shell_class=MatplotlibMTShell)
1127 _load_pylab(self.IP.user_ns)
1128
1129 class IPShellMatplotlibQt(IPShellQt):
1130 """Subclass IPShellQt with MatplotlibMTShell as the internal shell.
1131
1132 Multi-threaded class, meant for the Qt* backends."""
1133
1134 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
1135 IPShellQt.__init__(self,argv,user_ns,user_global_ns,debug,
1136 shell_class=MatplotlibMTShell)
1137 _load_pylab(self.IP.user_ns)
1138
1139 class IPShellMatplotlibQt4(IPShellQt4):
1140 """Subclass IPShellQt4 with MatplotlibMTShell as the internal shell.
1141
1142 Multi-threaded class, meant for the Qt4* backends."""
1143
1144 def __init__(self,argv=None,user_ns=None,user_global_ns=None,debug=1):
1145 IPShellQt4.__init__(self,argv,user_ns,user_global_ns,debug,
1146 shell_class=MatplotlibMTShell)
1147 _load_pylab(self.IP.user_ns)
1148
1149 #-----------------------------------------------------------------------------
1150 # Factory functions to actually start the proper thread-aware shell
1151
1152 def _select_shell(argv):
1153 """Select a shell from the given argv vector.
1154
1155 This function implements the threading selection policy, allowing runtime
1156 control of the threading mode, both for general users and for matplotlib.
1157
1158 Return:
1159 Shell class to be instantiated for runtime operation.
28 Please migrate your code to use these classes instead.
1160 29 """
1161 30
1162 global USE_TK
1163
1164 mpl_shell = {'gthread' : IPShellMatplotlibGTK,
1165 'wthread' : IPShellMatplotlibWX,
1166 'qthread' : IPShellMatplotlibQt,
1167 'q4thread' : IPShellMatplotlibQt4,
1168 'tkthread' : IPShellMatplotlib, # Tk is built-in
1169 }
1170
1171 th_shell = {'gthread' : IPShellGTK,
1172 'wthread' : IPShellWX,
1173 'qthread' : IPShellQt,
1174 'q4thread' : IPShellQt4,
1175 'tkthread' : IPShell, # Tk is built-in
1176 }
1177
1178 backends = {'gthread' : 'GTKAgg',
1179 'wthread' : 'WXAgg',
1180 'qthread' : 'QtAgg',
1181 'q4thread' :'Qt4Agg',
1182 'tkthread' :'TkAgg',
1183 }
1184
1185 all_opts = set(['tk','pylab','gthread','qthread','q4thread','wthread',
1186 'tkthread'])
1187 user_opts = set([s.replace('-','') for s in argv[:3]])
1188 special_opts = user_opts & all_opts
31 warn(msg, category=DeprecationWarning, stacklevel=1)
1189 32
1190 if 'tk' in special_opts:
1191 USE_TK = True
1192 special_opts.remove('tk')
33 from IPython.core.iplib import InteractiveShell as IPShell
34 from IPython.core.embed import InteractiveShellEmbed as IPShellEmbed
1193 35
1194 if 'pylab' in special_opts:
1195
1196 try:
1197 import matplotlib
1198 except ImportError:
1199 error('matplotlib could NOT be imported! Starting normal IPython.')
1200 return IPShell
1201
1202 special_opts.remove('pylab')
1203 # If there's any option left, it means the user wants to force the
1204 # threading backend, else it's auto-selected from the rc file
1205 if special_opts:
1206 th_mode = special_opts.pop()
1207 matplotlib.rcParams['backend'] = backends[th_mode]
1208 else:
1209 backend = matplotlib.rcParams['backend']
1210 if backend.startswith('GTK'):
1211 th_mode = 'gthread'
1212 elif backend.startswith('WX'):
1213 th_mode = 'wthread'
1214 elif backend.startswith('Qt4'):
1215 th_mode = 'q4thread'
1216 elif backend.startswith('Qt'):
1217 th_mode = 'qthread'
1218 else:
1219 # Any other backend, use plain Tk
1220 th_mode = 'tkthread'
1221
1222 return mpl_shell[th_mode]
36 def start(user_ns=None, embedded=False):
37 """Return an instance of :class:`InteractiveShell`."""
38 if embedded:
39 return InteractiveShellEmbed(user_ns=user_ns)
1223 40 else:
1224 # No pylab requested, just plain threads
1225 try:
1226 th_mode = special_opts.pop()
1227 except KeyError:
1228 th_mode = 'tkthread'
1229 return th_shell[th_mode]
1230
1231
1232 # This is the one which should be called by external code.
1233 def start(user_ns = None):
1234 """Return a running shell instance, dealing with threading options.
1235
1236 This is a factory function which will instantiate the proper IPython shell
1237 based on the user's threading choice. Such a selector is needed because
1238 different GUI toolkits require different thread handling details."""
1239
1240 shell = _select_shell(sys.argv)
1241 return shell(user_ns = user_ns)
41 return InteractiveShell(user_ns=user_ns)
1242 42
1243 # Some aliases for backwards compatibility
1244 IPythonShell = IPShell
1245 IPythonShellEmbed = IPShellEmbed
1246 #************************ End of file <Shell.py> ***************************
@@ -1,72 +1,64 b''
1 # -*- coding: utf-8 -*-
1 #!/usr/bin/env python
2 # encoding: utf-8
2 3 """
3 IPython -- An enhanced Interactive Python
4 IPython.
4 5
5 One of Python's nicest features is its interactive interpreter. This allows
6 very fast testing of ideas without the overhead of creating test files as is
7 typical in most programming languages. However, the interpreter supplied with
8 the standard Python distribution is fairly primitive (and IDLE isn't really
9 much better).
10
11 IPython tries to:
12
13 i - provide an efficient environment for interactive work in Python
14 programming. It tries to address what we see as shortcomings of the standard
15 Python prompt, and adds many features to make interactive work much more
16 efficient.
17
18 ii - offer a flexible framework so that it can be used as the base
19 environment for other projects and problems where Python can be the
20 underlying language. Specifically scientific environments like Mathematica,
21 IDL and Mathcad inspired its design, but similar ideas can be useful in many
22 fields. Python is a fabulous language for implementing this kind of system
23 (due to its dynamic and introspective features), and with suitable libraries
24 entire systems could be built leveraging Python's power.
25
26 iii - serve as an embeddable, ready to go interpreter for your own programs.
27
28 IPython requires Python 2.4 or newer.
6 IPython is a set of tools for interactive and exploratory computing in Python.
29 7 """
30 8
31 #*****************************************************************************
9 #-----------------------------------------------------------------------------
32 10 # Copyright (C) 2008-2009 The IPython Development Team
33 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
34 11 #
35 12 # Distributed under the terms of the BSD License. The full license is in
36 13 # the file COPYING, distributed as part of this software.
37 #*****************************************************************************
14 #-----------------------------------------------------------------------------
15
16 #-----------------------------------------------------------------------------
17 # Imports
18 #-----------------------------------------------------------------------------
38 19
39 # Enforce proper version requirements
20 import os
40 21 import sys
22 from IPython.core import release
23
24 #-----------------------------------------------------------------------------
25 # Setup everything
26 #-----------------------------------------------------------------------------
27
41 28
42 29 if sys.version[0:3] < '2.4':
43 30 raise ImportError('Python Version 2.4 or above is required for IPython.')
44 31
32
45 33 # Make it easy to import extensions - they are always directly on pythonpath.
46 # Therefore, non-IPython modules can be added to Extensions directory
47 import os
48 sys.path.append(os.path.dirname(__file__) + "/Extensions")
34 # Therefore, non-IPython modules can be added to extensions directory
35 sys.path.append(os.path.join(os.path.dirname(__file__), "extensions"))
49 36
50 # Define what gets imported with a 'from IPython import *'
51 __all__ = ['ipapi','generics','ipstruct','Release','Shell']
37 #-----------------------------------------------------------------------------
38 # Setup the top level names
39 #-----------------------------------------------------------------------------
52 40
53 # Load __all__ in IPython namespace so that a simple 'import IPython' gives
54 # access to them via IPython.<name>
55 glob,loc = globals(),locals()
56 for name in __all__:
57 #print 'Importing: ',name # dbg
58 __import__(name,glob,loc,[])
41 # In some cases, these are causing circular imports.
42 from IPython.core.iplib import InteractiveShell
43 from IPython.core.embed import embed
44 from IPython.core.error import TryNext
59 45
60 import Shell
46 from IPython.lib import (
47 enable_wx, disable_wx,
48 enable_gtk, disable_gtk,
49 enable_qt4, disable_qt4,
50 enable_tk, disable_tk,
51 set_inputhook, clear_inputhook,
52 current_gui, spin,
53 appstart_qt4, appstart_wx,
54 appstart_gtk, appstart_tk
55 )
61 56
62 57 # Release data
63 from IPython import Release # do it explicitly so pydoc can see it - pydoc bug
64 __author__ = '%s <%s>\n%s <%s>\n%s <%s>' % \
65 ( Release.authors['Fernando'] + Release.authors['Janko'] + \
66 Release.authors['Nathan'] )
67 __license__ = Release.license
68 __version__ = Release.version
69 __revision__ = Release.revision
58 __author__ = ''
59 for author, email in release.authors.values():
60 __author__ += author + ' <' + email + '>\n'
61 __license__ = release.license
62 __version__ = release.version
63 __revision__ = release.revision
70 64
71 # Namespace cleanup
72 del name,glob,loc
1 NO CONTENT: file renamed from IPython/UserConfig/__init__.py to IPython/config/default/__init__.py
1 NO CONTENT: file renamed from IPython/tests/__init__.py to IPython/config/profile/__init__.py
1 NO CONTENT: file renamed from IPython/tools/__init__.py to IPython/core/__init__.py
@@ -65,25 +65,28 b' used, and this module (and the readline module) are silently inactive.'
65 65 import __builtin__
66 66 import __main__
67 67 import glob
68 import itertools
68 69 import keyword
69 70 import os
70 71 import re
71 72 import shlex
72 73 import sys
73 import IPython.rlineimpl as readline
74 import itertools
75 from IPython.ipstruct import Struct
76 from IPython import ipapi
77 from IPython import generics
78 74 import types
79 75
76 from IPython.core.error import TryNext
77 from IPython.core.prefilter import ESC_MAGIC
78
79 import IPython.utils.rlineimpl as readline
80 from IPython.utils.ipstruct import Struct
81 from IPython.utils import generics
82
80 83 # Python 2.4 offers sets as a builtin
81 84 try:
82 85 set()
83 86 except NameError:
84 87 from sets import Set as set
85 88
86 from IPython.genutils import debugx, dir2
89 from IPython.utils.genutils import debugx, dir2
87 90
88 91 __all__ = ['Completer','IPCompleter']
89 92
@@ -195,7 +198,7 b' class Completer:'
195 198
196 199 try:
197 200 words = generics.complete_object(obj, words)
198 except ipapi.TryNext:
201 except TryNext:
199 202 pass
200 203 # Build match list to return
201 204 n = len(attr)
@@ -233,7 +236,7 b' class IPCompleter(Completer):'
233 236
234 237 Completer.__init__(self,namespace,global_namespace)
235 238 self.magic_prefix = shell.name+'.magic_'
236 self.magic_escape = shell.ESC_MAGIC
239 self.magic_escape = ESC_MAGIC
237 240 self.readline = readline
238 241 delims = self.readline.get_completer_delims()
239 242 delims = delims.replace(self.magic_escape,'')
@@ -241,7 +244,7 b' class IPCompleter(Completer):'
241 244 self.get_line_buffer = self.readline.get_line_buffer
242 245 self.get_endidx = self.readline.get_endidx
243 246 self.omit__names = omit__names
244 self.merge_completions = shell.rc.readline_merge_completions
247 self.merge_completions = shell.readline_merge_completions
245 248 if alias_table is None:
246 249 alias_table = {}
247 250 self.alias_table = alias_table
@@ -553,7 +556,7 b' class IPCompleter(Completer):'
553 556 return withcase
554 557 # if none, then case insensitive ones are ok too
555 558 return [r for r in res if r.lower().startswith(text.lower())]
556 except ipapi.TryNext:
559 except TryNext:
557 560 pass
558 561
559 562 return None
@@ -632,7 +635,7 b' class IPCompleter(Completer):'
632 635 except IndexError:
633 636 return None
634 637 except:
635 #from IPython.ultraTB import AutoFormattedTB; # dbg
638 #from IPython.core.ultratb import AutoFormattedTB; # dbg
636 639 #tb=AutoFormattedTB('Verbose');tb() #dbg
637 640
638 641 # If completion fails, don't annoy the user.
@@ -21,15 +21,14 b' Authors'
21 21 # From the standard library
22 22 import os
23 23 import sys
24 from pprint import pprint,pformat
24 from pprint import pformat
25 25
26 26 # Our own
27 from IPython import Release
28 from IPython import ultraTB
29 from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names
30 from IPython.Itpl import Itpl,itpl,printpl
27 from IPython.core import release
28 from IPython.core import ultratb
29 from IPython.external.Itpl import itpl
31 30
32 from IPython.genutils import *
31 from IPython.utils.genutils import *
33 32
34 33 #****************************************************************************
35 34 class CrashHandler:
@@ -125,7 +124,7 b' $self.bug_tracker'
125 124 #color_scheme = 'Linux' # dbg
126 125
127 126 try:
128 rptdir = self.IP.rc.ipythondir
127 rptdir = self.IP.ipython_dir
129 128 except:
130 129 rptdir = os.getcwd()
131 130 if not os.path.isdir(rptdir):
@@ -134,7 +133,7 b' $self.bug_tracker'
134 133 # write the report filename into the instance dict so it can get
135 134 # properly expanded out in the user message template
136 135 self.crash_report_fname = report_name
137 TBhandler = ultraTB.VerboseTB(color_scheme=color_scheme,
136 TBhandler = ultratb.VerboseTB(color_scheme=color_scheme,
138 137 long_header=1)
139 138 traceback = TBhandler.text(etype,evalue,etb,context=31)
140 139
@@ -167,12 +166,12 b' $self.bug_tracker'
167 166 rpt_add = report.append
168 167
169 168 rpt_add('*'*75+'\n\n'+'IPython post-mortem report\n\n')
170 rpt_add('IPython version: %s \n\n' % Release.version)
171 rpt_add('BZR revision : %s \n\n' % Release.revision)
169 rpt_add('IPython version: %s \n\n' % release.version)
170 rpt_add('BZR revision : %s \n\n' % release.revision)
172 171 rpt_add('Platform info : os.name -> %s, sys.platform -> %s' %
173 172 (os.name,sys.platform) )
174 173 rpt_add(sec_sep+'Current user configuration structure:\n\n')
175 rpt_add(pformat(self.IP.rc.dict()))
174 rpt_add(pformat(self.IP.dict()))
176 175 rpt_add(sec_sep+'Crash traceback:\n\n' + traceback)
177 176 try:
178 177 rpt_add(sec_sep+"History of session input:")
@@ -191,12 +190,12 b' class IPythonCrashHandler(CrashHandler):'
191 190 def __init__(self,IP):
192 191
193 192 # Set here which of the IPython authors should be listed as contact
194 AUTHOR_CONTACT = 'Ville'
193 AUTHOR_CONTACT = 'Fernando'
195 194
196 195 # Set argument defaults
197 196 app_name = 'IPython'
198 197 bug_tracker = 'https://bugs.launchpad.net/ipython/+filebug'
199 contact_name,contact_email = Release.authors[AUTHOR_CONTACT][:2]
198 contact_name,contact_email = release.authors[AUTHOR_CONTACT][:2]
200 199 crash_report_fname = 'IPython_crash_report.txt'
201 200 # Call parent constructor
202 201 CrashHandler.__init__(self,IP,app_name,contact_name,contact_email,
@@ -211,12 +210,12 b' class IPythonCrashHandler(CrashHandler):'
211 210 rpt_add = report.append
212 211
213 212 rpt_add('*'*75+'\n\n'+'IPython post-mortem report\n\n')
214 rpt_add('IPython version: %s \n\n' % Release.version)
215 rpt_add('BZR revision : %s \n\n' % Release.revision)
213 rpt_add('IPython version: %s \n\n' % release.version)
214 rpt_add('BZR revision : %s \n\n' % release.revision)
216 215 rpt_add('Platform info : os.name -> %s, sys.platform -> %s' %
217 216 (os.name,sys.platform) )
218 217 rpt_add(sec_sep+'Current user configuration structure:\n\n')
219 rpt_add(pformat(self.IP.rc.dict()))
218 # rpt_add(pformat(self.IP.dict()))
220 219 rpt_add(sec_sep+'Crash traceback:\n\n' + traceback)
221 220 try:
222 221 rpt_add(sec_sep+"History of session input:")
@@ -31,9 +31,11 b' import linecache'
31 31 import os
32 32 import sys
33 33
34 from IPython import PyColorize, ColorANSI, ipapi
35 from IPython.genutils import Term
36 from IPython.excolors import exception_colors
34 from IPython.utils import PyColorize
35 from IPython.core import ipapi
36 from IPython.utils import coloransi
37 from IPython.utils.genutils import Term
38 from IPython.core.excolors import exception_colors
37 39
38 40 # See if we can use pydb.
39 41 has_pydb = False
@@ -68,6 +70,7 b' def BdbQuit_excepthook(et,ev,tb):'
68 70 def BdbQuit_IPython_excepthook(self,et,ev,tb):
69 71 print 'Exiting Debugger.'
70 72
73
71 74 class Tracer(object):
72 75 """Class for local debugging, similar to pdb.set_trace.
73 76
@@ -93,7 +96,7 b' class Tracer(object):'
93 96
94 97 Usage example:
95 98
96 from IPython.Debugger import Tracer; debug_here = Tracer()
99 from IPython.core.debugger import Tracer; debug_here = Tracer()
97 100
98 101 ... later in your code
99 102 debug_here() # -> will open up the debugger at that point.
@@ -103,25 +106,22 b' class Tracer(object):'
103 106 from the Python standard library for usage details.
104 107 """
105 108
106 global __IPYTHON__
107 109 try:
108 __IPYTHON__
109 except NameError:
110 ip = ipapi.get()
111 except:
110 112 # Outside of ipython, we set our own exception hook manually
111 __IPYTHON__ = ipapi.get(True,False)
112 113 BdbQuit_excepthook.excepthook_ori = sys.excepthook
113 114 sys.excepthook = BdbQuit_excepthook
114 115 def_colors = 'NoColor'
115 116 try:
116 117 # Limited tab completion support
117 import rlcompleter,readline
118 import readline
118 119 readline.parse_and_bind('tab: complete')
119 120 except ImportError:
120 121 pass
121 122 else:
122 123 # In ipython, we use its custom exception handler mechanism
123 ip = ipapi.get()
124 def_colors = ip.options.colors
124 def_colors = ip.colors
125 125 ip.set_custom_exc((bdb.BdbQuit,),BdbQuit_IPython_excepthook)
126 126
127 127 if colors is None:
@@ -136,6 +136,7 b' class Tracer(object):'
136 136
137 137 self.debugger.set_trace(sys._getframe().f_back)
138 138
139
139 140 def decorate_fn_with_doc(new_fn, old_fn, additional_text=""):
140 141 """Make new_fn have old_fn's doc string. This is particularly useful
141 142 for the do_... commands that hook into the help system.
@@ -147,6 +148,7 b' def decorate_fn_with_doc(new_fn, old_fn, additional_text=""):'
147 148 wrapper.__doc__ = old_fn.__doc__ + additional_text
148 149 return wrapper
149 150
151
150 152 def _file_lines(fname):
151 153 """Return the contents of a named file as a list of lines.
152 154
@@ -162,10 +164,10 b' def _file_lines(fname):'
162 164 outfile.close()
163 165 return out
164 166
167
165 168 class Pdb(OldPdb):
166 169 """Modified Pdb class, does not load readline."""
167 170
168 if sys.version[:3] >= '2.5' or has_pydb:
169 171 def __init__(self,color_scheme='NoColor',completekey=None,
170 172 stdin=None, stdout=None):
171 173
@@ -180,6 +182,8 b' class Pdb(OldPdb):'
180 182 # IPython changes...
181 183 self.is_pydb = has_pydb
182 184
185 self.shell = ipapi.get()
186
183 187 if self.is_pydb:
184 188
185 189 # iplib.py's ipalias seems to want pdb's checkline
@@ -191,8 +195,8 b' class Pdb(OldPdb):'
191 195 self.curframe = None
192 196 self.do_restart = self.new_do_restart
193 197
194 self.old_all_completions = __IPYTHON__.Completer.all_completions
195 __IPYTHON__.Completer.all_completions=self.all_completions
198 self.old_all_completions = self.shell.Completer.all_completions
199 self.shell.Completer.all_completions=self.all_completions
196 200
197 201 self.do_list = decorate_fn_with_doc(self.list_command_pydb,
198 202 OldPdb.do_list)
@@ -207,54 +211,7 b' class Pdb(OldPdb):'
207 211 self.color_scheme_table = exception_colors()
208 212
209 213 # shorthands
210 C = ColorANSI.TermColors
211 cst = self.color_scheme_table
212
213 cst['NoColor'].colors.breakpoint_enabled = C.NoColor
214 cst['NoColor'].colors.breakpoint_disabled = C.NoColor
215
216 cst['Linux'].colors.breakpoint_enabled = C.LightRed
217 cst['Linux'].colors.breakpoint_disabled = C.Red
218
219 cst['LightBG'].colors.breakpoint_enabled = C.LightRed
220 cst['LightBG'].colors.breakpoint_disabled = C.Red
221
222 self.set_colors(color_scheme)
223
224 # Add a python parser so we can syntax highlight source while
225 # debugging.
226 self.parser = PyColorize.Parser()
227
228
229 else:
230 # Ugly hack: for Python 2.3-2.4, we can't call the parent constructor,
231 # because it binds readline and breaks tab-completion. This means we
232 # have to COPY the constructor here.
233 def __init__(self,color_scheme='NoColor'):
234 bdb.Bdb.__init__(self)
235 cmd.Cmd.__init__(self,completekey=None) # don't load readline
236 self.prompt = 'ipdb> ' # The default prompt is '(Pdb)'
237 self.aliases = {}
238
239 # These two lines are part of the py2.4 constructor, let's put them
240 # unconditionally here as they won't cause any problems in 2.3.
241 self.mainpyfile = ''
242 self._wait_for_mainpyfile = 0
243
244 # Read $HOME/.pdbrc and ./.pdbrc
245 try:
246 self.rcLines = _file_lines(os.path.join(os.environ['HOME'],
247 ".pdbrc"))
248 except KeyError:
249 self.rcLines = []
250 self.rcLines.extend(_file_lines(".pdbrc"))
251
252 # Create color table: we copy the default one from the traceback
253 # module and add a few attributes needed for debugging
254 self.color_scheme_table = exception_colors()
255
256 # shorthands
257 C = ColorANSI.TermColors
214 C = coloransi.TermColors
258 215 cst = self.color_scheme_table
259 216
260 217 cst['NoColor'].colors.breakpoint_enabled = C.NoColor
@@ -277,28 +234,28 b' class Pdb(OldPdb):'
277 234 self.color_scheme_table.set_active_scheme(scheme)
278 235
279 236 def interaction(self, frame, traceback):
280 __IPYTHON__.set_completer_frame(frame)
237 self.shell.set_completer_frame(frame)
281 238 OldPdb.interaction(self, frame, traceback)
282 239
283 240 def new_do_up(self, arg):
284 241 OldPdb.do_up(self, arg)
285 __IPYTHON__.set_completer_frame(self.curframe)
242 self.shell.set_completer_frame(self.curframe)
286 243 do_u = do_up = decorate_fn_with_doc(new_do_up, OldPdb.do_up)
287 244
288 245 def new_do_down(self, arg):
289 246 OldPdb.do_down(self, arg)
290 __IPYTHON__.set_completer_frame(self.curframe)
247 self.shell.set_completer_frame(self.curframe)
291 248
292 249 do_d = do_down = decorate_fn_with_doc(new_do_down, OldPdb.do_down)
293 250
294 251 def new_do_frame(self, arg):
295 252 OldPdb.do_frame(self, arg)
296 __IPYTHON__.set_completer_frame(self.curframe)
253 self.shell.set_completer_frame(self.curframe)
297 254
298 255 def new_do_quit(self, arg):
299 256
300 257 if hasattr(self, 'old_all_completions'):
301 __IPYTHON__.Completer.all_completions=self.old_all_completions
258 self.shell.Completer.all_completions=self.old_all_completions
302 259
303 260
304 261 return OldPdb.do_quit(self, arg)
@@ -312,7 +269,7 b' class Pdb(OldPdb):'
312 269 return self.do_quit(arg)
313 270
314 271 def postloop(self):
315 __IPYTHON__.set_completer_frame(None)
272 self.shell.set_completer_frame(None)
316 273
317 274 def print_stack_trace(self):
318 275 try:
@@ -329,7 +286,7 b' class Pdb(OldPdb):'
329 286 # vds: >>
330 287 frame, lineno = frame_lineno
331 288 filename = frame.f_code.co_filename
332 __IPYTHON__.hooks.synchronize_with_editor(filename, lineno, 0)
289 self.shell.hooks.synchronize_with_editor(filename, lineno, 0)
333 290 # vds: <<
334 291
335 292 def format_stack_entry(self, frame_lineno, lprefix=': ', context = 3):
@@ -498,7 +455,7 b' class Pdb(OldPdb):'
498 455 # vds: >>
499 456 lineno = first
500 457 filename = self.curframe.f_code.co_filename
501 __IPYTHON__.hooks.synchronize_with_editor(filename, lineno, 0)
458 self.shell.hooks.synchronize_with_editor(filename, lineno, 0)
502 459 # vds: <<
503 460
504 461 do_l = do_list
@@ -507,16 +464,16 b' class Pdb(OldPdb):'
507 464 """The debugger interface to magic_pdef"""
508 465 namespaces = [('Locals', self.curframe.f_locals),
509 466 ('Globals', self.curframe.f_globals)]
510 __IPYTHON__.magic_pdef(arg, namespaces=namespaces)
467 self.shell.magic_pdef(arg, namespaces=namespaces)
511 468
512 469 def do_pdoc(self, arg):
513 470 """The debugger interface to magic_pdoc"""
514 471 namespaces = [('Locals', self.curframe.f_locals),
515 472 ('Globals', self.curframe.f_globals)]
516 __IPYTHON__.magic_pdoc(arg, namespaces=namespaces)
473 self.shell.magic_pdoc(arg, namespaces=namespaces)
517 474
518 475 def do_pinfo(self, arg):
519 476 """The debugger equivalant of ?obj"""
520 477 namespaces = [('Locals', self.curframe.f_locals),
521 478 ('Globals', self.curframe.f_globals)]
522 __IPYTHON__.magic_pinfo("pinfo %s" % arg, namespaces=namespaces)
479 self.shell.magic_pinfo("pinfo %s" % arg, namespaces=namespaces)
@@ -12,7 +12,7 b' Color schemes for exception handling code in IPython.'
12 12
13 13 #****************************************************************************
14 14 # Required modules
15 from IPython.ColorANSI import ColorSchemeTable, TermColors, ColorScheme
15 from IPython.utils.coloransi import ColorSchemeTable, TermColors, ColorScheme
16 16
17 17 def exception_colors():
18 18 """Return a color table with fields for exception reporting.
@@ -34,9 +34,9 b' def exception_colors():'
34 34 >>> ec.active_scheme_name
35 35 'NoColor'
36 36 >>> ec.active_colors.keys()
37 ['em', 'caret', '__allownew', 'name', 'val', 'vName', 'Normal', 'normalEm',
38 'filename', 'linenoEm', 'excName', 'lineno', 'valEm', 'filenameEm',
39 'nameEm', 'line', 'topline']
37 ['em', 'filenameEm', 'excName', 'valEm', 'nameEm', 'line', 'topline',
38 'name', 'caret', 'val', 'vName', 'Normal', 'filename', 'linenoEm',
39 'lineno', 'normalEm']
40 40 """
41 41
42 42 ex_colors = ColorSchemeTable()
1 NO CONTENT: file renamed from IPython/FakeModule.py to IPython/core/fakemodule.py
@@ -5,9 +5,8 b''
5 5 import fnmatch
6 6 import os
7 7
8 # IPython imports
9 from IPython.genutils import Term, ask_yes_no, warn
10 import IPython.ipapi
8 from IPython.utils.genutils import Term, ask_yes_no, warn
9 from IPython.core import ipapi
11 10
12 11 def magic_history(self, parameter_s = ''):
13 12 """Print input history (_i<n> variables), with most recent last.
@@ -48,9 +47,7 b" def magic_history(self, parameter_s = ''):"
48 47 confirmation first if it already exists.
49 48 """
50 49
51 ip = self.api
52 shell = self.shell
53 if not shell.outputcache.do_full_cache:
50 if not self.outputcache.do_full_cache:
54 51 print 'This feature is only available if numbered prompts are in use.'
55 52 return
56 53 opts,args = self.parse_options(parameter_s,'gntsrf:',mode='list')
@@ -72,11 +69,11 b" def magic_history(self, parameter_s = ''):"
72 69 close_at_end = True
73 70
74 71 if 't' in opts:
75 input_hist = shell.input_hist
72 input_hist = self.input_hist
76 73 elif 'r' in opts:
77 input_hist = shell.input_hist_raw
74 input_hist = self.input_hist_raw
78 75 else:
79 input_hist = shell.input_hist
76 input_hist = self.input_hist
80 77
81 78 default_length = 40
82 79 pattern = None
@@ -106,7 +103,7 b" def magic_history(self, parameter_s = ''):"
106 103
107 104 found = False
108 105 if pattern is not None:
109 sh = ip.IP.shadowhist.all()
106 sh = self.shadowhist.all()
110 107 for idx, s in sh:
111 108 if fnmatch.fnmatch(s, pattern):
112 109 print "0%d: %s" %(idx, s)
@@ -169,9 +166,8 b' def rep_f(self, arg):'
169 166 """
170 167
171 168 opts,args = self.parse_options(arg,'',mode='list')
172 ip = self.api
173 169 if not args:
174 ip.set_next_input(str(ip.user_ns["_"]))
170 self.set_next_input(str(self.user_ns["_"]))
175 171 return
176 172
177 173 if len(args) == 1 and not '-' in args[0]:
@@ -180,33 +176,33 b' def rep_f(self, arg):'
180 176 # get from shadow hist
181 177 num = int(arg[1:])
182 178 line = self.shadowhist.get(num)
183 ip.set_next_input(str(line))
179 self.set_next_input(str(line))
184 180 return
185 181 try:
186 182 num = int(args[0])
187 ip.set_next_input(str(ip.IP.input_hist_raw[num]).rstrip())
183 self.set_next_input(str(self.input_hist_raw[num]).rstrip())
188 184 return
189 185 except ValueError:
190 186 pass
191 187
192 for h in reversed(self.shell.input_hist_raw):
188 for h in reversed(self.input_hist_raw):
193 189 if 'rep' in h:
194 190 continue
195 191 if fnmatch.fnmatch(h,'*' + arg + '*'):
196 ip.set_next_input(str(h).rstrip())
192 self.set_next_input(str(h).rstrip())
197 193 return
198 194
199 195 try:
200 196 lines = self.extract_input_slices(args, True)
201 197 print "lines",lines
202 ip.runlines(lines)
198 self.runlines(lines)
203 199 except ValueError:
204 200 print "Not found in recent history:", args
205 201
206 202
207 203 _sentinel = object()
208 204
209 class ShadowHist:
205 class ShadowHist(object):
210 206 def __init__(self,db):
211 207 # cmd => idx mapping
212 208 self.curidx = 0
@@ -229,7 +225,7 b' class ShadowHist:'
229 225 #print "new",newidx # dbg
230 226 self.db.hset('shadowhist',ent, newidx)
231 227 except:
232 IPython.ipapi.get().IP.showtraceback()
228 ipapi.get().showtraceback()
233 229 print "WARNING: disabling shadow history"
234 230 self.disabled = True
235 231
@@ -251,8 +247,8 b' class ShadowHist:'
251 247 def init_ipython(ip):
252 248 import ipy_completers
253 249
254 ip.expose_magic("rep",rep_f)
255 ip.expose_magic("hist",magic_hist)
256 ip.expose_magic("history",magic_history)
250 ip.define_magic("rep",rep_f)
251 ip.define_magic("hist",magic_hist)
252 ip.define_magic("history",magic_history)
257 253
258 254 ipy_completers.quick_completer('%hist' ,'-g -t -r -n')
@@ -19,14 +19,14 b" For example, suppose that you have a module called 'myiphooks' in your"
19 19 PYTHONPATH, which contains the following definition:
20 20
21 21 import os
22 import IPython.ipapi
23 ip = IPython.ipapi.get()
22 from IPython.core import ipapi
23 ip = ipapi.get()
24 24
25 25 def calljed(self,filename, linenum):
26 26 "My editor hook calls the jed editor directly."
27 27 print "Calling my own editor, jed ..."
28 28 if os.system('jed +%d %s' % (linenum,filename)) != 0:
29 raise ipapi.TryNext()
29 raise TryNext()
30 30
31 31 ip.set_hook('editor', calljed)
32 32
@@ -41,22 +41,21 b' somewhere in your configuration files or ipython command line.'
41 41 # the file COPYING, distributed as part of this software.
42 42 #*****************************************************************************
43 43
44 from IPython import ipapi
45
46 44 import os,bisect
47 45 import sys
48 from genutils import Term,shell
46 from IPython.utils.genutils import Term, shell
49 47 from pprint import PrettyPrinter
50 48
49 from IPython.core.error import TryNext
50
51 51 # List here all the default hooks. For now it's just the editor functions
52 52 # but over time we'll move here all the public API for user-accessible things.
53 # vds: >>
53
54 54 __all__ = ['editor', 'fix_error_editor', 'synchronize_with_editor', 'result_display',
55 55 'input_prefilter', 'shutdown_hook', 'late_startup_hook',
56 56 'generate_prompt', 'generate_output_prompt','shell_hook',
57 57 'show_in_pager','pre_prompt_hook', 'pre_runcode_hook',
58 58 'clipboard_get']
59 # vds: <<
60 59
61 60 pformat = PrettyPrinter().pformat
62 61
@@ -69,7 +68,7 b' def editor(self,filename, linenum=None):'
69 68
70 69 # IPython configures a default editor at startup by reading $EDITOR from
71 70 # the environment, and falling back on vi (unix) or notepad (win32).
72 editor = self.rc.editor
71 editor = self.editor
73 72
74 73 # marker for at which line to open the file (for existing objects)
75 74 if linenum is None or editor=='notepad':
@@ -83,7 +82,7 b' def editor(self,filename, linenum=None):'
83 82
84 83 # Call the actual editor
85 84 if os.system('%s %s %s' % (editor,linemark,filename)) != 0:
86 raise ipapi.TryNext()
85 raise TryNext()
87 86
88 87 import tempfile
89 88 def fix_error_editor(self,filename,linenum,column,msg):
@@ -99,20 +98,20 b' def fix_error_editor(self,filename,linenum,column,msg):'
99 98 t.write('%s:%d:%d:%s\n' % (filename,linenum,column,msg))
100 99 t.flush()
101 100 return t
102 if os.path.basename(self.rc.editor) != 'vim':
101 if os.path.basename(self.editor) != 'vim':
103 102 self.hooks.editor(filename,linenum)
104 103 return
105 104 t = vim_quickfix_file()
106 105 try:
107 106 if os.system('vim --cmd "set errorformat=%f:%l:%c:%m" -q ' + t.name):
108 raise ipapi.TryNext()
107 raise TryNext()
109 108 finally:
110 109 t.close()
111 110
112 # vds: >>
111
113 112 def synchronize_with_editor(self, filename, linenum, column):
114 113 pass
115 # vds: <<
114
116 115
117 116 class CommandChainDispatcher:
118 117 """ Dispatch calls to a chain of commands until some func can handle it
@@ -140,12 +139,12 b' class CommandChainDispatcher:'
140 139 try:
141 140 ret = cmd(*args, **kw)
142 141 return ret
143 except ipapi.TryNext, exc:
142 except TryNext, exc:
144 143 if exc.args or exc.kwargs:
145 144 args = exc.args
146 145 kw = exc.kwargs
147 146 # if no function will accept it, raise TryNext up to the caller
148 raise ipapi.TryNext
147 raise TryNext
149 148
150 149 def __str__(self):
151 150 return str(self.chain)
@@ -161,13 +160,14 b' class CommandChainDispatcher:'
161 160 """
162 161 return iter(self.chain)
163 162
163
164 164 def result_display(self,arg):
165 165 """ Default display hook.
166 166
167 167 Called for displaying the result to the user.
168 168 """
169 169
170 if self.rc.pprint:
170 if self.pprint:
171 171 out = pformat(arg)
172 172 if '\n' in out:
173 173 # So that multi-line strings line up with the left column of
@@ -183,6 +183,7 b' def result_display(self,arg):'
183 183 # the default display hook doesn't manipulate the value to put in history
184 184 return None
185 185
186
186 187 def input_prefilter(self,line):
187 188 """ Default input prefilter
188 189
@@ -197,6 +198,7 b' def input_prefilter(self,line):'
197 198 #print "attempt to rewrite",line #dbg
198 199 return line
199 200
201
200 202 def shutdown_hook(self):
201 203 """ default shutdown hook
202 204
@@ -206,32 +208,36 b' def shutdown_hook(self):'
206 208 #print "default shutdown hook ok" # dbg
207 209 return
208 210
211
209 212 def late_startup_hook(self):
210 213 """ Executed after ipython has been constructed and configured
211 214
212 215 """
213 216 #print "default startup hook ok" # dbg
214 217
218
215 219 def generate_prompt(self, is_continuation):
216 220 """ calculate and return a string with the prompt to display """
217 ip = self.api
218 221 if is_continuation:
219 return str(ip.IP.outputcache.prompt2)
220 return str(ip.IP.outputcache.prompt1)
222 return str(self.outputcache.prompt2)
223 return str(self.outputcache.prompt1)
224
221 225
222 226 def generate_output_prompt(self):
223 ip = self.api
224 return str(ip.IP.outputcache.prompt_out)
227 return str(self.outputcache.prompt_out)
228
225 229
226 230 def shell_hook(self,cmd):
227 231 """ Run system/shell command a'la os.system() """
228 232
229 shell(cmd, header=self.rc.system_header, verbose=self.rc.system_verbose)
233 shell(cmd, header=self.system_header, verbose=self.system_verbose)
234
230 235
231 236 def show_in_pager(self,s):
232 237 """ Run a string through pager """
233 238 # raising TryNext here will use the default paging functionality
234 raise ipapi.TryNext
239 raise TryNext
240
235 241
236 242 def pre_prompt_hook(self):
237 243 """ Run before displaying the next prompt
@@ -242,15 +248,19 b' def pre_prompt_hook(self):'
242 248
243 249 return None
244 250
251
245 252 def pre_runcode_hook(self):
246 253 """ Executed before running the (prefiltered) code in IPython """
247 254 return None
248 255
256
249 257 def clipboard_get(self):
250 258 """ Get text from the clipboard.
251 259 """
252 from IPython.clipboard import (osx_clipboard_get, tkinter_clipboard_get,
253 win32_clipboard_get)
260 from IPython.lib.clipboard import (
261 osx_clipboard_get, tkinter_clipboard_get,
262 win32_clipboard_get
263 )
254 264 if sys.platform == 'win32':
255 265 chain = [win32_clipboard_get, tkinter_clipboard_get]
256 266 elif sys.platform == 'darwin':
1 NO CONTENT: file renamed from IPython/Logger.py to IPython/core/logger.py
@@ -7,10 +7,8 b''
7 7 # the file COPYING, distributed as part of this software.
8 8 #*****************************************************************************
9 9
10 import IPython.ipapi
11
12 from IPython.genutils import Term
13 from IPython.ipapi import IPyAutocall
10 from IPython.utils.genutils import Term
11 from IPython.core.autocall import IPyAutocall
14 12
15 13 class Macro(IPyAutocall):
16 14 """Simple class to store the value of macros as strings.
@@ -21,6 +21,7 b' import os'
21 21 import pdb
22 22 import pydoc
23 23 import sys
24 import shutil
24 25 import re
25 26 import tempfile
26 27 import time
@@ -43,17 +44,20 b' except ImportError:'
43 44
44 45 # Homebrewed
45 46 import IPython
46 from IPython import Debugger, OInspect, wildcard
47 from IPython.FakeModule import FakeModule
48 from IPython.Itpl import Itpl, itpl, printpl,itplns
49 from IPython.PyColorize import Parser
50 from IPython.ipstruct import Struct
51 from IPython.macro import Macro
52 from IPython.genutils import *
53 from IPython import platutils
54 import IPython.generics
55 import IPython.ipapi
56 from IPython.ipapi import UsageError
47 from IPython.utils import wildcard
48 from IPython.core import debugger, oinspect
49 from IPython.core.error import TryNext
50 from IPython.core.fakemodule import FakeModule
51 from IPython.core.prefilter import ESC_MAGIC
52 from IPython.external.Itpl import Itpl, itpl, printpl,itplns
53 from IPython.utils.PyColorize import Parser
54 from IPython.utils.ipstruct import Struct
55 from IPython.core.macro import Macro
56 from IPython.utils.genutils import *
57 from IPython.core.page import page
58 from IPython.utils import platutils
59 import IPython.utils.generics
60 from IPython.core.error import UsageError
57 61 from IPython.testing import decorators as testdec
58 62
59 63 #***************************************************************************
@@ -203,9 +207,9 b' python-profiler package from non-free.""")'
203 207 namespaces = [ ('Interactive', self.shell.user_ns),
204 208 ('IPython internal', self.shell.internal_ns),
205 209 ('Python builtin', __builtin__.__dict__),
206 ('Alias', self.shell.alias_table),
210 ('Alias', self.shell.alias_manager.alias_table),
207 211 ]
208 alias_ns = self.shell.alias_table
212 alias_ns = self.shell.alias_manager.alias_table
209 213
210 214 # initialize results to 'null'
211 215 found = 0; obj = None; ospace = None; ds = None;
@@ -242,7 +246,7 b' python-profiler package from non-free.""")'
242 246
243 247 # Try to see if it's magic
244 248 if not found:
245 if oname.startswith(self.shell.ESC_MAGIC):
249 if oname.startswith(ESC_MAGIC):
246 250 oname = oname[1:]
247 251 obj = getattr(self,'magic_'+oname,None)
248 252 if obj is not None:
@@ -270,10 +274,10 b' python-profiler package from non-free.""")'
270 274 # Characters that need to be escaped for latex:
271 275 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
272 276 # Magic command names as headers:
273 cmd_name_re = re.compile(r'^(%s.*?):' % self.shell.ESC_MAGIC,
277 cmd_name_re = re.compile(r'^(%s.*?):' % ESC_MAGIC,
274 278 re.MULTILINE)
275 279 # Magic commands
276 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % self.shell.ESC_MAGIC,
280 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % ESC_MAGIC,
277 281 re.MULTILINE)
278 282 # Paragraph continue
279 283 par_re = re.compile(r'\\$',re.MULTILINE)
@@ -374,10 +378,10 b' python-profiler package from non-free.""")'
374 378 # Functions for IPython shell work (vars,funcs, config, etc)
375 379 def magic_lsmagic(self, parameter_s = ''):
376 380 """List currently available magic functions."""
377 mesc = self.shell.ESC_MAGIC
381 mesc = ESC_MAGIC
378 382 print 'Available magic functions:\n'+mesc+\
379 383 (' '+mesc).join(self.lsmagic())
380 print '\n' + Magic.auto_status[self.shell.rc.automagic]
384 print '\n' + Magic.auto_status[self.shell.automagic]
381 385 return None
382 386
383 387 def magic_magic(self, parameter_s = ''):
@@ -422,11 +426,11 b' python-profiler package from non-free.""")'
422 426
423 427
424 428 if mode == 'rest':
425 rest_docs.append('**%s%s**::\n\n\t%s\n\n' %(self.shell.ESC_MAGIC,
429 rest_docs.append('**%s%s**::\n\n\t%s\n\n' %(ESC_MAGIC,
426 430 fname,fndoc))
427 431
428 432 else:
429 magic_docs.append('%s%s:\n\t%s\n' %(self.shell.ESC_MAGIC,
433 magic_docs.append('%s%s:\n\t%s\n' %(ESC_MAGIC,
430 434 fname,fndoc))
431 435
432 436 magic_docs = ''.join(magic_docs)
@@ -469,22 +473,22 b' ipythonrc file, placing a line like:'
469 473
470 474 will define %pf as a new name for %profile.
471 475
472 You can also call magics in code using the ipmagic() function, which IPython
473 automatically adds to the builtin namespace. Type 'ipmagic?' for details.
476 You can also call magics in code using the magic() function, which IPython
477 automatically adds to the builtin namespace. Type 'magic?' for details.
474 478
475 479 For a list of the available magic functions, use %lsmagic. For a description
476 480 of any of them, type %magic_name?, e.g. '%cd?'.
477 481
478 482 Currently the magic system has the following functions:\n"""
479 483
480 mesc = self.shell.ESC_MAGIC
484 mesc = ESC_MAGIC
481 485 outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):"
482 486 "\n\n%s%s\n\n%s" % (outmsg,
483 487 magic_docs,mesc,mesc,
484 488 (' '+mesc).join(self.lsmagic()),
485 Magic.auto_status[self.shell.rc.automagic] ) )
489 Magic.auto_status[self.shell.automagic] ) )
486 490
487 page(outmsg,screen_lines=self.shell.rc.screen_length)
491 page(outmsg,screen_lines=self.shell.usable_screen_length)
488 492
489 493
490 494 def magic_autoindent(self, parameter_s = ''):
@@ -511,15 +515,14 b' Currently the magic system has the following functions:\\n"""'
511 515 delete the variable (del var), the previously shadowed magic function
512 516 becomes visible to automagic again."""
513 517
514 rc = self.shell.rc
515 518 arg = parameter_s.lower()
516 519 if parameter_s in ('on','1','true'):
517 rc.automagic = True
520 self.shell.automagic = True
518 521 elif parameter_s in ('off','0','false'):
519 rc.automagic = False
522 self.shell.automagic = False
520 523 else:
521 rc.automagic = not rc.automagic
522 print '\n' + Magic.auto_status[rc.automagic]
524 self.shell.automagic = not self.shell.automagic
525 print '\n' + Magic.auto_status[self.shell.automagic]
523 526
524 527 @testdec.skip_doctest
525 528 def magic_autocall(self, parameter_s = ''):
@@ -565,8 +568,6 b' Currently the magic system has the following functions:\\n"""'
565 568 # all-random (note for auto-testing)
566 569 """
567 570
568 rc = self.shell.rc
569
570 571 if parameter_s:
571 572 arg = int(parameter_s)
572 573 else:
@@ -577,18 +578,18 b' Currently the magic system has the following functions:\\n"""'
577 578 return
578 579
579 580 if arg in (0,1,2):
580 rc.autocall = arg
581 self.shell.autocall = arg
581 582 else: # toggle
582 if rc.autocall:
583 self._magic_state.autocall_save = rc.autocall
584 rc.autocall = 0
583 if self.shell.autocall:
584 self._magic_state.autocall_save = self.shell.autocall
585 self.shell.autocall = 0
585 586 else:
586 587 try:
587 rc.autocall = self._magic_state.autocall_save
588 self.shell.autocall = self._magic_state.autocall_save
588 589 except AttributeError:
589 rc.autocall = self._magic_state.autocall_save = 1
590 self.shell.autocall = self._magic_state.autocall_save = 1
590 591
591 print "Automatic calling is:",['OFF','Smart','Full'][rc.autocall]
592 print "Automatic calling is:",['OFF','Smart','Full'][self.shell.autocall]
592 593
593 594 def magic_system_verbose(self, parameter_s = ''):
594 595 """Set verbose printing of system calls.
@@ -600,9 +601,12 b' Currently the magic system has the following functions:\\n"""'
600 601 else:
601 602 val = None
602 603
603 self.shell.rc_set_toggle('system_verbose',val)
604 if self.shell.system_verbose:
605 self.shell.system_verbose = False
606 else:
607 self.shell.system_verbose = True
604 608 print "System verbose printing is:",\
605 ['OFF','ON'][self.shell.rc.system_verbose]
609 ['OFF','ON'][self.shell.system_verbose]
606 610
607 611
608 612 def magic_page(self, parameter_s=''):
@@ -632,8 +636,8 b' Currently the magic system has the following functions:\\n"""'
632 636
633 637 def magic_profile(self, parameter_s=''):
634 638 """Print your currently active IPyhton profile."""
635 if self.shell.rc.profile:
636 printpl('Current IPython profile: $self.shell.rc.profile.')
639 if self.shell.profile:
640 printpl('Current IPython profile: $self.shell.profile.')
637 641 else:
638 642 print 'No profile active.'
639 643
@@ -717,9 +721,9 b' Currently the magic system has the following functions:\\n"""'
717 721
718 722 if info.found:
719 723 try:
720 IPython.generics.inspect_object(info.obj)
724 IPython.utils.generics.inspect_object(info.obj)
721 725 return
722 except IPython.ipapi.TryNext:
726 except TryNext:
723 727 pass
724 728 # Get the docstring of the class property if it exists.
725 729 path = oname.split('.')
@@ -847,7 +851,7 b' Currently the magic system has the following functions:\\n"""'
847 851 elif opts.has_key('c'):
848 852 ignore_case = False
849 853 else:
850 ignore_case = not shell.rc.wildcards_case_sensitive
854 ignore_case = not shell.wildcards_case_sensitive
851 855
852 856 # Build list of namespaces to search from user options
853 857 def_search.extend(opt('s',[]))
@@ -972,7 +976,7 b' Currently the magic system has the following functions:\\n"""'
972 976 return self.shell.user_ns[i]
973 977
974 978 # some types are well known and can be shorter
975 abbrevs = {'IPython.macro.Macro' : 'Macro'}
979 abbrevs = {'IPython.core.macro.Macro' : 'Macro'}
976 980 def type_name(v):
977 981 tn = type(v).__name__
978 982 return abbrevs.get(tn,tn)
@@ -1131,7 +1135,6 b' Currently the magic system has the following functions:\\n"""'
1131 1135 log_raw_input = 'r' in opts
1132 1136 timestamp = 't' in opts
1133 1137
1134 rc = self.shell.rc
1135 1138 logger = self.shell.logger
1136 1139
1137 1140 # if no args are given, the defaults set in the logger constructor by
@@ -1148,11 +1151,12 b' Currently the magic system has the following functions:\\n"""'
1148 1151 # put logfname into rc struct as if it had been called on the command
1149 1152 # line, so it ends up saved in the log header Save it in case we need
1150 1153 # to restore it...
1151 old_logfile = rc.opts.get('logfile','')
1154 old_logfile = self.shell.logfile
1152 1155 if logfname:
1153 1156 logfname = os.path.expanduser(logfname)
1154 rc.opts.logfile = logfname
1155 loghead = self.shell.loghead_tpl % (rc.opts,rc.args)
1157 self.shell.logfile = logfname
1158
1159 loghead = '# IPython log file\n\n'
1156 1160 try:
1157 1161 started = logger.logstart(logfname,loghead,logmode,
1158 1162 log_output,timestamp,log_raw_input)
@@ -1265,7 +1269,6 b' Currently the magic system has the following functions:\\n"""'
1265 1269 If you want IPython to automatically do this on every exception, see
1266 1270 the %pdb magic for more details.
1267 1271 """
1268
1269 1272 self.shell.debugger(force=True)
1270 1273
1271 1274 @testdec.skip_doctest
@@ -1420,7 +1423,7 b' Currently the magic system has the following functions:\\n"""'
1420 1423 output = stdout_trap.getvalue()
1421 1424 output = output.rstrip()
1422 1425
1423 page(output,screen_lines=self.shell.rc.screen_length)
1426 page(output,screen_lines=self.shell.usable_screen_length)
1424 1427 print sys_exit,
1425 1428
1426 1429 dump_file = opts.D[0]
@@ -1561,14 +1564,14 b' Currently the magic system has the following functions:\\n"""'
1561 1564 filename = file_finder(arg_lst[0])
1562 1565 except IndexError:
1563 1566 warn('you must provide at least a filename.')
1564 print '\n%run:\n',OInspect.getdoc(self.magic_run)
1567 print '\n%run:\n',oinspect.getdoc(self.magic_run)
1565 1568 return
1566 1569 except IOError,msg:
1567 1570 error(msg)
1568 1571 return
1569 1572
1570 1573 if filename.lower().endswith('.ipy'):
1571 self.api.runlines(open(filename).read())
1574 self.safe_execfile_ipy(filename)
1572 1575 return
1573 1576
1574 1577 # Control the response to exit() calls made by the script being run
@@ -1621,7 +1624,7 b' Currently the magic system has the following functions:\\n"""'
1621 1624 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1622 1625 else:
1623 1626 if opts.has_key('d'):
1624 deb = Debugger.Pdb(self.shell.rc.colors)
1627 deb = debugger.Pdb(self.shell.colors)
1625 1628 # reset Breakpoint state, which is moronically kept
1626 1629 # in a class
1627 1630 bdb.Breakpoint.next = 1
@@ -1706,7 +1709,12 b' Currently the magic system has the following functions:\\n"""'
1706 1709 # (leaving dangling references).
1707 1710 self.shell.cache_main_mod(prog_ns,filename)
1708 1711 # update IPython interactive namespace
1709 del prog_ns['__name__']
1712
1713 # Some forms of read errors on the file may mean the
1714 # __name__ key was never set; using pop we don't have to
1715 # worry about a possible KeyError.
1716 prog_ns.pop('__name__', None)
1717
1710 1718 self.shell.user_ns.update(prog_ns)
1711 1719 finally:
1712 1720 # It's a bit of a mystery why, but __builtins__ can change from
@@ -1733,25 +1741,6 b' Currently the magic system has the following functions:\\n"""'
1733 1741
1734 1742 return stats
1735 1743
1736 def magic_runlog(self, parameter_s =''):
1737 """Run files as logs.
1738
1739 Usage:\\
1740 %runlog file1 file2 ...
1741
1742 Run the named files (treating them as log files) in sequence inside
1743 the interpreter, and return to the prompt. This is much slower than
1744 %run because each line is executed in a try/except block, but it
1745 allows running files with syntax errors in them.
1746
1747 Normally IPython will guess when a file is one of its own logfiles, so
1748 you can typically use %run even for logs. This shorthand allows you to
1749 force any file to be treated as a log file."""
1750
1751 for f in parameter_s.split():
1752 self.shell.safe_execfile(f,self.shell.user_ns,
1753 self.shell.user_ns,islog=1)
1754
1755 1744 @testdec.skip_doctest
1756 1745 def magic_timeit(self, parameter_s =''):
1757 1746 """Time execution of a Python statement or expression
@@ -2055,7 +2044,7 b' Currently the magic system has the following functions:\\n"""'
2055 2044 #print 'rng',ranges # dbg
2056 2045 lines = self.extract_input_slices(ranges,opts.has_key('r'))
2057 2046 macro = Macro(lines)
2058 self.shell.user_ns.update({name:macro})
2047 self.shell.define_macro(name, macro)
2059 2048 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
2060 2049 print 'Macro contents:'
2061 2050 print macro,
@@ -2252,7 +2241,7 b' Currently the magic system has the following functions:\\n"""'
2252 2241
2253 2242 If you wish to write your own editor hook, you can put it in a
2254 2243 configuration file which you load at startup time. The default hook
2255 is defined in the IPython.hooks module, and you can use that as a
2244 is defined in the IPython.core.hooks module, and you can use that as a
2256 2245 starting example for further modifications. That file also has
2257 2246 general instructions on how to set a new hook for use once you've
2258 2247 defined it."""
@@ -2385,7 +2374,7 b' Currently the magic system has the following functions:\\n"""'
2385 2374 sys.stdout.flush()
2386 2375 try:
2387 2376 self.shell.hooks.editor(filename,lineno)
2388 except IPython.ipapi.TryNext:
2377 except TryNext:
2389 2378 warn('Could not open editor')
2390 2379 return
2391 2380
@@ -2461,7 +2450,7 b' Currently the magic system has the following functions:\\n"""'
2461 2450 # local shortcut
2462 2451 shell = self.shell
2463 2452
2464 import IPython.rlineimpl as readline
2453 import IPython.utils.rlineimpl as readline
2465 2454
2466 2455 if not readline.have_readline and sys.platform == "win32":
2467 2456 msg = """\
@@ -2486,7 +2475,7 b' Defaulting color scheme to \'NoColor\'"""'
2486 2475 except:
2487 2476 color_switch_err('prompt')
2488 2477 else:
2489 shell.rc.colors = \
2478 shell.colors = \
2490 2479 shell.outputcache.color_table.active_scheme_name
2491 2480 # Set exception colors
2492 2481 try:
@@ -2503,7 +2492,7 b' Defaulting color scheme to \'NoColor\'"""'
2503 2492 color_switch_err('system exception handler')
2504 2493
2505 2494 # Set info (for 'object?') colors
2506 if shell.rc.color_info:
2495 if shell.color_info:
2507 2496 try:
2508 2497 shell.inspector.set_active_scheme(new_scheme)
2509 2498 except:
@@ -2522,17 +2511,17 b' Defaulting color scheme to \'NoColor\'"""'
2522 2511 than more) in your system, using colored object information displays
2523 2512 will not work properly. Test it and see."""
2524 2513
2525 self.shell.rc.color_info = 1 - self.shell.rc.color_info
2526 self.magic_colors(self.shell.rc.colors)
2514 self.shell.color_info = not self.shell.color_info
2515 self.magic_colors(self.shell.colors)
2527 2516 print 'Object introspection functions have now coloring:',
2528 print ['OFF','ON'][self.shell.rc.color_info]
2517 print ['OFF','ON'][int(self.shell.color_info)]
2529 2518
2530 2519 def magic_Pprint(self, parameter_s=''):
2531 2520 """Toggle pretty printing on/off."""
2532 2521
2533 self.shell.rc.pprint = 1 - self.shell.rc.pprint
2522 self.shell.pprint = 1 - self.shell.pprint
2534 2523 print 'Pretty printing has been turned', \
2535 ['OFF','ON'][self.shell.rc.pprint]
2524 ['OFF','ON'][self.shell.pprint]
2536 2525
2537 2526 def magic_exit(self, parameter_s=''):
2538 2527 """Exit IPython, confirming if configured to do so.
@@ -2611,52 +2600,27 b' Defaulting color scheme to \'NoColor\'"""'
2611 2600 par = parameter_s.strip()
2612 2601 if not par:
2613 2602 stored = self.db.get('stored_aliases', {} )
2614 atab = self.shell.alias_table
2615 aliases = atab.keys()
2616 aliases.sort()
2617 res = []
2618 showlast = []
2619 for alias in aliases:
2620 special = False
2621 try:
2622 tgt = atab[alias][1]
2623 except (TypeError, AttributeError):
2624 # unsubscriptable? probably a callable
2625 tgt = atab[alias]
2626 special = True
2627 # 'interesting' aliases
2628 if (alias in stored or
2629 special or
2630 alias.lower() != os.path.splitext(tgt)[0].lower() or
2631 ' ' in tgt):
2632 showlast.append((alias, tgt))
2633 else:
2634 res.append((alias, tgt ))
2635
2636 # show most interesting aliases last
2637 res.extend(showlast)
2603 aliases = sorted(self.shell.alias_manager.aliases)
2604 # for k, v in stored:
2605 # atab.append(k, v[0])
2606
2638 2607 print "Total number of aliases:",len(aliases)
2639 return res
2608 return aliases
2609
2610 # Now try to define a new one
2640 2611 try:
2641 2612 alias,cmd = par.split(None,1)
2642 2613 except:
2643 print OInspect.getdoc(self.magic_alias)
2644 else:
2645 nargs = cmd.count('%s')
2646 if nargs>0 and cmd.find('%l')>=0:
2647 error('The %s and %l specifiers are mutually exclusive '
2648 'in alias definitions.')
2649 else: # all looks OK
2650 self.shell.alias_table[alias] = (nargs,cmd)
2651 self.shell.alias_table_validate(verbose=0)
2614 print oinspect.getdoc(self.magic_alias)
2615 else:
2616 self.shell.alias_manager.soft_define_alias(alias, cmd)
2652 2617 # end magic_alias
2653 2618
2654 2619 def magic_unalias(self, parameter_s = ''):
2655 2620 """Remove an alias"""
2656 2621
2657 2622 aname = parameter_s.strip()
2658 if aname in self.shell.alias_table:
2659 del self.shell.alias_table[aname]
2623 self.shell.alias_manager.undefine_alias(aname)
2660 2624 stored = self.db.get('stored_aliases', {} )
2661 2625 if aname in stored:
2662 2626 print "Removing %stored alias",aname
@@ -2677,24 +2641,21 b' Defaulting color scheme to \'NoColor\'"""'
2677 2641 This function also resets the root module cache of module completer,
2678 2642 used on slow filesystems.
2679 2643 """
2680
2681
2682 ip = self.api
2644 from IPython.core.alias import InvalidAliasError
2683 2645
2684 2646 # for the benefit of module completer in ipy_completers.py
2685 del ip.db['rootmodules']
2647 del self.db['rootmodules']
2686 2648
2687 2649 path = [os.path.abspath(os.path.expanduser(p)) for p in
2688 2650 os.environ.get('PATH','').split(os.pathsep)]
2689 2651 path = filter(os.path.isdir,path)
2690 2652
2691 alias_table = self.shell.alias_table
2692 2653 syscmdlist = []
2654 # Now define isexec in a cross platform manner.
2693 2655 if os.name == 'posix':
2694 2656 isexec = lambda fname:os.path.isfile(fname) and \
2695 2657 os.access(fname,os.X_OK)
2696 2658 else:
2697
2698 2659 try:
2699 2660 winext = os.environ['pathext'].replace(';','|').replace('.','')
2700 2661 except KeyError:
@@ -2704,6 +2665,8 b' Defaulting color scheme to \'NoColor\'"""'
2704 2665 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
2705 2666 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
2706 2667 savedir = os.getcwd()
2668
2669 # Now walk the paths looking for executables to alias.
2707 2670 try:
2708 2671 # write the whole loop for posix/Windows so we don't have an if in
2709 2672 # the innermost part
@@ -2711,13 +2674,15 b' Defaulting color scheme to \'NoColor\'"""'
2711 2674 for pdir in path:
2712 2675 os.chdir(pdir)
2713 2676 for ff in os.listdir(pdir):
2714 if isexec(ff) and ff not in self.shell.no_alias:
2715 # each entry in the alias table must be (N,name),
2716 # where N is the number of positional arguments of the
2717 # alias.
2718 # Dots will be removed from alias names, since ipython
2719 # assumes names with dots to be python code
2720 alias_table[ff.replace('.','')] = (0,ff)
2677 if isexec(ff):
2678 try:
2679 # Removes dots from the name since ipython
2680 # will assume names with dots to be python.
2681 self.shell.alias_manager.define_alias(
2682 ff.replace('.',''), ff)
2683 except InvalidAliasError:
2684 pass
2685 else:
2721 2686 syscmdlist.append(ff)
2722 2687 else:
2723 2688 for pdir in path:
@@ -2727,17 +2692,15 b' Defaulting color scheme to \'NoColor\'"""'
2727 2692 if isexec(ff) and base.lower() not in self.shell.no_alias:
2728 2693 if ext.lower() == '.exe':
2729 2694 ff = base
2730 alias_table[base.lower().replace('.','')] = (0,ff)
2695 try:
2696 # Removes dots from the name since ipython
2697 # will assume names with dots to be python.
2698 self.shell.alias_manager.define_alias(
2699 base.lower().replace('.',''), ff)
2700 except InvalidAliasError:
2701 pass
2731 2702 syscmdlist.append(ff)
2732 # Make sure the alias table doesn't contain keywords or builtins
2733 self.shell.alias_table_validate()
2734 # Call again init_auto_alias() so we get 'rm -i' and other
2735 # modified aliases since %rehashx will probably clobber them
2736
2737 # no, we don't want them. if %rehashx clobbers them, good,
2738 # we'll probably get better versions
2739 # self.shell.init_auto_alias()
2740 db = ip.db
2703 db = self.db
2741 2704 db['syscmdlist'] = syscmdlist
2742 2705 finally:
2743 2706 os.chdir(savedir)
@@ -2847,9 +2810,8 b' Defaulting color scheme to \'NoColor\'"""'
2847 2810 if ps:
2848 2811 try:
2849 2812 os.chdir(os.path.expanduser(ps))
2850 if self.shell.rc.term_title:
2851 #print 'set term title:',self.shell.rc.term_title # dbg
2852 platutils.set_term_title('IPy ' + abbrev_cwd())
2813 if self.shell.term_title:
2814 platutils.set_term_title('IPython: ' + abbrev_cwd())
2853 2815 except OSError:
2854 2816 print sys.exc_info()[1]
2855 2817 else:
@@ -2861,8 +2823,8 b' Defaulting color scheme to \'NoColor\'"""'
2861 2823
2862 2824 else:
2863 2825 os.chdir(self.shell.home_dir)
2864 if self.shell.rc.term_title:
2865 platutils.set_term_title("IPy ~")
2826 if self.shell.term_title:
2827 platutils.set_term_title('IPython: ' + '~')
2866 2828 cwd = os.getcwd()
2867 2829 dhist = self.shell.user_ns['_dh']
2868 2830
@@ -3162,10 +3124,10 b' Defaulting color scheme to \'NoColor\'"""'
3162 3124 """
3163 3125
3164 3126 start = parameter_s.strip()
3165 esc_magic = self.shell.ESC_MAGIC
3127 esc_magic = ESC_MAGIC
3166 3128 # Identify magic commands even if automagic is on (which means
3167 3129 # the in-memory version is different from that typed by the user).
3168 if self.shell.rc.automagic:
3130 if self.shell.automagic:
3169 3131 start_magic = esc_magic+start
3170 3132 else:
3171 3133 start_magic = start
@@ -3259,7 +3221,7 b' Defaulting color scheme to \'NoColor\'"""'
3259 3221 return
3260 3222
3261 3223 page(self.shell.pycolorize(cont),
3262 screen_lines=self.shell.rc.screen_length)
3224 screen_lines=self.shell.usable_screen_length)
3263 3225
3264 3226 def _rerun_pasted(self):
3265 3227 """ Rerun a previously pasted command.
@@ -3273,7 +3235,7 b' Defaulting color scheme to \'NoColor\'"""'
3273 3235 def _get_pasted_lines(self, sentinel):
3274 3236 """ Yield pasted lines until the user enters the given sentinel value.
3275 3237 """
3276 from IPython import iplib
3238 from IPython.core import iplib
3277 3239 print "Pasting code; enter '%s' alone on the line to stop." % sentinel
3278 3240 while True:
3279 3241 l = iplib.raw_input_original(':')
@@ -3344,7 +3306,7 b' Defaulting color scheme to \'NoColor\'"""'
3344 3306
3345 3307 See also
3346 3308 --------
3347 %paste: automatically pull code from clipboard.
3309 paste: automatically pull code from clipboard.
3348 3310 """
3349 3311
3350 3312 opts,args = self.parse_options(parameter_s,'rs:',mode='string')
@@ -3364,7 +3326,8 b' Defaulting color scheme to \'NoColor\'"""'
3364 3326 """Allows you to paste & execute a pre-formatted code block from clipboard.
3365 3327
3366 3328 The text is pulled directly from the clipboard without user
3367 intervention.
3329 intervention and printed back on the screen before execution (unless
3330 the -q flag is given to force quiet mode).
3368 3331
3369 3332 The block is dedented prior to execution to enable execution of method
3370 3333 definitions. '>' and '+' characters at the beginning of a line are
@@ -3377,15 +3340,20 b' Defaulting color scheme to \'NoColor\'"""'
3377 3340 This assigns the pasted block to variable 'foo' as string, without
3378 3341 dedenting or executing it (preceding >>> and + is still stripped)
3379 3342
3380 '%paste -r' re-executes the block previously entered by cpaste.
3343 Options
3344 -------
3345
3346 -r: re-executes the block previously entered by cpaste.
3347
3348 -q: quiet mode: do not echo the pasted text back to the terminal.
3381 3349
3382 3350 IPython statements (magics, shell escapes) are not supported (yet).
3383 3351
3384 3352 See also
3385 3353 --------
3386 %cpaste: manually paste code into terminal until you mark its end.
3354 cpaste: manually paste code into terminal until you mark its end.
3387 3355 """
3388 opts,args = self.parse_options(parameter_s,'r:',mode='string')
3356 opts,args = self.parse_options(parameter_s,'rq',mode='string')
3389 3357 par = args.strip()
3390 3358 if opts.has_key('r'):
3391 3359 self._rerun_pasted()
@@ -3393,43 +3361,24 b' Defaulting color scheme to \'NoColor\'"""'
3393 3361
3394 3362 text = self.shell.hooks.clipboard_get()
3395 3363 block = self._strip_pasted_lines_for_code(text.splitlines())
3364
3365 # By default, echo back to terminal unless quiet mode is requested
3366 if not opts.has_key('q'):
3367 write = self.shell.write
3368 write(self.shell.pycolorize(block))
3369 if not block.endswith('\n'):
3370 write('\n')
3371 write("## -- End pasted text --\n")
3372
3396 3373 self._execute_block(block, par)
3397 3374
3398 3375 def magic_quickref(self,arg):
3399 3376 """ Show a quick reference sheet """
3400 import IPython.usage
3401 qr = IPython.usage.quick_reference + self.magic_magic('-brief')
3377 import IPython.core.usage
3378 qr = IPython.core.usage.quick_reference + self.magic_magic('-brief')
3402 3379
3403 3380 page(qr)
3404 3381
3405 def magic_upgrade(self,arg):
3406 """ Upgrade your IPython installation
3407
3408 This will copy the config files that don't yet exist in your
3409 ipython dir from the system config dir. Use this after upgrading
3410 IPython if you don't wish to delete your .ipython dir.
3411
3412 Call with -nolegacy to get rid of ipythonrc* files (recommended for
3413 new users)
3414
3415 """
3416 ip = self.getapi()
3417 ipinstallation = path(IPython.__file__).dirname()
3418 upgrade_script = '%s "%s"' % (sys.executable,ipinstallation / 'upgrade_dir.py')
3419 src_config = ipinstallation / 'UserConfig'
3420 userdir = path(ip.options.ipythondir)
3421 cmd = '%s "%s" "%s"' % (upgrade_script, src_config, userdir)
3422 print ">",cmd
3423 shell(cmd)
3424 if arg == '-nolegacy':
3425 legacy = userdir.files('ipythonrc*')
3426 print "Nuking legacy files:",legacy
3427
3428 [p.remove() for p in legacy]
3429 suffix = (sys.platform == 'win32' and '.ini' or '')
3430 (userdir / ('ipythonrc' + suffix)).write_text('# Empty, see ipy_user_conf.py\n')
3431
3432
3433 3382 def magic_doctest_mode(self,parameter_s=''):
3434 3383 """Toggle doctest mode on and off.
3435 3384
@@ -3451,13 +3400,12 b' Defaulting color scheme to \'NoColor\'"""'
3451 3400 """
3452 3401
3453 3402 # XXX - Fix this to have cleaner activate/deactivate calls.
3454 from IPython.Extensions import InterpreterPasteInput as ipaste
3455 from IPython.ipstruct import Struct
3403 from IPython.extensions import InterpreterPasteInput as ipaste
3404 from IPython.utils.ipstruct import Struct
3456 3405
3457 3406 # Shorthands
3458 3407 shell = self.shell
3459 3408 oc = shell.outputcache
3460 rc = shell.rc
3461 3409 meta = shell.meta
3462 3410 # dstore is a data store kept in the instance metadata bag to track any
3463 3411 # changes we make, so we can undo them later.
@@ -3466,12 +3414,12 b' Defaulting color scheme to \'NoColor\'"""'
3466 3414
3467 3415 # save a few values we'll need to recover later
3468 3416 mode = save_dstore('mode',False)
3469 save_dstore('rc_pprint',rc.pprint)
3417 save_dstore('rc_pprint',shell.pprint)
3470 3418 save_dstore('xmode',shell.InteractiveTB.mode)
3471 save_dstore('rc_separate_out',rc.separate_out)
3472 save_dstore('rc_separate_out2',rc.separate_out2)
3473 save_dstore('rc_prompts_pad_left',rc.prompts_pad_left)
3474 save_dstore('rc_separate_in',rc.separate_in)
3419 save_dstore('rc_separate_out',shell.separate_out)
3420 save_dstore('rc_separate_out2',shell.separate_out2)
3421 save_dstore('rc_prompts_pad_left',shell.prompts_pad_left)
3422 save_dstore('rc_separate_in',shell.separate_in)
3475 3423
3476 3424 if mode == False:
3477 3425 # turn on
@@ -3489,7 +3437,7 b' Defaulting color scheme to \'NoColor\'"""'
3489 3437 oc.prompt1.pad_left = oc.prompt2.pad_left = \
3490 3438 oc.prompt_out.pad_left = False
3491 3439
3492 rc.pprint = False
3440 shell.pprint = False
3493 3441
3494 3442 shell.magic_xmode('Plain')
3495 3443
@@ -3497,9 +3445,9 b' Defaulting color scheme to \'NoColor\'"""'
3497 3445 # turn off
3498 3446 ipaste.deactivate_prefilter()
3499 3447
3500 oc.prompt1.p_template = rc.prompt_in1
3501 oc.prompt2.p_template = rc.prompt_in2
3502 oc.prompt_out.p_template = rc.prompt_out
3448 oc.prompt1.p_template = shell.prompt_in1
3449 oc.prompt2.p_template = shell.prompt_in2
3450 oc.prompt_out.p_template = shell.prompt_out
3503 3451
3504 3452 oc.input_sep = oc.prompt1.sep = dstore.rc_separate_in
3505 3453
@@ -3518,4 +3466,115 b' Defaulting color scheme to \'NoColor\'"""'
3518 3466 print 'Doctest mode is:',
3519 3467 print ['OFF','ON'][dstore.mode]
3520 3468
3469 def magic_gui(self, parameter_s=''):
3470 """Enable or disable IPython GUI event loop integration.
3471
3472 %gui [-a] [GUINAME]
3473
3474 This magic replaces IPython's threaded shells that were activated
3475 using the (pylab/wthread/etc.) command line flags. GUI toolkits
3476 can now be enabled, disabled and swtiched at runtime and keyboard
3477 interrupts should work without any problems. The following toolkits
3478 are supports: wxPython, PyQt4, PyGTK, and Tk::
3479
3480 %gui wx # enable wxPython event loop integration
3481 %gui qt4|qt # enable PyQt4 event loop integration
3482 %gui gtk # enable PyGTK event loop integration
3483 %gui tk # enable Tk event loop integration
3484 %gui # disable all event loop integration
3485
3486 WARNING: after any of these has been called you can simply create
3487 an application object, but DO NOT start the event loop yourself, as
3488 we have already handled that.
3489
3490 If you want us to create an appropriate application object add the
3491 "-a" flag to your command::
3492
3493 %gui -a wx
3494
3495 This is highly recommended for most users.
3496 """
3497 from IPython.lib import inputhook
3498 if "-a" in parameter_s:
3499 app = True
3500 else:
3501 app = False
3502 if not parameter_s:
3503 inputhook.clear_inputhook()
3504 elif 'wx' in parameter_s:
3505 return inputhook.enable_wx(app)
3506 elif ('qt4' in parameter_s) or ('qt' in parameter_s):
3507 return inputhook.enable_qt4(app)
3508 elif 'gtk' in parameter_s:
3509 return inputhook.enable_gtk(app)
3510 elif 'tk' in parameter_s:
3511 return inputhook.enable_tk(app)
3512
3513 def magic_load_ext(self, module_str):
3514 """Load an IPython extension by its module name."""
3515 self.load_extension(module_str)
3516
3517 def magic_unload_ext(self, module_str):
3518 """Unload an IPython extension by its module name."""
3519 self.unload_extension(module_str)
3520
3521 def magic_reload_ext(self, module_str):
3522 """Reload an IPython extension by its module name."""
3523 self.reload_extension(module_str)
3524
3525 def magic_install_profiles(self, s):
3526 """Install the default IPython profiles into the .ipython dir.
3527
3528 If the default profiles have already been installed, they will not
3529 be overwritten. You can force overwriting them by using the ``-o``
3530 option::
3531
3532 In [1]: %install_profiles -o
3533 """
3534 if '-o' in s:
3535 overwrite = True
3536 else:
3537 overwrite = False
3538 from IPython.config import profile
3539 profile_dir = os.path.split(profile.__file__)[0]
3540 ipython_dir = self.ipython_dir
3541 files = os.listdir(profile_dir)
3542
3543 to_install = []
3544 for f in files:
3545 if f.startswith('ipython_config'):
3546 src = os.path.join(profile_dir, f)
3547 dst = os.path.join(ipython_dir, f)
3548 if (not os.path.isfile(dst)) or overwrite:
3549 to_install.append((f, src, dst))
3550 if len(to_install)>0:
3551 print "Installing profiles to: ", ipython_dir
3552 for (f, src, dst) in to_install:
3553 shutil.copy(src, dst)
3554 print " %s" % f
3555
3556 def magic_install_default_config(self, s):
3557 """Install IPython's default config file into the .ipython dir.
3558
3559 If the default config file (:file:`ipython_config.py`) is already
3560 installed, it will not be overwritten. You can force overwriting
3561 by using the ``-o`` option::
3562
3563 In [1]: %install_default_config
3564 """
3565 if '-o' in s:
3566 overwrite = True
3567 else:
3568 overwrite = False
3569 from IPython.config import default
3570 config_dir = os.path.split(default.__file__)[0]
3571 ipython_dir = self.ipython_dir
3572 default_config_file_name = 'ipython_config.py'
3573 src = os.path.join(config_dir, default_config_file_name)
3574 dst = os.path.join(ipython_dir, default_config_file_name)
3575 if (not os.path.isfile(dst)) or overwrite:
3576 shutil.copy(src, dst)
3577 print "Installing default config file: %s" % dst
3578
3579
3521 3580 # end Magic
@@ -27,11 +27,12 b' import sys'
27 27 import types
28 28
29 29 # IPython's own
30 from IPython import PyColorize
31 from IPython.genutils import page,indent,Term
32 from IPython.Itpl import itpl
33 from IPython.wildcard import list_namespace
34 from IPython.ColorANSI import *
30 from IPython.utils import PyColorize
31 from IPython.utils.genutils import indent, Term
32 from IPython.core.page import page
33 from IPython.external.Itpl import itpl
34 from IPython.utils.wildcard import list_namespace
35 from IPython.utils.coloransi import *
35 36
36 37 #****************************************************************************
37 38 # HACK!!! This is a crude fix for bugs in python 2.3's inspect module. We
1 NO CONTENT: file renamed from IPython/OutputTrap.py to IPython/core/outputtrap.py
@@ -20,23 +20,24 b' import sys'
20 20 import time
21 21
22 22 # IPython's own
23 from IPython import ColorANSI
24 from IPython import Release
23 from IPython.utils import coloransi
24 from IPython.core import release
25 25 from IPython.external.Itpl import ItplNS
26 from IPython.ipapi import TryNext
27 from IPython.ipstruct import Struct
28 from IPython.macro import Macro
26 from IPython.core.error import TryNext
27 from IPython.utils.ipstruct import Struct
28 from IPython.core.macro import Macro
29 import IPython.utils.generics
29 30
30 from IPython.genutils import *
31 from IPython.utils.genutils import *
31 32
32 33 #****************************************************************************
33 34 #Color schemes for Prompts.
34 35
35 PromptColors = ColorANSI.ColorSchemeTable()
36 InputColors = ColorANSI.InputTermColors # just a shorthand
37 Colors = ColorANSI.TermColors # just a shorthand
36 PromptColors = coloransi.ColorSchemeTable()
37 InputColors = coloransi.InputTermColors # just a shorthand
38 Colors = coloransi.TermColors # just a shorthand
38 39
39 PromptColors.add_scheme(ColorANSI.ColorScheme(
40 PromptColors.add_scheme(coloransi.ColorScheme(
40 41 'NoColor',
41 42 in_prompt = InputColors.NoColor, # Input prompt
42 43 in_number = InputColors.NoColor, # Input prompt number
@@ -50,7 +51,7 b' PromptColors.add_scheme(ColorANSI.ColorScheme('
50 51 ))
51 52
52 53 # make some schemes as instances so we can copy them for modification easily:
53 __PColLinux = ColorANSI.ColorScheme(
54 __PColLinux = coloransi.ColorScheme(
54 55 'Linux',
55 56 in_prompt = InputColors.Green,
56 57 in_number = InputColors.LightGreen,
@@ -169,7 +170,7 b' prompt_specials_color = {'
169 170 # Carriage return
170 171 r'\r': '\r',
171 172 # Release version
172 r'\v': Release.version,
173 r'\v': release.version,
173 174 # Root symbol ($ or #)
174 175 r'\$': ROOT_SYMBOL,
175 176 }
@@ -185,7 +186,7 b" prompt_specials_nocolor[r'\\#'] = '${self.cache.prompt_count}'"
185 186 # with a color name which may begin with a letter used by any other of the
186 187 # allowed specials. This of course means that \\C will never be allowed for
187 188 # anything else.
188 input_colors = ColorANSI.InputTermColors
189 input_colors = coloransi.InputTermColors
189 190 for _color in dir(input_colors):
190 191 if _color[0] != '_':
191 192 c_name = r'\C_'+_color
@@ -545,8 +546,11 b' class CachedOutput:'
545 546 # don't use print, puts an extra space
546 547 cout_write(self.output_sep)
547 548 outprompt = self.shell.hooks.generate_output_prompt()
549 # print "Got prompt: ", outprompt
548 550 if self.do_full_cache:
549 551 cout_write(outprompt)
552 else:
553 print "self.do_full_cache = False"
550 554
551 555 # and now call a possibly user-defined print mechanism
552 556 manipulated_val = self.display(arg)
@@ -573,7 +577,7 b' class CachedOutput:'
573 577 display, e.g. when your own objects need special formatting.
574 578 """
575 579 try:
576 return IPython.generics.result_display(arg)
580 return IPython.utils.generics.result_display(arg)
577 581 except TryNext:
578 582 return self.shell.hooks.result_display(arg)
579 583
@@ -2,8 +2,8 b''
2 2 """Release data for the IPython project."""
3 3
4 4 #*****************************************************************************
5 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
6 #
5 # Copyright (C) 2008-2009 The IPython Development Team
6 # Copyright (C) 2001-2008 Fernando Perez <fperez@colorado.edu>
7 7 # Copyright (c) 2001 Janko Hauser <jhauser@zscout.de> and Nathaniel Gray
8 8 # <n8gray@caltech.edu>
9 9 #
@@ -21,9 +21,9 b" name = 'ipython'"
21 21 # bdist_deb does not accept underscores (a Debian convention).
22 22
23 23 development = True # change this to False to do a release
24 version_base = '0.10'
24 version_base = '0.11'
25 25 branch = 'ipython'
26 revision = '1163'
26 revision = '1205'
27 27
28 28 if development:
29 29 if branch == 'ipython':
@@ -100,7 +100,7 b' site <http://launchpad.net/ipython>`_.'
100 100
101 101 license = 'BSD'
102 102
103 authors = {'Fernando' : ('Fernando Perez','fperez@colorado.edu'),
103 authors = {'Fernando' : ('Fernando Perez','fperez.net@gmail.com'),
104 104 'Janko' : ('Janko Hauser','jhauser@zscout.de'),
105 105 'Nathan' : ('Nathaniel Gray','n8gray@caltech.edu'),
106 106 'Ville' : ('Ville Vainio','vivainio@gmail.com'),
1 NO CONTENT: file renamed from IPython/shadowns.py to IPython/core/shadowns.py
1 NO CONTENT: file renamed from IPython/tools/tests/__init__.py to IPython/core/tests/__init__.py
@@ -31,4 +31,5 b' class A(object):'
31 31 a = A()
32 32
33 33 # Now, we force an exit, the caller will check that the del printout was given
34 _ip.IP.ask_exit()
34 _ip = get_ipython()
35 _ip.ask_exit()
@@ -18,7 +18,7 b' test_magic.py calls it.'
18 18 #-----------------------------------------------------------------------------
19 19 import sys
20 20
21 from IPython import ipapi
21 from IPython.core import ipapi
22 22
23 23 #-----------------------------------------------------------------------------
24 24 # Globals
1 NO CONTENT: file renamed from IPython/tests/tclass.py to IPython/core/tests/tclass.py
@@ -3,7 +3,7 b''
3 3
4 4 import nose.tools as nt
5 5
6 from IPython.FakeModule import FakeModule, init_fakemod_dict
6 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
7 7
8 8 # Make a fakemod and check a few properties
9 9 def test_mk_fakemod():
@@ -13,7 +13,9 b' import tempfile'
13 13 import nose.tools as nt
14 14
15 15 # our own packages
16 from IPython import ipapi, iplib
16 from IPython.core import iplib
17 from IPython.core import ipapi
18
17 19
18 20 #-----------------------------------------------------------------------------
19 21 # Globals
@@ -37,44 +39,19 b' if ip is None:'
37 39 from IPython.testing.plugin import ipdoctest
38 40 ip = ipapi.get()
39 41
40 IP = ip.IP # This is the actual IPython shell 'raw' object.
41
42 42 #-----------------------------------------------------------------------------
43 43 # Test functions
44 44 #-----------------------------------------------------------------------------
45 45
46 46 def test_reset():
47 47 """reset must clear most namespaces."""
48 IP.reset() # first, it should run without error
48 ip.reset() # first, it should run without error
49 49 # Then, check that most namespaces end up empty
50 for ns in IP.ns_refs_table:
51 if ns is IP.user_ns:
50 for ns in ip.ns_refs_table:
51 if ns is ip.user_ns:
52 52 # The user namespace is reset with some data, so we can't check for
53 53 # it being empty
54 54 continue
55 55 nt.assert_equals(len(ns),0)
56 56
57 57
No newline at end of file
58 # make sure that user_setup can be run re-entrantly in 'install' mode.
59 def test_user_setup():
60 # use a lambda to pass kwargs to the generator
61 user_setup = lambda a,k: iplib.user_setup(*a,**k)
62 kw = dict(mode='install', interactive=False)
63
64 # Call the user setup and verify that the directory exists
65 yield user_setup, (ip.options.ipythondir,''), kw
66 yield os.path.isdir, ip.options.ipythondir
67
68 # Now repeat the operation with a non-existent directory. Check both that
69 # the call succeeds and that the directory is created.
70 tmpdir = tempfile.mktemp(prefix='ipython-test-')
71 # Use a try with an empty except because try/finally doesn't work with a
72 # yield in Python 2.4.
73 try:
74 yield user_setup, (tmpdir,''), kw
75 yield os.path.isdir, tmpdir
76 except:
77 pass
78 # Clean up the temp dir once done
79 shutil.rmtree(tmpdir)
80 No newline at end of file
@@ -7,10 +7,11 b' import os'
7 7 import sys
8 8 import tempfile
9 9 import types
10 from cStringIO import StringIO
10 11
11 12 import nose.tools as nt
12 13
13 from IPython.platutils import find_cmd, get_long_path_name
14 from IPython.utils.platutils import find_cmd, get_long_path_name
14 15 from IPython.testing import decorators as dec
15 16 from IPython.testing import tools as tt
16 17
@@ -19,14 +20,15 b' from IPython.testing import tools as tt'
19 20
20 21 def test_rehashx():
21 22 # clear up everything
22 _ip.IP.alias_table.clear()
23 _ip = get_ipython()
24 _ip.alias_manager.alias_table.clear()
23 25 del _ip.db['syscmdlist']
24 26
25 27 _ip.magic('rehashx')
26 28 # Practically ALL ipython development systems will have more than 10 aliases
27 29
28 yield (nt.assert_true, len(_ip.IP.alias_table) > 10)
29 for key, val in _ip.IP.alias_table.items():
30 yield (nt.assert_true, len(_ip.alias_manager.alias_table) > 10)
31 for key, val in _ip.alias_manager.alias_table.items():
30 32 # we must strip dots from alias names
31 33 nt.assert_true('.' not in key)
32 34
@@ -43,7 +45,6 b' def doctest_hist_f():'
43 45 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
44 46
45 47 In [11]: %hist -n -f $tfile 3
46
47 48 """
48 49
49 50
@@ -52,7 +53,7 b' def doctest_hist_r():'
52 53
53 54 XXX - This test is not recording the output correctly. Not sure why...
54 55
55 In [20]: 'hist' in _ip.IP.lsmagic()
56 In [20]: 'hist' in _ip.lsmagic()
56 57 Out[20]: True
57 58
58 59 In [6]: x=1
@@ -65,11 +66,12 b' def doctest_hist_r():'
65 66 # This test is known to fail on win32.
66 67 # See ticket https://bugs.launchpad.net/bugs/366334
67 68 def test_obj_del():
69 _ip = get_ipython()
68 70 """Test that object's __del__ methods are called on exit."""
69 71 test_dir = os.path.dirname(__file__)
70 72 del_file = os.path.join(test_dir,'obj_del.py')
71 73 ipython_cmd = find_cmd('ipython')
72 out = _ip.IP.getoutput('%s %s' % (ipython_cmd, del_file))
74 out = _ip.getoutput('%s %s' % (ipython_cmd, del_file))
73 75 nt.assert_equals(out,'obj_del.py: object A deleted')
74 76
75 77
@@ -77,8 +79,8 b' def test_shist():'
77 79 # Simple tests of ShadowHist class - test generator.
78 80 import os, shutil, tempfile
79 81
80 from IPython.Extensions import pickleshare
81 from IPython.history import ShadowHist
82 from IPython.utils import pickleshare
83 from IPython.core.history import ShadowHist
82 84
83 85 tfile = tempfile.mktemp('','tmp-ipython-')
84 86
@@ -98,7 +100,7 b' def test_shist():'
98 100
99 101 @dec.skipif_not_numpy
100 102 def test_numpy_clear_array_undec():
101 from IPython.Extensions import clearcmd
103 from IPython.extensions import clearcmd
102 104
103 105 _ip.ex('import numpy as np')
104 106 _ip.ex('a = np.empty(2)')
@@ -124,7 +126,7 b' def doctest_refbug():'
124 126 """Very nasty problem with references held by multiple runs of a script.
125 127 See: https://bugs.launchpad.net/ipython/+bug/269966
126 128
127 In [1]: _ip.IP.clear_main_mod_cache()
129 In [1]: _ip.clear_main_mod_cache()
128 130
129 131 In [2]: run refbug
130 132
@@ -164,7 +166,6 b' def doctest_run_ns2():'
164 166 tclass.py: deleting object: C-first_pass
165 167 """
166 168
167 @dec.skip_win32
168 169 def doctest_run_builtins():
169 170 """Check that %run doesn't damage __builtins__ via a doctest.
170 171
@@ -177,24 +178,34 b' def doctest_run_builtins():'
177 178
178 179 In [2]: bid1 = id(__builtins__)
179 180
180 In [3]: f = tempfile.NamedTemporaryFile()
181 In [3]: fname = tempfile.mkstemp()[1]
182
183 In [3]: f = open(fname,'w')
181 184
182 185 In [4]: f.write('pass\\n')
183 186
184 187 In [5]: f.flush()
185 188
186 In [6]: print 'B1:',type(__builtins__)
187 B1: <type 'module'>
189 In [6]: print type(__builtins__)
190 <type 'module'>
188 191
189 In [7]: %run $f.name
192 In [7]: %run "$fname"
193
194 In [7]: f.close()
190 195
191 196 In [8]: bid2 = id(__builtins__)
192 197
193 In [9]: print 'B2:',type(__builtins__)
194 B2: <type 'module'>
198 In [9]: print type(__builtins__)
199 <type 'module'>
195 200
196 201 In [10]: bid1 == bid2
197 202 Out[10]: True
203
204 In [12]: try:
205 ....: os.unlink(fname)
206 ....: except:
207 ....: pass
208 ....:
198 209 """
199 210
200 211 # For some tests, it will be handy to organize them in a class with a common
@@ -204,34 +215,28 b' class TestMagicRun(object):'
204 215
205 216 def setup(self):
206 217 """Make a valid python temp file."""
207 f = tempfile.NamedTemporaryFile()
218 fname = tempfile.mkstemp()[1]
219 f = open(fname,'w')
208 220 f.write('pass\n')
209 221 f.flush()
210 222 self.tmpfile = f
223 self.fname = fname
211 224
212 225 def run_tmpfile(self):
226 _ip = get_ipython()
213 227 # This fails on Windows if self.tmpfile.name has spaces or "~" in it.
214 228 # See below and ticket https://bugs.launchpad.net/bugs/366353
215 _ip.magic('run %s' % self.tmpfile.name)
229 _ip.magic('run "%s"' % self.fname)
216 230
217 # See https://bugs.launchpad.net/bugs/366353
218 @dec.skip_if_not_win32
219 def test_run_tempfile_path(self):
220 tt.assert_equals(True,False,"%run doesn't work with tempfile paths on win32.")
221
222 # See https://bugs.launchpad.net/bugs/366353
223 @dec.skip_win32
224 231 def test_builtins_id(self):
225 232 """Check that %run doesn't damage __builtins__ """
226
233 _ip = get_ipython()
227 234 # Test that the id of __builtins__ is not modified by %run
228 235 bid1 = id(_ip.user_ns['__builtins__'])
229 236 self.run_tmpfile()
230 237 bid2 = id(_ip.user_ns['__builtins__'])
231 238 tt.assert_equals(bid1, bid2)
232 239
233 # See https://bugs.launchpad.net/bugs/366353
234 @dec.skip_win32
235 240 def test_builtins_type(self):
236 241 """Check that the type of __builtins__ doesn't change with %run.
237 242
@@ -239,33 +244,45 b' class TestMagicRun(object):'
239 244 be a dict (it should be a module) by a previous use of %run. So we
240 245 also check explicitly that it really is a module:
241 246 """
247 _ip = get_ipython()
242 248 self.run_tmpfile()
243 249 tt.assert_equals(type(_ip.user_ns['__builtins__']),type(sys))
244 250
245 # See https://bugs.launchpad.net/bugs/366353
246 @dec.skip_win32
247 251 def test_prompts(self):
248 252 """Test that prompts correctly generate after %run"""
249 253 self.run_tmpfile()
250 p2 = str(_ip.IP.outputcache.prompt2).strip()
254 _ip = get_ipython()
255 p2 = str(_ip.outputcache.prompt2).strip()
251 256 nt.assert_equals(p2[:3], '...')
252 257
253 258 def teardown(self):
254 259 self.tmpfile.close()
260 try:
261 os.unlink(self.fname)
262 except:
263 # On Windows, even though we close the file, we still can't delete
264 # it. I have no clue why
265 pass
255 266
256 267 # Multiple tests for clipboard pasting
257 268 def test_paste():
258
259 def paste(txt):
269 _ip = get_ipython()
270 def paste(txt, flags='-q'):
271 """Paste input text, by default in quiet mode"""
260 272 hooks.clipboard_get = lambda : txt
261 _ip.magic('paste')
273 _ip.magic('paste '+flags)
262 274
263 275 # Inject fake clipboard hook but save original so we can restore it later
264 hooks = _ip.IP.hooks
276 hooks = _ip.hooks
265 277 user_ns = _ip.user_ns
266 278 original_clip = hooks.clipboard_get
267 279
268 280 try:
281 # This try/except with an emtpy except clause is here only because
282 # try/yield/finally is invalid syntax in Python 2.4. This will be
283 # removed when we drop 2.4-compatibility, and the emtpy except below
284 # will be changed to a finally.
285
269 286 # Run tests with fake clipboard function
270 287 user_ns.pop('x', None)
271 288 paste('x=1')
@@ -285,6 +302,30 b' def test_paste():'
285 302 yield (nt.assert_equal, user_ns['x'], [1,2,3])
286 303 yield (nt.assert_equal, user_ns['y'], [1,4,9])
287 304
305 # Now, test that paste -r works
306 user_ns.pop('x', None)
307 yield (nt.assert_false, 'x' in user_ns)
308 _ip.magic('paste -r')
309 yield (nt.assert_equal, user_ns['x'], [1,2,3])
310
311 # Also test paste echoing, by temporarily faking the writer
312 w = StringIO()
313 writer = _ip.write
314 _ip.write = w.write
315 code = """
316 a = 100
317 b = 200"""
318 try:
319 paste(code,'')
320 out = w.getvalue()
321 finally:
322 _ip.write = writer
323 yield (nt.assert_equal, user_ns['a'], 100)
324 yield (nt.assert_equal, user_ns['b'], 200)
325 yield (nt.assert_equal, out, code+"\n## -- End pasted text --\n")
326
288 327 finally:
328 # This should be in a finally clause, instead of the bare except above.
289 329 # Restore original hook
290 330 hooks.clipboard_get = original_clip
331
@@ -1,6 +1,6 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 ultraTB.py -- Spice up your tracebacks!
3 ultratb.py -- Spice up your tracebacks!
4 4
5 5 * ColorTB
6 6 I've always found it a bit hard to visually parse tracebacks in Python. The
@@ -9,8 +9,8 b' traceback in a manner similar to what you would expect from a syntax-highlightin'
9 9 text editor.
10 10
11 11 Installation instructions for ColorTB:
12 import sys,ultraTB
13 sys.excepthook = ultraTB.ColorTB()
12 import sys,ultratb
13 sys.excepthook = ultratb.ColorTB()
14 14
15 15 * VerboseTB
16 16 I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds
@@ -37,8 +37,8 b' Note:'
37 37
38 38
39 39 Installation instructions for ColorTB:
40 import sys,ultraTB
41 sys.excepthook = ultraTB.VerboseTB()
40 import sys,ultratb
41 sys.excepthook = ultratb.VerboseTB()
42 42
43 43 Note: Much of the code in this module was lifted verbatim from the standard
44 44 library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'.
@@ -69,7 +69,8 b' possible inclusion in future releases.'
69 69 # the file COPYING, distributed as part of this software.
70 70 #*****************************************************************************
71 71
72 # Required modules
72 from __future__ import with_statement
73
73 74 import inspect
74 75 import keyword
75 76 import linecache
@@ -90,10 +91,12 b' from inspect import getsourcefile, getfile, getmodule,\\'
90 91
91 92 # IPython's own modules
92 93 # Modified pdb which doesn't damage IPython's readline handling
93 from IPython import Debugger, PyColorize, ipapi
94 from IPython.ipstruct import Struct
95 from IPython.excolors import exception_colors
96 from IPython.genutils import Term,uniq_stable,error,info
94 from IPython.utils import PyColorize
95 from IPython.core import debugger, ipapi
96 from IPython.core.display_trap import DisplayTrap
97 from IPython.utils.ipstruct import Struct
98 from IPython.core.excolors import exception_colors
99 from IPython.utils.genutils import Term, uniq_stable, error, info
97 100
98 101 # Globals
99 102 # amount of space to put line numbers before verbose tracebacks
@@ -102,7 +105,7 b' INDENT_SIZE = 8'
102 105 # Default color scheme. This is used, for example, by the traceback
103 106 # formatter. When running in an actual IPython instance, the user's rc.colors
104 107 # value is used, but havinga module global makes this functionality available
105 # to users of ultraTB who are NOT running inside ipython.
108 # to users of ultratb who are NOT running inside ipython.
106 109 DEFAULT_SCHEME = 'NoColor'
107 110
108 111 #---------------------------------------------------------------------------
@@ -267,10 +270,12 b' def _formatTracebackLines(lnum, index, lines, Colors, lvals=None,scheme=None):'
267 270
268 271 # This lets us get fully syntax-highlighted tracebacks.
269 272 if scheme is None:
270 try:
271 scheme = ipapi.get().IP.rc.colors
272 except:
273 ipinst = ipapi.get()
274 if ipinst is not None:
275 scheme = ipinst.colors
276 else:
273 277 scheme = DEFAULT_SCHEME
278
274 279 _line_format = _parser.format2
275 280
276 281 for line in lines:
@@ -320,7 +325,7 b' class TBTools:'
320 325 self.old_scheme = color_scheme # save initial value for toggles
321 326
322 327 if call_pdb:
323 self.pdb = Debugger.Pdb(self.color_scheme_table.active_scheme_name)
328 self.pdb = debugger.Pdb(self.color_scheme_table.active_scheme_name)
324 329 else:
325 330 self.pdb = None
326 331
@@ -489,7 +494,9 b' class ListTB(TBTools):'
489 494
490 495 # vds:>>
491 496 if have_filedata:
492 ipapi.get().IP.hooks.synchronize_with_editor(filename, lineno, 0)
497 ipinst = ipapi.get()
498 if ipinst is not None:
499 ipinst.hooks.synchronize_with_editor(filename, lineno, 0)
493 500 # vds:<<
494 501
495 502 return list
@@ -809,7 +816,9 b' class VerboseTB(TBTools):'
809 816 filepath, lnum = records[-1][1:3]
810 817 #print "file:", str(file), "linenb", str(lnum) # dbg
811 818 filepath = os.path.abspath(filepath)
812 ipapi.get().IP.hooks.synchronize_with_editor(filepath, lnum, 0)
819 ipinst = ipapi.get()
820 if ipinst is not None:
821 ipinst.hooks.synchronize_with_editor(filepath, lnum, 0)
813 822 # vds: <<
814 823
815 824 # return all our info assembled as a single string
@@ -837,12 +846,12 b' class VerboseTB(TBTools):'
837 846
838 847 if force or self.call_pdb:
839 848 if self.pdb is None:
840 self.pdb = Debugger.Pdb(
849 self.pdb = debugger.Pdb(
841 850 self.color_scheme_table.active_scheme_name)
842 851 # the system displayhook may have changed, restore the original
843 852 # for pdb
844 dhook = sys.displayhook
845 sys.displayhook = sys.__displayhook__
853 display_trap = DisplayTrap(None, sys.__displayhook__)
854 with display_trap:
846 855 self.pdb.reset()
847 856 # Find the right frame so we don't pop up inside ipython itself
848 857 if hasattr(self,'tb'):
@@ -851,13 +860,10 b' class VerboseTB(TBTools):'
851 860 etb = self.tb = sys.last_traceback
852 861 while self.tb.tb_next is not None:
853 862 self.tb = self.tb.tb_next
854 try:
855 863 if etb and etb.tb_next:
856 864 etb = etb.tb_next
857 865 self.pdb.botframe = etb.tb_frame
858 866 self.pdb.interaction(self.tb.tb_frame, self.tb)
859 finally:
860 sys.displayhook = dhook
861 867
862 868 if hasattr(self,'tb'):
863 869 del self.tb
@@ -6,6 +6,9 b''
6 6 # the file COPYING, distributed as part of this software.
7 7 #*****************************************************************************
8 8
9 import sys
10 from IPython.core import release
11
9 12 __doc__ = """
10 13 IPython -- An enhanced Interactive Python
11 14 =========================================
@@ -37,84 +40,7 b' USAGE'
37 40 in directories.
38 41
39 42 In the rest of this text, we will refer to this directory as
40 IPYTHONDIR.
41
42
43 SPECIAL THREADING OPTIONS
44 The following special options are ONLY valid at the beginning of the
45 command line, and not later. This is because they control the initial-
46 ization of ipython itself, before the normal option-handling mechanism
47 is active.
48
49 -gthread, -qthread, -q4thread, -wthread, -pylab
50
51 Only ONE of these can be given, and it can only be given as the
52 first option passed to IPython (it will have no effect in any
53 other position). They provide threading support for the GTK, QT
54 and WXWidgets toolkits, and for the matplotlib library.
55
56 With any of the first four options, IPython starts running a
57 separate thread for the graphical toolkit's operation, so that
58 you can open and control graphical elements from within an
59 IPython command line, without blocking. All four provide
60 essentially the same functionality, respectively for GTK, QT3,
61 QT4 and WXWidgets (via their Python interfaces).
62
63 Note that with -wthread, you can additionally use the -wxversion
64 option to request a specific version of wx to be used. This
65 requires that you have the 'wxversion' Python module installed,
66 which is part of recent wxPython distributions.
67
68 If -pylab is given, IPython loads special support for the mat-
69 plotlib library (http://matplotlib.sourceforge.net), allowing
70 interactive usage of any of its backends as defined in the
71 user's .matplotlibrc file. It automatically activates GTK, QT
72 or WX threading for IPyhton if the choice of matplotlib backend
73 requires it. It also modifies the %run command to correctly
74 execute (without blocking) any matplotlib-based script which
75 calls show() at the end.
76
77 -tk The -g/q/q4/wthread options, and -pylab (if matplotlib is
78 configured to use GTK, QT or WX), will normally block Tk
79 graphical interfaces. This means that when GTK, QT or WX
80 threading is active, any attempt to open a Tk GUI will result in
81 a dead window, and possibly cause the Python interpreter to
82 crash. An extra option, -tk, is available to address this
83 issue. It can ONLY be given as a SECOND option after any of the
84 above (-gthread, -qthread, q4thread, -wthread or -pylab).
85
86 If -tk is given, IPython will try to coordinate Tk threading
87 with GTK, QT or WX. This is however potentially unreliable, and
88 you will have to test on your platform and Python configuration
89 to determine whether it works for you. Debian users have
90 reported success, apparently due to the fact that Debian builds
91 all of Tcl, Tk, Tkinter and Python with pthreads support. Under
92 other Linux environments (such as Fedora Core 2/3), this option
93 has caused random crashes and lockups of the Python interpreter.
94 Under other operating systems (Mac OSX and Windows), you'll need
95 to try it to find out, since currently no user reports are
96 available.
97
98 There is unfortunately no way for IPython to determine at run-
99 time whether -tk will work reliably or not, so you will need to
100 do some experiments before relying on it for regular work.
101
102 A WARNING ABOUT SIGNALS AND THREADS
103
104 When any of the thread systems (GTK, QT or WX) are active, either
105 directly or via -pylab with a threaded backend, it is impossible to
106 interrupt long-running Python code via Ctrl-C. IPython can not pass
107 the KeyboardInterrupt exception (or the underlying SIGINT) across
108 threads, so any long-running process started from IPython will run to
109 completion, or will have to be killed via an external (OS-based)
110 mechanism.
111
112 To the best of my knowledge, this limitation is imposed by the Python
113 interpreter itself, and it comes from the difficulty of writing
114 portable signal/threaded code. If any user is an expert on this topic
115 and can suggest a better solution, I would love to hear about it. In
116 the IPython sources, look at the Shell.py module, and in particular at
117 the runcode() method.
43 IPYTHON_DIR.
118 44
119 45 REGULAR OPTIONS
120 46 After the above threading options have been given, regular options can
@@ -132,16 +58,6 b' REGULAR OPTIONS'
132 58 -h, --help
133 59 Show summary of options.
134 60
135 -pylab This can only be given as the first option passed to IPython (it
136 will have no effect in any other position). It adds special sup-
137 port for the matplotlib library (http://matplotlib.source-
138 forge.net), allowing interactive usage of any of its backends as
139 defined in the user's .matplotlibrc file. It automatically
140 activates GTK or WX threading for IPyhton if the choice of mat-
141 plotlib backend requires it. It also modifies the @run command
142 to correctly execute (without blocking) any matplotlib-based
143 script which calls show() at the end.
144
145 61 -autocall <val>
146 62 Make IPython automatically call any callable object even if you
147 63 didn't type explicit parentheses. For example, 'str 43' becomes
@@ -234,9 +150,9 b' REGULAR OPTIONS'
234 150 here (in case your default EDITOR is something like Emacs).
235 151
236 152 -ipythondir <name>
237 The name of your IPython configuration directory IPYTHONDIR.
153 The name of your IPython configuration directory IPYTHON_DIR.
238 154 This can also be specified through the environment variable
239 IPYTHONDIR.
155 IPYTHON_DIR.
240 156
241 157 -log|l Generate a log file of all input. The file is named
242 158 ipython_log.py in your current directory (which prevents logs
@@ -285,10 +201,10 b' REGULAR OPTIONS'
285 201
286 202 -profile|p <name>
287 203 Assume that your config file is ipythonrc-<name> (looks in cur-
288 rent dir first, then in IPYTHONDIR). This is a quick way to keep
204 rent dir first, then in IPYTHON_DIR). This is a quick way to keep
289 205 and load multiple config files for different tasks, especially
290 206 if you use the include option of config files. You can keep a
291 basic IPYTHONDIR/ipythonrc file and then have other 'profiles'
207 basic IPYTHON_DIR/ipythonrc file and then have other 'profiles'
292 208 which include this one and load extra things for particular
293 209 tasks. For example:
294 210
@@ -329,7 +245,7 b' REGULAR OPTIONS'
329 245 -rcfile <name>
330 246 Name of your IPython resource configuration file. normally
331 247 IPython loads ipythonrc (from current directory) or
332 IPYTHONDIR/ipythonrc. If the loading of your config file fails,
248 IPYTHON_DIR/ipythonrc. If the loading of your config file fails,
333 249 IPython starts with a bare bones configuration (no modules
334 250 loaded at all).
335 251
@@ -368,7 +284,7 b' REGULAR OPTIONS'
368 284 Simply removes all input/output separators.
369 285
370 286 -upgrade
371 Allows you to upgrade your IPYTHONDIR configuration when you
287 Allows you to upgrade your IPYTHON_DIR configuration when you
372 288 install a new version of IPython. Since new versions may
373 289 include new command lines options or example files, this copies
374 290 updated ipythonrc-type files. However, it backs up (with a .old
@@ -591,6 +507,18 b' MAIN FEATURES'
591 507 >>> x = ,my_function /home/me # syntax error
592 508 """
593 509
510 interactive_usage_min = """\
511 An enhanced console for Python.
512 Some of its features are:
513 - Readline support if the readline library is present.
514 - Tab completion in the local namespace.
515 - Logging of input, see command-line options.
516 - System shell escape via ! , eg !ls.
517 - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.)
518 - Keeps track of locally defined variables via %who, %whos.
519 - Show object information with a ? eg ?x or x? (use ?? for more info).
520 """
521
594 522 quick_reference = r"""
595 523 IPython -- An enhanced Interactive Python - Quick Reference Card
596 524 ================================================================
@@ -643,3 +571,18 b' or python names.'
643 571 The following magic functions are currently available:
644 572
645 573 """
574
575 quick_guide = """\
576 ? -> Introduction and overview of IPython's features.
577 %quickref -> Quick reference.
578 help -> Python's own help system.
579 object? -> Details about 'object'. ?object also works, ?? prints more."""
580
581 default_banner_parts = [
582 'Python %s' % (sys.version.split('\n')[0],),
583 'Type "copyright", "credits" or "license" for more information.\n',
584 'IPython %s -- An enhanced Interactive Python.' % (release.version,),
585 quick_guide
586 ]
587
588 default_banner = '\n'.join(default_banner_parts)
@@ -25,7 +25,7 b' import types'
25 25 import Gnuplot as Gnuplot_ori
26 26 import Numeric
27 27
28 from IPython.genutils import popkey,xsys
28 from IPython.utils.genutils import popkey,xsys
29 29
30 30 # needed by hardcopy():
31 31 gp = Gnuplot_ori.gp
@@ -16,7 +16,8 b" __all__ = ['Gnuplot','gp','gp_new','plot','plot2','splot','replot',"
16 16 'gphelp']
17 17
18 18 import IPython.GnuplotRuntime as GRun
19 from IPython.genutils import page,warn
19 from IPython.utils.genutils import warn
20 from IPython.core.page import page
20 21
21 22 # Set global names for interactive use
22 23 Gnuplot = GRun.Gnuplot
@@ -140,7 +141,7 b' else:'
140 141 print """*** Type `gphelp` for help on the Gnuplot integration features."""
141 142
142 143 # Add the new magic functions to the class dict
143 from IPython.iplib import InteractiveShell
144 from IPython.core.iplib import InteractiveShell
144 145 InteractiveShell.magic_gpc = magic_gpc
145 146 InteractiveShell.magic_gp_set_default = magic_gp_set_default
146 147
@@ -53,7 +53,7 b" __all__ = ['Gnuplot','gp','gp_new','Data','File','Func','GridData',"
53 53 'pm3d_config','eps_fix_bbox']
54 54
55 55 import os,tempfile,sys
56 from IPython.genutils import getoutput
56 from IPython.utils.genutils import getoutput
57 57
58 58 #---------------------------------------------------------------------------
59 59 # Notes on mouse support for Gnuplot.py
1 NO CONTENT: file renamed from IPython/Itpl.py to IPython/deathrow/Itpl.py
@@ -52,7 +52,7 b' def prefilter_PQ(self,line,continuation):'
52 52 imported."""
53 53
54 54 from re import match
55 from IPython.iplib import InteractiveShell
55 from IPython.core.iplib import InteractiveShell
56 56
57 57 # This regexp is what does the real work
58 58 unit_split = match(r'\s*(\w+)\s*=\s*(-?\d*\.?\d*[eE]?-?\d*)\s+([a-zA-Z].*)',
@@ -74,7 +74,7 b' def prefilter_PQ(self,line,continuation):'
74 74 return InteractiveShell._prefilter(self,line,continuation)
75 75
76 76 # Rebind this to be the new IPython prefilter:
77 from IPython.iplib import InteractiveShell
77 from IPython.core.iplib import InteractiveShell
78 78 InteractiveShell.prefilter = prefilter_PQ
79 79
80 80 # Clean up the namespace.
1 NO CONTENT: file renamed from IPython/Extensions/PhysicalQInteractive.py to IPython/deathrow/PhysicalQInteractive.py
1 NO CONTENT: file renamed from IPython/Extensions/astyle.py to IPython/deathrow/astyle.py
@@ -10,8 +10,8 b' See the idoctest docstring below for usage details.'
10 10 import doctest
11 11 import sys
12 12
13 import IPython.ipapi
14 ip = IPython.ipapi.get()
13 from IPython.core import ipapi
14 ip = ipapi.get()
15 15
16 16 def rundoctest(text,ns=None,eraise=False):
17 17 """Run a the input source as a doctest, in the caller's namespace.
@@ -77,7 +77,7 b' def idoctest(ns=None,eraise=False):'
77 77 if ns is None:
78 78 ns = ip.user_ns
79 79
80 ip.IP.savehist()
80 ip.savehist()
81 81 try:
82 82 while True:
83 83 line = raw_input()
@@ -96,7 +96,7 b' def idoctest(ns=None,eraise=False):'
96 96 print "KeyboardInterrupt - Discarding input."
97 97 run_test = False
98 98
99 ip.IP.reloadhist()
99 ip.reloadhist()
100 100
101 101 if run_test:
102 102 # Extra blank line at the end to ensure that the final docstring has a
@@ -2,7 +2,7 b''
2 2
3 3 import curses, fcntl, signal, struct, tty, textwrap, inspect
4 4
5 from IPython import ipapi
5 from IPython.core import ipapi
6 6
7 7 import astyle, ipipe
8 8
@@ -1,7 +1,7 b''
1 1 # -*- coding: iso-8859-1 -*-
2 2
3 3 import ipipe, os, webbrowser, urllib
4 from IPython import ipapi
4 from IPython.core import ipapi
5 5 import wx
6 6 import wx.grid, wx.html
7 7
1 NO CONTENT: file renamed from IPython/Extensions/igrid_help.css to IPython/deathrow/igrid_help.css
1 NO CONTENT: file renamed from IPython/Extensions/igrid_help.html to IPython/deathrow/igrid_help.html
@@ -133,12 +133,13 b' from IPython.external import simplegeneric'
133 133 from IPython.external import path
134 134
135 135 try:
136 from IPython import genutils, generics
136 from IPython.utils import genutils
137 from IPython.utils import generics
137 138 except ImportError:
138 139 genutils = None
139 140 generics = None
140 141
141 from IPython import ipapi
142 from IPython.core import ipapi
142 143
143 144
144 145 __all__ = [
@@ -1216,11 +1217,11 b' class ils(Table):'
1216 1217 Examples::
1217 1218
1218 1219 >>> ils
1219 <class 'IPython.Extensions.ipipe.ils'>
1220 <class 'IPython.extensions.ipipe.ils'>
1220 1221 >>> ils("/usr/local/lib/python2.4")
1221 IPython.Extensions.ipipe.ils('/usr/local/lib/python2.4')
1222 IPython.extensions.ipipe.ils('/usr/local/lib/python2.4')
1222 1223 >>> ils("~")
1223 IPython.Extensions.ipipe.ils('/home/fperez')
1224 IPython.extensions.ipipe.ils('/home/fperez')
1224 1225 # all-random
1225 1226 """
1226 1227 def __init__(self, base=os.curdir, dirs=True, files=True):
@@ -1258,7 +1259,7 b' class iglob(Table):'
1258 1259 Examples::
1259 1260
1260 1261 >>> iglob("*.py")
1261 IPython.Extensions.ipipe.iglob('*.py')
1262 IPython.extensions.ipipe.iglob('*.py')
1262 1263 """
1263 1264 def __init__(self, glob):
1264 1265 self.glob = glob
@@ -1284,11 +1285,11 b' class iwalk(Table):'
1284 1285 List all files and directories in a directory and it's subdirectory::
1285 1286
1286 1287 >>> iwalk
1287 <class 'IPython.Extensions.ipipe.iwalk'>
1288 <class 'IPython.extensions.ipipe.iwalk'>
1288 1289 >>> iwalk("/usr/lib")
1289 IPython.Extensions.ipipe.iwalk('/usr/lib')
1290 IPython.extensions.ipipe.iwalk('/usr/lib')
1290 1291 >>> iwalk("~")
1291 IPython.Extensions.ipipe.iwalk('/home/fperez') # random
1292 IPython.extensions.ipipe.iwalk('/home/fperez') # random
1292 1293
1293 1294 """
1294 1295 def __init__(self, base=os.curdir, dirs=True, files=True):
@@ -1393,7 +1394,7 b' class ipwd(Table):'
1393 1394 Example::
1394 1395
1395 1396 >>> ipwd | isort("uid")
1396 <IPython.Extensions.ipipe.isort key='uid' reverse=False at 0x849efec>
1397 <IPython.extensions.ipipe.isort key='uid' reverse=False at 0x849efec>
1397 1398 # random
1398 1399 """
1399 1400 def __iter__(self):
@@ -1579,7 +1580,7 b' class ienv(Table):'
1579 1580 Example::
1580 1581
1581 1582 >>> ienv
1582 <class 'IPython.Extensions.ipipe.ienv'>
1583 <class 'IPython.extensions.ipipe.ienv'>
1583 1584 """
1584 1585
1585 1586 def __iter__(self):
@@ -1601,9 +1602,9 b' class ihist(Table):'
1601 1602 Example::
1602 1603
1603 1604 >>> ihist
1604 <class 'IPython.Extensions.ipipe.ihist'>
1605 <class 'IPython.extensions.ipipe.ihist'>
1605 1606 >>> ihist(True) # raw mode
1606 <IPython.Extensions.ipipe.ihist object at 0x849602c> # random
1607 <IPython.extensions.ipipe.ihist object at 0x849602c> # random
1607 1608 """
1608 1609 def __init__(self, raw=True):
1609 1610 self.raw = raw
@@ -1611,10 +1612,10 b' class ihist(Table):'
1611 1612 def __iter__(self):
1612 1613 api = ipapi.get()
1613 1614 if self.raw:
1614 for line in api.IP.input_hist_raw:
1615 for line in api.input_hist_raw:
1615 1616 yield line.rstrip("\n")
1616 1617 else:
1617 for line in api.IP.input_hist:
1618 for line in api.input_hist:
1618 1619 yield line.rstrip("\n")
1619 1620
1620 1621
@@ -1638,12 +1639,12 b' class ialias(Table):'
1638 1639 Example::
1639 1640
1640 1641 >>> ialias
1641 <class 'IPython.Extensions.ipipe.ialias'>
1642 <class 'IPython.extensions.ipipe.ialias'>
1642 1643 """
1643 1644 def __iter__(self):
1644 1645 api = ipapi.get()
1645 1646
1646 for (name, (args, command)) in api.IP.alias_table.iteritems():
1647 for (name, (args, command)) in api.alias_manager.alias_table.iteritems():
1647 1648 yield Alias(name, args, command)
1648 1649
1649 1650
@@ -1701,10 +1702,10 b' class ix(Table):'
1701 1702 Examples::
1702 1703
1703 1704 >>> ix("ps x")
1704 IPython.Extensions.ipipe.ix('ps x')
1705 IPython.extensions.ipipe.ix('ps x')
1705 1706
1706 1707 >>> ix("find .") | ifile
1707 <IPython.Extensions.ipipe.ieval expr=<class 'IPython.Extensions.ipipe.ifile'> at 0x8509d2c>
1708 <IPython.extensions.ipipe.ieval expr=<class 'IPython.extensions.ipipe.ifile'> at 0x8509d2c>
1708 1709 # random
1709 1710 """
1710 1711 def __init__(self, cmd):
@@ -1927,9 +1928,9 b' class isort(Pipe):'
1927 1928 Examples::
1928 1929
1929 1930 >>> ils | isort("size")
1930 <IPython.Extensions.ipipe.isort key='size' reverse=False at 0x849ec2c>
1931 <IPython.extensions.ipipe.isort key='size' reverse=False at 0x849ec2c>
1931 1932 >>> ils | isort("_.isdir(), _.lower()", reverse=True)
1932 <IPython.Extensions.ipipe.isort key='_.isdir(), _.lower()' reverse=True at 0x849eacc>
1933 <IPython.extensions.ipipe.isort key='_.isdir(), _.lower()' reverse=True at 0x849eacc>
1933 1934 # all-random
1934 1935 """
1935 1936
@@ -15,7 +15,7 b' Website: physics.nist.gov/constants'
15 15 # inspired by maxima's physconst.mac by Cliff Yapp
16 16
17 17 #from math import * # math MUST be imported BEFORE PhysicalQInteractive
18 from IPython.Extensions.PhysicalQInteractive import PhysicalQuantityInteractive
18 from IPython.extensions.PhysicalQInteractive import PhysicalQuantityInteractive
19 19
20 20 # Math constants:
21 21
@@ -12,9 +12,9 b' ipy_profile_PROFILENAME etc.'
12 12
13 13 """
14 14
15 import IPython.rlineimpl as readline
16 import IPython.ipapi
17 ip = IPython.ipapi.get()
15 import IPython.utils.rlineimpl as readline
16 from IPython.core import ipapi
17 ip = ipapi.get()
18 18
19 19 o = ip.options
20 20
@@ -16,7 +16,7 b' def pylaunchers():'
16 16 for f in fs:
17 17 l = PyLauncher(f)
18 18 n = os.path.splitext(f)[0]
19 ip.defalias(n, l)
19 ip.define_alias(n, l)
20 20 ip.magic('store '+n)
21 21
22 22
@@ -39,7 +39,7 b' def main():'
39 39 return
40 40
41 41 os.environ["PATH"] = os.environ["PATH"] + ";" + kitroot() + "\\bin;"
42 ip.to_user_ns("pylaunchers")
42 ip.push("pylaunchers")
43 43 cmds = ip.db.get('syscmdlist', None)
44 44 if cmds is None:
45 45 ip.magic('rehashx')
@@ -63,8 +63,8 b' def ipython_firstrun(ip):'
63 63
64 64 print "First run of ipykit - configuring"
65 65
66 ip.defalias('py',selflaunch)
67 ip.defalias('d','dir /w /og /on')
66 ip.define_alias('py',selflaunch)
67 ip.define_alias('d','dir /w /og /on')
68 68 ip.magic('store py')
69 69 ip.magic('store d')
70 70
@@ -9,12 +9,12 b' Stuff that is considered obsolete / redundant is gradually moved here.'
9 9
10 10 """
11 11
12 import IPython.ipapi
13 ip = IPython.ipapi.get()
12 from IPython.core import ipapi
13 ip = ipapi.get()
14 14
15 15 import os,sys
16 16
17 from IPython.genutils import *
17 from IPython.utils.genutils import *
18 18
19 19 # use rehashx
20 20
@@ -43,7 +43,7 b" def magic_rehash(self, parameter_s = ''):"
43 43 # aliases since %rehash will probably clobber them
44 44 self.shell.init_auto_alias()
45 45
46 ip.expose_magic("rehash", magic_rehash)
46 ip.define_magic("rehash", magic_rehash)
47 47
48 48 # Exit
49 49 def magic_Quit(self, parameter_s=''):
@@ -51,7 +51,7 b" def magic_Quit(self, parameter_s=''):"
51 51
52 52 self.shell.ask_exit()
53 53
54 ip.expose_magic("Quit", magic_Quit)
54 ip.define_magic("Quit", magic_Quit)
55 55
56 56
57 57 # make it autocallable fn if you really need it
@@ -59,4 +59,4 b" def magic_p(self, parameter_s=''):"
59 59 """Just a short alias for Python's 'print'."""
60 60 exec 'print ' + parameter_s in self.shell.user_ns
61 61
62 ip.expose_magic("p", magic_p)
62 ip.define_magic("p", magic_p)
@@ -3,8 +3,8 b''
3 3 Add %p4 magic for pythonic p4 (Perforce) usage.
4 4 """
5 5
6 import IPython.ipapi
7 ip = IPython.ipapi.get()
6 from IPython.core import ipapi
7 ip = ipapi.get()
8 8
9 9 import os,sys,marshal
10 10
@@ -25,9 +25,9 b" def p4_f(self, parameter_s=''):"
25 25 def p4d(fname):
26 26 return os.popen('p4 where ' + fname).read().split()[0]
27 27
28 ip.to_user_ns("p4d")
28 ip.push("p4d")
29 29
30 ip.expose_magic('p4', p4_f)
30 ip.define_magic('p4', p4_f)
31 31
32 32 p4_commands = """\
33 33 add admin annotate branch branches change changes changelist
1 NO CONTENT: file renamed from IPython/Extensions/ipy_profile_none.py to IPython/deathrow/ipy_profile_none.py
@@ -5,11 +5,11 b' This profile loads the math/cmath modules as well as all of numpy.'
5 5 It exposes numpy via the 'np' shorthand as well for convenience.
6 6 """
7 7
8 import IPython.ipapi
8 from IPython.core import ipapi
9 9 import ipy_defaults
10 10
11 11 def main():
12 ip = IPython.ipapi.get()
12 ip = ipapi.get()
13 13
14 14 try:
15 15 ip.ex("import math,cmath")
@@ -6,11 +6,11 b" It exposes numpy and scipy via the 'np' and 'sp' shorthands as well for"
6 6 convenience.
7 7 """
8 8
9 import IPython.ipapi
9 from IPython.core import ipapi
10 10 import ipy_defaults
11 11
12 12 def main():
13 ip = IPython.ipapi.get()
13 ip = ipapi.get()
14 14
15 15 try:
16 16 ip.ex("import math,cmath")
1 NO CONTENT: file renamed from IPython/Extensions/ipy_profile_sh.py to IPython/deathrow/ipy_profile_sh.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/ipy_profile_zope.py to IPython/deathrow/ipy_profile_zope.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/ipy_traits_completer.py to IPython/deathrow/ipy_traits_completer.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/ipy_vimserver.py to IPython/deathrow/ipy_vimserver.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/numeric_formats.py to IPython/deathrow/numeric_formats.py
1 NO CONTENT: file renamed from IPython/numutils.py to IPython/deathrow/numutils.py
1 NO CONTENT: file renamed from IPython/Extensions/scitedirector.py to IPython/deathrow/scitedirector.py
1 NO CONTENT: file renamed from IPython/twshell.py to IPython/deathrow/twshell.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/__init__.py to IPython/extensions/__init__.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file chmod 100755 => 100644
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/background_jobs.py to IPython/lib/backgroundjobs.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/clipboard.py to IPython/lib/clipboard.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/deep_reload.py to IPython/lib/deepreload.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/demo.py to IPython/lib/demo.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/irunner.py to IPython/lib/irunner.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/InterpreterExec.py to IPython/quarantine/InterpreterExec.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/InterpreterPasteInput.py to IPython/quarantine/InterpreterPasteInput.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/clearcmd.py to IPython/quarantine/clearcmd.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/envpersist.py to IPython/quarantine/envpersist.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/ext_rescapture.py to IPython/quarantine/ext_rescapture.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/ipy_app_completers.py to IPython/quarantine/ipy_app_completers.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/ipy_autoreload.py to IPython/quarantine/ipy_autoreload.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/ipy_bzr.py to IPython/quarantine/ipy_bzr.py
1 NO CONTENT: file renamed from IPython/Extensions/ipy_completers.py to IPython/quarantine/ipy_completers.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/ipy_editors.py to IPython/quarantine/ipy_editors.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/ipy_exportdb.py to IPython/quarantine/ipy_exportdb.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/ipy_extutil.py to IPython/quarantine/ipy_extutil.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/ipy_fsops.py to IPython/quarantine/ipy_fsops.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/ipy_gnuglobal.py to IPython/quarantine/ipy_gnuglobal.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/ipy_greedycompleter.py to IPython/quarantine/ipy_greedycompleter.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/ipy_jot.py to IPython/quarantine/ipy_jot.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/ipy_lookfor.py to IPython/quarantine/ipy_lookfor.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/ipy_profile_doctest.py to IPython/quarantine/ipy_profile_doctest.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/ipy_pydb.py to IPython/quarantine/ipy_pydb.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/ipy_rehashdir.py to IPython/quarantine/ipy_rehashdir.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/ipy_render.py to IPython/quarantine/ipy_render.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/ipy_server.py to IPython/quarantine/ipy_server.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/ipy_signals.py to IPython/quarantine/ipy_signals.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/ipy_stock_completers.py to IPython/quarantine/ipy_stock_completers.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/ipy_synchronize_with.py to IPython/quarantine/ipy_synchronize_with.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/ipy_system_conf.py to IPython/quarantine/ipy_system_conf.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/ipy_which.py to IPython/quarantine/ipy_which.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/ipy_winpdb.py to IPython/quarantine/ipy_winpdb.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/ipy_workdir.py to IPython/quarantine/ipy_workdir.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/jobctrl.py to IPython/quarantine/jobctrl.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/ledit.py to IPython/quarantine/ledit.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/pspersistence.py to IPython/quarantine/pspersistence.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/win32clip.py to IPython/quarantine/win32clip.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from scripts/iptest to IPython/scripts/iptest
1 NO CONTENT: file renamed from scripts/ipython-wx to IPython/scripts/ipython-wx
1 NO CONTENT: file renamed from scripts/ipythonx to IPython/scripts/ipythonx
1 NO CONTENT: file renamed from scripts/irunner to IPython/scripts/irunner
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from scripts/pycolor to IPython/scripts/pycolor
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/DPyGetOpt.py to IPython/utils/DPyGetOpt.py
1 NO CONTENT: file renamed from IPython/PyColorize.py to IPython/utils/PyColorize.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/ColorANSI.py to IPython/utils/coloransi.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/genutils.py to IPython/utils/genutils.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/tools/growl.py to IPython/utils/growl.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/Extensions/pickleshare.py to IPython/utils/pickleshare.py
1 NO CONTENT: file renamed from IPython/platutils.py to IPython/utils/platutils.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/platutils_dummy.py to IPython/utils/platutils_dummy.py
1 NO CONTENT: file renamed from IPython/platutils_posix.py to IPython/utils/platutils_posix.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/platutils_win32.py to IPython/utils/platutils_win32.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/rlineimpl.py to IPython/utils/rlineimpl.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/strdispatch.py to IPython/utils/strdispatch.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/tests/test_genutils.py to IPython/utils/tests/test_genutils.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/kernel/core/tests/test_notification.py to IPython/utils/tests/test_notification.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/tests/test_platutils.py to IPython/utils/tests/test_platutils.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/upgrade_dir.py to IPython/utils/upgradedir.py
1 NO CONTENT: file renamed from IPython/wildcard.py to IPython/utils/wildcard.py
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from IPython/winconsole.py to IPython/utils/winconsole.py
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from docs/source/history.txt to docs/source/about/history.txt
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from docs/source/license_and_copyright.txt to docs/source/about/license_and_copyright.txt
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from docs/source/config/initial_config.txt to docs/source/config/old.txt
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file renamed from docs/source/changes.txt to docs/source/whatsnew/version0.9.txt
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
This diff has been collapsed as it changes many lines, (631 lines changed) Show them Hide them
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed, binary diff hidden
1 NO CONTENT: file was removed, binary diff hidden
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: file was removed
The requested commit or file is too big and content was truncated. Show full diff
General Comments 0
You need to be logged in to leave comments. Login now