Remove use of contextlib.nested

Removed use of contextlib.nested call from codebase, as it has been
deprecated since Python 2.7.

There are also known issues with contextlib.nested that were addressed
by the native support for multiple "with" variables. For instance, if
the first object is created but the second one throws an exception,
the first object's __exit__ is never called. For more information see
https://docs.python.org/2/library/contextlib.html#contextlib.nested
contextlib.nested is also not compatible with Python 3.

Multi-patch set for easier chunks. This one addresses the
neutron/plugins/ml2 directory.

Line continuation markers (e.g. '\') had to be used or syntax
errors were thrown. While using parentheses is the preferred way
for multiple line statements, but in case of long with statements
backslashes are acceptable.

Partial-Bug: 1428424
Change-Id: I7bbe4cec511125b4b2c954aa93e2d9ff6871b9e0
This commit is contained in:
ankitagrawal 2015-05-14 04:08:36 -07:00
parent 55c3cdecb3
commit 032847b303
2 changed files with 14 additions and 17 deletions

View File

@ -146,7 +146,6 @@ def check_no_contextlib_nested(logical_line, filename):
# these issues. It should be removed completely
# when bug 1428424 is closed.
ignore_dirs = [
"neutron/plugins/ml2",
"neutron/tests/unit/api",
"neutron/tests/unit/db",
"neutron/tests/unit/extensions",

View File

@ -13,8 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import contextlib
from eventlet import greenthread
from oslo_concurrency import lockutils
from oslo_config import cfg
@ -353,8 +351,8 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
# prevent deadlock waiting to acquire a DB lock held by
# another thread in the same process, leading to 'lock wait
# timeout' errors.
with contextlib.nested(lockutils.lock('db-access'),
session.begin(subtransactions=True)):
with lockutils.lock('db-access'),\
session.begin(subtransactions=True):
# Get the current port state and build a new PortContext
# reflecting this state as original state for subsequent
# mechanism driver update_port_*commit() calls.
@ -740,8 +738,8 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
self._process_l3_delete(context, id)
# Using query().with_lockmode isn't necessary. Foreign-key
# constraints prevent deletion if concurrent creation happens.
with contextlib.nested(lockutils.lock('db-access'),
session.begin(subtransactions=True)):
with lockutils.lock('db-access'),\
session.begin(subtransactions=True):
# Get ports to auto-delete.
ports = (session.query(models_v2.Port).
enable_eagerloads(False).
@ -860,8 +858,8 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
# prevent deadlock waiting to acquire a DB lock held by
# another thread in the same process, leading to 'lock
# wait timeout' errors.
with contextlib.nested(lockutils.lock('db-access'),
session.begin(subtransactions=True)):
with lockutils.lock('db-access'),\
session.begin(subtransactions=True):
record = self._get_subnet(context, id)
subnet = self._make_subnet_dict(record, None)
qry_allocated = (session.query(models_v2.IPAllocation).
@ -1107,8 +1105,8 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
# prevent deadlock waiting to acquire a DB lock held by
# another thread in the same process, leading to 'lock wait
# timeout' errors.
with contextlib.nested(lockutils.lock('db-access'),
session.begin(subtransactions=True)):
with lockutils.lock('db-access'),\
session.begin(subtransactions=True):
port_db, binding = db.get_locked_port_and_binding(session, id)
if not port_db:
raise exc.PortNotFound(port_id=id)
@ -1259,8 +1257,8 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
# prevent deadlock waiting to acquire a DB lock held by
# another thread in the same process, leading to 'lock wait
# timeout' errors.
with contextlib.nested(lockutils.lock('db-access'),
session.begin(subtransactions=True)):
with lockutils.lock('db-access'),\
session.begin(subtransactions=True):
port_db, binding = db.get_locked_port_and_binding(session, id)
if not port_db:
LOG.debug("The port '%s' was deleted", id)
@ -1386,8 +1384,8 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
# prevent deadlock waiting to acquire a DB lock held by
# another thread in the same process, leading to 'lock wait
# timeout' errors.
with contextlib.nested(lockutils.lock('db-access'),
session.begin(subtransactions=True)):
with lockutils.lock('db-access'),\
session.begin(subtransactions=True):
port = db.get_port(session, port_id)
if not port:
LOG.warning(_LW("Port %(port)s updated up by agent not found"),
@ -1418,8 +1416,8 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
if (updated and
port['device_owner'] == const.DEVICE_OWNER_DVR_INTERFACE):
with contextlib.nested(lockutils.lock('db-access'),
session.begin(subtransactions=True)):
with lockutils.lock('db-access'),\
session.begin(subtransactions=True):
port = db.get_port(session, port_id)
if not port:
LOG.warning(_LW("Port %s not found during update"),