MCPcopy Index your code
hub / github.com/ipython/ipython / get_encoding

Function get_encoding

IPython/core/oinspect.py:187–208  ·  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

185
186
187def 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
211def getdoc(obj) -> Union[str, None]:

Callers

nothing calls this directly

Calls 1

find_fileFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…