8000 Simplify · python/cpython@2a8c729 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2a8c729

Browse files
committed
Simplify
1 parent 198399e commit 2a8c729

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

Lib/sqlite3/__main__.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,9 @@
1010
from argparse import ArgumentParser
1111
from code import InteractiveConsole
1212
from textwrap import dedent
13-
import _colorize as colorize
13+
from _colorize import get_theme, theme_no_color
1414

15-
def _clr(color, use_color):
16-
if use_color:
17-
return color
18-
return ''
19-
20-
def execute(c, sql, suppress_errors=True, use_color=False):
15+
def execute(c, sql, suppress_errors=True, theme=theme_no_color):
2116
"""Helper that wraps execution of SQL code.
2217
2318
This is used both by the REPL and by direct execution from the CLI.
@@ -30,15 +25,16 @@ def execute(c, sql, suppress_errors=True, use_color=False):
3025
for row in c.execute(sql):
3126
print(row)
3227
except sqlite3.Error as e:
33-
theme = colorize.get_theme(force_color=True).traceback
28+
t = theme.traceback
3429
tp = type(e).__name__
3530
try:
36-
print(f"{_clr(theme.type, use_color)}{tp} ({e.sqlite_errorname})"
37-
f"{_clr(theme.reset, use_color)}: "
38-
f"{_clr(theme.message, use_color)}{e}{_clr(theme.reset, use_color)}", file=sys.stderr)
31+
tp += f" ({e.sqlite_errorname})"
3932
except AttributeError:
40-
print(f"{_clr(theme.type, use_color)}{tp}{_clr(theme.reset, use_color)}: "
41-
f"{_clr(theme.message, use_color)}{e}{_clr(theme.reset, use_color)}", file=sys.stderr)
33+
pass
34+
print(
35+
f"{t.type}{tp}{t.reset}: {t.message}{e}{t.reset}",
36+
file=sys.stderr,
37+
)
4238
if not suppress_errors:
4339
sys.exit(1)
4440

@@ -68,7 +64,8 @@ def runsource(self, source, filename="<input>", symbol="single"):
6864
case _:
6965
if not sqlite3.complete_statement(source):
7066
return True
71-
execute(self._cur, source, use_color=self._use_color)
67+
theme = get_theme(force_no_color=not self._use_color)
68+
execute(self._cur, source, theme=theme)
7269
return False
7370

7471

@@ -116,11 +113,10 @@ def main(*args):
116113
Type ".help" for more information; type ".quit" or {eofkey} to quit.
117114
""").strip()
118115

119-
use_color = colorize.can_colorize()
120-
theme = colorize.get_theme(force_color=True).syntax
116+
s = get_theme().syntax
121117

122-
sys.ps1 = f"{_clr(theme.prompt, use_color)}sqlite> {_clr(theme.reset, use_color)}"
123-
sys.ps2 = f"{_clr(theme.prompt, use_color)} ... {_clr(theme.reset, use_color)}"
118+
sys.ps1 = f"{s.prompt}sqlite> {s.reset}"
119+
sys.ps2 = f"{s.prompt} ... {s.reset}"
124120

125121
con = sqlite3.connect(args.filename, isolation_level=None)
126122
try:
@@ -129,7 +125,7 @@ def main(*args):
129125
execute(con, args.sql, suppress_errors=False)
130126
else:
131127
# No SQL provided; start the REPL.
132-
console = SqliteInteractiveConsole(con, use_color)
128+
console = SqliteInteractiveConsole(con, use_color=True)
133129
try:
134130
import readline # noqa: F401
135131
except ImportError:

0 commit comments

Comments
 (0)
0