Back to index

python3.2  3.2.2
Classes | Functions | Variables
idlelib.IOBinding Namespace Reference

Classes

class  IOBinding

Functions

def coding_spec
def test

Variables

tuple filesystemencoding = sys.getfilesystemencoding()
string locale_encoding = 'ascii'
 currently unused
 encoding = locale_encoding
tuple coding_re = re.compile("coding[:=]\s*([-\w_.]+)")
 'encoding' is used below in encode(), check!
 text
 flist

Function Documentation

Return the encoding declaration according to PEP 263.

When checking encoded data, only the first two lines should be passed
in to avoid a UnicodeDecodeError if the rest of the data is not unicode.
The first two lines would contain the encoding specification.

Raise a LookupError if the encoding is declared but unknown.

Definition at line 67 of file IOBinding.py.

00067 
00068 def coding_spec(data):
00069     """Return the encoding declaration according to PEP 263.
00070 
00071     When checking encoded data, only the first two lines should be passed
00072     in to avoid a UnicodeDecodeError if the rest of the data is not unicode.
00073     The first two lines would contain the encoding specification.
00074 
00075     Raise a LookupError if the encoding is declared but unknown.
00076     """
00077     if isinstance(data, bytes):
00078         # This encoding might be wrong. However, the coding
00079         # spec must be ASCII-only, so any non-ASCII characters
00080         # around here will be ignored. Decoding to Latin-1 should
00081         # never fail (except for memory outage)
00082         lines = data.decode('iso-8859-1')
00083     else:
00084         lines = data
00085     # consider only the first two lines
00086     if '\n' in lines:
00087         lst = lines.split('\n')[:2]
00088     elif '\r' in lines:
00089         lst = lines.split('\r')[:2]
00090     else:
00091         lst = list(lines)
00092     str = '\n'.join(lst)
00093     match = coding_re.search(str)
00094     if not match:
00095         return None
00096     name = match.group(1)
00097     try:
00098         codecs.lookup(name)
00099     except LookupError:
00100         # The standard encoding error does not indicate the encoding
00101         raise LookupError("Unknown encoding: "+name)
00102     return name
00103 

Here is the caller graph for this function:

Definition at line 517 of file IOBinding.py.

00517 
00518 def test():
00519     root = Tk()
00520     class MyEditWin:
00521         def __init__(self, text):
00522             self.text = text
00523             self.flist = None
00524             self.text.bind("<Control-o>", self.open)
00525             self.text.bind("<Control-s>", self.save)
00526             self.text.bind("<Alt-s>", self.save_as)
00527             self.text.bind("<Alt-z>", self.save_a_copy)
00528         def get_saved(self): return 0
00529         def set_saved(self, flag): pass
00530         def reset_undo(self): pass
00531         def open(self, event):
00532             self.text.event_generate("<<open-window-from-file>>")
00533         def save(self, event):
00534             self.text.event_generate("<<save-window>>")
00535         def save_as(self, event):
00536             self.text.event_generate("<<save-window-as-file>>")
00537         def save_a_copy(self, event):
00538             self.text.event_generate("<<save-copy-of-window-as-file>>")
00539     text = Text(root)
00540     text.pack()
00541     text.focus_set()
00542     editwin = MyEditWin(text)
00543     io = IOBinding(editwin)
00544     root.mainloop()

Here is the call graph for this function:


Variable Documentation

tuple idlelib.IOBinding.coding_re = re.compile("coding[:=]\s*([-\w_.]+)")

'encoding' is used below in encode(), check!

Definition at line 65 of file IOBinding.py.

Definition at line 62 of file IOBinding.py.

tuple idlelib.IOBinding.filesystemencoding = sys.getfilesystemencoding()

Definition at line 25 of file IOBinding.py.

Definition at line 522 of file IOBinding.py.

currently unused

Definition at line 27 of file IOBinding.py.

Definition at line 521 of file IOBinding.py.