Show More
@@ -13,11 +13,6 b'' | |||||
13 | #**************************************************************************** |
|
13 | #**************************************************************************** | |
14 | # Modules and globals |
|
14 | # Modules and globals | |
15 |
|
15 | |||
16 | from IPython import Release |
|
|||
17 | __author__ = '%s <%s>\n%s <%s>' % \ |
|
|||
18 | ( Release.authors['Janko'] + Release.authors['Fernando'] ) |
|
|||
19 | __license__ = Release.license |
|
|||
20 |
|
||||
21 | # Python standard modules |
|
16 | # Python standard modules | |
22 | import __builtin__ |
|
17 | import __builtin__ | |
23 | import bdb |
|
18 | import bdb | |
@@ -1049,10 +1044,33 b' Currently the magic system has the following functions:\\n"""' | |||||
1049 | def magic_reset(self, parameter_s=''): |
|
1044 | def magic_reset(self, parameter_s=''): | |
1050 | """Resets the namespace by removing all names defined by the user. |
|
1045 | """Resets the namespace by removing all names defined by the user. | |
1051 |
|
1046 | |||
1052 |
Input/Output history are left around in case you need them. |
|
1047 | Input/Output history are left around in case you need them. | |
|
1048 | ||||
|
1049 | Parameters | |||
|
1050 | ---------- | |||
|
1051 | -y : force reset without asking for confirmation. | |||
|
1052 | ||||
|
1053 | Examples | |||
|
1054 | -------- | |||
|
1055 | In [6]: a = 1 | |||
|
1056 | ||||
|
1057 | In [7]: a | |||
|
1058 | Out[7]: 1 | |||
|
1059 | ||||
|
1060 | In [8]: 'a' in _ip.user_ns | |||
|
1061 | Out[8]: True | |||
|
1062 | ||||
|
1063 | In [9]: %reset -f | |||
1053 |
|
|
1064 | ||
1054 | ans = self.shell.ask_yes_no( |
|
1065 | In [10]: 'a' in _ip.user_ns | |
1055 | "Once deleted, variables cannot be recovered. Proceed (y/[n])? ") |
|
1066 | Out[10]: False | |
|
1067 | """ | |||
|
1068 | ||||
|
1069 | if parameter_s == '-f': | |||
|
1070 | ans = True | |||
|
1071 | else: | |||
|
1072 | ans = self.shell.ask_yes_no( | |||
|
1073 | "Once deleted, variables cannot be recovered. Proceed (y/[n])? ") | |||
1056 | if not ans: |
|
1074 | if not ans: | |
1057 | print 'Nothing done.' |
|
1075 | print 'Nothing done.' | |
1058 | return |
|
1076 | return | |
@@ -1062,7 +1080,7 b' Currently the magic system has the following functions:\\n"""' | |||||
1062 |
|
1080 | |||
1063 | # Also flush the private list of module references kept for script |
|
1081 | # Also flush the private list of module references kept for script | |
1064 | # execution protection |
|
1082 | # execution protection | |
1065 |
self.shell. |
|
1083 | self.shell.clear_main_mod_cache() | |
1066 |
|
1084 | |||
1067 | def magic_logstart(self,parameter_s=''): |
|
1085 | def magic_logstart(self,parameter_s=''): | |
1068 | """Start logging anywhere in a session. |
|
1086 | """Start logging anywhere in a session. | |
@@ -1576,25 +1594,10 b' Currently the magic system has the following functions:\\n"""' | |||||
1576 |
|
1594 | |||
1577 | # The shell MUST hold a reference to main_mod so after %run exits, |
|
1595 | # The shell MUST hold a reference to main_mod so after %run exits, | |
1578 | # the python deletion mechanism doesn't zero it out (leaving |
|
1596 | # the python deletion mechanism doesn't zero it out (leaving | |
1579 | # dangling references) |
|
1597 | # dangling references). However, we should drop old versions of | |
1580 |
|
1598 | # main_mod. There is now a proper API to manage this caching in | ||
1581 | # XXX - the note above was written without further detail, but this |
|
1599 | # the main shell object, we use that. | |
1582 | # code actually causes problems. By holding references to the |
|
1600 | self.shell.cache_main_mod(main_mod) | |
1583 | # namespace where every script is executed, we effectively disable |
|
|||
1584 | # just about all possible variable cleanup. In particular, |
|
|||
1585 | # generator expressions and other variables that point to open |
|
|||
1586 | # files are kept alive, and as a user session lives on, it may run |
|
|||
1587 | # out of available file descriptors. Such a bug has already been |
|
|||
1588 | # reported by JD Hunter. I'm disabling this for now, but we need |
|
|||
1589 | # to clarify exactly (and add tests) what from main_mod needs to be |
|
|||
1590 | # kept alive and what is save to remove... In fact, see note |
|
|||
1591 | # below, where we append main_mod to sys.modules and then delete it |
|
|||
1592 | # again. The final cleanup is rendered moot by this reference kept |
|
|||
1593 | # in _user_main_modules(), so we really need to look into this. |
|
|||
1594 |
|
||||
1595 | self.shell._user_main_modules.append(main_mod) |
|
|||
1596 |
|
||||
1597 | # /XXX |
|
|||
1598 |
|
1601 | |||
1599 | # Since '%run foo' emulates 'python foo.py' at the cmd line, we must |
|
1602 | # Since '%run foo' emulates 'python foo.py' at the cmd line, we must | |
1600 | # set the __file__ global in the script's namespace |
|
1603 | # set the __file__ global in the script's namespace |
@@ -335,15 +335,19 b' class InteractiveShell(object,Magic):' | |||||
335 | # code ran is deleted. Now that this object is a true module (needed |
|
335 | # code ran is deleted. Now that this object is a true module (needed | |
336 | # so docetst and other tools work correctly), the Python module |
|
336 | # so docetst and other tools work correctly), the Python module | |
337 | # teardown mechanism runs over it, and sets to None every variable |
|
337 | # teardown mechanism runs over it, and sets to None every variable | |
338 |
# present in that module. T |
|
338 | # present in that module. Top-level references to objects from the | |
339 | # defined in the script (which have become interactively visible after |
|
339 | # script survive, because the user_ns is updated with them. However, | |
340 | # script exit) fail, because they hold references to objects that have |
|
340 | # calling functions defined in the script that use other things from | |
341 | # become overwritten into None. The only solution I see right now is |
|
341 | # the script will fail, because the function's closure had references | |
342 | # to protect every FakeModule used by %run by holding an internal |
|
342 | # to the original objects, which are now all None. So we must protect | |
343 | # reference to it. This private list will be used for that. The |
|
343 | # these modules from deletion by keeping a cache. To avoid keeping | |
344 | # %reset command will flush it as well. |
|
344 | # stale modules around (we only need the one from the last run), we use | |
345 | self._user_main_modules = [] |
|
345 | # a dict keyed with the full path to the script, so only the last | |
346 |
|
346 | # version of the module is held in the cache. The %reset command will | ||
|
347 | # flush this cache. See the cache_main_mod() and clear_main_mod_cache() | |||
|
348 | # methods for details on use. | |||
|
349 | self._user_main_modules = {} | |||
|
350 | ||||
347 | # List of input with multi-line handling. |
|
351 | # List of input with multi-line handling. | |
348 | # Fill its zero entry, user counter starts at 1 |
|
352 | # Fill its zero entry, user counter starts at 1 | |
349 | self.input_hist = InputList(['\n']) |
|
353 | self.input_hist = InputList(['\n']) | |
@@ -594,10 +598,6 b' class InteractiveShell(object,Magic):' | |||||
594 |
|
598 | |||
595 | #TODO: remove this, redundant |
|
599 | #TODO: remove this, redundant | |
596 | self.add_builtins() |
|
600 | self.add_builtins() | |
597 |
|
||||
598 |
|
||||
599 |
|
||||
600 |
|
||||
601 | # end __init__ |
|
601 | # end __init__ | |
602 |
|
602 | |||
603 | def var_expand(self,cmd,depth=0): |
|
603 | def var_expand(self,cmd,depth=0): | |
@@ -626,16 +626,15 b' class InteractiveShell(object,Magic):' | |||||
626 | """ |
|
626 | """ | |
627 | rc = self.rc |
|
627 | rc = self.rc | |
628 | try: |
|
628 | try: | |
629 |
self.db = pickleshare.PickleShareDB(rc.ipythondir + "/db") |
|
629 | self.db = pickleshare.PickleShareDB(rc.ipythondir + "/db") | |
630 | except exceptions.UnicodeDecodeError: |
|
630 | except exceptions.UnicodeDecodeError: | |
631 | print "Your ipythondir can't be decoded to unicode!" |
|
631 | print "Your ipythondir can't be decoded to unicode!" | |
632 | print "Please set HOME environment variable to something that" |
|
632 | print "Please set HOME environment variable to something that" | |
633 | print r"only has ASCII characters, e.g. c:\home" |
|
633 | print r"only has ASCII characters, e.g. c:\home" | |
634 | print "Now it is",rc.ipythondir |
|
634 | print "Now it is",rc.ipythondir | |
635 | sys.exit() |
|
635 | sys.exit() | |
636 |
self.shadowhist = IPython.history.ShadowHist(self.db) |
|
636 | self.shadowhist = IPython.history.ShadowHist(self.db) | |
637 |
|
637 | |||
638 |
|
||||
639 | def post_config_initialization(self): |
|
638 | def post_config_initialization(self): | |
640 | """Post configuration init method |
|
639 | """Post configuration init method | |
641 |
|
640 | |||
@@ -902,7 +901,6 b' class InteractiveShell(object,Magic):' | |||||
902 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, |
|
901 | call_pdb = property(_get_call_pdb,_set_call_pdb,None, | |
903 | 'Control auto-activation of pdb at exceptions') |
|
902 | 'Control auto-activation of pdb at exceptions') | |
904 |
|
903 | |||
905 |
|
||||
906 | # These special functions get installed in the builtin namespace, to |
|
904 | # These special functions get installed in the builtin namespace, to | |
907 | # provide programmatic (pure python) access to magics, aliases and system |
|
905 | # provide programmatic (pure python) access to magics, aliases and system | |
908 | # calls. This is important for logging, user scripting, and more. |
|
906 | # calls. This is important for logging, user scripting, and more. | |
@@ -1132,7 +1130,8 b' IPython will create a minimal default configuration for you.' | |||||
1132 | inif = 'ipythonrc.ini' |
|
1130 | inif = 'ipythonrc.ini' | |
1133 | else: |
|
1131 | else: | |
1134 | inif = 'ipythonrc' |
|
1132 | inif = 'ipythonrc' | |
1135 |
minimal_setup = {'ipy_user_conf.py' : 'import ipy_defaults', |
|
1133 | minimal_setup = {'ipy_user_conf.py' : 'import ipy_defaults', | |
|
1134 | inif : '# intentionally left blank' } | |||
1136 | os.makedirs(ipythondir, mode = 0777) |
|
1135 | os.makedirs(ipythondir, mode = 0777) | |
1137 | for f, cont in minimal_setup.items(): |
|
1136 | for f, cont in minimal_setup.items(): | |
1138 | open(ipythondir + '/' + f,'w').write(cont) |
|
1137 | open(ipythondir + '/' + f,'w').write(cont) | |
@@ -1291,7 +1290,6 b' want to merge them back into the new files.""" % locals()' | |||||
1291 | finally: |
|
1290 | finally: | |
1292 | readline.read_history_file(self.histfile) |
|
1291 | readline.read_history_file(self.histfile) | |
1293 | return wrapper |
|
1292 | return wrapper | |
1294 |
|
||||
1295 |
|
1293 | |||
1296 | def pre_readline(self): |
|
1294 | def pre_readline(self): | |
1297 | """readline hook to be used at the start of each line. |
|
1295 | """readline hook to be used at the start of each line. | |
@@ -1389,7 +1387,59 b' want to merge them back into the new files.""" % locals()' | |||||
1389 | if self.rc.quiet: |
|
1387 | if self.rc.quiet: | |
1390 | return True |
|
1388 | return True | |
1391 | return ask_yes_no(prompt,default) |
|
1389 | return ask_yes_no(prompt,default) | |
1392 |
|
1390 | |||
|
1391 | def cache_main_mod(self,mod): | |||
|
1392 | """Cache a main module. | |||
|
1393 | ||||
|
1394 | When scripts are executed via %run, we must keep a reference to their | |||
|
1395 | __main__ module (a FakeModule instance) around so that Python doesn't | |||
|
1396 | clear it, rendering objects defined therein useless. | |||
|
1397 | ||||
|
1398 | This method keeps said reference in a private dict, keyed by the | |||
|
1399 | absolute path of the module object (which corresponds to the script | |||
|
1400 | path). This way, for multiple executions of the same script we only | |||
|
1401 | keep one copy of __main__ (the last one), thus preventing memory leaks | |||
|
1402 | from old references while allowing the objects from the last execution | |||
|
1403 | to be accessible. | |||
|
1404 | ||||
|
1405 | Parameters | |||
|
1406 | ---------- | |||
|
1407 | mod : a module object | |||
|
1408 | ||||
|
1409 | Examples | |||
|
1410 | -------- | |||
|
1411 | ||||
|
1412 | In [10]: import IPython | |||
|
1413 | ||||
|
1414 | In [11]: _ip.IP.cache_main_mod(IPython) | |||
|
1415 | ||||
|
1416 | In [12]: IPython.__file__ in _ip.IP._user_main_modules | |||
|
1417 | Out[12]: True | |||
|
1418 | """ | |||
|
1419 | self._user_main_modules[os.path.abspath(mod.__file__) ] = mod | |||
|
1420 | ||||
|
1421 | def clear_main_mod_cache(self): | |||
|
1422 | """Clear the cache of main modules. | |||
|
1423 | ||||
|
1424 | Mainly for use by utilities like %reset. | |||
|
1425 | ||||
|
1426 | Examples | |||
|
1427 | -------- | |||
|
1428 | ||||
|
1429 | In [15]: import IPython | |||
|
1430 | ||||
|
1431 | In [16]: _ip.IP.cache_main_mod(IPython) | |||
|
1432 | ||||
|
1433 | In [17]: len(_ip.IP._user_main_modules) > 0 | |||
|
1434 | Out[17]: True | |||
|
1435 | ||||
|
1436 | In [18]: _ip.IP.clear_main_mod_cache() | |||
|
1437 | ||||
|
1438 | In [19]: len(_ip.IP._user_main_modules) == 0 | |||
|
1439 | Out[19]: True | |||
|
1440 | """ | |||
|
1441 | self._user_main_modules.clear() | |||
|
1442 | ||||
1393 | def _should_recompile(self,e): |
|
1443 | def _should_recompile(self,e): | |
1394 | """Utility routine for edit_syntax_error""" |
|
1444 | """Utility routine for edit_syntax_error""" | |
1395 |
|
1445 | |||
@@ -1545,8 +1595,6 b' want to merge them back into the new files.""" % locals()' | |||||
1545 | self.set_completer() |
|
1595 | self.set_completer() | |
1546 | except KeyboardInterrupt: |
|
1596 | except KeyboardInterrupt: | |
1547 | self.write("\nKeyboardInterrupt\n") |
|
1597 | self.write("\nKeyboardInterrupt\n") | |
1548 |
|
||||
1549 |
|
||||
1550 |
|
1598 | |||
1551 | def mainloop(self,banner=None): |
|
1599 | def mainloop(self,banner=None): | |
1552 | """Creates the local namespace and starts the mainloop. |
|
1600 | """Creates the local namespace and starts the mainloop. | |
@@ -1569,7 +1617,9 b' want to merge them back into the new files.""" % locals()' | |||||
1569 | try: |
|
1617 | try: | |
1570 | self.interact(banner) |
|
1618 | self.interact(banner) | |
1571 | #self.interact_with_readline() |
|
1619 | #self.interact_with_readline() | |
1572 | # XXX for testing of a readline-decoupled repl loop, call interact_with_readline above |
|
1620 | ||
|
1621 | # XXX for testing of a readline-decoupled repl loop, call | |||
|
1622 | # interact_with_readline above | |||
1573 |
|
1623 | |||
1574 | break |
|
1624 | break | |
1575 | except KeyboardInterrupt: |
|
1625 | except KeyboardInterrupt: |
@@ -1,6 +1,26 b'' | |||||
1 | """Simple script to instantiate a class for testing %run""" |
|
1 | """Simple script to instantiate a class for testing %run""" | |
2 |
|
2 | |||
|
3 | import sys | |||
|
4 | ||||
|
5 | # An external test will check that calls to f() work after %run | |||
3 | class foo: pass |
|
6 | class foo: pass | |
4 |
|
7 | |||
5 | def f(): |
|
8 | def f(): | |
6 | foo() |
|
9 | return foo() | |
|
10 | ||||
|
11 | # We also want to ensure that while objects remain available for immediate | |||
|
12 | # access, objects from *previous* runs of the same script get collected, to | |||
|
13 | # avoid accumulating massive amounts of old references. | |||
|
14 | class C(object): | |||
|
15 | def __init__(self,name): | |||
|
16 | self.name = name | |||
|
17 | ||||
|
18 | def __del__(self): | |||
|
19 | print 'Deleting object:',self.name | |||
|
20 | ||||
|
21 | try: | |||
|
22 | name = sys.argv[1] | |||
|
23 | except IndexError: | |||
|
24 | pass | |||
|
25 | else: | |||
|
26 | c = C(name) |
@@ -37,11 +37,23 b' def test_rehashx():' | |||||
37 | def doctest_run_ns(): |
|
37 | def doctest_run_ns(): | |
38 | """Classes declared %run scripts must be instantiable afterwards. |
|
38 | """Classes declared %run scripts must be instantiable afterwards. | |
39 |
|
39 | |||
|
40 | In [11]: run tclass | |||
|
41 | ||||
|
42 | In [12]: isinstance(f(),foo) | |||
|
43 | Out[12]: True | |||
|
44 | """ | |||
|
45 | ||||
|
46 | ||||
|
47 | def doctest_run_ns2(): | |||
|
48 | """Classes declared %run scripts must be instantiable afterwards. | |||
|
49 | ||||
40 | In [3]: run tclass.py |
|
50 | In [3]: run tclass.py | |
41 |
|
51 | |||
42 | In [4]: f() |
|
52 | In [4]: run tclass first_pass | |
|
53 | ||||
|
54 | In [5]: run tclass second_pass | |||
|
55 | Deleting object: first_pass | |||
43 | """ |
|
56 | """ | |
44 | pass # doctest only |
|
|||
45 |
|
57 | |||
46 |
|
58 | |||
47 | def doctest_hist_f(): |
|
59 | def doctest_hist_f(): |
General Comments 0
You need to be logged in to leave comments.
Login now