Debian Codegraph @

testsuite/systemtap.python/python-var.py

raw
1 import sys
2 import types
3
4 # Try to create instances of (most) every python type.
5
6 def ret0():
7 return 0
8
9 class xqueue:
10 def __init__(self):
11 self.items = []
12 def insert(self, item):
13 self.items.append(item)
14 def remove(self):
15 return self.items.pop()
16
17 #
18 # Base types: None, type, and bool
19 #
20 g_none = None
21 g_type = type(g_none)
22 g_bool = True
23
24 #
25 # Numeric types: int, long, float, and complex
26 #
27 # Note that in python 2, there are 2 integer types 'int' and
28 # 'long'. To test both, we've got two integer variables, one with a
29 # small value, one with a large value. In python 3, there is no 'int'
30 # type.
31 g_int = 9
32 g_long = 0x7eadbeeffeedbabe
33 g_float = 0.5
34 g_complex = 1.2 + 12.34J
35
36 #
37 # Sequence types: strings, unicode strings, lists, tuples, and xrange
38 # (python 2 only)
39 #
40 # In python 2, there are 2 string types 'string' and 'unicode'. We'll
41 # test both. In python 3, there are really just unicode strings, but
42 # this should still work.
43 g_string = 'regular string'
44 g_unicode = u'unicode string'
45 g_tuple = (0, 'abc', 2)
46 g_list = [0, 1, 2, 3]
47 # The 'xrange' type only exists in python 2.
48 if sys.version_info[0] == 2:
49 g_xrange = xrange(10)
50
51 #
52 # Mapping types: dictionary
53 #
54 g_dictionary = {'Bacon': 1, 'Ham': 0, 'Ribs': 2}
55
56 #
57 # Callable objects: functions, methods, classes, class instances,
58 # unbound class methods
59 #
60 g_func = ret0
61 g_class = xqueue
62 g_unbound_method = xqueue.insert
63 g_instance = xqueue()
64 g_method = g_instance.insert
65
66 #
67 # Misc types: zip, file, ellipsis
68 #
69 g_zip = zip(g_list, [4, 5, 6])
70 g_file = sys.stdout
71 g_ellipsis = Ellipsis
72
73 def main():
74 l_none = None
75 l_type = type(g_bool)
76 l_bool = False
77
78 l_int = 15
79 l_long = 0x7eedfacecafebeef
80 l_float = 0.33
81 l_complex = 2.33 + 23.45J
82
83 l_string = 'another regular string'
84 l_unicode = u'another unicode string'
85 l_tuple = ('hello', 99, 'there', 0xbeef)
86 l_list = [4, 5, 6, 7, 8, 9, 10]
87 if sys.version_info[0] == 2:
88 l_xrange = xrange(10)
89
90 # Notice this dictionary has mixed numeric and string indices.
91 l_dictionary = {1: 'numeric index', 'abc': 3}
92
93 l_func = ret0
94 l_class = xqueue
95 l_unbound_method = xqueue.remove
96 l_instance = xqueue()
97 l_method = g_instance.remove
98
99 l_zip = zip(l_list, g_list, [11, 12, 13, 14, 15])
100 l_file = sys.stderr
101 l_ellipsis = Ellipsis
102 return 0
103
104 if __name__ == "__main__":
105 sys.exit(main())
106