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

Enabling IPython vi mode crashes calcpy on startup #2

Closed
dogeystamp opened this issue Jun 15, 2023 · 1 comment
Closed

Enabling IPython vi mode crashes calcpy on startup #2

dogeystamp opened this issue Jun 15, 2023 · 1 comment

Comments

@dogeystamp
Copy link
Contributor

dogeystamp commented Jun 15, 2023

Hi, thank you for writing calcpy, it's a great tool.

I tried to add this line to my IPython config file .config/ipython/profile_calcpy/ipython_config.py:

c.TerminalInteractiveShell.editing_mode = "vi"

I then ran python3 calcpy_cli and got this error:

Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3.11/pathlib.py", line 1250, in is_dir
    return S_ISDIR(self.stat().st_mode)
                   ^^^^^^^^^
AttributeError: 'str' object has no attribute 'stat'

Original exception was:
Traceback (most recent call last):
  File "/home/dogeystamp/.local/bin/calcpy/calcpy_cli.py", line 109, in <module>
    main()
  File "/home/dogeystamp/.local/bin/calcpy/calcpy_cli.py", line 106, in main
    IPython.start_ipython(ipython_args)
  File "/usr/lib/python3.11/site-packages/IPython/__init__.py", line 129, in start_ipython
    return launch_new_instance(argv=argv, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/traitlets/config/application.py", line 1043, in launch_instance
    app.start()
  File "/usr/lib/python3.11/site-packages/IPython/terminal/ipapp.py", line 318, in start
    self.shell.mainloop()
  File "/usr/lib/python3.11/site-packages/IPython/terminal/interactiveshell.py", line 888, in mainloop
    self.interact()
  File "/usr/lib/python3.11/site-packages/IPython/terminal/interactiveshell.py", line 873, in interact
    code = self.prompt_for_code()
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/IPython/terminal/interactiveshell.py", line 812, in prompt_for_code
    text = self.pt_app.prompt(
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/prompt_toolkit/shortcuts/prompt.py", line 1035, in prompt
    return self.app.run(
           ^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/prompt_toolkit/application/application.py", line 961, in run
    return loop.run_until_complete(coro)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/prompt_toolkit/application/application.py", line 875, in run_async
    return await _run_async(f)
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/prompt_toolkit/application/application.py", line 676, in _run_async
    self.reset()
  File "/usr/lib/python3.11/site-packages/prompt_toolkit/application/application.py", line 432, in reset
    self.vi_state.reset()
  File "/usr/lib/python3.11/site-packages/prompt_toolkit/key_binding/vi_state.py", line 99, in reset
    self.input_mode = InputMode.INSERT
    ^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/IPython/terminal/shortcuts/__init__.py", line 370, in set_input_mode
    sys.stdout.write(cursor)
TypeError: no_asyncio_write() missing 1 required positional argument: 'data'

It seems like the source of the error is this code from calcpy_cli.py:

    # monkey patch prompt_toolkit to avoid printin from background for preview
    # TODO: need a better fix
    prompt_toolkit.patch_stdout.StdoutProxy.write = \
        partial(no_asyncio_write, prompt_toolkit.patch_stdout.StdoutProxy.write)

I patched no_asyncio_write like this:

def no_asyncio_write(write, self, data=None):
    if data is None:
        return
    if not threading.currentThread().name.startswith('asyncio'):
        write(self, data)

and it seemed to fix the crash.

Completely removing the monkey patch also fixes the crash.

I'm not quite sure what the purpose of your code is here, so I do not know if these fixes break any functionality of calcpy.

EDIT: the above no_asyncio_write patch is just a workaround because it fails to actually write the data

@idanpa
Copy link
Owner

idanpa commented Jun 15, 2023

Hi @dogeystamp 👋,
Thank you for the report and the suggested patch! ❤

Merged a fix - 42f9c16
(like you said, default value is not good enough, actually the function was completly wrong, you might have noticed that vim mode has no color even with your fix)

The code is blocking prints from the thread that is evaluating the cell while typing, so for example typing 'print(1)' won't mess up everything.
As the TODO suggests, I think I would move everything to a different process (where it can have a separate stdout) soon.

Please let me know if you have any other problems / suggestions.

@idanpa idanpa closed this as completed Jun 15, 2023
dogeystamp added a commit to dogeystamp/dots that referenced this issue Jun 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants