Skip to content

Commit 77e9264

Browse files
committed
Make SharedObjects AutoCloseable
1 parent fb7b0a7 commit 77e9264

File tree

6 files changed

+55
-58
lines changed

6 files changed

+55
-58
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/CApiContext.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,9 @@ public ModuleSpec(TruffleString name, TruffleString path, Object originalModuleS
263263
}
264264

265265
/**
266-
* Get the variable part of a module's export symbol name. Returns a bytes instance. For
267-
* non-ASCII-named modules, the name is encoded as per PEP 489. The hook_prefix pointer is
268-
* set to either ascii_only_prefix or nonascii_prefix, as appropriate.
266+
* Get the variable part of a module's export symbol name. For non-ASCII-named modules, the
267+
* name is encoded as per PEP 489. The hook_prefix pointer is set to either
268+
* ascii_only_prefix or nonascii_prefix, as appropriate.
269269
*/
270270
@TruffleBoundary
271271
TruffleString getEncodedName() {
@@ -1048,9 +1048,9 @@ private static String dlopenFlagsToString(int flags) {
10481048
* @param context The Python context object.
10491049
* @param spec The name and path of the module (also containing the original module spec
10501050
* object).
1051-
* @param checkFunctionResultNode An adopted node instance. This is necessary because the result
1052-
* check could raise an exception and only an adopted node will report useful source
1053-
* locations.
1051+
* @param checkFunctionResultNode A node to check that the function result does not indicate
1052+
* that an exception was raised on the native side. It should be an adopted node,
1053+
* because only an adopted node will report useful source locations.
10541054
* @return A Python module.
10551055
* @throws IOException If the specified file cannot be loaded.
10561056
* @throws ApiInitException If the corresponding native context could not be initialized.

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/copying/ElfFile.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
package com.oracle.graal.python.builtins.objects.cext.copying;
4343

4444
import java.io.IOException;
45+
import java.nio.file.StandardCopyOption;
4546
import java.nio.file.StandardOpenOption;
4647

4748
import com.oracle.graal.python.runtime.PythonContext;
@@ -68,13 +69,6 @@ private String getPatchelf() {
6869
ElfFile(byte[] b, PythonContext context) throws IOException {
6970
this.context = context;
7071
this.tempfile = context.getEnv().createTempFile(null, null, ".so");
71-
this.context.registerAtexitHook((ctx) -> {
72-
try {
73-
this.tempfile.delete();
74-
} catch (IOException e) {
75-
// ignore
76-
}
77-
});
7872
try (var os = this.tempfile.newOutputStream(StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
7973
os.write(b);
8074
}
@@ -106,12 +100,12 @@ public void changeOrAddDependency(String oldName, String newName) throws IOExcep
106100
}
107101

108102
@Override
109-
public byte[] write() throws IOException {
110-
return tempfile.readAllBytes();
103+
public void write(TruffleFile copy) throws IOException {
104+
tempfile.copy(copy, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
111105
}
112106

113107
@Override
114-
protected void fixup(TruffleFile copy) {
115-
// Nothing to do
108+
public void close() throws IOException {
109+
tempfile.delete();
116110
}
117111
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/copying/MachOFile.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
import java.io.IOException;
4444
import java.nio.ByteBuffer;
45+
import java.nio.file.StandardOpenOption;
4546
import java.util.ArrayList;
4647
import java.util.List;
4748

@@ -150,13 +151,23 @@ public void changeOrAddDependency(String oldName, String newName) throws IOExcep
150151
}
151152

152153
@Override
153-
public byte[] write() {
154+
public void write(TruffleFile copy) throws IOException, InterruptedException {
154155
buffer.position(0);
155156
mh.put(buffer);
156157
for (var cmd : loadCommands) {
157158
cmd.put(buffer);
158159
}
159-
return buffer.array();
160+
161+
try (var os = copy.newOutputStream(StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
162+
os.write(buffer.array());
163+
}
164+
165+
var pb = newProcessBuilder(context);
166+
pb.command(getCodesign(), "--force", "--sign", "-", copy.getAbsoluteFile().getPath());
167+
var proc = pb.start();
168+
if (proc.waitFor() != 0) {
169+
throw new IOException("Failed to run `codesign` command. Make sure you have it on your PATH.");
170+
}
160171
}
161172

162173
private String getCodesign() {
@@ -171,12 +182,7 @@ private String getCodesign() {
171182
}
172183

173184
@Override
174-
protected void fixup(TruffleFile copy) throws IOException, InterruptedException {
175-
var pb = newProcessBuilder(context);
176-
pb.command(getCodesign(), "--force", "--sign", "-", copy.getAbsoluteFile().getPath());
177-
var proc = pb.start();
178-
if (proc.waitFor() != 0) {
179-
throw new IOException("Failed to run `codesign` command. Make sure you have it on your PATH.");
180-
}
185+
public void close() {
186+
// Nothing to do
181187
}
182188
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/copying/NativeLibraryLocator.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,18 @@
4646
import static com.oracle.graal.python.util.PythonUtils.toTruffleStringUncached;
4747

4848
import java.io.IOException;
49-
import java.nio.file.StandardOpenOption;
5049
import java.util.concurrent.atomic.AtomicInteger;
5150

5251
import com.oracle.graal.python.builtins.objects.cext.common.LoadCExtException.ApiInitException;
5352
import com.oracle.graal.python.builtins.objects.cext.common.LoadCExtException.ImportException;
5453
import com.oracle.graal.python.nodes.ErrorMessages;
54+
import com.oracle.graal.python.nodes.util.CannotCastException;
55+
import com.oracle.graal.python.nodes.util.CastToTruffleStringNode;
5556
import com.oracle.graal.python.runtime.PythonContext;
5657
import com.oracle.graal.python.runtime.PythonOptions;
5758
import com.oracle.graal.python.util.BiFunction;
5859
import com.oracle.truffle.api.TruffleFile;
5960
import com.oracle.truffle.api.TruffleLanguage.Env;
60-
import com.oracle.truffle.api.strings.TruffleString;
6161

6262
/**
6363
* Given a GraalPy virtual environment, this class helps prepare that environment so that multiple
@@ -157,9 +157,9 @@ public static void replicate(TruffleFile venvDirectory, PythonContext context, i
157157
}
158158
} catch (RuntimeException e) {
159159
var cause = e.getCause();
160-
if (cause != null && cause instanceof IOException ioCause) {
160+
if (cause instanceof IOException ioCause) {
161161
throw ioCause;
162-
} else if (cause != null && cause instanceof InterruptedException intCause) {
162+
} else if (cause instanceof InterruptedException intCause) {
163163
throw intCause;
164164
} else {
165165
throw e;
@@ -185,10 +185,13 @@ private static String resolve(PythonContext context, TruffleFile original, int c
185185
Object sysBasePrefix = context.getSysModule().getAttribute(T_BASE_PREFIX);
186186
if (sysPrefix.equals(sysBasePrefix)) {
187187
throw new ApiInitException(ErrorMessages.SYS_PREFIX_MUST_POINT_TO_A_VENV_FOR_CAPI_ISOLATION);
188-
} else if (sysPrefix instanceof TruffleString tsSysPrefix) {
189-
copy = env.getPublicTruffleFile(tsSysPrefix.toJavaStringUncached()).resolve(newName);
190188
} else {
191-
throw new ApiInitException(ErrorMessages.SYS_PREFIX_MUST_BE_STRING_NOT_P_FOR_CAPI_ISOLATION, sysPrefix);
189+
try {
190+
var tsSysPrefix = CastToTruffleStringNode.executeUncached(sysPrefix);
191+
copy = env.getPublicTruffleFile(tsSysPrefix.toJavaStringUncached()).resolve(newName);
192+
} catch (CannotCastException e) {
193+
throw new ApiInitException(ErrorMessages.SYS_PREFIX_MUST_BE_STRING_NOT_P_FOR_CAPI_ISOLATION, sysPrefix);
194+
}
192195
}
193196
} else {
194197
copy = original.resolveSibling(newName);
@@ -204,18 +207,16 @@ private static String resolve(PythonContext context, TruffleFile original, int c
204207
}
205208

206209
private static void replicate(TruffleFile original, TruffleFile copy, PythonContext context, int slot, String... dependenciesToUpdate) throws IOException, InterruptedException {
207-
var o = SharedObject.open(original, context);
208-
for (var depToUpdate : dependenciesToUpdate) {
209-
if (depToUpdate != null) {
210-
var newDepName = copyNameOf(depToUpdate, slot);
211-
o.changeOrAddDependency(depToUpdate, newDepName);
210+
try (var o = SharedObject.open(original, context)) {
211+
for (var depToUpdate : dependenciesToUpdate) {
212+
if (depToUpdate != null) {
213+
var newDepName = copyNameOf(depToUpdate, slot);
214+
o.changeOrAddDependency(depToUpdate, newDepName);
215+
}
212216
}
217+
o.setId(copy.getName());
218+
o.write(copy);
213219
}
214-
o.setId(copy.getName());
215-
try (var os = copy.newOutputStream(StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
216-
os.write(o.write());
217-
}
218-
o.fixup(copy);
219220
}
220221

221222
private static void walk(TruffleFile dir, String suffix, String capiOriginalName, PythonContext context, int capiSlot, BiFunction<TruffleFile, String, TruffleFile> f)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/copying/PEFile.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
package com.oracle.graal.python.builtins.objects.cext.copying;
4343

4444
import java.io.IOException;
45+
import java.io.OutputStream;
46+
import java.nio.file.CopyOption;
47+
import java.nio.file.StandardCopyOption;
4548
import java.nio.file.StandardOpenOption;
4649

4750
import com.oracle.graal.python.runtime.PythonContext;
@@ -55,13 +58,6 @@ final class PEFile extends SharedObject {
5558
PEFile(byte[] b, PythonContext context) throws IOException {
5659
this.context = context;
5760
this.tempfile = context.getEnv().createTempFile(null, null, ".dll");
58-
this.context.registerAtexitHook((ctx) -> {
59-
try {
60-
this.tempfile.delete();
61-
} catch (IOException e) {
62-
// ignore
63-
}
64-
});
6561
try (var os = this.tempfile.newOutputStream(StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
6662
os.write(b);
6763
}
@@ -86,12 +82,12 @@ public void changeOrAddDependency(String oldName, String newName) throws IOExcep
8682
}
8783

8884
@Override
89-
public byte[] write() throws IOException {
90-
return tempfile.readAllBytes();
85+
public void write(TruffleFile copy) throws IOException {
86+
tempfile.copy(copy, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
9187
}
9288

9389
@Override
94-
protected void fixup(TruffleFile copy) {
95-
// TODO: Maybe this should be signed again?
90+
public void close() throws IOException {
91+
tempfile.delete();
9692
}
9793
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/copying/SharedObject.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@
4949
import com.oracle.truffle.api.TruffleLogger;
5050
import com.oracle.truffle.api.io.TruffleProcessBuilder;
5151

52-
abstract class SharedObject {
52+
abstract class SharedObject implements AutoCloseable {
5353
abstract void setId(String newId) throws IOException, InterruptedException;
5454

5555
abstract void changeOrAddDependency(String oldName, String newName) throws IOException, InterruptedException;
5656

57-
abstract byte[] write() throws IOException;
57+
abstract void write(TruffleFile copy) throws IOException, InterruptedException;
58+
59+
abstract public void close() throws IOException, InterruptedException;
5860

5961
static SharedObject open(TruffleFile file, PythonContext context) throws IOException {
6062
var f = file.readAllBytes();
@@ -98,6 +100,4 @@ protected static TruffleProcessBuilder newProcessBuilder(PythonContext context)
98100
pb.redirectError(pb.createRedirectToStream(new LoggingOutputStream()));
99101
return pb;
100102
}
101-
102-
protected abstract void fixup(TruffleFile copy) throws IOException, InterruptedException;
103103
}

0 commit comments

Comments
 (0)