Skip to content

Commit 8f77102

Browse files
author
James William Pye
committed
Remove hard parameters and the -1 option.
hard parameters were basically unused, and the effect of -1 is not so useful as a runtime option in python scripts.(well, i suppose it could be, but errors would have to be treated as messages) Also, prompt the user for a new password on authspec connection failures. However, this should probably be generalized to allow the user to reconfigure the connection parameters entirely.
1 parent 661e08d commit 8f77102

1 file changed

Lines changed: 31 additions & 32 deletions

File tree

postgresql/bin/pg_python.py

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# http://python.projects.postgresql.org
44
##
55
"""
6-
Python command with a postgresql.driver.pq3 connection.
6+
Python command with a PG-API connection.
77
"""
88
import os
99
import sys
@@ -15,6 +15,7 @@
1515
from ..resolved import pythoncommand as pycmd
1616

1717
from ..driver import default as pg_driver
18+
from .. import exceptions as pg_exc
1819

1920
pq_trace = optparse.make_option(
2021
'--pq-trace',
@@ -23,50 +24,50 @@
2324
default = None,
2425
)
2526
default_options = [
26-
clientparameters.option_in_xact,
2727
pq_trace,
2828
] + pycmd.default_optparse_options
2929

30-
param_pattern = re.compile(
31-
r'^\s*#\s+-\*-\s+postgresql\.([^:]+):\s+([^\s]*)\s+-\*-\s*$',
32-
re.M
33-
)
34-
def extract_parameters(src):
35-
'extract hard parameters out of the "-*- postgresql.*: -*-" magic lines'
36-
return [
37-
x for x in re.findall(param_pattern, src)
38-
]
39-
40-
def command(args = sys.argv):
30+
def command(argv = sys.argv):
4131
p = clientparameters.DefaultParser(
4232
"%prog [connection options] [script] ...",
4333
version = '1.0',
4434
option_list = default_options
4535
)
4636
p.disable_interspersed_args()
47-
co, ca = p.parse_args(args[1:])
48-
in_xact = co.in_xact
37+
co, ca = p.parse_args(argv[1:])
4938

50-
cond = clientparameters.standard(co = co, prompt_title = "pg_python")
51-
connector = pg_driver.create(**cond)
52-
connection = connector.create()
39+
need_prompt = False
40+
cond = None
41+
connector = None
42+
connection = None
43+
while connection is None:
44+
try:
45+
cond = clientparameters.standard(co = co, prompt_title = None)
46+
if need_prompt:
47+
# authspec error thrown last time
48+
cond['prompt_password'] = True
49+
try:
50+
clientparameters.resolve_password(cond, prompt_title = 'pg_python')
51+
except EOFError:
52+
sys.stderr.write(os.linesep + "ERROR: session aborted" + os.linesep)
53+
raise SystemExit(1)
54+
connector = pg_driver.create(**cond)
55+
connection = connector()
56+
except pg_exc.ClientCannotConnectError as err:
57+
for (didssl, sc, cf) in err.connection_failures:
58+
if isinstance(cf, pg_exc.AuthenticationSpecificationError):
59+
sys.stderr.write(os.linesep + cf.message + (os.linesep*2))
60+
# keep prompting the user
61+
need_prompt = True
62+
break
63+
else:
64+
# no invalid password failures..
65+
raise
5366

5467
pythonexec = pycmd.Execution(ca,
5568
context = getattr(co, 'python_context', None),
5669
loader = getattr(co, 'python_main', None),
5770
)
58-
# Some points of configuration need to be demanded by a script.
59-
src = pythonexec.get_main_source()
60-
if src is not None:
61-
hard_params = dict(extract_parameters(src))
62-
if hard_params:
63-
iso = hard_params.get('isolation')
64-
if iso is not None:
65-
if iso == 'none':
66-
in_xact = False
67-
else:
68-
in_xact = True
69-
connection.xact(isolation = iso)
7071

7172
builtin_overload = {
7273
# New built-ins
@@ -92,8 +93,6 @@ def command(args = sys.argv):
9293
if trace_file is not None:
9394
connection.tracer = trace_file.write
9495
context = [connection]
95-
if in_xact:
96-
context.append(connection.xact)
9796
with contextlib.nested(*context):
9897
rv = pythonexec(
9998
context = pycmd.postmortem(os.environ.get('PYTHON_POSTMORTEM'))

0 commit comments

Comments
 (0)