Skip to content

Commit 61b7516

Browse files
committed
fix support for utf-8
1 parent 650a7df commit 61b7516

File tree

2 files changed

+45
-22
lines changed

2 files changed

+45
-22
lines changed

testgres/testgres.py

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import tempfile
3939
import threading
4040
import time
41+
import traceback
4142

4243
import port_for
4344

@@ -349,7 +350,7 @@ def _prepare_dir(self, destroy):
349350
# Copy backup to new data dir
350351
shutil.copytree(data1, data2)
351352
except Exception as e:
352-
raise BackupException(str(e))
353+
raise BackupException(_explain_exception(e))
353354
else:
354355
base_dir = self.base_dir
355356

@@ -691,13 +692,13 @@ def print_node_file(node_file):
691692
return "### file not found ###\n"
692693

693694
error_text = (
694-
"Cannot start node\n"
695-
"{}\n" # pg_ctl log
696-
"{}:\n----\n{}\n" # postgresql.log
697-
"{}:\n----\n{}\n" # postgresql.conf
698-
"{}:\n----\n{}\n" # pg_hba.conf
699-
"{}:\n----\n{}\n" # recovery.conf
700-
).format(str(e),
695+
u"Cannot start node\n"
696+
u"{}\n" # pg_ctl log
697+
u"{}:\n----\n{}\n" # postgresql.log
698+
u"{}:\n----\n{}\n" # postgresql.conf
699+
u"{}:\n----\n{}\n" # pg_hba.conf
700+
u"{}:\n----\n{}\n" # recovery.conf
701+
).format(_explain_exception(e),
701702
log_filename, print_node_file(log_filename),
702703
conf_filename, print_node_file(conf_filename),
703704
hba_filename, print_node_file(hba_filename),
@@ -867,7 +868,9 @@ def safe_psql(self, dbname, query, username=None):
867868

868869
ret, out, err = self.psql(dbname, query, username=username)
869870
if ret:
870-
raise QueryException(six.text_type(err))
871+
if err:
872+
err = err.decode('utf-8')
873+
raise QueryException(err)
871874
return out
872875

873876
def dump(self, dbname, filename=None):
@@ -1041,7 +1044,7 @@ def catchup(self):
10411044
lsn = master.execute('postgres', poll_lsn)[0][0]
10421045
self.poll_query_until('postgres', wait_lsn.format(lsn))
10431046
except Exception as e:
1044-
raise CatchUpException(str(e))
1047+
raise CatchUpException(_explain_exception(e))
10451048

10461049
def pgbench_init(self, dbname='postgres', scale=1, options=[]):
10471050
"""
@@ -1103,6 +1106,15 @@ def connect(self, dbname='postgres', username=None):
11031106
username=username)
11041107

11051108

1109+
def _explain_exception(e):
1110+
"""
1111+
Use this function instead of str(e).
1112+
"""
1113+
1114+
lines = traceback.format_exception_only(type(e), e)
1115+
return ''.join(lines)
1116+
1117+
11061118
def _cached_initdb(data_dir, initdb_logfile, initdb_params=[]):
11071119
"""
11081120
Perform initdb or use cached node files.
@@ -1113,7 +1125,7 @@ def call_initdb(_data_dir):
11131125
_params = [_data_dir, "-N"] + initdb_params
11141126
_execute_utility("initdb", _params, initdb_logfile)
11151127
except Exception as e:
1116-
raise InitNodeException(str(e))
1128+
raise InitNodeException(_explain_exception(e))
11171129

11181130
# Call initdb if we have custom params
11191131
if initdb_params or not TestgresConfig.cache_initdb:
@@ -1144,7 +1156,7 @@ def rm_cached_data_dir(rm_dir):
11441156
shutil.copytree(cached_data_dir, data_dir)
11451157

11461158
except Exception as e:
1147-
raise InitNodeException(str(e))
1159+
raise InitNodeException(_explain_exception(e))
11481160

11491161

11501162
def _execute_utility(util, args, logfile, write_to_pipe=True):
@@ -1174,23 +1186,29 @@ def _execute_utility(util, args, logfile, write_to_pipe=True):
11741186

11751187
# get result
11761188
out, _ = process.communicate()
1177-
out = '' if not out else six.text_type(out)
11781189

11791190
# write new log entry if possible
11801191
try:
11811192
with open(logfile, "a") as file_out:
1182-
# write util name + args
1183-
file_out.write(''.join(map(lambda x: str(x) + ' ',
1184-
[util] + args)))
1185-
file_out.write('\n')
1186-
file_out.write(out)
1193+
# write util name
1194+
file_out.write(util)
1195+
# write args
1196+
for arg in args:
1197+
file_out.write(arg)
1198+
with open(logfile, "ab") as file_out:
1199+
# write output
1200+
if out:
1201+
file_out.write(out)
11871202
except IOError:
11881203
pass
11891204

1205+
# decode output
1206+
out = '' if not out else out.decode('utf-8')
1207+
11901208
if process.returncode:
11911209
error_text = (
1192-
"{} failed\n"
1193-
"log:\n----\n{}\n"
1210+
u"{} failed\n"
1211+
u"log:\n----\n{}\n"
11941212
).format(util, out)
11951213

11961214
raise ExecUtilException(error_text, process.returncode)

tests/test_simple.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env python
2+
# coding: utf-8
23

34
import os
45
import re
@@ -364,14 +365,18 @@ def test_logging(self):
364365
with get_new_node('master', use_logging=True) as master:
365366
master.init().start()
366367

368+
# execute a dummy query a few times
369+
for i in range(20):
370+
master.execute('postgres', 'select 1')
371+
372+
# let logging worker do the job
367373
import time
368374
time.sleep(0.5)
369375

370376
# check that master's port is found
371377
with open(logfile.name, 'r') as log:
372378
lines = log.readlines()
373-
port = str(master.port)
374-
self.assertTrue(any(port in s for s in lines))
379+
self.assertTrue(any('select' in s for s in lines))
375380

376381
def test_pgbench(self):
377382
with get_new_node('node') as node:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy