/usr/lib/python2.7/dist-packages/traits/tests/test_cythonized_traits.py is in python-traits 4.6.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | """ Test some usage of Trait classes when the code is cythonized.
The tests reflects some of the patterns needed in different applications. They
probably don't cover all of the user case.
Each test case is written as if the test code was in a separate module then
compiled with Cython Inline before evaluation the produced object behaves
properly.
The tests need a Cython version > 0.19 and a compiler.
"""
try:
import cython
no_cython = False
except ImportError:
no_cython = True
from ..testing.unittest_tools import unittest, UnittestTools
def has_no_compiler():
if no_cython:
return True
# Easy way to check if we have access to a compiler
code = "return 1+1"
try:
cython.inline(code)
return False
except:
return True
def cython_version():
if no_cython:
return None
from Cython.Compiler.Version import version
return tuple(int(v) for v in version.split('.'))
SKIP_TEST = has_no_compiler()
# Cython 0.19 implementation of safe_type fails while parsing some of the
# code. We provide a very basic implementation that always returns object
# (we don't need any particular optimizations)
def _always_object_type(arg, context):
return 'object'
class CythonizedTraitsTestCase(unittest.TestCase, UnittestTools):
@unittest.skipIf(SKIP_TEST, 'Missing Cython and/or compiler')
def test_simple_default_methods(self):
code = """
from traits.api import HasTraits, Str
class Test(HasTraits):
name = Str
def _name_default(self):
return 'Joe'
return Test()
"""
obj = cython.inline(code)
self.assertEqual(obj.name, 'Joe')
@unittest.skipIf(SKIP_TEST, 'Missing Cython and/or compiler')
def test_basic_events(self):
code = """
from traits.api import HasTraits, Str
class Test(HasTraits):
name = Str
return Test()
"""
obj = cython.inline(code)
with self.assertTraitChanges(obj, 'name', count=1):
obj.name = 'changing_name'
@unittest.skipIf(SKIP_TEST, 'Missing Cython and/or compiler')
def test_on_trait_static_handlers(self):
code = """
from traits.api import HasTraits, Str, Int
class Test(HasTraits):
name = Str
value = Int
def _name_changed(self):
self.value += 1
return Test()
"""
obj = cython.inline(code, get_type=_always_object_type, force=True)
with self.assertTraitChanges(obj, 'value', count=1):
obj.name = 'changing_name'
self.assertEqual(obj.value, 1)
@unittest.skipIf(SKIP_TEST, 'Missing Cython and/or compiler')
def test_on_trait_on_trait_change_decorator(self):
code = """
from traits.api import HasTraits, Str, Int, on_trait_change
class Test(HasTraits):
name = Str
value = Int
@on_trait_change('name')
def _update_value(self):
self.value += 1
return Test()
"""
obj = cython.inline(code, get_type=_always_object_type, force=True,
locals={}, globals={})
with self.assertTraitChanges(obj, 'value', count=1):
obj.name = 'changing_name'
self.assertEqual(obj.value, 1)
@unittest.skipIf(SKIP_TEST, 'Missing Cython and/or compiler')
def test_on_trait_properties(self):
code = """
from traits.api import HasTraits, Str, Int, Property, cached_property
class Test(HasTraits):
name = Str
name_len = Property(depends_on='name')
@cached_property
def _get_name_len(self):
return len(self.name)
return Test()
"""
obj = cython.inline(code, get_type=_always_object_type, force=True,
locals={}, globals={})
self.assertEqual(obj.name_len, len(obj.name))
# Assert dependency works
obj.name = 'Bob'
self.assertEqual(obj.name_len, len(obj.name))
@unittest.skipIf(SKIP_TEST, 'Missing Cython and/or compiler')
def test_on_trait_properties_with_standard_getter(self):
code = """
from traits.api import HasTraits, Str, Int, Property
class Test(HasTraits):
name = Str
def _get_name_length(self):
return len(self.name)
name_len = Property(_get_name_length)
return Test()
"""
obj = cython.inline(code, get_type=_always_object_type, force=True,
locals={}, globals={})
self.assertEqual(obj.name_len, len(obj.name))
# Assert dependency works
obj.name = 'Bob'
self.assertEqual(obj.name_len, len(obj.name))
@unittest.skipIf(SKIP_TEST, 'Missing Cython and/or compiler')
def test_on_trait_aliasing(self):
code = """
from traits.api import HasTraits, Str, Int, Property
def Alias(name):
def _get_value(self):
return getattr(self, name)
def _set_value(self, value):
return setattr(self, name, value)
return Property(_get_value, _set_value)
class Test(HasTraits):
name = Str
funky_name = Alias('name')
return Test()
"""
obj = cython.inline(code, get_type=_always_object_type, force=True,
locals={}, globals={})
self.assertEqual(obj.funky_name, obj.name)
# Assert dependency works
obj.name = 'Bob'
self.assertEqual(obj.funky_name, obj.name)
@unittest.skipIf(SKIP_TEST, 'Missing Cython and/or compiler')
def test_on_trait_aliasing_different_scope(self):
code = """
from traits.api import HasTraits, Str, Int, Property
def _get_value(self, name):
return getattr(self, 'name')
def _set_value(self, name, value):
return setattr(self, 'name', value)
class Test(HasTraits):
name = Str
funky_name = Property(_get_value, _set_value)
return Test()
"""
obj = cython.inline(code, get_type=_always_object_type)
self.assertEqual(obj.funky_name, obj.name)
# Assert dependency works
obj.name = 'Bob'
self.assertEqual(obj.funky_name, obj.name)
@unittest.skipIf(SKIP_TEST, 'Missing Cython and/or compiler')
def test_on_trait_lambda_failure(self):
# Lambda function are converted like builtins when cythonized which
# causes the following code to fail
code = """
from traits.api import HasTraits, Str, Int, Property
def Alias(name):
return Property(
lambda obj: getattr(obj, name),
lambda obj, value: setattr(obj, name, value)
)
class Test(HasTraits):
name = Str
funky_name = Alias('name')
return Test()
"""
try:
cython.inline(code, get_type=_always_object_type, force=True,
locals={}, globals={})
except:
# We suppose we have an exception. Because of the usage of the
# skipIf decorator on the test, we can't use an expectedFailure
# decorator as they don't play well together.
pass
else:
self.fail(
'Unexpected results. Cython was not managing lambda as regular'
' functions. Behaviour changed ...'
)
|