Example #1
0
def test_slots_data_member_change():
    origin_file = """
class A(object):
    __slots__ = ["m_member1", "m_member2"]
    def __init__(self):
        self.m_member1 = None
        self.m_member2 = None
    """

    new_file = """
class A(object):
    __slots__ = ["m_member1", "m_member3"]
    def __init__(self):
        self.m_member1 = None
        self.m_member3 = None
    """

    file_name = "scripts/slots_data_member_change.py"
    ReplaceScripts(file_name, origin_file)
    import scripts.slots_data_member_change

    ReplaceScripts(file_name, new_file)

    with pytest.raises(Exception) as excinfo:
        xreload.xreload(scripts.slots_data_member_change.__name__)

    assert "data member" in str(excinfo.value)
Example #2
0
def test_before_slot_class():
    origin_file = """
class A(object):
    __slots__ = ["m_member", "Foo"]
    def __init__(self, member):
        self.m_member = member

    def Foo(self):
        pass
    """

    new_file = """
class A(object):
    def __init__(self, member):
        self.m_member = member

    def Foo(self):
        pass
    """

    file_name = "scripts/before_slot_class.py"
    ReplaceScripts(file_name, origin_file)
    import scripts.before_slot_class

    ReplaceScripts(file_name, new_file)

    with pytest.raises(Exception) as excinfo:
        xreload.xreload(scripts.before_slot_class.__name__)

    assert excinfo.match(r"__slots__.*before.*after modified")
Example #3
0
def upgrade():
    '''Performs code upgrade, beware the bugs!'''
    print "reloading"
    import xreload
    reload(xreload)
    import bot_module
    xreload.xreload(bot_module)
    return
Example #4
0
def test_slots_update_class():
    origin_file = """
class A(object):
    __slots__ = ["m_member", "Foo", "DeleteMethod", "Static"]

    def __init__(self):
        self.m_member = 1

    def Foo(self):
        return 'before'

    def DeleteMethod(self):
        pass

    @staticmethod
    def Static():
        pass
    """

    new_file = """
class A(object):
    __slots__ = ["m_member", "Foo", "Bar"]

    def __init__(self):
        self.m_member = 2

    def Foo(self):
        return 'after'

    @classmethod
    def Bar(cls):
        pass
    """

    file_name = "scripts/slots_update_class.py"
    ReplaceScripts(file_name, origin_file)
    import scripts.slots_update_class
    module = scripts.slots_update_class

    assert hasattr(module.A, 'DeleteMethod')
    assert isinstance(module.A.DeleteMethod, types.MethodType)
    assert hasattr(module.A, 'Foo')
    assert hasattr(module.A, 'm_member')

    ReplaceScripts(file_name, new_file)
    xreload.xreload(module.__name__)

    assert hasattr(module.A, 'Foo')
    assert isinstance(module.A.Foo, types.MethodType)
    a = module.A()
    assert a.m_member == 2
    assert a.Foo() == 'after'

    assert hasattr(module.A, 'Bar')
    assert isinstance(module.A.__dict__['Bar'], classmethod)
    assert not hasattr(module.A, 'DeleteMethod')
    assert hasattr(module.A, 'm_member')
    assert not hasattr(module.A, 'Static')
Example #5
0
def test_global_values():

    origin_file = \
    """
g_user_table = {}
def LoadUsers():
    global g_user_table
    g_user_table[1234] = 'Alice'
    g_user_table[5678] = 'Bob'

manager = None
def GetManagerInstance():
    global manager
    if not manager:
        manager = "God"
    return manager

CONSTANT_VALUE = "I don't know"
    """

    new_file = \
    """
g_user_table = {}
def LoadUsers():
    global g_user_table
    g_user_table[1234] = 'Alice'
    g_user_table[5678] = 'Bob'

manager = None
def GetManagerInstance():
    global manager
    if not manager:
        manager = "God"
    return manager

CONSTANT_VALUE = 31415926  # compare to its origin value which is a different type, string
    """

    file_name = "scripts/global_values.py"
    ReplaceScripts(file_name, origin_file)
    import scripts.global_values
    module = scripts.global_values

    module.LoadUsers()
    module.GetManagerInstance()

    assert module.g_user_table != {}
    assert module.manager is not None
    assert module.CONSTANT_VALUE == "I don't know"

    ReplaceScripts(file_name, new_file)
    xreload.xreload(module.__name__)

    assert module.g_user_table != {}
    assert module.manager is not None
    assert module.CONSTANT_VALUE == 31415926
