dbcli / mycli

A Terminal Client for MySQL with AutoCompletion and Syntax Highlighting.
http://mycli.net
Other
11.42k stars 661 forks source link

Avoid open('/dev/tty') until absolutely necessary #938

Closed rolandwalker closed 3 years ago

rolandwalker commented 3 years ago

Description

In #899, a user is unable to run a non-interactive command, because mycli attempts to open /dev/tty in a situation where it is not available.

Setting sys.stdin looks like a shady thing to do anyway.

This PR defers sys.stdin = open('/dev/tty') until we are certain that interaction with the user is needed. --warn must be on, and the query being considered must contain destructive statements. is_destructive() ends up being called twice in the destructive case, which I think is no big deal.

I believe that the code being replaced had a bug, in that confirm_destructive_query() could have been None in addition to False.

Checklist