Commit 0a32b86b authored by Michael Büsch's avatar Michael Büsch

Use contextlib.nullcontext

Also add compatibility for Python <3.7
Signed-off-by: Michael Büsch's avatarMichael Buesch <m@bues.ch>
parent ec554b1b
......@@ -2,7 +2,7 @@
#
# AWL simulator - utility functions
#
# Copyright 2012-2016 Michael Buesch <m@bues.ch>
# Copyright 2012-2018 Michael Buesch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
......@@ -58,7 +58,6 @@ __all__ = [
"isalnum",
"isdecimal",
"compat_gcd",
"contextlib",
"dictItems",
"dictKeys",
"dictValues",
......@@ -223,6 +222,17 @@ if not hasattr(contextlib, "suppress"):
return exctype is not None and issubclass(exctype, self._excs)
contextlib.suppress = _suppress
# contextlib.nullcontext compatibility
if not hasattr(contextlib, "nullcontext"):
class _nullcontext(object):
def __init__(self, enter_result=None):
self.enter_result = enter_result
def __enter__(self):
return self.enter_result
def __exit__(self, *unused):
pass
contextlib.nullcontext = _nullcontext
# Dict items(), keys(), values() compatibility.
# Use Python3 behavior.
dictItems = py23(lambda d: d.viewitems(),
......
......@@ -74,7 +74,6 @@ __all__ = [
"clamp",
"math_gcd",
"math_lcm",
"nopContext",
"RelPath",
"shortUUID",
"copy",
......@@ -433,17 +432,6 @@ def math_lcm(*args):
return reduce(lambda x, y: x * y // math_gcd(x, y),
args)
class nopContextManager(object):
"""No-operation context manager.
"""
def __enter__(self):
return None
def __exit__(self, exctype, excinst, exctb):
return False
nopContext = nopContextManager()
class RelPath(object):
def __init__(self, relativeToDir):
self.__relativeToDir = relativeToDir
......
......@@ -2,7 +2,7 @@
#
# AWL simulator - Block type edit widget
#
# Copyright 2016 Michael Buesch <m@bues.ch>
# Copyright 2016-2018 Michael Buesch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
......@@ -287,7 +287,7 @@ class BlockTypeWidget(QWidget):
def clear(self, noChangeSignals=False):
"""Clear content.
"""
with self.__changeSignalsBlocked if noChangeSignals else nopContext:
with self.__changeSignalsBlocked if noChangeSignals else contextlib.nullcontext():
self.typeCombo.setCurrentIndex(0)
self.blockNameEdit.setText("FC 1")
self.dbEdit.clear()
......@@ -295,7 +295,7 @@ class BlockTypeWidget(QWidget):
def setBlockTypeString(self, blockTypeString, noChangeSignals=False):
"""Set the block type.
"""
with self.__changeSignalsBlocked if noChangeSignals else nopContext:
with self.__changeSignalsBlocked if noChangeSignals else contextlib.nullcontext():
index = self.typeCombo.findData(blockTypeString,
Qt.UserRole, Qt.MatchFixedString)
if index >= 0:
......@@ -306,7 +306,7 @@ class BlockTypeWidget(QWidget):
def setBlockName(self, blockName, noChangeSignals=False):
"""Set the block name.
"""
with self.__changeSignalsBlocked if noChangeSignals else nopContext:
with self.__changeSignalsBlocked if noChangeSignals else contextlib.nullcontext():
self.blockNameEdit.setText(blockName)
def addInstanceDB(self, dbName, noChangeSignals=False):
......@@ -314,7 +314,7 @@ class BlockTypeWidget(QWidget):
"""
if not dbName.strip():
return False
with self.__changeSignalsBlocked if noChangeSignals else nopContext:
with self.__changeSignalsBlocked if noChangeSignals else contextlib.nullcontext():
typeStr = self.typeCombo.itemData(self.typeCombo.currentIndex())
if typeStr == "FB":
if self.dbEdit.text().strip():
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment