Skip to content

Commit c2d1579

Browse files
committed
[GR-15046] Load _pyio and encodings during context preinitialization
PullRequest: graalpython/479
2 parents 9d7abe6 + 2d268dd commit c2d1579

File tree

7 files changed

+124
-67
lines changed

7 files changed

+124
-67
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,11 @@ protected String toString(PythonContext context, Object value) {
493493
}
494494
// This is not a good place to report inconsistencies in any of the above conditions. Just
495495
// return a String
496-
return ((PythonAbstractObject) value).toString();
496+
if (value instanceof PythonAbstractObject) {
497+
return ((PythonAbstractObject) value).toString();
498+
} else {
499+
return "illegal object";
500+
}
497501
}
498502

499503
public static TruffleLogger getLogger() {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ private static final String[] initializeCoreFiles() {
231231
"_queue",
232232
"_ast",
233233
"java",
234+
"pyio_patches",
234235
"_contextvars"));
235236

236237
return coreFiles.toArray(new String[coreFiles.size()]);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/WeakRefModuleBuiltins.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.lang.ref.ReferenceQueue;
4545
import java.util.List;
4646

47+
import com.oracle.graal.python.PythonLanguage;
4748
import com.oracle.graal.python.builtins.Builtin;
4849
import com.oracle.graal.python.builtins.CoreFunctions;
4950
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
@@ -183,7 +184,14 @@ private ReferenceQueue<Object> getWeakReferenceQueue() {
183184
ReferenceQueue<Object> queue = (ReferenceQueue<Object>) queueObject;
184185
return queue;
185186
} else {
186-
throw new IllegalStateException("the weak reference queue was modified!");
187+
CompilerDirectives.transferToInterpreter();
188+
if (PythonLanguage.getContextRef().get().getCore().isInitialized()) {
189+
throw new IllegalStateException("the weak reference queue was modified!");
190+
} else {
191+
// returning a null reference queue is fine, it just means
192+
// that the finalizer won't run
193+
return null;
194+
}
187195
}
188196
}
189197
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/statement/AbstractImportNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ protected Object importModule(String name, Object globals, String[] fromList, in
120120
}
121121

