Skip to content

Commit e5d33de

Browse files
committed
Improve VirtualFileSystem documentation
1 parent 8bd61d4 commit e5d33de

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

graalpython/org.graalvm.python.embedding/src/org/graalvm/python/embedding/GraalPyResources.java

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
* }
127127
* }
128128
* </pre>
129-
*
129+
*
130130
* In this example we:
131131
* <ul>
132132
* <li>create a {@link VirtualFileSystem} configured to have the root
@@ -152,7 +152,7 @@
152152
* <p>
153153
* <b>Example</b> creating a GraalPy context configured for the usage with an external resource
154154
* directory:
155-
*
155+
*
156156
* <pre>
157157
* try (Context context = GraalPyResources.contextBuilder(Path.of("python-resources")).build()) {
158158
* context.eval("python", "import mymodule; mymodule.print_hello_world()");
@@ -164,7 +164,7 @@
164164
* }
165165
* }
166166
* </pre>
167-
*
167+
*
168168
* In this example we:
169169
* <ul>
170170
* <li>create a GraalPy context which is preconfigured with GraalPy resources in an external
@@ -191,8 +191,9 @@ private GraalPyResources() {
191191

192192
/**
193193
* 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.
196197
* <p>
197198
* Following resource paths are preconfigured:
198199
* <ul>
@@ -216,8 +217,9 @@ public static Context createContext() {
216217

217218
/**
218219
* 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.
221223
* <p>
222224
* Following resource paths are preconfigured:
223225
* <ul>
@@ -246,7 +248,7 @@ public static Context createContext() {
246248
* use {@link #contextBuilder(VirtualFileSystem)} and
247249
* {@link VirtualFileSystem.Builder#resourceDirectory(String)} when building the
248250
* {@link VirtualFileSystem}.
249-
*
251+
*
250252
* @see <a href=
251253
* "https://github.com/oracle/graalpython/blob/master/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonOptions.java">PythonOptions</a>
252254
* @return a new {@link org.graalvm.polyglot.Context.Builder} instance
@@ -259,8 +261,9 @@ public static Context.Builder contextBuilder() {
259261

260262
/**
261263
* 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.
264267
* <p>
265268
* Following resource paths are preconfigured:
266269
* <ul>
@@ -288,7 +291,7 @@ public static Context.Builder contextBuilder() {
288291
* }
289292
* }
290293
* </pre>
291-
*
294+
*
292295
* In this example we:
293296
* <ul>
294297
* <li>create a {@link VirtualFileSystem} configured to have the root
@@ -333,7 +336,7 @@ public static Context.Builder contextBuilder(VirtualFileSystem vfs) {
333336
* </p>
334337
* <p>
335338
* <b>Example</b>
336-
*
339+
*
337340
* <pre>
338341
* Context.Builder builder = GraalPyResources.contextBuilder(Path.of("python-resources"));
339342
* try (Context context = builder.build()) {
@@ -346,17 +349,31 @@ public static Context.Builder contextBuilder(VirtualFileSystem vfs) {
346349
* }
347350
* }
348351
* </pre>
349-
*
352+
*
350353
* In this example we:
351354
* <ul>
352355
* <li>create a GraalPy context which is preconfigured with GraalPy resources in an external
353356
* resource directory</li>
354357
* <li>use the context to import the python module <code>mymodule</code>, which should be either
355358
* located in <code>python-resources/src</code> or in a python package installed in
356359
* <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>
357362
* </ul>
358363
* </p>
359364
*
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+
*
360377
* <p/>
361378
* When Maven or Gradle GraalPy plugin is used to build the virtual environment, it also has to
362379
* be configured to generate the virtual environment into the same directory using the

graalpython/org.graalvm.python.embedding/src/org/graalvm/python/embedding/VirtualFileSystem.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,9 @@ public Builder resourceLoadingClass(Class<?> c) {
229229
/**
230230
* This filter applied to files in the virtual filesystem treats them as symlinks to real
231231
* files in the host filesystem. This is useful, for example, if files in the virtual
232-
* filesystem need to be accessed outside the Truffle sandbox. They will be extracted to the
233-
* Java temporary directory on demand. The default filter matches any DLLs, dynamic
234-
* libraries, shared objects, and Python C extension files, because these need to be
232+
* filesystem need to be accessed outside the Truffle IO virtualization. They will be
233+
* extracted to the Java temporary directory on demand. The default filter matches any DLLs,
234+
* dynamic libraries, shared objects, and Python C extension files, because these need to be
235235
* accessed by the operating system loader. Setting this filter to <code>null</code> denies
236236
* any extraction. Any other filter is combined with the default filter.
237237
*

0 commit comments

Comments
 (0)