Get encoding for python source file defining obj Returns None if obj is not defined in a sourcefile.
(obj)
| 185 | |
| 186 | |
| 187 | def get_encoding(obj): |
| 188 | """Get encoding for python source file defining obj |
| 189 | |
| 190 | Returns None if obj is not defined in a sourcefile. |
| 191 | """ |
| 192 | ofile = find_file(obj) |
| 193 | # run contents of file through pager starting at line where the object |
| 194 | # is defined, as long as the file isn't binary and is actually on the |
| 195 | # filesystem. |
| 196 | if ofile is None: |
| 197 | return None |
| 198 | elif ofile.endswith(('.so', '.dll', '.pyd')): |
| 199 | return None |
| 200 | elif not os.path.isfile(ofile): |
| 201 | return None |
| 202 | else: |
| 203 | # Print only text files, not extension binaries. Note that |
| 204 | # getsourcelines returns lineno with 1-offset and page() uses |
| 205 | # 0-offset, so we must adjust. |
| 206 | with stdlib_io.open(ofile, 'rb') as buffer: # Tweaked to use io.open for Python 2 |
| 207 | encoding, _lines = openpy.detect_encoding(buffer.readline) |
| 208 | return encoding |
| 209 | |
| 210 | |
| 211 | def getdoc(obj) -> Union[str, None]: |
nothing calls this directly
no test coverage detected
searching dependent graphs…