Skip to content

expressions in blocks don't print their value in the REPL #2527

Closed

Description

@verhovsky

Try the following snippets:

for i in range(10):
    i

with open('/tmp/file.txt', 'w') as f:
    f.write('hello')

In CPython:

>>> for i in range(10):
...     i
... 
0
1
2
3
4
5
6
7
8
9
>>> with open('/tmp/file.txt', 'w') as f:
...     f.write('hello')
... 
5
>>>

In RustPython:

>>>>> for i in range(10):
.....     i
..... 
>>>>> with open('/tmp/file.txt', 'w') as f:
.....     f.write('hello')
..... 
>>>>> 

Activity

  1. DimitrisJim commented on May 16, 2021

    @DimitrisJim
    Member

    Not sure if this is really a problem, though CPython's and PyPy's REPL do this, it up to whatever sys.displayhook does. IPython, for example, tinkers with the displayhook to do its own things and doesn't write out the repr of whatever expression is inside a compound statement.

    Personally, I've never really found this useful and wouldn't really miss it.

  2. coolreader18 commented on May 16, 2021

    @coolreader18
    Member

    I think the difference here is basically that in single mode, CPython emits PRINT_EXPR for the result of statements instead of POP_TOP, while we just return the value of the last statement and print that.

  3. DimitrisJim commented on May 17, 2021

    @DimitrisJim
    Member

    You're right on that:

    dis(compile("for i in range(10):\n\ti", '', 'single'))
      1           0 LOAD_NAME                0 (range)
                  2 LOAD_CONST               0 (10)
                  4 CALL_FUNCTION            1
                  6 GET_ITER
            >>    8 FOR_ITER                 8 (to 18)
                 10 STORE_NAME               1 (i)
    
      2          12 LOAD_NAME                1 (i)
                 14 PRINT_EXPR
                 16 JUMP_ABSOLUTE            8
            >>   18 LOAD_CONST               1 (None)
                 20 RETURN_VALUE

    do you think this warrants a change?

  4. youknowone commented on Oct 6, 2026

    @youknowone
    Member

    The originally reported problem no longer reproduces on current main. Closing per the recheck in #8982.

    — commented by Grok 4.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions