Utilities/compatibility_fixes.py

changeset 3591
2f2a4a76dd22
parent 3539
0c2dc1446ebf
child 3670
f0cb7579c0b4
equal deleted inserted replaced
3590:5280e37405b8 3591:2f2a4a76dd22
40 Facade for the original file class. 40 Facade for the original file class.
41 """ 41 """
42 def __init__(self, filein, mode='r', buffering=-1, 42 def __init__(self, filein, mode='r', buffering=-1,
43 encoding=None, errors=None, newline=None, closefd=True): 43 encoding=None, errors=None, newline=None, closefd=True):
44 """ 44 """
45 Constructor, checks for unimplemented parameters. 45 Constructor
46
47 It checks for unimplemented parameters.
46 48
47 @param filein filename or file descriptor (string) 49 @param filein filename or file descriptor (string)
48 @keyparam mode access mode (string) 50 @keyparam mode access mode (string)
49 @keyparam buffering size of the read buffer (string) 51 @keyparam buffering size of the read buffer (string)
50 @keyparam encoding character encoding for reading/ writing (string) 52 @keyparam encoding character encoding for reading/ writing (string)
76 78
77 file.__init__(self, filein, mode, buffering) # __IGNORE_WARNING__ 79 file.__init__(self, filein, mode, buffering) # __IGNORE_WARNING__
78 80
79 def read(self, n=-1): 81 def read(self, n=-1):
80 """ 82 """
81 Read n bytes or all if n=-1 from file. 83 Public method to read n bytes or all if n=-1 from file.
82 84
83 @keyparam n bytecount or all if n=-1 (int) 85 @keyparam n bytecount or all if n=-1 (int)
84 @return decoded bytes read 86 @return decoded bytes read
85 """ 87 """
86 txt = super(File, self).read(n) 88 txt = super(File, self).read(n)
89 else: 91 else:
90 return codecs.decode(txt, self.__encoding) 92 return codecs.decode(txt, self.__encoding)
91 93
92 def readline(self, limit=-1): 94 def readline(self, limit=-1):
93 """ 95 """
94 Read one line from file. 96 Public method to read one line from file.
95 97
96 @keyparam limit maximum bytes to read or all if limit=-1 (int) 98 @keyparam limit maximum bytes to read or all if limit=-1 (int)
97 @return decoded line read 99 @return decoded line read
98 """ 100 """
99 txt = super(File, self).readline(limit) 101 txt = super(File, self).readline(limit)
102 else: 104 else:
103 return codecs.decode(txt, self.__encoding) 105 return codecs.decode(txt, self.__encoding)
104 106
105 def readlines(self, hint=-1): 107 def readlines(self, hint=-1):
106 """ 108 """
107 Read all lines from file. 109 Public method to read all lines from file.
108 110
109 @keyparam hint maximum bytes to read or all if hint=-1 (int) 111 @keyparam hint maximum bytes to read or all if hint=-1 (int)
110 @return decoded lines read 112 @return decoded lines read
111 """ 113 """
112 if self.__encoding is None: 114 if self.__encoding is None:
115 return [codecs.decode(txt, self.__encoding) 117 return [codecs.decode(txt, self.__encoding)
116 for txt in super(File, self).readlines(hint)] 118 for txt in super(File, self).readlines(hint)]
117 119
118 def write(self, txt): 120 def write(self, txt):
119 """ 121 """
120 Write given data to file. Encode if needed. 122 Public method to write given data to file and encode if needed.
121 123
122 @param txt data to write. (str, bytes) 124 @param txt data to write. (str, bytes)
123 """ 125 """
124 if self.__encoding is not None: 126 if self.__encoding is not None:
125 txt = codecs.encode(txt, self.__encoding, self.__errors) 127 txt = codecs.encode(txt, self.__encoding, self.__errors)
129 131
130 super(File, self).write(txt) 132 super(File, self).write(txt)
131 133
132 def next(self): 134 def next(self):
133 """ 135 """
134 Method if used in a iterator. 136 Public method used in an iterator.
135 137
136 @return decoded data read 138 @return decoded data read
137 """ 139 """
138 txt = super(File, self).next() 140 txt = super(File, self).next()
139 if self.__encoding is None: 141 if self.__encoding is None:

eric ide

mercurial