Skip to content

Commit e5d0111

Browse files
committed
added info about I/O functions open/read/write, and return value of task.executor()
1 parent a510aa8 commit e5d0111

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

docs/new_features.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ Planned new features post 1.0.0 include:
2020
- use ``aionofity`` to auto-reload newly written script files, at least on linux (#74)
2121
- consider allowing native Python functions inside pyscript (#71)
2222
- consider implementing function decorators (#43)
23+
- consider supporting the built-in functions that do I/O, such as ``open``, ``read`` and ``write``, which
24+
are not currently supported to avoid I/O in the main event loop, and also to avoid security issues if people
25+
share pyscripts. The ``print`` function only logs a message, rather than implements the real ``print`` features,
26+
such as specifying an output file handle. Support might be added in the future using an executor job, perhaps
27+
enabled when ``allow_all_imports`` is set.
2328

2429
The new features since 1.0.0 in master include:
2530

docs/reference.rst

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,9 @@ main event loop using ``task.executor``:
979979
``task.executor(func, *args, **kwargs)``
980980
Run the given function in a separate thread. The first argument is the function to be called,
981981
followed by each of the positional or keyword arguments that function expects. The ``func``
982-
argument can only be a regular Python function, not a function defined in pyscript.
982+
argument can only be a regular Python function (eg, defined in an imported module), not a
983+
function defined in pyscript. The return value of ``task.executor`` is the return value of
984+
the function ``func``.
983985

984986
See `this section <#avoiding-event-loop-i-o>`__ for more information.
985987

@@ -1133,8 +1135,10 @@ worry about the details. However, the performance will be much slower that regul
11331135
which is typically compiled. Any Python packages you import will run at native, compiled speed.
11341136

11351137
So if you plan to run large chunks of code in pyscript without needing any of the pyscript-specific
1136-
features, you might consider putting them in a package and importing it instead. That way it will
1137-
run at native compiled speed.
1138+
features, or you want access to native Python features that aren't supported in pyscript (like
1139+
``yield``, ``open``, ``read`` or ``write``), you might consider putting them in a package and
1140+
importing it instead. That way it will run at native compiled speed and have full access to
1141+
the native Python language.
11381142

11391143
One way to do that is in one of your pyscript script files, add this code:
11401144

@@ -1422,6 +1426,18 @@ If you forget to use ``task.executor``, you might get this warning from HASS:
14221426
causing stability issues. Please report issue to the custom component author for pyscript doing
14231427
I/O at custom_components/pyscript/eval.py, line 1583: return func(*args, **kwargs)
14241428

1429+
Currently the built-in functions that do I/O, such as ``open``, ``read`` and ``write`` are not supported
1430+
to avoid I/O in the main event loop, and also to avoid security issues if people share pyscripts. Also,
1431+
the ``print`` function only logs a message, rather than implements the real ``print`` features, such
1432+
as specifying an output file handle. If you want to do file I/O from pyscript, you have two choices:
1433+
1434+
- put the code in a separate native Python module, so that functions like ``open``, ``read`` and ``write``
1435+
are available, and call the function in that module from pyscript using ``task.executor``. See
1436+
`Importing <#importing>`__ for how to set Python's ``sys.path`` to import a local Python module.
1437+
- you could use the ``os`` package (which can be imported by setting ``allow_all_imports``) and
1438+
calling the low-level functions like ``os.open`` and ``os.read`` using ``task.executor`` to
1439+
wrap every function.
1440+
14251441
Here's an example fetching a URL. Inside pyscript, this is the wrong way since it does I/O without
14261442
using a separate thread:
14271443

@@ -1508,6 +1524,10 @@ Here are some areas where pyscript differs from real Python:
15081524
A handful of language features are not supported:
15091525

15101526
- generators and the ``yield`` statement; these are difficult to implement in an interpreter.
1527+
- built-in functions that do I/O, such as ``open``, ``read`` and ``write`` are not supported to avoid
1528+
I/O in the main event loop, and also to avoid security issues if people share pyscripts. The ``print``
1529+
function only logs a message, rather than implements the real ``print`` features, such as specifying
1530+
an output file handle.
15111531
- function decorators, beyond the builtin ones, are not yet supported. If there is interest, support
15121532
for function decorators could be added. Additionally, the builtin function decorators aren't
15131533
functions that can be called and used in-line. There is a feature request to add this.

0 commit comments

Comments
 (0)