122122
Object __import__(String name, Object globals, String[] fromList, int level) {
123-
PMethod builtinImport = (PMethod) getContext().getBuiltins().getAttribute(__IMPORT__);
123+
PMethod builtinImport = (PMethod) getContext().getCore().lookupBuiltinModule("builtins").getAttribute(__IMPORT__);
124124
assert fromList != null;
125125
assert globals != null;
126126
return getCallNode().execute(null, builtinImport, new Object[]{name}, new PKeyword[]{

graalpython/lib-graalpython/__builtins_patches__.py

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -37,52 +37,3 @@
3737
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838
# SOFTWARE.
3939

40-
import _pyio
41-
import io
42-
43-
import _io
44-
import _sysconfig
45-
import builtins
46-
import sys
47-
48-
49-
# ----------------------------------------------------------------------------------------------------------------------
50-
#
51-
# patch _io
52-
#
53-
# ----------------------------------------------------------------------------------------------------------------------
54-
55-
@__builtin__
56-
def open(*args, **kwargs):
57-
return _pyio.open(*args, **kwargs)
58-
59-
60-
for module in [_io, io]:
61-
setattr(module, 'open', open)
62-
setattr(module, 'TextIOWrapper', _pyio.TextIOWrapper)
63-
setattr(module, 'IncrementalNewlineDecoder', _pyio.IncrementalNewlineDecoder)
64-
setattr(module, 'BufferedRandom', _pyio.BufferedRandom)
65-
setattr(module, 'BufferedRWPair', _pyio.BufferedRWPair)
66-
setattr(module, 'BufferedWriter', _pyio.BufferedWriter)
67-
setattr(module, 'BufferedReader', _pyio.BufferedReader)
68-
setattr(module, 'StringIO', _pyio.StringIO)
69-
setattr(module, '_IOBase', _pyio.IOBase)
70-
setattr(module, 'BufferedIOBase', _pyio.BufferedIOBase)
71-
setattr(module, 'RawIOBase', _pyio.RawIOBase)
72-
setattr(module, 'FileIO', _pyio.FileIO)
73-
setattr(module, 'BytesIO', _pyio.BytesIO)
74-
setattr(module, '_TextIOBase', _pyio.TextIOBase)
75-
76-
77-
setattr(builtins, 'open', open)
78-
79-
80-
sys.stdin = _pyio.TextIOWrapper(_pyio.BufferedReader(sys.stdin), encoding="utf-8", line_buffering=True)
81-
sys.stdin.mode = "r"
82-
sys.__stdin__ = sys.stdin
83-
sys.stdout = _pyio.TextIOWrapper(_pyio.BufferedWriter(sys.stdout), encoding="utf-8", line_buffering=True)
84-
sys.stdout.mode = "w"
85-
sys.__stdout__ = sys.stdout
86-
sys.stderr = _pyio.TextIOWrapper(_pyio.BufferedWriter(sys.stderr), encoding="utf-8", line_buffering=True)
87-
sys.stderr.mode = "w"
88-
sys.__stderr__ = sys.stderr

graalpython/lib-graalpython/_codecs.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -45,9 +45,6 @@
4545

4646
@__builtin__
4747
def register(search_function):
48-
if not __codec_search_path__:
49-
__codec_registry_init__()
50-
5148
if not hasattr(search_function, "__call__"):
5249
raise TypeError("argument must be callable")
5350
__codec_search_path__.append(search_function)
@@ -59,9 +56,6 @@ def __normalizestring(string):
5956

6057
@__builtin__
6158
def lookup(encoding):
62-
if not __codec_search_path__:
63-
__codec_registry_init__()
64-
6559
normalized_encoding = __normalizestring(encoding)
6660
# First, try to lookup the name in the registry dictionary
6761
result = __codec_search_cache__.get(normalized_encoding)
@@ -136,14 +130,6 @@ def lookup_error(errors='strict'):
136130
return handler
137131

138132

139-
def __codec_registry_init__():
140-
# TODO: error method handlers setup
141-
try:
142-
import encodings
143-
except Exception:
144-
raise RuntimeError("can't initialize codec registry")
145-
146-
147133
# TODO implement the encode / decode methods
148134
@__builtin__
149135
def escape_encode(data, errors=None):
@@ -333,3 +319,14 @@ def code_page_encode(code_page, string, errors=None):
333319
@__builtin__
334320
def code_page_decode(code_page, string, errors=None, final=False):
335321
raise NotImplementedError("code_page_decode")
322+
323+
324+
# at this point during context startup, sys.path isn't initialized, so we need
325+
# to set it up
326+
import sys
327+
sys.path.append(sys.graal_python_stdlib_home)
328+
try:
329+
import encodings
330+
finally:
331+
assert len(sys.path) == 1
332+
sys.path.pop()
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
# at this point during context startup, sys.path isn't initialized, so we need
41+
# to set it up
42+
import sys
43+
sys.path.append(sys.graal_python_stdlib_home)
44+
try:
45+
import _pyio
46+
import io
47+
finally:
48+
assert len(sys.path) == 1
49+
sys.path.pop()
50+
51+
52+
import _io
53+
import _sysconfig
54+
import builtins
55+
56+
57+
# ----------------------------------------------------------------------------------------------------------------------
58+
#
59+
# patch _io
60+
#
61+
# ----------------------------------------------------------------------------------------------------------------------
62+
63+
@__builtin__
64+
def open(*args, **kwargs):
65+
return _pyio.open(*args, **kwargs)
66+
67+
68+
for module in [_io, io]:
69+
setattr(module, 'open', open)
70+
setattr(module, 'TextIOWrapper', _pyio.TextIOWrapper)
71+
setattr(module, 'IncrementalNewlineDecoder', _pyio.IncrementalNewlineDecoder)
72+
setattr(module, 'BufferedRandom', _pyio.BufferedRandom)
73+
setattr(module, 'BufferedRWPair', _pyio.BufferedRWPair)
74+
setattr(module, 'BufferedWriter', _pyio.BufferedWriter)
75+
setattr(module, 'BufferedReader', _pyio.BufferedReader)
76+
setattr(module, 'StringIO', _pyio.StringIO)
77+
setattr(module, '_IOBase', _pyio.IOBase)
78+
setattr(module, 'BufferedIOBase', _pyio.BufferedIOBase)
79+
setattr(module, 'RawIOBase', _pyio.RawIOBase)
80+
setattr(module, 'FileIO', _pyio.FileIO)
81+
setattr(module, 'BytesIO', _pyio.BytesIO)
82+
setattr(module, '_TextIOBase', _pyio.TextIOBase)
83+
84+
85+
setattr(builtins, 'open', open)
86+
87+
88+
sys.stdin = _pyio.TextIOWrapper(_pyio.BufferedReader(sys.stdin), encoding="utf-8", line_buffering=True)
89+
sys.stdin.mode = "r"
90+
sys.__stdin__ = sys.stdin
91+
sys.stdout = _pyio.TextIOWrapper(_pyio.BufferedWriter(sys.stdout), encoding="utf-8", line_buffering=True)
92+
sys.stdout.mode = "w"
93+
sys.__stdout__ = sys.stdout
94+
sys.stderr = _pyio.TextIOWrapper(_pyio.BufferedWriter(sys.stderr), encoding="utf-8", line_buffering=True)
95+
sys.stderr.mode = "w"
96+
sys.__stderr__ = sys.stderr

0 commit comments

Comments
 (0)