Changeset: 890f8ef433ed for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/890f8ef433ed
Modified Files:
testing/Mtest.py.in
Branch: Mar2025
Log Message:
Use with statements for pymonetdb connection and cursor.
diffs (147 lines):
diff --git a/testing/Mtest.py.in b/testing/Mtest.py.in
--- a/testing/Mtest.py.in
+++ b/testing/Mtest.py.in
@@ -1312,13 +1312,19 @@ def PerformDir(env, testdir, testlist, t
FdOut = FdErr = F_FAIL
break
try:
- dbh = pymonetdb.connect(username='monetdb',
- password='monetdb',
- hostname=HOST,
- port=int(pSrvr.port),
- database=TSTDB,
- autocommit=True,
- connect_timeout=1.0)
+ with pymonetdb.connect(username='monetdb',
+ password='monetdb',
+ hostname=HOST,
+ port=int(pSrvr.port),
+ database=TSTDB,
+ autocommit=True,
+ connect_timeout=1.0) as dbh:
+ dbh.settimeout(10)
+ with dbh.cursor() as crs:
+ try:
+ crs.execute("call
logging.setcomplevel('SQL_EXECUTION', 'INFO')")
+ except TimeoutError:
+ print('\nTimeout setting log level.\n')
except KeyboardInterrupt:
raise
except ConnectionRefusedError:
@@ -1327,15 +1333,6 @@ def PerformDir(env, testdir, testlist, t
break
except:
pass
- else:
- dbh.settimeout(10)
- crs = dbh.cursor()
- try:
- crs.execute("call
logging.setcomplevel('SQL_EXECUTION', 'INFO')")
- except TimeoutError:
- print('\nTimeout setting log level.\n')
- crs.close()
- dbh.close()
os.environ['MAPIPORT'] = env['MAPIPORT'] = pSrvr.port
SetExecEnv(env['exe'], pSrvr.port, verbosity > 1)
if global_timeout and start_time + global_timeout <
time.time():
@@ -1672,34 +1669,31 @@ def GetBitsAndModsAndThreads(env) :
connurl = open(os.path.join(dbpath, '.conn')).read()
res = mapiportre.search(connurl)
try:
- dbh = pymonetdb.connect(username='monetdb',
- password='monetdb',
- hostname=HOST,
- port=int(res.group('port')),
- database=TSTPREF,
- autocommit=True,
- connect_timeout=1.0)
+ with pymonetdb.connect(username='monetdb',
+ password='monetdb',
+ hostname=HOST,
+ port=int(res.group('port')),
+ database=TSTPREF,
+ autocommit=True,
+ connect_timeout=1.0) as dbh:
+ if timeout:
+ dbh.settimeout(30)
+ with dbh.cursor() as crs:
+ try:
+ crs.execute('select distinct module from
sys.malfunctions() order by module')
+ except TimeoutError:
+ pass
+ else:
+ mods = crs.fetchall()
+ mods = [x[0] for x in mods]
+ try:
+ mods.remove('user')
+ except ValueError:
+ pass
except KeyboardInterrupt:
raise
except:
pass
- else:
- if timeout:
- dbh.settimeout(30)
- crs = dbh.cursor()
- try:
- crs.execute('select distinct module from
sys.malfunctions() order by module')
- except TimeoutError:
- pass
- else:
- mods = crs.fetchall()
- mods = [x[0] for x in mods]
- try:
- mods.remove('user')
- except ValueError:
- pass
- crs.close()
- dbh.close()
proc.terminate()
qOut = proc.stdout.read(timeout=5)
qErr = proc.stderr.read(timeout=5)
@@ -2687,27 +2681,25 @@ class ServerClass:
if self.timer is not None:
self.stacktrace()
try:
- dbh = pymonetdb.connect(username='monetdb',
- password='monetdb',
- hostname=HOST,
- port=int(self.port),
- database=self.dbname,
- connect_timeout=1.0)
- dbh.settimeout(20)
- crs = dbh.cursor()
- crs.execute('select sessionid from sys.sessions()
where sessionid <> sys.current_sessionid()')
- ids = crs.fetchall()
- dbh.settimeout(10)
- for x in ids:
- if procdebug:
- print(f'stopping session {x[0]}')
- crs.execute(f'call sys.stopsession({x[0]})')
- if procdebug and not ids:
- print('no sessions to stop')
+ with pymonetdb.connect(username='monetdb',
+ password='monetdb',
+ hostname=HOST,
+ port=int(self.port),
+ database=self.dbname,
+ connect_timeout=1.0) as dbh:
+ dbh.settimeout(20)
+ with dbh.cursor() as crs:
+ crs.execute('select sessionid from
sys.sessions() where sessionid <> sys.current_sessionid()')
+ ids = crs.fetchall()
+ dbh.settimeout(10)
+ for x in ids:
+ if procdebug:
+ print(f'stopping session {x[0]}')
+ crs.execute(f'call
sys.stopsession({x[0]})')
+ if procdebug and not ids:
+ print('no sessions to stop')
except TimeoutError:
self.proc.kill()
- crs.close()
- dbh.close()
self.timer = None
finally:
self.lock.release()
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]