Show More
@@ -24,6 +24,8 b' import linecache' | |||
|
24 | 24 | import os |
|
25 | 25 | import sys |
|
26 | 26 | import types |
|
27 | import io as stdlib_io | |
|
28 | ||
|
27 | 29 | from collections import namedtuple |
|
28 | 30 | try: |
|
29 | 31 | from itertools import izip_longest |
@@ -92,6 +94,29 b' def object_info(**kw):' | |||
|
92 | 94 | return infodict |
|
93 | 95 | |
|
94 | 96 | |
|
97 | def get_encoding(obj): | |
|
98 | """Get encoding for python source file defining obj | |
|
99 | ||
|
100 | Returns None if obj is not defined in a sourcefile. | |
|
101 | """ | |
|
102 | ofile = find_file(obj) | |
|
103 | # run contents of file through pager starting at line where the object | |
|
104 | # is defined, as long as the file isn't binary and is actually on the | |
|
105 | # filesystem. | |
|
106 | if ofile is None: | |
|
107 | return None | |
|
108 | elif ofile.endswith(('.so', '.dll', '.pyd')): | |
|
109 | return None | |
|
110 | elif not os.path.isfile(ofile): | |
|
111 | return None | |
|
112 | else: | |
|
113 | # Print only text files, not extension binaries. Note that | |
|
114 | # getsourcelines returns lineno with 1-offset and page() uses | |
|
115 | # 0-offset, so we must adjust. | |
|
116 | buffer = stdlib_io.open(ofile, 'rb') # Tweaked to use io.open for Python 2 | |
|
117 | encoding, lines = openpy.detect_encoding(buffer.readline) | |
|
118 | return encoding | |
|
119 | ||
|
95 | 120 | def getdoc(obj): |
|
96 | 121 | """Stable wrapper around inspect.getdoc. |
|
97 | 122 | |
@@ -111,10 +136,16 b' def getdoc(obj):' | |||
|
111 | 136 | return inspect.cleandoc(ds) |
|
112 | 137 | |
|
113 | 138 | try: |
|
114 |
|
|
|
139 | docstr = inspect.getdoc(obj) | |
|
140 | encoding = get_encoding(obj) | |
|
141 | if encoding: | |
|
142 | return py3compat.cast_unicode(docstr, encoding=encoding) | |
|
143 | else: | |
|
144 | return docstr | |
|
115 | 145 | except Exception: |
|
116 | 146 | # Harden against an inspect failure, which can occur with |
|
117 | 147 | # SWIG-wrapped extensions. |
|
148 | raise | |
|
118 | 149 | return None |
|
119 | 150 | |
|
120 | 151 |
General Comments 0
You need to be logged in to leave comments.
Login now