Skip to content

print throws error with "flush" argument #4888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jerryneedell opened this issue Jun 14, 2021 · 10 comments
Closed

print throws error with "flush" argument #4888

jerryneedell opened this issue Jun 14, 2021 · 10 comments
Assignees
Labels
Milestone

Comments

@jerryneedell
Copy link
Collaborator

Just ran across this on CLUE (nrf52840)
using the "flush" argument for print throws an error

Adafruit CircuitPython 7.0.0-alpha.2-527-g47947dc23 on 2021-05-13; Adafruit CLUE nRF52840 Express with nRF52840
>>> print("hi",flush=True)
hi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument
>>> 
@jerryneedell
Copy link
Collaborator Author

hmmm -- may just be on the nRF52840


Adafruit CircuitPython 7.0.0-alpha.3-45-g5f81f9ed1 on 2021-06-13; Adafruit Feather M0 Adalogger with samd21g18
>>> print("hi",flush=True)
hi
>>> 

@jerryneedell
Copy link
Collaborator Author

jerryneedell commented Jun 14, 2021

Nope....


Adafruit CircuitPython 7.0.0-alpha.3-45-g5f81f9ed1 on 2021-06-13; Adafruit Feather M4 Express with samd51j19
>>> 
>>> print("hi",flush=True)
hi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument
>>> 


also failed on an STM32F405 and an ESP32S2


Adafruit CircuitPython 7.0.0-alpha.3-55-g2cbdd18a2 on 2021-06-10; Kaluga 1 with ESP32S2
>>> print("hi",flush=True)
hi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument
>>> 

@dhalbert
Copy link
Collaborator

dhalbert commented Jun 14, 2021

I believe this is due to:

circuitpython/py/stream.c

Lines 494 to 496 in 84e1d77

if (stream_p->ioctl == NULL) {
mp_raise_OSError(MP_EINVAL);
}

but I don't know why there is no ioctl support for stdout on those ports. It may just be a typo somewhere.

@dhalbert
Copy link
Collaborator

This works on Metro M0 and fails on Metro M4. I believe the difference is whether MICROPY_PY_IO is 0 (M0) or 1 (M4).

@dhalbert dhalbert added this to the Long term milestone Jun 15, 2021
@jerryneedell
Copy link
Collaborator Author

It looks like MICROPY_PY_IO gets enabled if JSON is enabled.
https://github.com/adafruit/circuitpython/blob/main/py/circuitpy_mpconfig.h#L516
I think JSON is enable for all the "full" builds so that would be consistent with what we are seeing.

Is this likely an upstream issue?

@dhalbert
Copy link
Collaborator

Yes, it is all a micropythonism. It provides the io module, whose code also seems to provide extra functionality for stdin/stdout (maybe...), and is also needed by json.

@tannewt tannewt added the cpython api modules from cpython label Jun 22, 2021
@tannewt
Copy link
Member

tannewt commented Jun 22, 2021

Why does PY_IO disable flush? CPython always has it: https://docs.python.org/3/library/functions.html#print

@Flameeyes
Copy link

FWIW this is also affecting the FeatherS2 — and it's a regression from CircuitPython 6.2.

Press any key to enter the REPL. Use CTRL-D to reload.

Adafruit CircuitPython 6.2.0 on 2021-04-05; Adafruit Feather M4 Express with samd51j19
>>> print("foo", flush=True)
foo
Adafruit CircuitPython 7.0.0-alpha.5 on 2021-07-21; FeatherS2 with ESP32S2
>>> print("foo", flush=True)
foo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument

Flameeyes added a commit to Flameeyes/pysdrc that referenced this issue Aug 1, 2021
There is a regression in CiruitPython 7 alpha that throws an OSError
when using `flush=True`:

adafruit/circuitpython#4888

This makes sure the code works both for CircuitPython 6 and 7.
@tannewt tannewt modified the milestones: Long term, 7.0.0 Aug 2, 2021
@tannewt tannewt self-assigned this Aug 4, 2021
@tannewt
Copy link
Member

tannewt commented Aug 4, 2021

This is actually more correct. The change comes from the 1.13 merge where I removed the "pretend to flush" https://github.com/adafruit/circuitpython/pull/1555/files#diff-d10683776c4bfa5c707b7af8a20a136c9d59131e9803ba1bc547020e43fe0704R92.

Basically, the old version was lying to you that it actually flushed. So I propose we leave it as-is. We're on a major version boundary so breaking something "that used to work" is ok.

@tannewt
Copy link
Member

tannewt commented Aug 11, 2021

I don't think it's worth worrying about this. Very little code uses flush. If you have a reason you'd like to to work, then please file a new issue. If needed, we can make it work without IO instead of faking the flush.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants