Skip to content

Commit 5f5670f

Browse files
committed
use TruffleFile to parse services and protocols
1 parent 1711a5a commit 5f5670f

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

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

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@
4141
package com.oracle.graal.python.builtins.modules;
4242

4343
import java.io.BufferedReader;
44-
import java.io.File;
45-
import java.io.FileReader;
4644
import java.io.IOException;
4745
import java.net.Inet4Address;
4846
import java.net.InetAddress;
@@ -72,9 +70,12 @@
7270
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
7371
import com.oracle.graal.python.nodes.util.CastToJavaIntNode;
7472
import com.oracle.graal.python.nodes.util.CastToStringNode;
73+
import com.oracle.graal.python.runtime.PythonCore;
7574
import com.oracle.graal.python.runtime.exception.PythonErrorType;
7675
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
7776
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
77+
import com.oracle.truffle.api.TruffleFile;
78+
import com.oracle.truffle.api.TruffleLanguage;
7879
import com.oracle.truffle.api.dsl.Cached;
7980
import com.oracle.truffle.api.dsl.Fallback;
8081
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
@@ -106,10 +107,10 @@ public Service(int port, String protocol) {
106107
protected static Map<String, Integer> protocols;
107108

108109
@TruffleBoundary
109-
private static Map<String, List<Service>> parseServices() {
110-
File services_file = new File("/etc/services");
110+
private static Map<String, List<Service>> parseServices(TruffleLanguage.Env env) {
111+
TruffleFile services_file = env.getPublicTruffleFile("/etc/services");
111112
try {
112-
BufferedReader br = new BufferedReader(new FileReader(services_file));
113+
BufferedReader br = services_file.newBufferedReader();
113114
String line;
114115
Map<String, List<Service>> parsedServices = new HashMap<>();
115116
while ((line = br.readLine()) != null) {
@@ -135,10 +136,10 @@ private static Map<String, List<Service>> parseServices() {
135136
}
136137

137138
@TruffleBoundary
138-
private static Map<String, Integer> parseProtocols() {
139-
File protocols_file = new File("/etc/protocols");
139+
private static Map<String, Integer> parseProtocols(TruffleLanguage.Env env) {
140+
TruffleFile protocols_file = env.getPublicTruffleFile("/etc/protocols");
140141
try {
141-
BufferedReader br = new BufferedReader(new FileReader(protocols_file));
142+
BufferedReader br = protocols_file.newBufferedReader();
142143
String line;
143144
Map<String, Integer> parsedProtocols = new HashMap<>();
144145
while ((line = br.readLine()) != null) {
@@ -157,9 +158,9 @@ private static Map<String, Integer> parseProtocols() {
157158
}
158159

159160
@TruffleBoundary
160-
private static String searchServicesForPort(int port, String protocol) {
161+
private static String searchServicesForPort(TruffleLanguage.Env env, int port, String protocol) {
161162
if (services == null) {
162-
services = parseServices();
163+
services = parseServices(env);
163164
}
164165

165166
Set<String> servicesNames = services.keySet();
@@ -192,10 +193,13 @@ private static String[] cleanLine(String input) {
192193
return words;
193194
}
194195

195-
static {
196+
@Override
197+
public void initialize(PythonCore core) {
198+
super.initialize(core);
196199
if (ImageInfo.inImageBuildtimeCode()) {
197-
services = parseServices();
198-
protocols = parseProtocols();
200+
// we do this eagerly for SVM images
201+
services = parseServices(core.getContext().getEnv());
202+
protocols = parseProtocols(core.getContext().getEnv());
199203
}
200204
}
201205

@@ -356,7 +360,7 @@ public abstract static class GetServByNameNode extends PythonBuiltinNode {
356360
@Specialization(guards = {"isNoValue(protocolName)"})
357361
Object getServByName(String serviceName, @SuppressWarnings("unused") PNone protocolName) {
358362
if (services == null) {
359-
services = parseServices();
363+
services = parseServices(getContext().getEnv());
360364
}
361365

362366
List<Service> portsForService = services.get(serviceName);
@@ -371,7 +375,7 @@ Object getServByName(String serviceName, @SuppressWarnings("unused") PNone proto
371375
@Specialization
372376
Object getServByName(String serviceName, String protocolName) {
373377
if (services == null) {
374-
services = parseServices();
378+
services = parseServices(getContext().getEnv());
375379
}
376380
int port = op(serviceName, protocolName);
377381
if (port >= 0) {
@@ -426,7 +430,7 @@ Object getServByPort(int port, String protocolName) {
426430
if (port < 0 || port > 65535) {
427431
throw raise(PythonBuiltinClassType.OverflowError);
428432
}
429-
String service = searchServicesForPort(port, protocolName);
433+
String service = searchServicesForPort(getContext().getEnv(), port, protocolName);
430434
if (service != null) {
431435
return service;
432436
}
@@ -463,7 +467,7 @@ Object getNameInfo(VirtualFrame frame, PTuple sockaddr, Object flagArg,
463467

464468
String portServ = String.valueOf(port);
465469
if ((flags & PSocket.NI_NUMERICSERV) != PSocket.NI_NUMERICSERV) {
466-
portServ = searchServicesForPort(port, null);
470+
portServ = searchServicesForPort(getContext().getEnv(), port, null);
467471
if (portServ == null) {
468472
throw raise(PythonBuiltinClassType.OSError, "port/proto not found");
469473
}
@@ -545,7 +549,7 @@ private Object getAddrInfo(InetAddress[] addresses, String port, int family, int
545549
throw raise(PythonBuiltinClassType.UnicodeEncodeError);
546550
}
547551
if (services == null) {
548-
services = parseServices();
552+
services = parseServices(getContext().getEnv());
549553
}
550554
List<Service> serviceList = services.get(port);
551555
return mergeAdressesAndServices(addresses, serviceList, family, type, proto, flags);
@@ -554,7 +558,7 @@ private Object getAddrInfo(InetAddress[] addresses, String port, int family, int
554558
@TruffleBoundary
555559
private Object mergeAdressesAndServices(InetAddress[] adresses, List<Service> serviceList, int family, int type, int proto, int flags) {
556560
if (protocols == null) {
557-
protocols = parseProtocols();
561+
protocols = parseProtocols(getContext().getEnv());
558562
}
559563
List<Object> addressTuples = new ArrayList<>();
560564
for (InetAddress addr : adresses) {

0 commit comments

Comments
 (0)