Show More
@@ -1079,7 +1079,49 b' class InteractiveShell(Configurable, Magic):' | |||||
1079 |
|
1079 | |||
1080 | # Clear out the namespace from the last %run |
|
1080 | # Clear out the namespace from the last %run | |
1081 | self.new_main_mod() |
|
1081 | self.new_main_mod() | |
1082 |
|
1082 | |||
|
1083 | def del_var(self, varname, by_name=False): | |||
|
1084 | """Delete a variable from the various namespaces, so that, as | |||
|
1085 | far as possible, we're not keeping any hidden references to it. | |||
|
1086 | ||||
|
1087 | Parameters | |||
|
1088 | ---------- | |||
|
1089 | varname : str | |||
|
1090 | The name of the variable to delete. | |||
|
1091 | by_name : bool | |||
|
1092 | If True, delete variables with the given name in each | |||
|
1093 | namespace. If False (default), find the variable in the user | |||
|
1094 | namespace, and delete references to it. | |||
|
1095 | """ | |||
|
1096 | if varname in ('__builtin__', '__builtins__'): | |||
|
1097 | raise ValueError("Refusing to delete %s" % varname) | |||
|
1098 | ns_refs = self.ns_refs_table + [self.user_ns, | |||
|
1099 | self.user_global_ns, self._user_main_module.__dict__] +\ | |||
|
1100 | self._main_ns_cache.values() | |||
|
1101 | ||||
|
1102 | if by_name: # Delete by name | |||
|
1103 | for ns in ns_refs: | |||
|
1104 | try: | |||
|
1105 | del ns[varname] | |||
|
1106 | except KeyError: | |||
|
1107 | pass | |||
|
1108 | else: # Delete by object | |||
|
1109 | try: | |||
|
1110 | obj = self.user_ns[varname] | |||
|
1111 | except KeyError: | |||
|
1112 | raise NameError("name '%s' is not defined" % varname) | |||
|
1113 | # Also check in output history | |||
|
1114 | ns_refs.append(self.history_manager.output_hist) | |||
|
1115 | for ns in ns_refs: | |||
|
1116 | to_delete = [n for n, o in ns.iteritems() if o is obj] | |||
|
1117 | for name in to_delete: | |||
|
1118 | del ns[name] | |||
|
1119 | ||||
|
1120 | # displayhook keeps extra references, but not in a dictionary | |||
|
1121 | for name in ('_', '__', '___'): | |||
|
1122 | if getattr(self.displayhook, name) is obj: | |||
|
1123 | setattr(self.displayhook, name, None) | |||
|
1124 | ||||
1083 | def reset_selective(self, regex=None): |
|
1125 | def reset_selective(self, regex=None): | |
1084 | """Clear selective variables from internal namespaces based on a |
|
1126 | """Clear selective variables from internal namespaces based on a | |
1085 | specified regular expression. |
|
1127 | specified regular expression. |
@@ -1081,7 +1081,24 b' Currently the magic system has the following functions:\\n"""' | |||||
1081 | raise TypeError('regex must be a string or compiled pattern') |
|
1081 | raise TypeError('regex must be a string or compiled pattern') | |
1082 | for i in self.magic_who_ls(): |
|
1082 | for i in self.magic_who_ls(): | |
1083 | if m.search(i): |
|
1083 | if m.search(i): | |
1084 |
del(user_ns[i]) |
|
1084 | del(user_ns[i]) | |
|
1085 | ||||
|
1086 | def magic_xdel(self, parameter_s=''): | |||
|
1087 | """Delete a variable, trying to clear it from anywhere that | |||
|
1088 | IPython's machinery has references to it. By default, this uses | |||
|
1089 | the identity of the named object in the user namespace to remove | |||
|
1090 | references held under other names. The object is also removed | |||
|
1091 | from the output history. | |||
|
1092 | ||||
|
1093 | Options | |||
|
1094 | -n : Delete the specified name from all namespaces, without | |||
|
1095 | checking their identity. | |||
|
1096 | """ | |||
|
1097 | opts, varname = self.parse_options(parameter_s,'n') | |||
|
1098 | try: | |||
|
1099 | self.shell.del_var(varname, ('n' in opts)) | |||
|
1100 | except (NameError, ValueError) as e: | |||
|
1101 | print type(e).__name__ +": "+ str(e) | |||
1085 |
|
1102 | |||
1086 | def magic_logstart(self,parameter_s=''): |
|
1103 | def magic_logstart(self,parameter_s=''): | |
1087 | """Start logging anywhere in a session. |
|
1104 | """Start logging anywhere in a session. |
General Comments 0
You need to be logged in to leave comments.
Login now