126
126
* }
127
127
* }
128
128
* </pre>
129
- *
129
+ *
130
130
* In this example we:
131
131
* <ul>
132
132
* <li>create a {@link VirtualFileSystem} configured to have the root
152
152
* <p>
153
153
* <b>Example</b> creating a GraalPy context configured for the usage with an external resource
154
154
* directory:
155
- *
155
+ *
156
156
* <pre>
157
157
* try (Context context = GraalPyResources.contextBuilder(Path.of("python-resources")).build()) {
158
158
* context.eval("python", "import mymodule; mymodule.print_hello_world()");
164
164
* }
165
165
* }
166
166
* </pre>
167
- *
167
+ *
168
168
* In this example we:
169
169
* <ul>
170
170
* <li>create a GraalPy context which is preconfigured with GraalPy resources in an external
@@ -191,8 +191,9 @@ private GraalPyResources() {
191
191
192
192
/**
193
193
* Creates a GraalPy context preconfigured with a {@link VirtualFileSystem} and other GraalPy
194
- * and polyglot Context configuration options optimized for the usage of the Python virtual
195
- * environment contained in the virtual filesystem.
194
+ * and polyglot Context configuration options optimized for the usage of the
195
+ * <a href="https://docs.python.org/3/library/venv.html">Python virtual environment</a>
196
+ * contained in the virtual filesystem.
196
197
* <p>
197
198
* Following resource paths are preconfigured:
198
199
* <ul>
@@ -216,8 +217,9 @@ public static Context createContext() {
216
217
217
218
/**
218
219
* Creates a GraalPy context builder preconfigured with a {@link VirtualFileSystem} and other
219
- * GraalPy and polyglot Context configuration options optimized for the usage of the Python
220
- * virtual environment contained in the virtual filesystem.
220
+ * GraalPy and polyglot Context configuration options optimized for the usage of the
221
+ * <a href="https://docs.python.org/3/library/venv.html">Python virtual environment</a>
222
+ * contained in the virtual filesystem.
221
223
* <p>
222
224
* Following resource paths are preconfigured:
223
225
* <ul>
@@ -246,7 +248,7 @@ public static Context createContext() {
246
248
* use {@link #contextBuilder(VirtualFileSystem)} and
247
249
* {@link VirtualFileSystem.Builder#resourceDirectory(String)} when building the
248
250
* {@link VirtualFileSystem}.
249
- *
251
+ *
250
252
* @see <a href=
251
253
* "https://github.com/oracle/graalpython/blob/master/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonOptions.java">PythonOptions</a>
252
254
* @return a new {@link org.graalvm.polyglot.Context.Builder} instance
@@ -259,8 +261,9 @@ public static Context.Builder contextBuilder() {
259
261
260
262
/**
261
263
* Creates a GraalPy context builder preconfigured with the given {@link VirtualFileSystem} and
262
- * other GraalPy and polygot Context configuration options optimized for the usage of the Python
263
- * virtual environment contained in the virtual filesystem.
264
+ * other GraalPy and polygot Context configuration options optimized for the usage of the
265
+ * <a href="https://docs.python.org/3/library/venv.html">Python virtual environment</a>
266
+ * contained in the virtual filesystem.
264
267
* <p>
265
268
* Following resource paths are preconfigured:
266
269
* <ul>
@@ -288,7 +291,7 @@ public static Context.Builder contextBuilder() {
288
291
* }
289
292
* }
290
293
* </pre>
291
- *
294
+ *
292
295
* In this example we:
293
296
* <ul>
294
297
* <li>create a {@link VirtualFileSystem} configured to have the root
@@ -333,7 +336,7 @@ public static Context.Builder contextBuilder(VirtualFileSystem vfs) {
333
336
* </p>
334
337
* <p>
335
338
* <b>Example</b>
336
- *
339
+ *
337
340
* <pre>
338
341
* Context.Builder builder = GraalPyResources.contextBuilder(Path.of("python-resources"));
339
342
* try (Context context = builder.build()) {
@@ -346,17 +349,31 @@ public static Context.Builder contextBuilder(VirtualFileSystem vfs) {
346
349
* }
347
350
* }
348
351
* </pre>
349
- *
352
+ *
350
353
* In this example we:
351
354
* <ul>
352
355
* <li>create a GraalPy context which is preconfigured with GraalPy resources in an external
353
356
* resource directory</li>
354
357
* <li>use the context to import the python module <code>mymodule</code>, which should be either
355
358
* located in <code>python-resources/src</code> or in a python package installed in
356
359
* <code>/python/venv</code> (python virtual environment)</li>
360
+ * <li>note that in this scenario, the Python context has access to the extracted resources as
361
+ * well as the rest of the real filesystem</li>
357
362
* </ul>
358
363
* </p>
359
364
*
365
+ * <p>
366
+ * External resources directory is often used for better compatibility with Python native
367
+ * extensions that may bypass the Python abstractions and access the filesystem directly from
368
+ * native code. Setting the {@code PosixModuleBackend} option to "native" increases the
369
+ * compatibility further, but in such case even Python code bypasses the Truffle abstractions
370
+ * and accesses native POSIX APIs directly. Usage:
371
+ *
372
+ * <pre>
373
+ * GraalPyResources.contextBuilder(Path.of("python-resources")).option("python.PosixModuleBackend", "native")
374
+ * </pre>
375
+ * </p>
376
+ *
360
377
* <p/>
361
378
* When Maven or Gradle GraalPy plugin is used to build the virtual environment, it also has to
362
379
* be configured to generate the virtual environment into the same directory using the
0 commit comments