1. Naoki INADA
  2. bpython

Commits

Naoki INADA  committed 7142e7e

Split get_source function.

  • Participants
  • Parent commits d0cb438
  • Branches default

Comments (0)

Files changed (1)

File bpython/repl.py

View file
 import inspect
 import os
 import pydoc
+import re
 import subprocess
 import sys
 import textwrap
                 line = self.current_line()
                 if inspection.is_eval_safe_name(line):
                     obj = self.get_object(line)
-            source = inspect.getsource(obj)
-        except (AttributeError, IOError, NameError, TypeError):
+            return get_source(obj)
+        except (AttributeError, IOError, NameError, TypeError, UnicodeError):
             return None
-        else:
-            # PEP 0263: Encoding declarations.
-            header = '\n'.join(source.split('\n', 2)[:2])
-            m = re.search(r"coding[:=]\s*([-\w.]+)", header)
-            if m:
-                encoding = m.group(1)
-            else:
-                encoding = 'utf-8'
-            try:
-                return source.decode(encoding)
-            except UnicodeError:
-                return source
 
     def complete(self, tab=False):
         """Construct a full list of possible completions and construct and
         return any(check(token) for check in is_token_types)
 
     return token_is_any_of
+
+if py3:
+    get_source = inspect.get_source
+else:
+    def get_source(object):
+        lines, lnum = inspect.findsource(objects)
+
+        # PEP 0263: Encoding declarations.
+        m = re.search(r"coding[:=]\s*([-\w.]+)", ''.join(lines[:2]))
+        if m:
+            encoding = m.group(1)
+        else:
+            encoding = 'ascii'
+
+        if not inspect.ismodule(object):
+            lines = inspect.getblock(lines[lnum:])
+
+        return ''.join(lines).decode(encoding)