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 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
|
#------------------------------------------------------------------------------
# Copyright (c) 2005, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/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!
#
# Author: Enthought, Inc.
# Description: <Enthought util package component>
#------------------------------------------------------------------------------
# NOTE:
# This file, `refresh.py` can not be run by nosetests directly.
# It is run through `test_spawner.py`, see notes in there.
import unittest
import gc
import os
import sys
import pickle
from types import ClassType, FunctionType
from enthought.util.refresh import refresh
def create_module(name, code):
# get rid of pyc file
remove_module(name)
f = open(name+'.py','w')
f.write(code)
f.close()
def remove_module(name):
# get rid of pyc file
try:
os.remove(name+'.pyc')
os.remove(name+'.py')
except:
pass
class RefreshTestCase(unittest.TestCase):
def test_old_style_class(self):
module_name = 'foo_test'
try:
# clean out any existing versions of foo_test in memory
try:
del sys.modules[module_name]
except:
pass
f1 = """class Foo:
def method1(self):
return 0
def method2(self):
return 0
"""
create_module(module_name, f1)
exec "import %s" % module_name
exec "from %s import Foo" % module_name
foo = Foo()
assert(foo.method1() == 0)
f2 = """class Foo:
def method1(self):
return 1
def method2(self):
return 1
"""
create_module(module_name, f2)
refresh()
assert(foo.method1() == 1)
self.single_version_check("Foo")
finally:
remove_module(module_name)
def test_new_style_class(self):
module_name = 'foo_test'
try:
# clean out any existing versions of foo_test in memory
try:
del sys.modules[module_name]
except:
pass
f1 = """class FooNew(object):
def method1(self):
return 0
def method2(self):
return 0
"""
create_module(module_name, f1)
exec "import %s" % module_name
exec "from %s import FooNew" % module_name
foo = FooNew()
assert(foo.method1() == 0)
f2 = """class FooNew(object):
def method1(self):
return 1
def method2(self):
return 1
"""
create_module(module_name, f2)
refresh()
assert(foo.method1() == 1)
#self.single_version_check("FooNew")
finally:
remove_module(module_name)
def test_traits_class(self):
module_name = 'foo_test'
try:
# clean out any existing versions of foo_test in memory
try:
del sys.modules[module_name]
except:
pass
f1 = "from enthought.traits.api import HasTraits, Float\n" + \
"""class FooTraits(HasTraits):
bar = Float(1.0)
def method1(self):
return 0
def method2(self):
return 0
"""
create_module(module_name, f1)
exec "import %s" % module_name
exec "from %s import FooTraits" % module_name
foo = FooTraits()
assert(foo.method1() == 0)
f2 = "from enthought.traits.api import HasTraits, Int\n" + \
"""class FooTraits(HasTraits):
bar = Int(2)
def method1(self):
return 1
def method2(self):
return 1
"""
create_module(module_name, f2)
refresh()
assert(foo.method1() == 1)
# classes derived from HasTraits seem to continue to have
# a copy hanging around. This doesn't really cause problems,
# it is just a little untidy.
#self.single_version_check("FooTraits")
finally:
remove_module(module_name)
def test_traits_instance(self):
module_name = 'foo_test'
try:
# clean out any existing versions of foo_test in memory
try:
del sys.modules[module_name]
except:
pass
f1 = "from enthought.traits.api import HasTraits, Float\n" + \
"""class FooTraits(HasTraits):
bar = Float(1.0)
def method1(self):
return 0
def method2(self):
return 0
"""
create_module(module_name, f1)
exec "import %s" % module_name
exec "from %s import FooTraits" % module_name
foo = FooTraits()
assert(foo.method1() == 0)
f2 = "from enthought.traits.api import HasTraits, Int\n" + \
"""class FooTraits(HasTraits):
bar = Int(2)
def method1(self):
return 1
def method2(self):
return 1
"""
create_module(module_name, f2)
refresh()
assert(foo.method1() == 1)
# classes derived from HasTraits seem to continue to have
# a copy hanging around. This doesn't really cause problems,
# it is just a little untidy.
#self.single_version_check("FooTraits")
finally:
remove_module(module_name)
def test_inheritance_class(self):
module_name = 'foo_test'
try:
# clean out any existing versions of foo_test in memory
try:
del sys.modules[module_name]
except:
pass
f0 = """class Bar:
def method1(self):
return -1
def method2(self):
return -1
"""
create_module('bar_test', f0)
f1 = "import bar_test\n" + \
"""class FooSub(bar_test.Bar):
def method1(self):
return 0
def method2(self):
return 0
"""
create_module(module_name, f1)
from bar_test import Bar
bar = Bar()
assert(bar.method1() == -1)
exec "import %s" % module_name
exec "from %s import FooSub" % module_name
foo = FooSub()
assert(foo.method1() == 0)
f2 = """class FooSub:
def method1(self):
return 1
def method2(self):
return 1
"""
create_module(module_name, f2)
refresh()
# test method calls
assert(foo.method1() == 1)
assert(bar.method1() == -1)
# isinstance
assert(isinstance(foo, Bar))
assert(isinstance(foo, FooSub))
assert(isinstance(bar, Bar))
# classes derived from object seem to continue to have
# a copy hanging around. This doesn't really cause problems,
# it is just a little untidy.
#self.single_class_check()
finally:
remove_module(module_name)
remove_module('bar_test')
def test_inheritance_class2(self):
""" Change the base class definition and make sure that
existing subclasses still behaves like the new
base class.
"""
module_name = 'foo_test2'
try:
# clean out any existing versions of foo_test in memory
try:
del sys.modules[module_name]
except:
pass
f0 = """class Bar:
def method1(self):
return -1
def method2(self):
return -1
"""
create_module('bar_test2', f0)
f1 = "from bar_test2 import Bar\n" + \
"""class FooSub(Bar):
def method1(self):
#print "base", self.__class__.__bases__
#print "Bar", repr(Bar)
#print "method1 im_class", repr(Bar.method1.im_class)
#print 'is instance:', isinstance(self, Bar)
result = Bar.method1(self)
return result
def method2(self):
return 0
"""
create_module(module_name, f1)
from bar_test2 import Bar
bar = Bar()
assert(bar.method1() == -1)
exec "import %s" % module_name
exec "from %s import FooSub" % module_name
foo = FooSub()
assert(foo.method1() == -1)
f2 = """class Bar:
def method1(self):
return -2
def method2(self):
return -1
"""
create_module('bar_test2', f2)
refresh()
assert(foo.method1() == -2)
assert(bar.method1() == -2)
# classes derived from object seem to continue to have
# a copy hanging around. This doesn't really cause problems,
# it is just a little untidy.
#self.single_class_check()
finally:
remove_module(module_name)
remove_module('bar_test2')
def test_pickle(self):
# not sure this is really a worth while test. It was added
# based on some errors I (eric) was getting when testing in
# envisage, but I am now not sure that the errors were refresh()
# related. Regardless, I have left the test here since it
# doesn't do any harm, and I may need it in the future.
module_name = 'foo_test'
try:
# clean out any existing versions of foo_test in memory
try:
del sys.modules[module_name]
except:
pass
f1 = """class FooNew(object):
def method1(self):
return 0
def method2(self):
return 0
"""
create_module(module_name, f1)
exec "import %s" % module_name
exec "from %s import FooNew" % module_name
foo = FooNew()
assert(foo.method1() == 0)
f2 = """class FooNew(object):
def method1(self):
return 1
def method2(self):
return 1
"""
create_module(module_name, f2)
refresh()
assert(foo.method1() == 1)
foo2 = pickle.loads(pickle.dumps(foo))
assert(foo2.method1() == 1)
#self.single_version_check("FooNew")
finally:
remove_module(module_name)
def test_function(self):
module_name = 'foo_test'
try:
# clean out any existing versions of foo_test in memory
try:
del sys.modules[module_name]
except:
pass
f1 = """def foo(): return 0 """
create_module(module_name, f1)
exec "import %s" % module_name
exec "from %s import foo" % module_name
assert(foo() == 0)
f2 = """def foo():
return 1
"""
create_module(module_name, f2)
refresh()
assert(foo() == 1)
# fixme: this is failing with our new reload scheme
self.single_version_check("foo", FunctionType)
finally:
remove_module(module_name)
def single_version_check(self, name, types=(ClassType, type)):
# Force garbage collection to ensure Python cleans up all
# references to the reloaded class
gc.collect()
# now search the objects and make sure only one version of the
# given class exists.
gc_objs = gc.get_objects()
versions = [obj for obj in gc_objs
if isinstance(obj, types) and
obj.__name__ == name]
#there can be only one...
assert len(versions) == 1
if __name__ == "__main__":
unittest.main()
|