Example #6
0
 def pressbutton(self, button, event):
   self.xml.update()
   if event == "Reload code":
     import yai, xmltree
     for module in [yai, xmltree, texttree]:
       xreload(module)
   elif event == "Set":
     obj = self.xml.selection[0]
     obj[self.editor.prop] = self.editor.get_text()
     self.xml.update()
Example #7
0
def upgrade():
    """Performs code upgrade, beware the bugs!"""
    print "reloading"
    import xreload

    reload(xreload)
    import bot_module

    xreload.xreload(bot_module)
    return
Example #8
0
def test_pyc():
    import os
    import py_compile

    import scripts.new_style_class
    path = "scripts/new_style_class.py"
    py_compile.compile(path)
    os.remove(path)
    assert not os.path.exists(path)
    pyc_path = path + 'c'
    assert os.path.exists(pyc_path)

    xreload.xreload("scripts.new_style_class")
    op = scripts.new_style_class.Operate(1, 1)
Example #9
0
def reload_code():
    global MOD_TIMES
    reloaded = []
    for path, time, module in check_mod_times():
        module = xreload.xreload(module)
        MOD_TIMES[path] = (time, module)
        reloaded.append(path)
    return reloaded
Example #10
0
def reload_code():
    global MOD_TIMES
    reloaded = []
    for path, time, module in check_mod_times():
        module = xreload.xreload(module)
        MOD_TIMES[path] = (time, module)
        reloaded.append(path)
    return reloaded
Example #11
0
def eimport(modname, dir, use_xreload=False):
    """Import module MODNAME from directory DIR at the head of the search path.
    NB doesn't load from DIR if MODNAME shadows a system module."""
    from __main__ import __dict__

    if _emacs_py_debug:
        print "Loading module %r from directory %r" % (modname, dir)

    old_path = sys.path
    sys.path = [dir]  # XXX for now + sys.path
    try:
        if True:  # try:
            already_loaded = False
            try:
                # easiest way to peel back the package structure
                # print len(__dict__.keys())
                mod = eval(modname, __dict__)
                already_loaded = True
            except:
                print "ACK!"
                already_loaded = False

            if already_loaded:
                if use_xreload or _emacs_py_xreload:
                    if _emacs_py_debug:
                        print "xreload-ing %s" % mod
                    xreload.xreload(mod, path=[dir])
                else:
                    if _emacs_py_debug:
                        print "reload-ing %s" % mod
                    reload(mod)
            else:
                if _emacs_py_debug:
                    print "__import__(%r)" % modname
                from __main__ import __dict__
                __dict__[modname] = __import__(modname)


# 	except:
#             traceback.print_exc()
#             raise
    finally:
        sys.path = old_path
Example #12
0
def eimport (modname, dir, use_xreload=False):
    """Import module MODNAME from directory DIR at the head of the search path.
    NB doesn't load from DIR if MODNAME shadows a system module."""
    from __main__ import __dict__

    if _emacs_py_debug:
        print "Loading module %r from directory %r" % (modname, dir)

    old_path = sys.path
    sys.path = [dir] # XXX for now + sys.path
    try:
	if True: # try:
            already_loaded = False
            try:
                # easiest way to peel back the package structure
                # print len(__dict__.keys())
                mod = eval(modname, __dict__)
                already_loaded = True
            except:
                print "ACK!"
                already_loaded = False            

            if already_loaded:
                if use_xreload or _emacs_py_xreload:
                    if _emacs_py_debug:
                        print "xreload-ing %s" % mod
                    xreload.xreload (mod, path=[dir])
                else:
                    if _emacs_py_debug:
                        print "reload-ing %s" % mod
                    reload (mod)
	    else:
                if _emacs_py_debug:
                    print "__import__(%r)" % modname
                from __main__ import __dict__
		__dict__[modname] = __import__ (modname)
# 	except:
#             traceback.print_exc()
#             raise
    finally:
	sys.path = old_path
Example #13
0
def test_function():

    origin_file = \
    """
def Foo():
    return 13579
    """

    new_file = \
    """
def Foo():
    return 97531
    """

    file_name = "scripts/functions.py"
    ReplaceScripts(file_name, origin_file)
    import scripts.functions
    module = scripts.functions

    assert module.Foo() == 13579

    ReplaceScripts(file_name, new_file)
    xreload.xreload(module.__name__)
    assert module.Foo() == 97531
