Show More
@@ -6,7 +6,7 b' Requires Python 2.1 or newer.' | |||||
6 |
|
6 | |||
7 | This file contains all the classes and helper functions specific to IPython. |
|
7 | This file contains all the classes and helper functions specific to IPython. | |
8 |
|
8 | |||
9 |
$Id: iplib.py 97 |
|
9 | $Id: iplib.py 978 2005-12-30 02:37:15Z fperez $ | |
10 | """ |
|
10 | """ | |
11 |
|
11 | |||
12 | #***************************************************************************** |
|
12 | #***************************************************************************** | |
@@ -101,8 +101,13 b' def softspace(file, newvalue):' | |||||
101 |
|
101 | |||
102 | #**************************************************************************** |
|
102 | #**************************************************************************** | |
103 | # These special functions get installed in the builtin namespace, to provide |
|
103 | # These special functions get installed in the builtin namespace, to provide | |
104 |
# programmatic (pure python) access to magics |
|
104 | # programmatic (pure python) access to magics, aliases and system calls. This | |
105 | # for logging, user scripting, and more. |
|
105 | # is important for logging, user scripting, and more. | |
|
106 | ||||
|
107 | # We are basically exposing, via normal python functions, the three mechanisms | |||
|
108 | # in which ipython offers special call modes (magics for internal control, | |||
|
109 | # aliases for direct system access via pre-selected names, and !cmd for | |||
|
110 | # calling arbitrary system commands). | |||
106 |
|
111 | |||
107 | def ipmagic(arg_s): |
|
112 | def ipmagic(arg_s): | |
108 | """Call a magic function by name. |
|
113 | """Call a magic function by name. | |
@@ -166,6 +171,11 b' def ipalias(arg_s):' | |||||
166 | else: |
|
171 | else: | |
167 | error("Alias `%s` not found." % alias_name) |
|
172 | error("Alias `%s` not found." % alias_name) | |
168 |
|
173 | |||
|
174 | def ipsystem(arg_s): | |||
|
175 | """Make a system call, using IPython.""" | |||
|
176 | __IPYTHON__.system(arg_s) | |||
|
177 | ||||
|
178 | ||||
169 | #**************************************************************************** |
|
179 | #**************************************************************************** | |
170 | # Local use exceptions |
|
180 | # Local use exceptions | |
171 | class SpaceInInput(exceptions.Exception): pass |
|
181 | class SpaceInInput(exceptions.Exception): pass | |
@@ -249,9 +259,10 b' class InteractiveShell(Magic):' | |||||
249 | # imported code can test for being inside IPython. |
|
259 | # imported code can test for being inside IPython. | |
250 | __builtin__.__IPYTHON__ = self |
|
260 | __builtin__.__IPYTHON__ = self | |
251 |
|
261 | |||
252 | # And load into builtins ipmagic/ipalias as well |
|
262 | # And load into builtins ipmagic/ipalias/ipsystem as well | |
253 | __builtin__.ipmagic = ipmagic |
|
263 | __builtin__.ipmagic = ipmagic | |
254 | __builtin__.ipalias = ipalias |
|
264 | __builtin__.ipalias = ipalias | |
|
265 | __builtin__.ipsystem = ipsystem | |||
255 |
|
266 | |||
256 | # Add to __builtin__ other parts of IPython's public API |
|
267 | # Add to __builtin__ other parts of IPython's public API | |
257 | __builtin__.ip_set_hook = self.set_hook |
|
268 | __builtin__.ip_set_hook = self.set_hook | |
@@ -471,8 +482,6 b' class InteractiveShell(Magic):' | |||||
471 |
|
482 | |||
472 | # Storage |
|
483 | # Storage | |
473 | self.rc = rc # This will hold all configuration information |
|
484 | self.rc = rc # This will hold all configuration information | |
474 | self.inputcache = [] |
|
|||
475 | self._boundcache = [] |
|
|||
476 | self.pager = 'less' |
|
485 | self.pager = 'less' | |
477 | # temporary files used for various purposes. Deleted at exit. |
|
486 | # temporary files used for various purposes. Deleted at exit. | |
478 | self.tempfiles = [] |
|
487 | self.tempfiles = [] | |
@@ -1217,15 +1226,6 b' want to merge them back into the new files.""" % locals()' | |||||
1217 | # pdb mucks up readline, fix it back |
|
1226 | # pdb mucks up readline, fix it back | |
1218 | self.readline.set_completer(self.Completer.complete) |
|
1227 | self.readline.set_completer(self.Completer.complete) | |
1219 |
|
1228 | |||
1220 | def update_cache(self, line): |
|
|||
1221 | """puts line into cache""" |
|
|||
1222 | return # dbg |
|
|||
1223 |
|
||||
1224 | # This copies the cache every time ... :-( |
|
|||
1225 | self.inputcache.insert(0, line) |
|
|||
1226 | if len(self.inputcache) >= self.CACHELENGTH: |
|
|||
1227 | self.inputcache.pop() # This doesn't :-) |
|
|||
1228 |
|
||||
1229 | def mainloop(self,banner=None): |
|
1229 | def mainloop(self,banner=None): | |
1230 | """Creates the local namespace and starts the mainloop. |
|
1230 | """Creates the local namespace and starts the mainloop. | |
1231 |
|
1231 | |||
@@ -1462,11 +1462,8 b' want to merge them back into the new files.""" % locals()' | |||||
1462 | # skip blank lines so we don't mess up the prompt counter, but do |
|
1462 | # skip blank lines so we don't mess up the prompt counter, but do | |
1463 | # NOT skip even a blank line if we are in a code block (more is |
|
1463 | # NOT skip even a blank line if we are in a code block (more is | |
1464 | # true) |
|
1464 | # true) | |
1465 | #print 'rl line:<%s>' % line # dbg |
|
|||
1466 | if line or more: |
|
1465 | if line or more: | |
1467 | #print 'doit' # dbg |
|
1466 | more = self.push(self.prefilter(line,more)) | |
1468 | newline = self.prefilter(line,more) |
|
|||
1469 | more = self.push(newline) |
|
|||
1470 | # IPython's runsource returns None if there was an error |
|
1467 | # IPython's runsource returns None if there was an error | |
1471 | # compiling the code. This allows us to stop processing right |
|
1468 | # compiling the code. This allows us to stop processing right | |
1472 | # away, so the user gets the error message at the right place. |
|
1469 | # away, so the user gets the error message at the right place. | |
@@ -1595,8 +1592,14 b' want to merge them back into the new files.""" % locals()' | |||||
1595 | is left as it was after the line was appended. The return |
|
1592 | is left as it was after the line was appended. The return | |
1596 | value is 1 if more input is required, 0 if the line was dealt |
|
1593 | value is 1 if more input is required, 0 if the line was dealt | |
1597 | with in some way (this is the same as runsource()). |
|
1594 | with in some way (this is the same as runsource()). | |
1598 |
|
||||
1599 | """ |
|
1595 | """ | |
|
1596 | ||||
|
1597 | # autoindent management should be done here, and not in the | |||
|
1598 | # interactive loop, since that one is only seen by keyboard input. We | |||
|
1599 | # need this done correctly even for code run via runlines (which uses | |||
|
1600 | # push). | |||
|
1601 | self.autoindent_update(line) | |||
|
1602 | ||||
1600 | self.buffer.append(line) |
|
1603 | self.buffer.append(line) | |
1601 | more = self.runsource('\n'.join(self.buffer), self.filename) |
|
1604 | more = self.runsource('\n'.join(self.buffer), self.filename) | |
1602 | if not more: |
|
1605 | if not more: | |
@@ -1676,7 +1679,6 b' want to merge them back into the new files.""" % locals()' | |||||
1676 | self._last_input_line = line |
|
1679 | self._last_input_line = line | |
1677 |
|
1680 | |||
1678 | #print '***line: <%s>' % line # dbg |
|
1681 | #print '***line: <%s>' % line # dbg | |
1679 | self.autoindent_update(line) |
|
|||
1680 |
|
1682 | |||
1681 | # the input history needs to track even empty lines |
|
1683 | # the input history needs to track even empty lines | |
1682 | if not line.strip(): |
|
1684 | if not line.strip(): | |
@@ -1798,25 +1800,21 b' want to merge them back into the new files.""" % locals()' | |||||
1798 | # clear the line. The rule will be in this case, that either two |
|
1800 | # clear the line. The rule will be in this case, that either two | |
1799 | # lines of pure whitespace in a row, or a line of pure whitespace but |
|
1801 | # lines of pure whitespace in a row, or a line of pure whitespace but | |
1800 | # of a size different to the indent level, will exit the input loop. |
|
1802 | # of a size different to the indent level, will exit the input loop. | |
|
1803 | ||||
1801 | if (continue_prompt and self.autoindent and isspace(line) and |
|
1804 | if (continue_prompt and self.autoindent and isspace(line) and | |
1802 | (line != self.indent_current or isspace(self.buffer[-1]))): |
|
1805 | (line != self.indent_current or isspace(self.buffer[-1]))): | |
1803 | line = '' |
|
1806 | line = '' | |
1804 |
|
1807 | |||
1805 | self.log(line,continue_prompt) |
|
1808 | self.log(line,continue_prompt) | |
1806 | self.update_cache(line) |
|
|||
1807 | return line |
|
1809 | return line | |
1808 |
|
1810 | |||
1809 | def handle_alias(self,line,continue_prompt=None, |
|
1811 | def handle_alias(self,line,continue_prompt=None, | |
1810 | pre=None,iFun=None,theRest=None): |
|
1812 | pre=None,iFun=None,theRest=None): | |
1811 | """Handle alias input lines. """ |
|
1813 | """Handle alias input lines. """ | |
1812 |
|
1814 | |||
1813 |
|
|
1815 | line_out = 'ipalias("%s %s")' % (iFun,esc_quotes(theRest)) | |
1814 | # log the ipalias form, which doesn't depend on the instance name |
|
1816 | self.log(line_out,continue_prompt) | |
1815 | line_log = 'ipalias("%s %s")' % (iFun,theRest) |
|
1817 | return line_out | |
1816 | self.log(line_log,continue_prompt) |
|
|||
1817 | self.update_cache(line_log) |
|
|||
1818 | # this is what actually gets executed |
|
|||
1819 | return "%s%s.call_alias('%s','%s')" % (pre,self.name,iFun,theRest) |
|
|||
1820 |
|
1818 | |||
1821 | def handle_shell_escape(self, line, continue_prompt=None, |
|
1819 | def handle_shell_escape(self, line, continue_prompt=None, | |
1822 | pre=None,iFun=None,theRest=None): |
|
1820 | pre=None,iFun=None,theRest=None): | |
@@ -1829,10 +1827,8 b' want to merge them back into the new files.""" % locals()' | |||||
1829 | print 'SyntaxError: !! is not allowed in multiline statements' |
|
1827 | print 'SyntaxError: !! is not allowed in multiline statements' | |
1830 | return pre |
|
1828 | return pre | |
1831 | else: |
|
1829 | else: | |
1832 |
cmd = ("%s %s" % (iFun[1:],theRest)) |
|
1830 | cmd = ("%s %s" % (iFun[1:],theRest)) | |
1833 |
|
|
1831 | line_out = 'ipsystem(r"""%s"""[:-1])' % (cmd + "_") | |
1834 | line_out = '%s%s.system(r"""%s"""[:-1])' % (pre,self.name,cmd + "_") |
|
|||
1835 | #line_out = ('%s%s.system(' % (pre,self.name)) + repr(cmd) + ')' |
|
|||
1836 | else: # single-line input |
|
1832 | else: # single-line input | |
1837 | if line.startswith('!!'): |
|
1833 | if line.startswith('!!'): | |
1838 | # rewrite iFun/theRest to properly hold the call to %sx and |
|
1834 | # rewrite iFun/theRest to properly hold the call to %sx and | |
@@ -1843,16 +1839,10 b' want to merge them back into the new files.""" % locals()' | |||||
1843 | return self.handle_magic('%ssx %s' % (self.ESC_MAGIC,line[2:]), |
|
1839 | return self.handle_magic('%ssx %s' % (self.ESC_MAGIC,line[2:]), | |
1844 | continue_prompt,pre,iFun,theRest) |
|
1840 | continue_prompt,pre,iFun,theRest) | |
1845 | else: |
|
1841 | else: | |
1846 | #cmd = esc_quotes(line[1:]) |
|
|||
1847 | cmd=line[1:] |
|
1842 | cmd=line[1:] | |
1848 |
|
|
1843 | line_out = 'ipsystem(r"""%s"""[:-1])' % (cmd +"_") | |
1849 | line_out = '%s.system(r"""%s"""[:-1])' % (self.name,cmd +"_") |
|
|||
1850 | #line_out = ('%s.system(' % self.name) + repr(cmd)+ ')' |
|
|||
1851 | # update cache/log and return |
|
1844 | # update cache/log and return | |
1852 | self.log(line_out,continue_prompt) |
|
1845 | self.log(line_out,continue_prompt) | |
1853 | self.update_cache(line_out) # readline cache gets normal line |
|
|||
1854 | #print 'line out r:', `line_out` # dbg |
|
|||
1855 | #print 'line out s:', line_out # dbg |
|
|||
1856 | return line_out |
|
1846 | return line_out | |
1857 |
|
1847 | |||
1858 | def handle_magic(self, line, continue_prompt=None, |
|
1848 | def handle_magic(self, line, continue_prompt=None, | |
@@ -1863,7 +1853,6 b' want to merge them back into the new files.""" % locals()' | |||||
1863 |
|
1853 | |||
1864 | cmd = '%sipmagic("%s")' % (pre,esc_quotes('%s %s' % (iFun,theRest))) |
|
1854 | cmd = '%sipmagic("%s")' % (pre,esc_quotes('%s %s' % (iFun,theRest))) | |
1865 | self.log(cmd,continue_prompt) |
|
1855 | self.log(cmd,continue_prompt) | |
1866 | self.update_cache(line) |
|
|||
1867 | #print 'in handle_magic, cmd=<%s>' % cmd # dbg |
|
1856 | #print 'in handle_magic, cmd=<%s>' % cmd # dbg | |
1868 | return cmd |
|
1857 | return cmd | |
1869 |
|
1858 | |||
@@ -1921,7 +1910,6 b' want to merge them back into the new files.""" % locals()' | |||||
1921 | elif line[-1]==self.ESC_HELP: |
|
1910 | elif line[-1]==self.ESC_HELP: | |
1922 | line = line[:-1] |
|
1911 | line = line[:-1] | |
1923 | self.log('#?'+line) |
|
1912 | self.log('#?'+line) | |
1924 | self.update_cache(line) |
|
|||
1925 | if line: |
|
1913 | if line: | |
1926 | self.magic_pinfo(line) |
|
1914 | self.magic_pinfo(line) | |
1927 | else: |
|
1915 | else: | |
@@ -2019,8 +2007,6 b' want to merge them back into the new files.""" % locals()' | |||||
2019 | # don't re-insert logger status info into cache |
|
2007 | # don't re-insert logger status info into cache | |
2020 | if line.startswith('#log#'): |
|
2008 | if line.startswith('#log#'): | |
2021 | continue |
|
2009 | continue | |
2022 | elif line.startswith('#!'): |
|
|||
2023 | self.update_cache(line[1:]) |
|
|||
2024 | else: |
|
2010 | else: | |
2025 | # build a block of code (maybe a single line) for execution |
|
2011 | # build a block of code (maybe a single line) for execution | |
2026 | block = line |
|
2012 | block = line | |
@@ -2038,7 +2024,6 b' want to merge them back into the new files.""" % locals()' | |||||
2038 | # now execute the block of one or more lines |
|
2024 | # now execute the block of one or more lines | |
2039 | try: |
|
2025 | try: | |
2040 | exec block in globs,locs |
|
2026 | exec block in globs,locs | |
2041 | self.update_cache(block.rstrip()) |
|
|||
2042 | except SystemExit: |
|
2027 | except SystemExit: | |
2043 | pass |
|
2028 | pass | |
2044 | except: |
|
2029 | except: |
@@ -9,6 +9,13 b'' | |||||
9 | 'y' has side effects, doesn't trigger the side effect TWICE. It |
|
9 | 'y' has side effects, doesn't trigger the side effect TWICE. It | |
10 | is important to note that, with autocall on, these side effects |
|
10 | is important to note that, with autocall on, these side effects | |
11 | can still happen. |
|
11 | can still happen. | |
|
12 | (ipsystem): new builtin, to complete the ip{magic/alias/system} | |||
|
13 | trio. IPython offers these three kinds of special calls which are | |||
|
14 | not python code, and it's a good thing to have their call method | |||
|
15 | be accessible as pure python functions (not just special syntax at | |||
|
16 | the command line). It gives us a better internal implementation | |||
|
17 | structure, as well as exposing these for user scripting more | |||
|
18 | cleanly. | |||
12 |
|
19 | |||
13 | * IPython/macro.py (Macro.__init__): moved macros to a standalone |
|
20 | * IPython/macro.py (Macro.__init__): moved macros to a standalone | |
14 | file. Now that they'll be more likely to be used with the |
|
21 | file. Now that they'll be more likely to be used with the |
General Comments 0
You need to be logged in to leave comments.
Login now