Skip to content

Commit 9b6925e

Browse files
Always exit unsuccessfully with sys.exit (#1481)
`exit` is undefined when the `site` module isn't loaded (i.e. when Python is executed with the `-S` option), and `exit` isn't explicitly defined as a function or imported from elsewhere in `main.py`, so reaching any code path that attempts to execute `exit(1)` in this environment results in a `NameError` being raised: ``` Traceback (most recent call last): File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "/usr/lib/python3.10/runpy.py", line 86, in _run_code exec(code, run_globals) File "[...]/__main__.py", line 415, in <module> File "[...]/__main__.py", line 399, in run File "[...]/__main__.py", line 389, in main File "/usr/lib/python3.10/runpy.py", line 220, in run_module mod_name, mod_spec, code = _get_module_details(mod_name) File "/usr/lib/python3.10/runpy.py", line 157, in _get_module_details code = loader.get_code(mod_name) File "[...]/__main__.py", line 264, in get_code File "[...]/__main__.py", line 204, in load_module File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1050, in _gcd_import File "<frozen importlib._bootstrap>", line 1027, in _find_and_load File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 688, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 883, in exec_module File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed File "third_party/python3/__pgcli_main__.py", line 3, in <module> File "[...]/click/core.py", line 1157, in __call__ File "[...]/click/core.py", line 1078, in main File "[...]/click/core.py", line 1434, in invoke File "[...]/click/core.py", line 783, in invoke File "[...]/pgcli/main.py", line 1617, in cli File "[...]/pgcli/main.py", line 766, in connect NameError: name 'exit' is not defined. Did you mean: 'atexit'? ``` When exiting unsuccessfully, do so with `sys.exit(1)` - this is already used in several other places in `main.py`, and is always used (with exit code 0) when exiting successfully.
1 parent 0fa81ef commit 9b6925e

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ Contributors:
135135
* Andrew M. MacFie (amacfie)
136136
* saucoide
137137
* Chris Rose (offbyone/offby1)
138+
* Chris Novakovic
138139

139140
Creator:
140141
--------

changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ Features
55
--------
66
* Add a `--ping` command line option; allows pgcli to replace `pg_isready`
77

8+
Bug fixes:
9+
----------
10+
* Avoid raising `NameError` when exiting unsuccessfully in some cases
11+
812
4.1.0 (2024-03-09)
913
==================
1014

pgcli/main.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ def connect_service(self, service, user):
610610
click.secho(
611611
f"service '{service}' was not found in {file}", err=True, fg="red"
612612
)
613-
exit(1)
613+
sys.exit(1)
614614
self.connect(
615615
database=service_config.get("dbname"),
616616
host=service_config.get("host"),
@@ -710,7 +710,7 @@ def should_ask_for_password(exc):
710710
self.logger.handlers = logger_handlers
711711
self.logger.error("traceback: %r", traceback.format_exc())
712712
click.secho(str(e), err=True, fg="red")
713-
exit(1)
713+
sys.exit(1)
714714
self.logger.handlers = logger_handlers
715715

716716
atexit.register(self.ssh_tunnel.stop)
@@ -763,7 +763,7 @@ def should_ask_for_password(exc):
763763
self.logger.debug("Database connection failed: %r.", e)
764764
self.logger.error("traceback: %r", traceback.format_exc())
765765
click.secho(str(e), err=True, fg="red")
766-
exit(1)
766+
sys.exit(1)
767767

768768
self.pgexecute = pgexecute
769769

@@ -1551,7 +1551,7 @@ def cli(
15511551
err=True,
15521552
fg="red",
15531553
)
1554-
exit(1)
1554+
sys.exit(1)
15551555

15561556
if ssh_tunnel and not SSH_TUNNEL_SUPPORT:
15571557
click.secho(
@@ -1560,7 +1560,7 @@ def cli(
15601560
err=True,
15611561
fg="red",
15621562
)
1563-
exit(1)
1563+
sys.exit(1)
15641564

15651565
pgcli = PGCli(
15661566
prompt_passwd,
@@ -1604,15 +1604,15 @@ def cli(
16041604
err=True,
16051605
fg="red",
16061606
)
1607-
exit(1)
1607+
sys.exit(1)
16081608
except Exception:
16091609
click.secho(
16101610
"Invalid DSNs found in the config file. "
16111611
'Please check the "[alias_dsn]" section in pgclirc.',
16121612
err=True,
16131613
fg="red",
16141614
)
1615-
exit(1)
1615+
sys.exit(1)
16161616
pgcli.connect_uri(dsn_config)
16171617
pgcli.dsn_alias = dsn
16181618
elif "://" in database:

0 commit comments

Comments
 (0)