Example #14
0
    Cfoo = C.foo
    Cbar = C.bar
    Cstomp = C.stomp

    b = C()
    bfoo = b.foo
    b.foo()
    bfoo()
    Cfoo(b)
    Cbar()
    Cstomp()

    #modify mod and reload
    count = 0
    while count < 1:
        count += 1
        make_mod(repl="0", subst=str(count))
        xreload(x)

        C = x.C
        Cfoo = C.foo
        Cbar = C.bar
        Cstomp = C.stomp
        b = C()
        bfoo = b.foo
        b.foo()
        bfoo()
        Cfoo(b)
        Cbar()
        Cstomp()
Example #15
0
 def reload_code(self):
     import inspect
     import xreload
     mod = inspect.getmodule(self)
     x = xreload.xreload(mod)
     print("Reloading from code", mod, x)
Example #16
0
def reload_script():
    return xreload(sys.modules[__name__], new_annotations={'RELOADING': True})
Example #17
0
def test_new_style_class():

    origin_file = \
    """
class Operate(object):
    def __init__(self, x, y):
        self.m_x = x
        self.m_y = y

    def add(self):
        return self.m_x + self.m_y + 1

    @classmethod
    def minus(cls, x, y):
        return x - y

    @staticmethod
    def multiply(x, y):
        return x * y

    def to_bo_delete_method(self):
        pass
    """

    new_file = \
    """
class Operate(object):
    def __init__(self, x, y):
        self.m_x = x
        self.m_y = y

    def add(self):
        return self.m_x + self.m_y

    @classmethod
    def minus(cls, x, y):
        return y - x

    @staticmethod
    def multiply(x, y):
        return x * y * 2

    def divide(self):
        return self.m_x / self.m_y
    """

    file_name = "scripts/new_style_class.py"
    ReplaceScripts(file_name, origin_file)
    import scripts.new_style_class
    module = scripts.new_style_class

    x = 17
    y = 3

    op = module.Operate(x, y)
    assert op.add() == 21
    assert module.Operate.minus(x, y) == 14
    assert module.Operate.multiply(x, y) == 51
    assert hasattr(module.Operate, "to_bo_delete_method")

    Operate_instance_add = op.add
    Operate_minus = module.Operate.minus
    Operate_multiply = module.Operate.multiply

    ReplaceScripts(file_name, new_file)
    xreload.xreload(module.__name__)

    new_op = module.Operate(x, y)

    assert Operate_instance_add == op.add

    assert op.add() == 20
    assert Operate_minus(x, y) == -14
    assert Operate_multiply(x, y) == 102
    assert op.divide() == 5
    assert not hasattr(module.Operate, "to_bo_delete_method")
Example #18
0
    Cbar = C.bar
    Cstomp = C.stomp

    b = C()
    bfoo = b.foo  
    b.foo() 
    bfoo() 
    Cfoo(b)
    Cbar()
    Cstomp()

    #modify mod and reload
    count = 0
    while count < 1:
	count += 1
	make_mod(repl="0", subst=str(count)) 
	xreload(x) 

	C = x.C
	Cfoo = C.foo
	Cbar = C.bar
	Cstomp = C.stomp
        b = C()
        bfoo = b.foo  
	b.foo() 
	bfoo() 
	Cfoo(b)
	Cbar()
	Cstomp()

Example #19
0
def test_derived_class():

    origin_file = """
class A(object):
    def __init__(self):
        pass

class B(A):
    def __init__(self):
        super(B, self).__init__()


    """

    new_file = """
class A(object):
    def __init__(self):
        pass

class B(A):
    def __init__(self):
        super(B, self).__init__()

class C(B):
    def __init__(self):
        super(C, self).__init__()


def Foo():
    a = A()
    b = B()
    c = C()

    """

    other_file = """

class E(object):
    def __init__(self):
        super(E, self).__init__()

    """

    other_new_file = """
import base_derived_classes

class E(object):

    def __init__(self):
        super(E, self).__init__()

class D(base_derived_classes.B, E):
    def __init__(self):
        super(D, self).__init__()

def Foo():
    b = base_derived_classes.B()
    d = D()
    """

    file_name = "scripts/base_derived_classes.py"
    ReplaceScripts(file_name, origin_file)
    import scripts.base_derived_classes

    other_name = "scripts/other.py"
    ReplaceScripts(other_name, other_file)
    import scripts.other

    module = scripts.base_derived_classes
    ReplaceScripts(file_name, new_file)
    ReplaceScripts(other_name, other_new_file)

    xreload.xreload(module.__name__)
    xreload.xreload(scripts.other.__name__)

    module.Foo()
    scripts.other.Foo()