##// END OF EJS Templates
find_user_code improvement for not py-files...
Matthias BUSSONNIER -
Show More
@@ -2738,8 +2738,8 b' class InteractiveShell(SingletonConfigurable, Magic):'
2738 """Show a usage message"""
2738 """Show a usage message"""
2739 page.page(IPython.core.usage.interactive_usage)
2739 page.page(IPython.core.usage.interactive_usage)
2740
2740
2741 def find_user_code(self, target, raw=True):
2741 def find_user_code(self, target, raw=True, py_only=False):
2742 """Get a code string from history, file, or a string or macro.
2742 """Get a code string from history, file, url, or a string or macro.
2743
2743
2744 This is mainly used by magic functions.
2744 This is mainly used by magic functions.
2745
2745
@@ -2757,6 +2757,10 b' class InteractiveShell(SingletonConfigurable, Magic):'
2757 If true (default), retrieve raw history. Has no effect on the other
2757 If true (default), retrieve raw history. Has no effect on the other
2758 retrieval mechanisms.
2758 retrieval mechanisms.
2759
2759
2760 py_only : bool
2761 Only try to fetch python code, do not try alternative methods to decode file
2762 if unicode fails.
2763
2760 Returns
2764 Returns
2761 -------
2765 -------
2762 A string of code.
2766 A string of code.
@@ -2768,21 +2772,33 b' class InteractiveShell(SingletonConfigurable, Magic):'
2768 code = self.extract_input_lines(target, raw=raw) # Grab history
2772 code = self.extract_input_lines(target, raw=raw) # Grab history
2769 if code:
2773 if code:
2770 return code
2774 return code
2775 utarget = unquote_filename(target)
2771 try:
2776 try:
2772 utarget = unquote_filename(target)
2773 if utarget.startswith(('http://', 'https://')):
2777 if utarget.startswith(('http://', 'https://')):
2774 return openpy.read_py_url(utarget, skip_encoding_cookie=True)
2778 return openpy.read_py_url(utarget, skip_encoding_cookie=True)
2775 except UnicodeDecodeError:
2779 except UnicodeDecodeError:
2776 raise ValueError(("'%s' seem to be unredable.") % utarget)
2780 if not py_only :
2781 return openpy.read_py_url(utarget, skip_encoding_cookie=False)
2782 raise ValueError(("'%s' seem to be unreadable.") % utarget)
2777
2783
2778 try :
2784 try :
2779 pyfile = get_py_filename(target)
2785 pyfile = get_py_filename(target)
2780 return openpy.read_py_file(pyfile, skip_encoding_cookie=True)
2786 return openpy.read_py_file(pyfile, skip_encoding_cookie=True)
2781 except IOError:
2787 except IOError:
2788 #py file don't exist... we just made a bad guess, don't raise
2782 pass
2789 pass
2790 except UnicodeDecodeError :
2791 if not py_only :
2792 return openpy.read_py_file(utarget, skip_encoding_cookie=False)
2793 raise ValueError(("'%s' seem to be unreadable.") % target)
2783
2794
2784 if os.path.isfile(target): # Read file
2795 if os.path.isfile(target): # Read file
2785 return openpy.read_py_file(target, skip_encoding_cookie=True)
2796 try :
2797 return openpy.read_py_file(target, skip_encoding_cookie=True)
2798 except UnicodeDecodeError :
2799 if not py_only :
2800 return openpy.read_py_file(utarget, skip_encoding_cookie=False)
2801 raise ValueError(("'%s' seem to be unreadable.") % target)
2786
2802
2787 try: # User namespace
2803 try: # User namespace
2788 codeobj = eval(target, self.user_ns)
2804 codeobj = eval(target, self.user_ns)
@@ -189,3 +189,4 b" def read_py_url(url, errors='replace', skip_encoding_cookie=True):"
189 return "".join(strip_encoding_cookie(text))
189 return "".join(strip_encoding_cookie(text))
190 else:
190 else:
191 return text.read()
191 return text.read()
192
General Comments 0
You need to be logged in to leave comments. Login now