mirror of https://github.com/dbcli/pgcli
Convert psycopg2 erros to unicode. Fixes #124
This commit is contained in:
parent
6944ef60f8
commit
c2d539df8a
|
@ -0,0 +1,24 @@
|
|||
import sys
|
||||
|
||||
PY2 = sys.version_info[0] == 2
|
||||
PY3 = sys.version_info[0] == 3
|
||||
|
||||
def unicode2utf8(arg):
|
||||
"""
|
||||
Only in Python 2. Psycopg2 expects the args as bytes not unicode.
|
||||
In Python 3 the args are expected as unicode.
|
||||
"""
|
||||
|
||||
if PY2 and isinstance(arg, unicode):
|
||||
return arg.encode('utf-8')
|
||||
return arg
|
||||
|
||||
def utf8tounicode(arg):
|
||||
"""
|
||||
Only in Python 2. Psycopg2 returns the error message as utf-8.
|
||||
In Python 3 the errors are returned as unicode.
|
||||
"""
|
||||
|
||||
if PY2 and isinstance(arg, str):
|
||||
return arg.decode('utf-8')
|
||||
return arg
|
|
@ -26,6 +26,7 @@ from .pgexecute import PGExecute
|
|||
from .pgbuffer import PGBuffer
|
||||
from .config import write_default_config, load_config
|
||||
from .key_bindings import pgcli_bindings
|
||||
from .encodingutils import utf8tounicode
|
||||
|
||||
from time import time
|
||||
|
||||
|
@ -135,7 +136,8 @@ class PGCli(object):
|
|||
try:
|
||||
pgexecute = PGExecute(database, user, passwd, host, port)
|
||||
except OperationalError as e:
|
||||
if 'no password supplied' in e.args[0] and auto_passwd_prompt:
|
||||
if ('no password supplied' in utf8tounicode(e.args[0]) and
|
||||
auto_passwd_prompt):
|
||||
passwd = click.prompt('Password', hide_input=True,
|
||||
show_default=False, type=str)
|
||||
pgexecute = PGExecute(database, user, passwd, host, port)
|
||||
|
@ -144,6 +146,7 @@ class PGCli(object):
|
|||
|
||||
except Exception as e: # Connecting to a database could fail.
|
||||
self.logger.debug('Database connection failed: %r.', e)
|
||||
self.logger.error("traceback: %r", traceback.format_exc())
|
||||
click.secho(str(e), err=True, fg='red')
|
||||
exit(1)
|
||||
|
||||
|
|
|
@ -1,13 +1,10 @@
|
|||
import sys
|
||||
import logging
|
||||
import psycopg2
|
||||
import psycopg2.extras
|
||||
import psycopg2.extensions as ext
|
||||
import sqlparse
|
||||
from .packages import pgspecial
|
||||
|
||||
PY2 = sys.version_info[0] == 2
|
||||
PY3 = sys.version_info[0] == 3
|
||||
from .encodingutils import unicode2utf8
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
@ -87,16 +84,6 @@ class PGExecute(object):
|
|||
def connect(self, database=None, user=None, password=None, host=None,
|
||||
port=None):
|
||||
|
||||
def unicode2utf8(arg):
|
||||
"""
|
||||
Only in Python 2. Psycopg2 expects the args as bytes not unicode.
|
||||
In Python 3 the args are expected as unicode.
|
||||
"""
|
||||
|
||||
if PY2 and isinstance(arg, unicode):
|
||||
return arg.encode('utf-8')
|
||||
return arg
|
||||
|
||||
db = unicode2utf8(database or self.dbname)
|
||||
user = unicode2utf8(user or self.user)
|
||||
password = unicode2utf8(password or self.password)
|
||||
|
|
Loading…
Reference in New Issue