MCPcopy Create free account
hub / github.com/ipython/ipython / get_encoding

Function get_encoding

IPython/core/oinspect.py:91–112  ·  view source on GitHub ↗

Get encoding for python source file defining obj Returns None if obj is not defined in a sourcefile.

(obj)

Source from the content-addressed store, hash-verified

89
90
91def 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
114def getdoc(obj) -> Union[str,None]:
115 """Stable wrapper around inspect.getdoc.

Callers 1

getsourceFunction · 0.85

Calls 1

find_fileFunction · 0.85

Tested by

no test coverage detected