Utilities/compatibility_fixes.py

branch
Py2 comp.
changeset 3065
070b35dde35e
parent 2790
6686a3326df8
child 3145
a9de05d4a22f
equal deleted inserted replaced
3061:1c0ea3a87390 3065:070b35dde35e
3 # Copyright (c) 2013 - 2013 Tobias Rzepka <tobias.rzepka@gmail.com> 3 # Copyright (c) 2013 - 2013 Tobias Rzepka <tobias.rzepka@gmail.com>
4 # 4 #
5 5
6 """ 6 """
7 Module implementing the open behavior of Python3 for use with Eric5. 7 Module implementing the open behavior of Python3 for use with Eric5.
8
8 The Eric5 used features are emulated only. The not emulated features 9 The Eric5 used features are emulated only. The not emulated features
9 should throw a NotImplementedError exception. 10 should throw a NotImplementedError exception.
10 """ 11 """
11 12
12 from __future__ import unicode_literals # __IGNORE_WARNING__ 13 from __future__ import unicode_literals # __IGNORE_WARNING__
13 14
14 import __builtin__ 15 import __builtin__
15 import codecs 16 import codecs
16 17
17 18
18 def open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True): 19 def open(file, mode='r', buffering=-1, encoding=None,
20 errors=None, newline=None, closefd=True):
19 """ 21 """
20 Replacement for the build in open function. 22 Replacement for the build in open function.
21 23
22 @param file filename or file descriptor (string) 24 @param file filename or file descriptor (string)
23 @keyparam mode access mode (string) 25 @keyparam mode access mode (string)
24 @keyparam buffering size of the read buffer (string) 26 @keyparam buffering size of the read buffer (string)
25 @keyparam encoding character encoding for reading/ writing (string) 27 @keyparam encoding character encoding for reading/ writing (string)
26 @keyparam errors behavior for the character encoding ('strict', 'explicit', ...) (string) 28 @keyparam errors behavior for the character encoding ('strict',
29 'explicit', ...) (string)
27 @keyparam newline controls how universal newlines works (string) 30 @keyparam newline controls how universal newlines works (string)
28 @keyparam closefd close underlying file descriptor if given as file parameter (boolean) 31 @keyparam closefd close underlying file descriptor if given as file
32 parameter (boolean)
33 @return Returns the new file object
29 """ 34 """
30 return File(file, mode, buffering, encoding, errors, newline, closefd) 35 return File(file, mode, buffering, encoding, errors, newline, closefd)
31 36
32 37
33 class File(file): #__IGNORE_WARNING__ 38 class File(file): #__IGNORE_WARNING__
34 """ 39 """
35 Facade for the original file class. 40 Facade for the original file class.
36 """ 41 """
37 def __init__(self, filein, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True): 42 def __init__(self, filein, mode='r', buffering=-1,
43 encoding=None, errors=None, newline=None, closefd=True):
38 """ 44 """
39 Constructor, checks for unimplemented parameters. 45 Constructor, checks for unimplemented parameters.
40 46
41 @param filein filename or file descriptor (string) 47 @param filein filename or file descriptor (string)
42 @keyparam mode access mode (string) 48 @keyparam mode access mode (string)
43 @keyparam buffering size of the read buffer (string) 49 @keyparam buffering size of the read buffer (string)
44 @keyparam encoding character encoding for reading/ writing (string) 50 @keyparam encoding character encoding for reading/ writing (string)
45 @keyparam errors behavior for the character encoding ('strict', 'explicit', ...) (string) 51 @keyparam errors behavior for the character encoding ('strict',
52 'explicit', ...) (string)
46 @keyparam newline controls how universal newlines works (string) 53 @keyparam newline controls how universal newlines works (string)
47 @keyparam closefd close underlying file descriptor if given as file parameter (boolean) 54 @keyparam closefd close underlying file descriptor if given as file
55 parameter (boolean)
56 @exception NotImplementedError for not implemented method parameters
48 """ 57 """
49 self.__encoding = encoding 58 self.__encoding = encoding
50 self.__newline = newline 59 self.__newline = newline
51 self.__closefd = closefd 60 self.__closefd = closefd
52 if newline is not None: 61 if newline is not None:
55 else: 64 else:
56 mode = mode.replace('t', 'b') 65 mode = mode.replace('t', 'b')
57 if 'b' not in mode: 66 if 'b' not in mode:
58 mode = mode + 'b' 67 mode = mode + 'b'
59 68
60 if closefd == False: 69 if closefd is False:
61 raise NotImplementedError 70 raise NotImplementedError
62 71
63 if errors is None: 72 if errors is None:
64 self.__errors = 'strict' 73 self.__errors = 'strict'
65 else: 74 else:
101 @return decoded lines read 110 @return decoded lines read
102 """ 111 """
103 if self.__encoding is None: 112 if self.__encoding is None:
104 return super(File, self).readlines(hint) 113 return super(File, self).readlines(hint)
105 else: 114 else:
106 return [codecs.decode(txt, self.__encoding) for txt in super(File, self).readlines(hint)] 115 return [codecs.decode(txt, self.__encoding)
116 for txt in super(File, self).readlines(hint)]
107 117
108 def write(self, txt): 118 def write(self, txt):
109 """ 119 """
110 Write given data to file. Encode if needed. 120 Write given data to file. Encode if needed.
111 121

eric ide

mercurial