Show More
@@ -3089,7 +3089,7 b' class InteractiveShell(SingletonConfigurable):' | |||||
3089 | lines = self.history_manager.get_range_by_str(range_str, raw=raw) |
|
3089 | lines = self.history_manager.get_range_by_str(range_str, raw=raw) | |
3090 | return "\n".join(x for _, _, x in lines) |
|
3090 | return "\n".join(x for _, _, x in lines) | |
3091 |
|
3091 | |||
3092 | def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True): |
|
3092 | def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False): | |
3093 | """Get a code string from history, file, url, or a string or macro. |
|
3093 | """Get a code string from history, file, url, or a string or macro. | |
3094 |
|
3094 | |||
3095 | This is mainly used by magic functions. |
|
3095 | This is mainly used by magic functions. | |
@@ -3156,11 +3156,31 b' class InteractiveShell(SingletonConfigurable):' | |||||
3156 | elif os.path.isdir(os.path.expanduser(tgt)): |
|
3156 | elif os.path.isdir(os.path.expanduser(tgt)): | |
3157 | raise ValueError("'%s' is a directory, not a regular file." % target) |
|
3157 | raise ValueError("'%s' is a directory, not a regular file." % target) | |
3158 |
|
3158 | |||
|
3159 | if search_ns: | |||
|
3160 | obj = None | |||
|
3161 | parts = target.split(".") | |||
|
3162 | try: | |||
|
3163 | if len(parts) >= 1: | |||
|
3164 | obj = self.user_ns[parts[0]] | |||
|
3165 | ||||
|
3166 | for new_obj in parts[1:]: | |||
|
3167 | obj = getattr(obj, new_obj) | |||
|
3168 | ||||
|
3169 | if obj: | |||
|
3170 | ||||
|
3171 | code = oinspect.getsource(obj) | |||
|
3172 | return code | |||
|
3173 | except Exception: | |||
|
3174 | # Either the value wa't in th user_ns or the objects could | |||
|
3175 | # not be inspected. It still might load below, so just pass. | |||
|
3176 | pass | |||
|
3177 | ||||
3159 | try: # User namespace |
|
3178 | try: # User namespace | |
3160 | codeobj = eval(target, self.user_ns) |
|
3179 | codeobj = eval(target, self.user_ns) | |
3161 | except Exception: |
|
3180 | except Exception: | |
3162 | raise ValueError(("'%s' was not found in history, as a file, url, " |
|
3181 | raise ValueError(("'%s' was not found in history, as a file, url, " | |
3163 | "nor in the user namespace.") % target) |
|
3182 | "nor in the user namespace.") % target) | |
|
3183 | ||||
3164 | if isinstance(codeobj, string_types): |
|
3184 | if isinstance(codeobj, string_types): | |
3165 | return codeobj |
|
3185 | return codeobj | |
3166 | elif isinstance(codeobj, Macro): |
|
3186 | elif isinstance(codeobj, Macro): |
@@ -272,7 +272,8 b' class CodeMagics(Magics):' | |||||
272 | Usage:\\ |
|
272 | Usage:\\ | |
273 | %load [options] source |
|
273 | %load [options] source | |
274 |
|
274 | |||
275 |
where source can be a filename, URL, input history range |
|
275 | where source can be a filename, URL, input history range, macro, or | |
|
276 | element in the user namespace | |||
276 |
|
277 | |||
277 | Options: |
|
278 | Options: | |
278 |
|
279 | |||
@@ -285,6 +286,8 b' class CodeMagics(Magics):' | |||||
285 |
|
286 | |||
286 | -y : Don't ask confirmation for loading source above 200 000 characters. |
|
287 | -y : Don't ask confirmation for loading source above 200 000 characters. | |
287 |
|
288 | |||
|
289 | -n : Include the user's namespace when searching for source code. | |||
|
290 | ||||
288 | This magic command can either take a local filename, a URL, an history |
|
291 | This magic command can either take a local filename, a URL, an history | |
289 | range (see %history) or a macro as argument, it will prompt for |
|
292 | range (see %history) or a macro as argument, it will prompt for | |
290 | confirmation before loading source with more than 200 000 characters, unless |
|
293 | confirmation before loading source with more than 200 000 characters, unless | |
@@ -297,14 +300,18 b' class CodeMagics(Magics):' | |||||
297 | %load -r 5-10 myscript.py |
|
300 | %load -r 5-10 myscript.py | |
298 | %load -r 10-20,30,40: foo.py |
|
301 | %load -r 10-20,30,40: foo.py | |
299 | %load -s MyClass,wonder_function myscript.py |
|
302 | %load -s MyClass,wonder_function myscript.py | |
|
303 | %load -n MyClass | |||
|
304 | %load -n my_module.wonder_function | |||
300 | """ |
|
305 | """ | |
301 | opts,args = self.parse_options(arg_s,'ys:r:') |
|
306 | opts,args = self.parse_options(arg_s,'yns:r:') | |
302 |
|
307 | |||
303 | if not args: |
|
308 | if not args: | |
304 | raise UsageError('Missing filename, URL, input history range, ' |
|
309 | raise UsageError('Missing filename, URL, input history range, ' | |
305 |
' |
|
310 | 'macro, or element in the user namespace.') | |
|
311 | ||||
|
312 | search_ns = 'n' in opts | |||
306 |
|
313 | |||
307 | contents = self.shell.find_user_code(args) |
|
314 | contents = self.shell.find_user_code(args, search_ns=search_ns) | |
308 |
|
315 | |||
309 | if 's' in opts: |
|
316 | if 's' in opts: | |
310 | try: |
|
317 | try: |
General Comments 0
You need to be logged in to leave comments.
Login now