From 6bf3584616b9bf61b6a3374521f69c85b3310d8a Mon Sep 17 00:00:00 2001 From: Greg Back Date: Tue, 18 Apr 2017 15:05:20 -0500 Subject: [PATCH] Create custom exception class for modifying an immutable object. --- stix2/base.py | 6 +++--- stix2/exceptions.py | 7 +++++++ stix2/test/test_indicator.py | 2 +- stix2/test/test_malware.py | 2 +- stix2/test/test_relationship.py | 2 +- 5 files changed, 13 insertions(+), 6 deletions(-) diff --git a/stix2/base.py b/stix2/base.py index 3b383f8..130d8d3 100644 --- a/stix2/base.py +++ b/stix2/base.py @@ -4,7 +4,8 @@ import collections import datetime as dt import json -from .exceptions import ExtraFieldsError, InvalidValueError, MissingFieldsError +from .exceptions import ExtraFieldsError, ImmutableError, InvalidValueError, \ + MissingFieldsError from .utils import format_datetime, get_timestamp, NOW __all__ = ['STIXJSONEncoder', '_STIXBase'] @@ -85,8 +86,7 @@ class _STIXBase(collections.Mapping): def __setattr__(self, name, value): if name != '_inner' and not name.startswith("_STIXBase__"): - print(name) - raise ValueError("Cannot modify properties after creation.") + raise ImmutableError super(_STIXBase, self).__setattr__(name, value) def __str__(self): diff --git a/stix2/exceptions.py b/stix2/exceptions.py index d63891e..2606796 100644 --- a/stix2/exceptions.py +++ b/stix2/exceptions.py @@ -42,3 +42,10 @@ class ExtraFieldsError(STIXError, TypeError): msg = "Unexpected field(s) for {0}: ({1})." return msg.format(self.cls.__name__, ", ".join(x for x in self.fields)) + + +class ImmutableError(STIXError, ValueError): + """Attempted to modify an object after creation""" + + def __init__(self): + super(ImmutableError, self).__init__("Cannot modify properties after creation.") diff --git a/stix2/test/test_indicator.py b/stix2/test/test_indicator.py index cb64d40..a941bc9 100644 --- a/stix2/test/test_indicator.py +++ b/stix2/test/test_indicator.py @@ -125,7 +125,7 @@ def test_indicator_revoked_invalid(): def test_cannot_assign_to_indicator_attributes(indicator): - with pytest.raises(ValueError) as excinfo: + with pytest.raises(stix2.exceptions.ImmutableError) as excinfo: indicator.valid_from = dt.datetime.now() assert str(excinfo.value) == "Cannot modify properties after creation." diff --git a/stix2/test/test_malware.py b/stix2/test/test_malware.py index bce2447..f0431c4 100644 --- a/stix2/test/test_malware.py +++ b/stix2/test/test_malware.py @@ -89,7 +89,7 @@ def test_malware_required_field_name(): def test_cannot_assign_to_malware_attributes(malware): - with pytest.raises(ValueError) as excinfo: + with pytest.raises(stix2.exceptions.ImmutableError) as excinfo: malware.name = "Cryptolocker II" assert str(excinfo.value) == "Cannot modify properties after creation." diff --git a/stix2/test/test_relationship.py b/stix2/test/test_relationship.py index c0478d8..f250ec8 100644 --- a/stix2/test/test_relationship.py +++ b/stix2/test/test_relationship.py @@ -98,7 +98,7 @@ def test_relationship_required_field_target_ref(): def test_cannot_assign_to_relationship_attributes(relationship): - with pytest.raises(ValueError) as excinfo: + with pytest.raises(stix2.exceptions.ImmutableError) as excinfo: relationship.relationship_type = "derived-from" assert str(excinfo.value) == "Cannot modify properties after creation."