Get encoding for python source file defining obj Returns None if obj is not defined in a sourcefile.
(obj)
| 89 | |
| 90 | |
| 91 | def get_encoding(obj): |
| 92 | """Get encoding for python source file defining obj |
| 93 | |
| 94 | Returns None if obj is not defined in a sourcefile. |
| 95 | """ |
| 96 | ofile = find_file(obj) |
| 97 | # run contents of file through pager starting at line where the object |
| 98 | # is defined, as long as the file isn't binary and is actually on the |
| 99 | # filesystem. |
| 100 | if ofile is None: |
| 101 | return None |
| 102 | elif ofile.endswith(('.so', '.dll', '.pyd')): |
| 103 | return None |
| 104 | elif not os.path.isfile(ofile): |
| 105 | return None |
| 106 | else: |
| 107 | # Print only text files, not extension binaries. Note that |
| 108 | # getsourcelines returns lineno with 1-offset and page() uses |
| 109 | # 0-offset, so we must adjust. |
| 110 | with stdlib_io.open(ofile, 'rb') as buffer: # Tweaked to use io.open for Python 2 |
| 111 | encoding, lines = openpy.detect_encoding(buffer.readline) |
| 112 | return encoding |
| 113 | |
| 114 | def getdoc(obj) -> Union[str,None]: |
| 115 | """Stable wrapper around inspect.getdoc. |