MCPcopy Index your code
hub / github.com/python/cpython / encode

Method encode

Lib/idlelib/iomenu.py:270–299  ·  view source on GitHub ↗
(self, chars)

Source from the content-addressed store, hash-verified

268 return text
269
270 def encode(self, chars):
271 if isinstance(chars, bytes):
272 # This is either plain ASCII, or Tk was returning mixed-encoding
273 # text to us. Don't try to guess further.
274 return chars
275 # Preserve a BOM that might have been present on opening
276 if self.fileencoding == 'utf-8-sig':
277 return chars.encode('utf-8-sig')
278 # See whether there is anything non-ASCII in it.
279 # If not, no need to figure out the encoding.
280 try:
281 return chars.encode('ascii')
282 except UnicodeEncodeError:
283 pass
284 # Check if there is an encoding declared
285 try:
286 encoded = chars.encode('ascii', 'replace')
287 enc, _ = tokenize.detect_encoding(io.BytesIO(encoded).readline)
288 return chars.encode(enc)
289 except SyntaxError as err:
290 failed = str(err)
291 except UnicodeEncodeError:
292 failed = "Invalid encoding '%s'" % enc
293 messagebox.showerror(
294 "I/O Error",
295 "%s.\nSaving as UTF-8" % failed,
296 parent=self.text)
297 # Fallback: save as UTF-8, with BOM - ignoring the incorrect
298 # declared encoding
299 return chars.encode('utf-8-sig')
300
301 def print_window(self, event):
302 confirm = messagebox.askokcancel(

Callers 4

writefileMethod · 0.95
sprintFunction · 0.45
writeMethod · 0.45
displayhookFunction · 0.45

Calls 2

strFunction · 0.85
showerrorMethod · 0.45

Tested by

no test coverage detected