Utilities/compatibility_fixes.py

changeset 3539
0c2dc1446ebf
parent 3515
1b8381afe38f
child 3591
2f2a4a76dd22
equal deleted inserted replaced
3538:33a75660df08 3539:0c2dc1446ebf
30 @keyparam newline controls how universal newlines works (string) 30 @keyparam newline controls how universal newlines works (string)
31 @keyparam closefd close underlying file descriptor if given as file 31 @keyparam closefd close underlying file descriptor if given as file
32 parameter (boolean) 32 parameter (boolean)
33 @return Returns the new file object 33 @return Returns the new file object
34 """ 34 """
35 return File(file, mode, buffering, encoding, errors, newline, closefd) 35 return File(file, mode, buffering, encoding, errors, newline, closefd)
36 36
37 37
38 class File(file): # __IGNORE_WARNING__ 38 class File(file): # __IGNORE_WARNING__
39 """ 39 """
40 Facade for the original file class. 40 Facade for the original file class.
74 else: 74 else:
75 self.__errors = errors 75 self.__errors = errors
76 76
77 file.__init__(self, filein, mode, buffering) # __IGNORE_WARNING__ 77 file.__init__(self, filein, mode, buffering) # __IGNORE_WARNING__
78 78
79 def read(self, n=-1): 79 def read(self, n=-1):
80 """ 80 """
81 Read n bytes or all if n=-1 from file. 81 Read n bytes or all if n=-1 from file.
82 82
83 @keyparam n bytecount or all if n=-1 (int) 83 @keyparam n bytecount or all if n=-1 (int)
84 @return decoded bytes read 84 @return decoded bytes read
85 """ 85 """
86 txt = super(File, self).read(n) 86 txt = super(File, self).read(n)
87 if self.__encoding is None: 87 if self.__encoding is None:
88 return txt 88 return txt
89 else: 89 else:
90 return codecs.decode(txt, self.__encoding) 90 return codecs.decode(txt, self.__encoding)
91 91
92 def readline(self, limit=-1): 92 def readline(self, limit=-1):
93 """ 93 """
94 Read one line from file. 94 Read one line from file.
95 95
96 @keyparam limit maximum bytes to read or all if limit=-1 (int) 96 @keyparam limit maximum bytes to read or all if limit=-1 (int)
97 @return decoded line read 97 @return decoded line read
98 """ 98 """
99 txt = super(File, self).readline(limit) 99 txt = super(File, self).readline(limit)
100 if self.__encoding is None: 100 if self.__encoding is None:
101 return txt 101 return txt
102 else: 102 else:
103 return codecs.decode(txt, self.__encoding) 103 return codecs.decode(txt, self.__encoding)
104 104
105 def readlines(self, hint=-1): 105 def readlines(self, hint=-1):
106 """ 106 """
107 Read all lines from file. 107 Read all lines from file.
108 108
109 @keyparam hint maximum bytes to read or all if hint=-1 (int) 109 @keyparam hint maximum bytes to read or all if hint=-1 (int)
110 @return decoded lines read 110 @return decoded lines read
111 """ 111 """
112 if self.__encoding is None: 112 if self.__encoding is None:
113 return super(File, self).readlines(hint) 113 return super(File, self).readlines(hint)
114 else: 114 else:
115 return [codecs.decode(txt, self.__encoding) 115 return [codecs.decode(txt, self.__encoding)
116 for txt in super(File, self).readlines(hint)] 116 for txt in super(File, self).readlines(hint)]
117 117
118 def write(self, txt): 118 def write(self, txt):
119 """ 119 """
120 Write given data to file. Encode if needed. 120 Write given data to file. Encode if needed.
121 121
122 @param txt data to write. (str, bytes) 122 @param txt data to write. (str, bytes)
123 """ 123 """
137 """ 137 """
138 txt = super(File, self).next() 138 txt = super(File, self).next()
139 if self.__encoding is None: 139 if self.__encoding is None:
140 return txt 140 return txt
141 else: 141 else:
142 return codecs.decode(txt, self.__encoding) 142 return codecs.decode(txt, self.__encoding)
143 143
144 # Inject into the __builtin__ dictionary 144 # Inject into the __builtin__ dictionary
145 __builtin__.open = open 145 __builtin__.open = open
146 146
147 if __name__ == '__main__': 147 if __name__ == '__main__':

eric ide

mercurial