示例#1
0
文件: refresh.py 项目: fspaolo/code
    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')
示例#2
0
文件: refresh.py 项目: fspaolo/code
    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')
示例#3
0
    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')