1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
import sys
import unittest.mock
import warnings
import weakref
from traits.api import HasTraits
from traits.constants import (
ComparisonMode, DefaultValue, TraitKind, MAXIMUM_DEFAULT_VALUE_TYPE
)
from traits.ctrait import CTrait
from traits.trait_errors import TraitError
from traits.trait_types import Any, Int, List
def getter():
""" Trivial getter. """
return True
def setter(value):
""" Trivial setter. """
pass
def validator(value):
""" Trivial validator. """
return value
class TestCTrait(unittest.TestCase):
""" Tests for the CTrait class. """
def test_initial_default_value(self):
trait = CTrait(TraitKind.trait)
self.assertEqual(
trait.default_value(), (DefaultValue.constant, None),
)
def test_set_and_get_default_value(self):
trait = CTrait(TraitKind.trait)
trait.set_default_value(DefaultValue.constant, 2.3)
self.assertEqual(trait.default_value(), (DefaultValue.constant, 2.3))
trait.set_default_value(DefaultValue.list_copy, [1, 2, 3])
self.assertEqual(
trait.default_value(), (DefaultValue.list_copy, [1, 2, 3])
)
def test_validate_default_value_for_callable_and_args(self):
bad_values = [
None,
123,
(int, (2,)),
(int, 2, 3, 4),
]
trait = CTrait(TraitKind.trait)
for value in bad_values:
with self.subTest(value=value):
with self.assertRaises(ValueError):
trait.set_default_value(
DefaultValue.callable_and_args, value)
def test_default_value_for_set_is_deprecated(self):
trait = CTrait(TraitKind.trait)
with warnings.catch_warnings(record=True) as warn_msgs:
warnings.simplefilter("always", DeprecationWarning)
trait.default_value(DefaultValue.constant, 3.7)
self.assertEqual(len(warn_msgs), 1)
warn_msg = warn_msgs[0]
self.assertIn(
"default_value method with arguments is deprecated",
str(warn_msg.message),
)
_, _, this_module = __name__.rpartition(".")
self.assertIn(this_module, warn_msg.filename)
def test_bad_default_value_type(self):
trait = CTrait(TraitKind.trait)
with self.assertRaises(ValueError):
trait.set_default_value(-1, None)
with self.assertRaises(ValueError):
trait.set_default_value(MAXIMUM_DEFAULT_VALUE_TYPE + 1, None)
def test_is_property(self):
trait = CTrait(TraitKind.trait)
self.assertFalse(trait.is_property)
trait.property_fields = (getter, setter, validator)
self.assertTrue(trait.is_property)
with self.assertRaises(AttributeError):
trait.is_property = False
def test_get_set_property(self):
trait = CTrait(TraitKind.trait)
# Get the property, ensure None
self.assertIsNone(trait.property_fields)
def value_get(self):
return self.__dict__.get("_value", 0)
def value_set(self, value):
old_value = self.__dict__.get("_value", 0)
if value != old_value:
self._value = value
self.trait_property_changed("value", old_value, value)
# Set the callables
trait.property_fields = (value_get, value_set, None)
fget, fset, validate = trait.property_fields
self.assertIs(fget, value_get)
self.assertIs(fset, value_set)
self.assertIsNone(validate)
# Ensure that _get_property does not accept arguments.
with self.assertRaises(TypeError):
trait._get_property(fget)
def test_modify_delegate(self):
trait = CTrait(TraitKind.trait)
self.assertFalse(trait.modify_delegate)
trait.modify_delegate = True
self.assertTrue(trait.modify_delegate)
def test_setattr_original_value(self):
trait = CTrait(TraitKind.trait)
self.assertFalse(trait.setattr_original_value)
trait.setattr_original_value = True
self.assertTrue(trait.setattr_original_value)
def test_post_setattr_original_value(self):
trait = CTrait(TraitKind.trait)
self.assertFalse(trait.post_setattr_original_value)
trait.post_setattr_original_value = True
self.assertTrue(trait.post_setattr_original_value)
def test_is_mapped(self):
trait = CTrait(TraitKind.trait)
self.assertFalse(trait.is_mapped)
trait.is_mapped = True
self.assertTrue(trait.is_mapped)
def test_default_comparison_mode(self):
trait = CTrait(TraitKind.trait)
self.assertIsInstance(trait.comparison_mode, ComparisonMode)
self.assertEqual(trait.comparison_mode, ComparisonMode.equality)
def test_invalid_comparison_mode(self):
trait = CTrait(TraitKind.trait)
# comparison modes other than {0,1,2}
# are invalid
with self.assertRaises(ValueError):
trait.comparison_mode = -1
with self.assertRaises(ValueError):
trait.comparison_mode = 3
def test_comparison_mode_unchanged_if_invalid(self):
trait = CTrait(TraitKind.trait)
default_comparison_mode = trait.comparison_mode
self.assertNotEqual(default_comparison_mode, ComparisonMode.none)
trait.comparison_mode = ComparisonMode.none
with self.assertRaises(ValueError):
trait.comparison_mode = -1
self.assertEqual(trait.comparison_mode, ComparisonMode.none)
def test_comparison_mode_int(self):
trait = CTrait(TraitKind.trait)
trait.comparison_mode = 0
self.assertIsInstance(trait.comparison_mode, ComparisonMode)
self.assertEqual(trait.comparison_mode, ComparisonMode.none)
trait.comparison_mode = 1
self.assertIsInstance(trait.comparison_mode, ComparisonMode)
self.assertEqual(trait.comparison_mode, ComparisonMode.identity)
trait.comparison_mode = 2
self.assertIsInstance(trait.comparison_mode, ComparisonMode)
self.assertEqual(trait.comparison_mode, ComparisonMode.equality)
def test_comparison_mode_enum(self):
trait = CTrait(TraitKind.trait)
trait.comparison_mode = ComparisonMode.none
self.assertIsInstance(trait.comparison_mode, ComparisonMode)
self.assertEqual(trait.comparison_mode, ComparisonMode.none)
trait.comparison_mode = ComparisonMode.identity
self.assertIsInstance(trait.comparison_mode, ComparisonMode)
self.assertEqual(trait.comparison_mode, ComparisonMode.identity)
trait.comparison_mode = ComparisonMode.equality
self.assertIsInstance(trait.comparison_mode, ComparisonMode)
self.assertEqual(trait.comparison_mode, ComparisonMode.equality)
def test_assign_post_setattr_none(self):
old_value = "old_value"
new_value = "new_value"
def post_setattr_func(obj, name, value):
obj.output_variable = value
trait = CTrait(TraitKind.trait)
class TestClass(HasTraits):
atr = trait
trait.post_setattr = post_setattr_func
self.assertIsNotNone(trait.post_setattr)
obj = TestClass()
obj.atr = old_value
self.assertEqual(old_value, obj.output_variable)
trait.post_setattr = None
self.assertIsNone(trait.post_setattr)
obj.atr = new_value
self.assertEqual(old_value, obj.output_variable)
trait.post_setattr = post_setattr_func
obj.atr = old_value
self.assertEqual(old_value, obj.output_variable)
with self.assertRaises(ValueError):
trait.post_setattr = "Invalid"
def test_unsafe_set_value(self):
# Regression test for enthought/traits#832. The test below causes
# a segfault (on at least some systems) before the fix.
def get_handler_refcount():
sys.getrefcount(tr.handler)
# Anything that we can create a weakref to works here.
weakrefable_object = {1, 2, 3}
tr = CTrait(0)
tr.handler = Any(weakrefable_object)
finalizer = weakref.finalize(weakrefable_object, get_handler_refcount)
del weakrefable_object
# Reassigning the handler should trigger the finaliser.
self.assertTrue(finalizer.alive)
tr.handler = None
self.assertFalse(finalizer.alive)
self.assertIsNone(tr.handler)
def test_invalid_initialization(self):
with self.assertRaises(TraitError):
CTrait(max(TraitKind) + 1)
def test_initialization_with_keywords_fails(self):
with self.assertRaises(TraitError):
CTrait(kind=0)
def test_default_initialization(self):
ctrait = CTrait()
validate = unittest.mock.MagicMock(return_value="baz")
ctrait.set_validate(validate)
class Foo(HasTraits):
bar = ctrait
bar_changed = List
def _bar_changed(self, new):
self.bar_changed.append(new)
foo = Foo()
self.assertEqual(len(foo.bar_changed), 0)
foo.bar = 1
validate.assert_called_once_with(foo, "bar", 1)
self.assertEqual(foo.bar, "baz")
self.assertEqual(len(foo.bar_changed), 1)
self.assertEqual(foo.bar_changed[0], "baz")
def test_failed_attribute_access(self):
# Double-underscore names are special-cased.
non_dunder_names = [
"non_existent",
"__non_existent",
"non_existent__",
"_non_existent_",
"__a__b_",
"_",
]
dunder_names = [
"__package__",
"__a__",
"____",
"___",
"__",
]
ctrait = CTrait(0)
for name in non_dunder_names:
with self.subTest(name=name):
self.assertIsNone(getattr(ctrait, name))
for name in dunder_names:
with self.subTest(name=name):
with self.assertRaises(AttributeError):
getattr(ctrait, name)
def test_exception_from_attribute_access(self):
# Regression test for enthought/traits#946.
# Danger: we're (temporarily) mutating global state here! Check that
# we're not touching an attribute that actually exists.
self.assertFalse(hasattr(CTrait, "badattr_test"))
CTrait.badattr_test = property(lambda self: 1 / 0)
try:
ctrait = CTrait(0)
with self.assertRaises(ZeroDivisionError):
ctrait.badattr_test
finally:
del CTrait.badattr_test
class TestCTraitNotifiers(unittest.TestCase):
""" Test calling trait notifiers and object notifiers. """
def test_notifiers_empty(self):
class Foo(HasTraits):
x = Int()
foo = Foo(x=1)
x_ctrait = foo.trait("x")
self.assertEqual(x_ctrait._notifiers(True), [])
def test_notifiers_on_trait(self):
class Foo(HasTraits):
x = Int()
def _x_changed(self):
pass
foo = Foo(x=1)
x_ctrait = foo.trait("x")
tnotifiers = x_ctrait._notifiers(True)
self.assertEqual(len(tnotifiers), 1)
notifier, = tnotifiers
self.assertEqual(notifier.handler, Foo._x_changed)
|