Skip to content

Commit eacdaef

Browse files
committed
add _post_init hook
- fix io overrides (reuse the _pyio impls) - fix builtin codecs definitions - fix strcount
1 parent 1f72aeb commit eacdaef

File tree

7 files changed

+159
-44
lines changed

7 files changed

+159
-44
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@
153153
* {@link PythonContext} has its own core.
154154
*/
155155
public final class Python3Core implements PythonCore {
156+
private static final String POST_INIT_MODULE_NAME = "_post_init";
157+
156158
// Order matters!
157159
private static final String[] CORE_FILES = new String[]{
158160
"_descriptor",
@@ -355,6 +357,10 @@ public void initialize() {
355357
exportCInterface(getContext());
356358
currentException = null;
357359
initialized = true;
360+
361+
// apply the patches after initialization is done (we need to lookup modules that are mostly
362+
// in stdlib)
363+
loadFile(POST_INIT_MODULE_NAME, coreHome);
358364
}
359365

360366
public Object duplicate(Map<Object, Object> replacements, Object value) {

graalpython/lib-graalpython/_codecs.py

Lines changed: 79 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -136,162 +136,202 @@ def __codec_registry_init__():
136136
raise RuntimeError("can't initialize codec registry")
137137

138138

139-
# TODO implement the methods
140-
def escape_encode(data, errors):
139+
# TODO implement the encode / decode methods
140+
@staticmethod
141+
def escape_encode(data, errors=None):
141142
raise NotImplementedError()
142143

143144

144-
def escape_decode(data, errors):
145+
@staticmethod
146+
def escape_decode(data, errors=None):
145147
raise NotImplementedError()
146148

147149

148-
def utf_8_encode(string, errors):
150+
@staticmethod
151+
def utf_8_encode(string, errors=None):
149152
return __truffle_encode(string, "utf-8", errors)
150153

151154

152-
def utf_8_decode(string, errors, final):
155+
@staticmethod
156+
def utf_8_decode(string, errors=None, final=False):
153157
return __truffle_decode(string, "utf-8", errors)
154158

155159

156-
def utf_7_encode(string, errors):
160+
@staticmethod
161+
def utf_7_encode(string, errors=None):
157162
return __truffle_encode(string, "utf-7", errors)
158163

159164

160-
def utf_7_decode(string, errors, final):
165+
@staticmethod
166+
def utf_7_decode(string, errors=None, final=False):
161167
return __truffle_decode(string, "utf-7", errors)
162168

163169

164-
def utf_16_encode(string, errors, byteorder):
170+
@staticmethod
171+
def utf_16_encode(string, errors=None, byteorder=0):
165172
return __truffle_encode(string, "utf-16", errors)
166173

167174

168-
def utf_16_decode(string, errors, final):
175+
@staticmethod
176+
def utf_16_decode(string, errors=None, final=False):
169177
return __truffle_decode(string, "utf-16", errors)
170178

171179

172-
def utf_16_le_encode(string, errors):
180+
@staticmethod
181+
def utf_16_le_encode(string, errors=None):
173182
return __truffle_encode(string, "utf-16-le", errors)
174183

175184

176-
def utf_16_le_decode(string, errors, final):
185+
@staticmethod
186+
def utf_16_le_decode(string, errors=None, final=False):
177187
return __truffle_decode(string, "utf-16-le", errors)
178188

179189

180-
def utf_16_be_encode(string, errors):
190+
@staticmethod
191+
def utf_16_be_encode(string, errors=None):
181192
return __truffle_encode(string, "utf-16-be", errors)
182193

183194

184-
def utf_16_be_decode(string, errors, final):
195+
@staticmethod
196+
def utf_16_be_decode(string, errors=None, final=False):
185197
return __truffle_decode(string, "utf-16-be", errors)
186198

187199

188-
def utf_16_ex_decode(data, errors, byteorder, final):
200+
@staticmethod
201+
def utf_16_ex_decode(data, errors=None, byteorder=0, final=False):
189202
raise NotImplementedError()
190203

191204

192-
def utf_32_encode(string, errors, byteorder):
205+
@staticmethod
206+
def utf_32_encode(string, errors=None, byteorder=0):
193207
return __truffle_encode(string, "utf-32", errors)
194208

195209

196-
def utf_32_decode(string, errors, final):
210+
@staticmethod
211+
def utf_32_decode(string, errors=None, final=False):
197212
return __truffle_decode(string, "utf-32", errors)
198213

199214

200-
def utf_32_le_encode(string, errors):
215+
@staticmethod
216+
def utf_32_le_encode(string, errors=None):
201217
return __truffle_encode(string, "utf-32-le", errors)
202218

203219

204-
def utf_32_le_decode(string, errors, final):
220+
@staticmethod
221+
def utf_32_le_decode(string, errors=None, final=False):
205222
return __truffle_decode(string, "utf-32-le", errors)
206223

207224

208-
def utf_32_be_encode(string, errors):
225+
@staticmethod
226+
def utf_32_be_encode(string, errors=None):
209227
return __truffle_encode(string, "utf-32-be", errors)
210228

211229

212-
def utf_32_be_decode(string, errors, final):
230+
@staticmethod
231+
def utf_32_be_decode(string, errors=None, final=False):
213232
return __truffle_decode(string, "utf-32-be", errors)
214233

215234

216-
def utf_32_ex_decode(data, errors, byteorder, final):
235+
@staticmethod
236+
def utf_32_ex_decode(data, errors=None, byteorder=0, final=False):
217237
raise NotImplementedError()
218238

219239

220-
def unicode_escape_encode(string, errors):
240+
@staticmethod
241+
def unicode_escape_encode(string, errors=None):
221242
raise NotImplementedError()
222243

223244

224-
def unicode_escape_decode(string, errors):
245+
@staticmethod
246+
def unicode_escape_decode(string, errors=None):
225247
raise NotImplementedError()
226248

227249

228-
def unicode_internal_encode(obj, errors):
250+
@staticmethod
251+
def unicode_internal_encode(obj, errors=None):
229252
raise NotImplementedError()
230253

231254

232-
def unicode_internal_decode(obj, errors):
255+
@staticmethod
256+
def unicode_internal_decode(obj, errors=None):
233257
raise NotImplementedError()
234258

235259

236-
def raw_unicode_escape_encode(string, errors):
260+
@staticmethod
261+
def raw_unicode_escape_encode(string, errors=None):
237262
raise NotImplementedError()
238263

239264

240-
def raw_unicode_escape_decode(string, errors):
265+
@staticmethod
266+
def raw_unicode_escape_decode(string, errors=None):
241267
raise NotImplementedError()
242268

243269

244-
def latin_1_encode(string, errors):
270+
@staticmethod
271+
def latin_1_encode(string, errors=None):
245272
return __truffle_encode(string, "latin-1", errors)
246273

247274

248-
def latin_1_decode(string, errors):
275+
@staticmethod
276+
def latin_1_decode(string, errors=None):
249277
return __truffle_decode(string, "latin-1", errors)
250278

251279

252-
def ascii_encode(string, errors):
280+
@staticmethod
281+
def ascii_encode(string, errors=None):
253282
return __truffle_encode(string, "ascii", errors)
254283

255284

256-
def ascii_decode(string, errors):
285+
@staticmethod
286+
def ascii_decode(string, errors=None):
257287
return __truffle_decode(string, "ascii", errors)
258288

259289

260-
def charmap_encode(string, errors, mapping):
290+
@staticmethod
291+
def charmap_encode(string, errors=None, mapping=None):
261292
raise NotImplementedError()
262293

263294

264-
def charmap_decode(string, errors, mapping):
295+
@staticmethod
296+
def charmap_decode(string, errors=None, mapping=None):
265297
raise NotImplementedError()
266298

267299

300+
@staticmethod
268301
def charmap_build(mapping):
269302
raise NotImplementedError()
270303

271304

272-
def readbuffer_encode(data, errors):
305+
@staticmethod
306+
def readbuffer_encode(data, errors=None):
273307
raise NotImplementedError()
274308

275309

276-
def mbcs_encode(string, errors):
310+
@staticmethod
311+
def mbcs_encode(string, errors=None):
277312
raise NotImplementedError()
278313

279314

280-
def mbcs_decode(string, errors, final):
315+
@staticmethod
316+
def mbcs_decode(string, errors=None, final=False):
281317
raise NotImplementedError()
282318

283319

320+
@staticmethod
284321
def oem_encode(string, errors):
285322
raise NotImplementedError()
286323

287324

288-
def oem_decode(string, errors, final):
325+
@staticmethod
326+
def oem_decode(string, errors=None, final=False):
289327
raise NotImplementedError()
290328

291329

292-
def code_page_encode(code_page, string, errors):
330+
@staticmethod
331+
def code_page_encode(code_page, string, errors=None):
293332
raise NotImplementedError()
294333

295334

296-
def code_page_decode(code_page, string, errors, final):
335+
@staticmethod
336+
def code_page_decode(code_page, string, errors=None, final=False):
297337
raise NotImplementedError()

graalpython/lib-graalpython/_io.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -671,11 +671,8 @@ class TextIOWrapper(_TextIOBase):
671671

672672

673673
def open(*args, **kwargs):
674-
import _pyio
675-
import builtins
676-
setattr(builtins, 'open', _pyio.open)
677-
globals()['open'] = _pyio.open
678-
return _pyio.open(*args, **kwargs)
674+
# this method will be overwritten in _patches
675+
pass
679676

680677

681678
# set the builtins open method
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Copyright (c) 2018, Oracle and/or its affiliates.
2+
#
3+
# The Universal Permissive License (UPL), Version 1.0
4+
#
5+
# Subject to the condition set forth below, permission is hereby granted to any
6+
# person obtaining a copy of this software, associated documentation and/or data
7+
# (collectively the "Software"), free of charge and under any and all copyright
8+
# rights in the Software, and any and all patent rights owned or freely
9+
# licensable by each licensor hereunder covering either (i) the unmodified
10+
# Software as contributed to or provided by such licensor, or (ii) the Larger
11+
# Works (as defined below), to deal in both
12+
#
13+
# (a) the Software, and
14+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
15+
# one is included with the Software (each a "Larger Work" to which the
16+
# Software is contributed by such licensors),
17+
#
18+
# without restriction, including without limitation the rights to copy, create
19+
# derivative works of, display, perform, and distribute the Software and make,
20+
# use, sell, offer for sale, import, export, have made, and have sold the
21+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
22+
# either these or other terms.
23+
#
24+
# This license is subject to the following condition:
25+
#
26+
# The above copyright notice and either this complete permission notice or at a
27+
# minimum a reference to the UPL must be included in all copies or substantial
28+
# portions of the Software.
29+
#
30+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
# SOFTWARE.
37+
38+
import sys
39+
import builtins
40+
41+
# ----------------------------------------------------------------------------------------------------------------------
42+
#
43+
# patch _io
44+
#
45+
# ----------------------------------------------------------------------------------------------------------------------
46+
47+
import _io
48+
import _pyio
49+
import io
50+
51+
for module in [_io, io]:
52+
setattr(module, 'open', _pyio.open)
53+
setattr(module, 'TextIOWrapper', _pyio.TextIOWrapper)
54+
setattr(module, 'IncrementalNewlineDecoder', _pyio.IncrementalNewlineDecoder)
55+
setattr(module, 'BufferedRandom', _pyio.BufferedRandom)
56+
setattr(module, 'BufferedRWPair', _pyio.BufferedRWPair)
57+
setattr(module, 'BufferedWriter', _pyio.BufferedWriter)
58+
setattr(module, 'BufferedReader', _pyio.BufferedReader)
59+
setattr(module, 'StringIO', _pyio.StringIO)
60+
setattr(module, '_IOBase', _pyio.IOBase)
61+
setattr(module, 'RawIOBase', _pyio.RawIOBase)
62+
setattr(module, 'BytesIO', _pyio.BytesIO)
63+
setattr(module, '_TextIOBase', _pyio.TextIOBase)
64+
65+
setattr(builtins, 'open', _pyio.open)
66+
globals()['open'] = _pyio.open

graalpython/lib-graalpython/staticmethod.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,6 @@ def __init__(self, func):
4141

4242
def __get__(self, instance, owner=None):
4343
return self.__func__
44+
45+
def __call__(self, *args, **kwargs):
46+
return self.__func__(*args, **kwargs)

graalpython/lib-graalpython/str.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ def __iter__(self):
316316

317317

318318
def strcount(self, sub, start=0, end=-1):
319+
if len(self) == 0:
320+
return 0
319321
if end < 0:
320322
end = (len(self) + end) % len(self)
321323
cnt = 0

graalpython/lib-python/3/codecs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ def _buffer_decode(self, input, errors, final):
318318
def decode(self, input, final=False):
319319
# decode input (taking the buffer into account)
320320
data = self.buffer + input
321+
__breakpoint__()
321322
(result, consumed) = self._buffer_decode(data, self.errors, final)
322323
# keep undecoded input until the next call
323324
self.buffer = data[consumed:]

0 commit comments

Comments
 (0)