FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

`-i` and `-c <code>`? · Issue #994 · bpython/bpython · GitHub

Repository navigation

-i and -c <code>? #994

Description

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).

Activity

  1. sebastinas commented on Jul 12, 2023

    Contributor

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

  2. fferri commented on Jul 12, 2023

    Author

    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?

  3. sebastinas commented on Jul 12, 2023

    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.

  4. fferri commented on Jul 12, 2023

    Author

    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.

  5. thomasballinger commented on Aug 1, 2023

    Member

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

  6. fferri commented on Aug 2, 2023

    Author

    --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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL