Utilities/compatibility_fixes.py

Sun, 05 Jan 2014 23:22:17 +0100

author
T.Rzepka <Tobias.Rzepka@gmail.com>
date
Sun, 05 Jan 2014 23:22:17 +0100
branch
Py2 comp.
changeset 3178
f25fc1364c88
parent 3145
a9de05d4a22f
child 3515
1b8381afe38f
permissions
-rw-r--r--

Merge with default branch.

2526
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
2
2790
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
3 # Copyright (c) 2013 - 2013 Tobias Rzepka <tobias.rzepka@gmail.com>
2526
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
4 #
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
5
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
6 """
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
7 Module implementing the open behavior of Python3 for use with Eric5.
3065
070b35dde35e Fixed a bunch of indentation issues.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2790
diff changeset
8
2790
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
9 The Eric5 used features are emulated only. The not emulated features
2526
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
10 should throw a NotImplementedError exception.
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
11 """
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
12
3145
a9de05d4a22f # __IGNORE_WARNING__ added/ removed.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3065
diff changeset
13 from __future__ import unicode_literals
2526
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
14
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
15 import __builtin__
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
16 import codecs
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
17
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
18
3065
070b35dde35e Fixed a bunch of indentation issues.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2790
diff changeset
19 def open(file, mode='r', buffering=-1, encoding=None,
070b35dde35e Fixed a bunch of indentation issues.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2790
diff changeset
20 errors=None, newline=None, closefd=True):
2790
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
21 """
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
22 Replacement for the build in open function.
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
23
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
24 @param file filename or file descriptor (string)
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
25 @keyparam mode access mode (string)
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
26 @keyparam buffering size of the read buffer (string)
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
27 @keyparam encoding character encoding for reading/ writing (string)
3065
070b35dde35e Fixed a bunch of indentation issues.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2790
diff changeset
28 @keyparam errors behavior for the character encoding ('strict',
070b35dde35e Fixed a bunch of indentation issues.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2790
diff changeset
29 'explicit', ...) (string)
2790
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
30 @keyparam newline controls how universal newlines works (string)
3065
070b35dde35e Fixed a bunch of indentation issues.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2790
diff changeset
31 @keyparam closefd close underlying file descriptor if given as file
070b35dde35e Fixed a bunch of indentation issues.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2790
diff changeset
32 parameter (boolean)
070b35dde35e Fixed a bunch of indentation issues.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2790
diff changeset
33 @return Returns the new file object
2790
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
34 """
2526
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
35 return File(file, mode, buffering, encoding, errors, newline, closefd)
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
36
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
37
2790
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
38 class File(file): #__IGNORE_WARNING__
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
39 """
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
40 Facade for the original file class.
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
41 """
3065
070b35dde35e Fixed a bunch of indentation issues.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2790
diff changeset
42 def __init__(self, filein, mode='r', buffering=-1,
070b35dde35e Fixed a bunch of indentation issues.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2790
diff changeset
43 encoding=None, errors=None, newline=None, closefd=True):
2790
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
44 """
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
45 Constructor, checks for unimplemented parameters.
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
46
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
47 @param filein filename or file descriptor (string)
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
48 @keyparam mode access mode (string)
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
49 @keyparam buffering size of the read buffer (string)
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
50 @keyparam encoding character encoding for reading/ writing (string)
3065
070b35dde35e Fixed a bunch of indentation issues.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2790
diff changeset
51 @keyparam errors behavior for the character encoding ('strict',
070b35dde35e Fixed a bunch of indentation issues.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2790
diff changeset
52 'explicit', ...) (string)
2790
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
53 @keyparam newline controls how universal newlines works (string)
3065
070b35dde35e Fixed a bunch of indentation issues.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2790
diff changeset
54 @keyparam closefd close underlying file descriptor if given as file
070b35dde35e Fixed a bunch of indentation issues.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2790
diff changeset
55 parameter (boolean)
070b35dde35e Fixed a bunch of indentation issues.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2790
diff changeset
56 @exception NotImplementedError for not implemented method parameters
2790
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
57 """
2526
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
58 self.__encoding = encoding
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
59 self.__newline = newline
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
60 self.__closefd = closefd
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
61 if newline is not None:
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
62 if 'r' in mode:
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
63 raise NotImplementedError
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
64 else:
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
65 mode = mode.replace('t', 'b')
2790
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
66 if 'b' not in mode:
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
67 mode = mode + 'b'
2526
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
68
3065
070b35dde35e Fixed a bunch of indentation issues.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2790
diff changeset
69 if closefd is False:
2526
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
70 raise NotImplementedError
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
71
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
72 if errors is None:
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
73 self.__errors = 'strict'
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
74 else:
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
75 self.__errors = errors
2790
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
76
3145
a9de05d4a22f # __IGNORE_WARNING__ added/ removed.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3065
diff changeset
77 file.__init__(self, filein, mode, buffering) #__IGNORE_WARNING__
2526
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
78
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
79 def read(self, n=-1):
2790
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
80 """
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
81 Read n bytes or all if n=-1 from file.
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
82
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
83 @keyparam n bytecount or all if n=-1 (int)
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
84 @return decoded bytes read
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
85 """
2526
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
86 txt = super(File, self).read(n)
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
87 if self.__encoding is None:
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
88 return txt
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
89 else:
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
90 return codecs.decode(txt, self.__encoding)
2790
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
91
2526
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
92 def readline(self, limit=-1):
2790
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
93 """
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
94 Read one line from file.
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
95
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
96 @keyparam limit maximum bytes to read or all if limit=-1 (int)
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
97 @return decoded line read
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
98 """
2526
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
99 txt = super(File, self).readline(limit)
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
100 if self.__encoding is None:
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
101 return txt
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
102 else:
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
103 return codecs.decode(txt, self.__encoding)
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
104
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
105 def readlines(self, hint=-1):
2790
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
106 """
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
107 Read all lines from file.
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
108
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
109 @keyparam hint maximum bytes to read or all if hint=-1 (int)
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
110 @return decoded lines read
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
111 """
2526
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
112 if self.__encoding is None:
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
113 return super(File, self).readlines(hint)
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
114 else:
3065
070b35dde35e Fixed a bunch of indentation issues.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2790
diff changeset
115 return [codecs.decode(txt, self.__encoding)
070b35dde35e Fixed a bunch of indentation issues.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2790
diff changeset
116 for txt in super(File, self).readlines(hint)]
2526
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
117
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
118 def write(self, txt):
2790
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
119 """
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
120 Write given data to file. Encode if needed.
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
121
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
122 @param txt data to write. (str, bytes)
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
123 """
2526
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
124 if self.__encoding is not None:
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
125 txt = codecs.encode(txt, self.__encoding, self.__errors)
2790
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
126
2526
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
127 if self.__newline in ['\r\n', '\r']:
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
128 txt = txt.replace('\n', self.__newline)
2790
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
129
2526
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
130 super(File, self).write(txt)
2790
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
131
2526
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
132 def next(self):
2790
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
133 """
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
134 Method if used in a iterator.
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
135
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
136 @return decoded data read
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
137 """
2526
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
138 txt = super(File, self).next()
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
139 if self.__encoding is None:
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
140 return txt
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
141 else:
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
142 return codecs.decode(txt, self.__encoding)
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
143
2790
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
144 # Inject into the __builtin__ dictionary
2526
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
145 __builtin__.open = open
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
146
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
147 if __name__ == '__main__':
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
148 fp = open('compatibility_fixes.py', encoding='latin1')
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
149 print(fp.read())
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
150 fp.close()
2790
6686a3326df8 Special case if no 'b' or 't' in access mode fixed. Source docu added.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2526
diff changeset
151
2526
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
152 with open('compatibility_fixes.py', encoding='UTF-8') as fp:
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
153 rlines = fp.readlines()
a91cba8291b9 Minimum modifications to start Eric5 with Py2.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents:
diff changeset
154 print(rlines[-1])

eric ide

mercurial