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

-i and -c <code>? #994

Open
fferri opened this issue Jul 12, 2023 · 6 comments
Open

-i and -c <code>? #994

fferri opened this issue Jul 12, 2023 · 6 comments

Comments

@fferri
Copy link

fferri commented Jul 12, 2023

Running:

bpython -c 'from mymodule import *' -i

imports the desiret items correctly, but then doesn't drop into the python shell (it quits instead).

Reversing the order of arguments:

bpython -i -c 'from mymodule import *'

has a "strange" effect: the bpython banner (the line with bpython & python version) is not printed anymore, also $PYTHONSTARTUP is ignored, and all the nice ncurses-based highlighting and completion functionalities are disabled. Is this the intended behavior? Why?

Currently, to automate importing a module (e.g. for doing interactive testing during development on that module) I have to put my import line in a file, and do bpython -i import_mymodule.py. (I tend to use $PYTHONSTARTUP just for importing some commonly used modules).

@sebastinas
Copy link
Contributor

The behavior is consistent with running those commands with python3. Do you see a difference?

@fferri
Copy link
Author

fferri commented Jul 12, 2023

Well, python3 never prints the banner when using -c.

Also, python3 doesn't uses ncurses, so a comparison is not really possible.

Since bpython uses ncurses, it should do so in all interactive cases, including bpython -i -c 'from mymodule import *', no?

@sebastinas
Copy link
Contributor

Well, python3 never prints the banner when using -c.

In which case does bpython print a banner when using -c?

Also, python3 doesn't uses ncurses, so a comparison is not really possible.

The goal is that the common command line options behave the same. If they don't, that's a bug. If they are consistent with python3, it's not.

@fferri
Copy link
Author

fferri commented Jul 12, 2023

The goal is that the common command line options behave the same. If they don't, that's a bug. If they are consistent with python3, it's not.

Then perhaps a new command line switch (which injects an initial command into the interactive session) would be best.

For the record: both bpython -i file.py and setting PYTHONSTARTUP=/path/to/file.py would work, but for a short simple oneliner, having to create a file is not that convenient. That probably is also the reason of having a -c command line switch in python, although that could be intended for different usecases than interactive sessions.

@thomasballinger
Copy link
Member

thomasballinger commented Aug 1, 2023

A similar option exists called --paste that reads from a file. I see, you'd like this from the command line.

@fferri
Copy link
Author

fferri commented Aug 2, 2023

--paste pastes from file. In this scenario it doesn't add anything to -i (see above).

Pasting from a string would make sense.

If I have gone thru the effort of putting my from mymodule import * line into a file, then also bpython -i file.py would work, as I already said above; or the same work can be off-loaded to the shell (YMMV), e.g. with zsh, there are two ways.

  1. process substitution, but bpython won't work with a fd:
❯ bpython -p <(echo 'from mymodule import *')
bpython version 0.24 on top of Python 3.11.4 /opt/homebrew/opt/python@3.11/bin/python3.11

Traceback (most recent call last):
  File "/opt/homebrew/bin/bpython", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/bpython/curtsies.py", line 248, in main
    repl.mainloop(True, paste)
  File "/opt/homebrew/lib/python3.11/site-packages/bpython/curtsies.py", line 170, in mainloop
    self.process_event(paste)
  File "/opt/homebrew/lib/python3.11/site-packages/bpython/curtsiesfrontend/repl.py", line 656, in process_event
    return self.process_control_event(e)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/bpython/curtsiesfrontend/repl.py", line 681, in process_control_event
    ctrl_char = compress_paste_event(e)
                ^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/bpython/curtsiesfrontend/repl.py", line 2228, in compress_paste_event
    event = paste_event.events[0]
            ~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range
  1. temp file substitution:
❯ bpython -p =(echo 'from mymodule import *')
bpython version 0.24 on top of Python 3.11.4 /opt/homebrew/opt/python@3.11/bin/python3.11
>>> from mymodule import *
>>>

The second form works, but it creates a temp file, so I somewhat still dislike that (I think as a temp file as the very last resort workaround, the apotheosis of failed integration between two programs).

Reading from a fd (named pipe) <(echo 'from mymodule import *') would be slightly better, but the best I think is supporting also an actual string to paste.

Also, --paste - would not work (in most command line programs, passing - where an input file is expected, means "use stdin"), so it is not possible to use echo 'from mymodule import *' | bpython -p - or bpython -p - <<<'from mymodule import *', but maybe stdin cannot be used like that if we want an interactive session.

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

No branches or pull requests

3 participants