def test_cache(self): import clr ArrayList = clr.load_cli_class(self.mscorlib, 'System.Collections', 'ArrayList') ArrayList2 = clr.load_cli_class(self.mscorlib, 'System.Collections', 'ArrayList') assert ArrayList is ArrayList2
def test_None_as_null(self): import clr ArrayList = clr.load_cli_class('System.Collections', 'ArrayList') Hashtable = clr.load_cli_class('System.Collections', 'Hashtable') x = ArrayList() x.Add(None) assert x[0] is None y = Hashtable() assert y["foo"] is None
def test_None_as_null(self): import clr ArrayList = clr.load_cli_class(self.mscorlib, 'System.Collections', 'ArrayList') Hashtable = clr.load_cli_class(self.mscorlib, 'System.Collections', 'Hashtable') x = ArrayList() x.Add(None) assert x[0] is None y = Hashtable() assert y["foo"] is None
def test_getitem(self): import clr ArrayList = clr.load_cli_class(self.mscorlib, 'System.Collections', 'ArrayList') obj = ArrayList() obj.Add(42) assert obj[0] == 42
def test_unboundmethod(self): import clr ArrayList = clr.load_cli_class(self.mscorlib, 'System.Collections', 'ArrayList') obj = ArrayList() ArrayList.Add(obj, 42) assert obj.get_Item(0) == 42
def test_GetEnumerator(self): import clr ArrayList = clr.load_cli_class(self.mscorlib, 'System.Collections', 'ArrayList') x = ArrayList() enum = x.GetEnumerator() assert enum.MoveNext() is False
def test_float_conversion(self): import clr ArrayList = clr.load_cli_class('System.Collections', 'ArrayList') obj = ArrayList() obj.Add(42.0) item = obj.get_Item(0) assert isinstance(item, float)
def test_static_property(self): import clr import os Environment = clr.load_cli_class('System', 'Environment') assert Environment.CurrentDirectory == os.getcwd() Environment.CurrentDirectory == '/' assert Environment.CurrentDirectory == os.getcwd()
def test_string_wrapping(self): import clr ArrayList = clr.load_cli_class('System.Collections', 'ArrayList') x = ArrayList() x.Add("bar") s = x[0] assert s == "bar"
def test_generic_class_typeerror(self): import clr ListInt = clr.load_cli_class(self.mscorlib, "System.Collections.Generic", "List`1[System.Int32]") x = ListInt() raises(TypeError, x.Add, "test")
def test_ArrayList(self): import clr ArrayList = clr.load_cli_class('System.Collections', 'ArrayList') obj = ArrayList() obj.Add(42) obj.Add(43) total = obj.get_Item(0) + obj.get_Item(1) assert total == 42+43
def test_static_property(self): import clr import os Environment = clr.load_cli_class(self.mscorlib, 'System', 'Environment') assert Environment.CurrentDirectory == os.getcwd() Environment.CurrentDirectory == '/' assert Environment.CurrentDirectory == os.getcwd()
def test_property(self): import clr ArrayList = clr.load_cli_class('System.Collections', 'ArrayList') obj = ArrayList() obj.Add(42) assert obj.Count == 1 obj.Capacity = 10 assert obj.Capacity == 10
def test_overload(self): import clr ArrayList = clr.load_cli_class('System.Collections', 'ArrayList') obj = ArrayList() for i in range(10): obj.Add(i) assert obj.IndexOf(7) == 7 assert obj.IndexOf(7, 0, 5) == -1
def test_float_conversion(self): import clr ArrayList = clr.load_cli_class(self.mscorlib, 'System.Collections', 'ArrayList') obj = ArrayList() obj.Add(42.0) item = obj.get_Item(0) assert isinstance(item, float)
def test_string_wrapping(self): import clr ArrayList = clr.load_cli_class(self.mscorlib, 'System.Collections', 'ArrayList') x = ArrayList() x.Add("bar") s = x[0] assert s == "bar"
def test_overload(self): import clr ArrayList = clr.load_cli_class(self.mscorlib, 'System.Collections', 'ArrayList') obj = ArrayList() for i in range(10): obj.Add(i) assert obj.IndexOf(7) == 7 assert obj.IndexOf(7, 0, 5) == -1
def test_ArrayList(self): import clr ArrayList = clr.load_cli_class(self.mscorlib, 'System.Collections', 'ArrayList') obj = ArrayList() obj.Add(42) obj.Add(43) total = obj.get_Item(0) + obj.get_Item(1) assert total == 42 + 43
def test_staticmethod(self): import clr Math = clr.load_cli_class(self.mscorlib, 'System', 'Math') res = Math.Abs(-42) assert res == 42 assert type(res) is int res = Math.Abs(-42.0) assert res == 42.0 assert type(res) is float
def test_staticmethod(self): import clr Math = clr.load_cli_class('System', 'Math') res = Math.Abs(-42) assert res == 42 assert type(res) is int res = Math.Abs(-42.0) assert res == 42.0 assert type(res) is float
def test_property(self): import clr ArrayList = clr.load_cli_class(self.mscorlib, 'System.Collections', 'ArrayList') obj = ArrayList() obj.Add(42) assert obj.Count == 1 obj.Capacity = 10 assert obj.Capacity == 10
def test_pass_opaque_arguments(self): import clr ArrayList = clr.load_cli_class('System.Collections', 'ArrayList') class Foo: pass obj = Foo() x = ArrayList() x.Add(obj) obj2 = x[0] assert obj is obj2
def test_load_generic_class(self): import clr ListInt = clr.load_cli_class(self.mscorlib, "System.Collections.Generic", "List`1[System.Int32]") x = ListInt() x.Add(42) x.Add(4) x.Add(4) sum = 0 for i in x: sum += i assert sum == 42+4+4
def test_iteration_stack(self): import clr Stack = clr.load_cli_class(self.mscorlib, 'System.Collections', 'Stack') obj = Stack() obj.Push(1) obj.Push(54) obj.Push(21) sum = 0 for i in obj: sum += i assert sum == 1+54+21
def test_iteration_arrayList(self): import clr ArrayList = clr.load_cli_class(self.mscorlib, 'System.Collections', 'ArrayList') x = ArrayList() x.Add(1) x.Add(2) x.Add(3) x.Add(4) sum = 0 for i in x: sum += i assert sum == 1+2+3+4
def test_bool_conversion(self): import clr ArrayList = clr.load_cli_class('System.Collections', 'ArrayList') obj = ArrayList() obj.Add(True) obj.Add(False) t = obj.get_Item(0) f = obj.get_Item(1) assert t and isinstance(t, bool) assert not f and isinstance(f, bool) obj.Add(42) assert obj.Contains(42)
def test_iteration_stack(self): import clr Stack = clr.load_cli_class(self.mscorlib, 'System.Collections', 'Stack') obj = Stack() obj.Push(1) obj.Push(54) obj.Push(21) sum = 0 for i in obj: sum += i assert sum == 1 + 54 + 21
def test_pass_opaque_arguments(self): import clr ArrayList = clr.load_cli_class(self.mscorlib, 'System.Collections', 'ArrayList') class Foo: pass obj = Foo() x = ArrayList() x.Add(obj) obj2 = x[0] assert obj is obj2
def test_iteration_arrayList(self): import clr ArrayList = clr.load_cli_class(self.mscorlib, 'System.Collections', 'ArrayList') x = ArrayList() x.Add(1) x.Add(2) x.Add(3) x.Add(4) sum = 0 for i in x: sum += i assert sum == 1 + 2 + 3 + 4
def test_load_generic_class(self): import clr ListInt = clr.load_cli_class(self.mscorlib, "System.Collections.Generic", "List`1[System.Int32]") x = ListInt() x.Add(42) x.Add(4) x.Add(4) sum = 0 for i in x: sum += i assert sum == 42 + 4 + 4
def test_bool_conversion(self): import clr ArrayList = clr.load_cli_class(self.mscorlib, 'System.Collections', 'ArrayList') obj = ArrayList() obj.Add(True) obj.Add(False) t = obj.get_Item(0) f = obj.get_Item(1) assert t and isinstance(t, bool) assert not f and isinstance(f, bool) obj.Add(42) assert obj.Contains(42)
def test_generic_dict(self): import clr genDictIntStr = clr.load_cli_class(self.mscorlib, "System.Collections.Generic", "Dictionary`2[System.Int32,System.String]") x = genDictIntStr() x[1] = "test" x[2] = "rest" assert x[1] == "test" assert x[2] == "rest" raises(TypeError, x.__setitem__, 3, 3) raises(TypeError, x.__setitem__, 4, 4.453) raises(TypeError, x.__setitem__, "test", 3)
def test_generic_dict(self): import clr genDictIntStr = clr.load_cli_class( self.mscorlib, "System.Collections.Generic", "Dictionary`2[System.Int32,System.String]") x = genDictIntStr() x[1] = "test" x[2] = "rest" assert x[1] == "test" assert x[2] == "rest" raises(TypeError, x.__setitem__, 3, 3) raises(TypeError, x.__setitem__, 4, 4.453) raises(TypeError, x.__setitem__, "test", 3)
def __getitem__(cls, type_or_tuple): import clr if isinstance(type_or_tuple, tuple): types = type_or_tuple else: types = (type_or_tuple,) namespace, generic_class = cls.__cliclass__.rsplit('.', 1) generic_params = [cls._cli_name(t) for t in types] instance_class = '%s[%s]' % (generic_class, ','.join(generic_params)) try: return clr.load_cli_class(cls.__assemblyname__, namespace, instance_class) except ImportError: raise TypeError, "Cannot load type %s.%s" % (namespace, instance_class)
def test_import_hook_simple(self): mscorlib = 'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' import clr import System.Math assert System.Math.Abs(-5) == 5 assert System.Math.Pow(2, 5) == 2**5 Math = clr.load_cli_class(mscorlib, 'System', 'Math') assert Math is System.Math import System a = System.Collections.Stack() a.Push(3) a.Push(44) sum = 0 for i in a: sum += i assert sum == 3+44 import System.Collections.ArrayList ArrayList = clr.load_cli_class(mscorlib, 'System.Collections', 'ArrayList') assert ArrayList is System.Collections.ArrayList
def load_module(self, fullname): ''' The load_module() must fulfill the following *before* it runs any code: Note that the module object *must* be in sys.modules before the loader executes the module code. A If 'fullname' exists in sys.modules, the loader must use that else the loader must create a new module object and add it to sys.modules. module = sys.modules.setdefault(fullname, new.module(fullname)) B The __file__ attribute must be set. String say "<frozen>" C The __name__ attribute must be set. If one uses imp.new_module() then the attribute is set automatically. D If it\'s a package, the __path__ variable must be set. This must be a list, but may be empty if __path__ has no further significance to the importer (more on this later). E It should add a __loader__ attribute to the module, set to the loader object. ''' # If it is a call for a Class then return with the Class reference import clr namespaces, classes, generics = clr.get_assemblies_info() if fullname in classes: assemblyname = classes[fullname] fullname = generics.get(fullname, fullname) ns, classname = fullname.rsplit('.', 1) sys.modules[fullname] = clr.load_cli_class(assemblyname, ns, classname) else: # if not a call for actual class (say for namespaces) assign an empty module if fullname not in sys.modules: mod = CLRModule(fullname) mod.__file__ = "<%s>" % self.__class__.__name__ mod.__loader__ = self mod.__name__ = fullname # add it to the modules dict sys.modules[fullname] = mod else: mod = sys.modules[fullname] # if it is a PACKAGE then we are to initialize the __path__ for the module # we won't deal with Packages here return sys.modules[fullname]
def test_snippet_1(self): import clr ArrayList = clr.load_cli_class('System.Collections', 'ArrayList') obj = ArrayList() obj.Add(1)
def test_wrong_overload(self): import clr Math = clr.load_cli_class('System', 'Math') raises(TypeError, Math.Abs, "foo")
def test_unboundmethod_typeerror(self): import clr ArrayList = clr.load_cli_class(self.mscorlib, 'System.Collections', 'ArrayList') raises(TypeError, ArrayList.Add) raises(TypeError, ArrayList.Add, 0)
def test_unboundmethod_typeerror(self): import clr ArrayList = clr.load_cli_class('System.Collections', 'ArrayList') raises(TypeError, ArrayList.Add) raises(TypeError, ArrayList.Add, 0)
def test_unboundmethod(self): import clr ArrayList = clr.load_cli_class('System.Collections', 'ArrayList') obj = ArrayList() ArrayList.Add(obj, 42) assert obj.get_Item(0) == 42
def test_wrong_overload(self): import clr Math = clr.load_cli_class(self.mscorlib, 'System', 'Math') raises(TypeError, Math.Abs, "foo")
def test_getitem(self): import clr ArrayList = clr.load_cli_class('System.Collections', 'ArrayList') obj = ArrayList() obj.Add(42) assert obj[0] == 42
def test_constructor_args(self): import clr ArrayList = clr.load_cli_class(self.mscorlib, 'System.Collections', 'ArrayList') obj = ArrayList(42) assert obj.Capacity == 42
def test_ArrayList_error(self): import clr ArrayList = clr.load_cli_class(self.mscorlib, 'System.Collections', 'ArrayList') obj = ArrayList() raises(StandardError, obj.get_Item, 0)
def test_ArrayList_error(self): import clr ArrayList = clr.load_cli_class('System.Collections', 'ArrayList') obj = ArrayList() raises(StandardError, obj.get_Item, 0)
def test_cache(self): import clr ArrayList = clr.load_cli_class('System.Collections', 'ArrayList') ArrayList2 = clr.load_cli_class('System.Collections', 'ArrayList') assert ArrayList is ArrayList2
def test_constructor_args(self): import clr ArrayList = clr.load_cli_class('System.Collections', 'ArrayList') obj = ArrayList(42) assert obj.Capacity == 42