Skip to content
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

InteractiveConsole behaves differently on terminal, within script #77450

Closed
KadirHaldenbilen mannequin opened this issue Apr 12, 2018 · 4 comments
Closed

InteractiveConsole behaves differently on terminal, within script #77450

KadirHaldenbilen mannequin opened this issue Apr 12, 2018 · 4 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@KadirHaldenbilen
Copy link
Mannequin

KadirHaldenbilen mannequin commented Apr 12, 2018

BPO 33269
Nosy @terryjreedy
Files
  • iitests.py
  • iiteswok.py
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2018-04-14.00:22:29.593>
    created_at = <Date 2018-04-12.14:55:59.560>
    labels = ['type-bug', 'invalid']
    title = 'InteractiveConsole behaves differently on terminal, within script'
    updated_at = <Date 2018-04-14.00:22:29.569>
    user = 'https://bugs.python.org/KadirHaldenbilen'

    bugs.python.org fields:

    activity = <Date 2018-04-14.00:22:29.569>
    actor = 'terry.reedy'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-04-14.00:22:29.593>
    closer = 'terry.reedy'
    components = []
    creation = <Date 2018-04-12.14:55:59.560>
    creator = 'Kadir Haldenbilen'
    dependencies = []
    files = ['47533', '47534']
    hgrepos = []
    issue_num = 33269
    keywords = []
    message_count = 4.0
    messages = ['315224', '315226', '315227', '315277']
    nosy_count = 2.0
    nosy_names = ['terry.reedy', 'Kadir Haldenbilen']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue33269'
    versions = ['Python 3.6']

    @KadirHaldenbilen
    Copy link
    Mannequin Author

    KadirHaldenbilen mannequin commented Apr 12, 2018

    on terminal push and runcode accepts indentation where required (like for loop etc), within script gives error message

    on terminal import works properly and objects can be found as normal, within script you may need to add module name upfront

    simple example
    import code
    ii = code.InteractiveConsole()
    ii.push("for ii in range(3):")
    ii.push(" print('i', i)")

    you will get normal expected output on terminal, but indentation error within script

    ii.push("from time import sleep")
    ii.push("sleep(1)")
    will sleep 1 sec on terminal, but will give name error

    @KadirHaldenbilen KadirHaldenbilen mannequin added the type-bug An unexpected behavior, bug, or error label Apr 12, 2018
    @KadirHaldenbilen
    Copy link
    Mannequin Author

    KadirHaldenbilen mannequin commented Apr 12, 2018

    Attached is a non working example
    I could not attach a second file, which works OK within script I will attach separately

    @KadirHaldenbilen
    Copy link
    Mannequin Author

    KadirHaldenbilen mannequin commented Apr 12, 2018

    This script works OK. Compare with iitests.py uploaded before

    @terryjreedy
    Copy link
    Member

    [For future reference, if I were not closing this, I would ask the following:  Does 'on terminal mean that you started Python in interactive mode and then entered lines in response to the '>>> prompt'?  If so, did you start Python from an actual (non-Python) terminal or console? or from an icon or start menu?  What OS?  When you ran a script, did you run it from the system console or from the file itself?  However, I now consider this a moot point.]

    The test for the code module is test.test_code_module, as test_code
    tests code objects. It created a code.InteractiveConsole and calls .interact. There are no direct unittests of any of the other methods, but most, if not all, including .push, are called directly or indirecty within .interact. This is all within a script, which you say has problmes. Input is fed to the interact loop with lines such as

            self.infunc.side_effect = ["try: ham\nexcept: eggs\n",
                                        EOFError('Finished')]

    I cannot see that you have identified a real problem. Your iitests.py fails because it is buggy. It creates a new interactive console for each line. When " print('i', i)" is pushed to a new console, in response to >>>, there *should* be an indentation error. Similarly, 'sleep(1)' in a new console *should* fail with a NameError.

    In iiteswok.py, which you say works, only one console is created. After adding 'ii.push("")', to tell the console that the statement is complete, and removing unneeded code, the following works for me when run as a script

    import code
    ii = code.InteractiveConsole()
    ii.push("for i in range(3):")
    ii.push("    print ('i', i)")
    ii.push("")

    # prints

    i 0
    i 1
    i 2

    Adding

    ii.push("from time import sleep")
    ii.push("print('start')")
    ii.push("sleep(1)")
    ii.push("print('stop')")

    prints 'start' and 'stop' with a one second delay.

    @terryjreedy terryjreedy changed the title InteractiveConsole behaves differently when used on terminal and used within script InteractiveConsole behaves differently on terminal, within script Apr 14, 2018
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant