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"""
88import os
99import sys
1515from ..resolved import pythoncommand as pycmd
1616
1717from ..driver import default as pg_driver
18+ from .. import exceptions as pg_exc
1819
1920pq_trace = optparse .make_option (
2021 '--pq-trace' ,
2324 default = None ,
2425)
2526default_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