Skip to content

Commit

Permalink
Allow escalation of UUID validation warning to error
Browse files Browse the repository at this point in the history
Today UUIDField emits a warning if the provided value is not a valid
UUID. However this warning cannot be escalated to an exception through
the standard warnings module because the validation code also adds a
warning filter to the warning filter list that overrides the existing
filters.

This patch ensures that the 'once' warning filter only added to the
end of the filter list. This way if the client has already specified
another filter for this warning (e.g. and error filter) then that
filter will not be overriden.

Change-Id: I17cb96d16fcd91195478b738fbdda01b47cfd69d
Closes-Bug: #1746966
  • Loading branch information
Balazs Gibizer committed Feb 2, 2018
1 parent 339b5f8 commit 0e35267
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 4 additions & 1 deletion oslo_versionedobjects/fields.py
Expand Up @@ -347,7 +347,10 @@ class UUID(StringPattern):
def coerce(obj, attr, value):
# FIXME(danms): We should actually verify the UUIDness here
with warnings.catch_warnings():
warnings.simplefilter("once")
# Change the warning action only if no other filter exists
# for this warning to allow the client to define other action
# like 'error' for this warning.
warnings.filterwarnings(action="once", append=True)
try:
uuid.UUID(str(value))
except Exception:
Expand Down
6 changes: 6 additions & 0 deletions oslo_versionedobjects/tests/test_fields.py
Expand Up @@ -13,6 +13,7 @@
# under the License.

import datetime
import warnings

import iso8601
import mock
Expand Down Expand Up @@ -296,6 +297,11 @@ def test_validation_enabled(self):
self.test_from_primitive()
self.test_to_primitive()

def test_validation_warning_can_be_escalated_to_exception(self):
warnings.filterwarnings(action='error')
self.assertRaises(FutureWarning, self.field.coerce, 'obj', 'attr',
'not a uuid')

def test_get_schema(self):
field = fields.UUIDField()
schema = field.get_schema()
Expand Down

0 comments on commit 0e35267

Please sign in to comment.