From c0cf1f388b61609a6429ab071845fd270e7395c7 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 22 Jun 2016 11:12:58 +0200 Subject: [PATCH] Port test_hacking to Python 3 * CheckForStrUnicodeExc: visit also "Try" AST node on Python 3 * tests-py3.txt: run nova.tests.unit.test_hacking on Python 3 This change is partially based on the work of dims, change Ibb4fa47cd71d697a4996425b1797ac2f8cc363cd. Co-Authored-By: Davanum Srinivas Partially-Implements: blueprint nova-python3-newton Change-Id: I075c03dd168eef25fdde0b8e2284815ddd638ca0 --- nova/hacking/checks.py | 29 +++++++++++++++++++++-------- tests-py3.txt | 1 - 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/nova/hacking/checks.py b/nova/hacking/checks.py index 43c0388bca5e..36f5d30a5a10 100644 --- a/nova/hacking/checks.py +++ b/nova/hacking/checks.py @@ -18,6 +18,7 @@ import os import re import pep8 +import six """ Guidelines for writing new hacking checks @@ -429,14 +430,26 @@ class CheckForStrUnicodeExc(BaseASTChecker): self.name = [] self.already_checked = [] - def visit_TryExcept(self, node): - for handler in node.handlers: - if handler.name: - self.name.append(handler.name.id) - super(CheckForStrUnicodeExc, self).generic_visit(node) - self.name = self.name[:-1] - else: - super(CheckForStrUnicodeExc, self).generic_visit(node) + # Python 2 produces ast.TryExcept and ast.TryFinally nodes, but Python 3 + # only produces ast.Try nodes. + if six.PY2: + def visit_TryExcept(self, node): + for handler in node.handlers: + if handler.name: + self.name.append(handler.name.id) + super(CheckForStrUnicodeExc, self).generic_visit(node) + self.name = self.name[:-1] + else: + super(CheckForStrUnicodeExc, self).generic_visit(node) + else: + def visit_Try(self, node): + for handler in node.handlers: + if handler.name: + self.name.append(handler.name) + super(CheckForStrUnicodeExc, self).generic_visit(node) + self.name = self.name[:-1] + else: + super(CheckForStrUnicodeExc, self).generic_visit(node) def visit_Call(self, node): if self._check_call_names(node, ['str', 'unicode']): diff --git a/tests-py3.txt b/tests-py3.txt index cfb234e8264f..441bee4b571e 100644 --- a/tests-py3.txt +++ b/tests-py3.txt @@ -59,7 +59,6 @@ nova.tests.unit.pci.test_stats.PciDeviceStatsWithTagsTestCase nova.tests.unit.test_api_validation.Base64TestCase nova.tests.unit.test_bdm.BlockDeviceMappingEc2CloudTestCase nova.tests.unit.test_configdrive2.ConfigDriveTestCase -nova.tests.unit.test_hacking.HackingTestCase nova.tests.unit.test_matchers.TestDictMatches nova.tests.unit.test_metadata.MetadataHandlerTestCase nova.tests.unit.test_metadata.MetadataPasswordTestCase