this repo has no description
1#!/usr/bin/env python3
2# Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
3import grp
4import posix
5import sys
6import time
7import unittest
8
9from test.support import _TPFLAGS_HEAPTYPE
10from test_support import pyro_only
11
12
13try:
14 from _builtins import _structseq_new_type
15except ImportError:
16 pass
17
18
19class StructSequenceTests(unittest.TestCase):
20 def test_new_returns_instance(self):
21 sseq_type = grp.struct_group
22 instance = sseq_type(("foo", "bar", 42, "baz"))
23 self.assertEqual(instance[0], "foo")
24 self.assertEqual(instance[1], "bar")
25 self.assertEqual(instance[2], 42)
26 self.assertEqual(instance[3], "baz")
27 self.assertEqual(instance.gr_name, "foo")
28 self.assertEqual(instance.gr_passwd, "bar")
29 self.assertEqual(instance.gr_gid, 42)
30 self.assertEqual(instance.gr_mem, "baz")
31 with self.assertRaises(IndexError):
32 instance[4]
33
34 def test_new_with_iteratble_returns_instance(self):
35 sseq_type = grp.struct_group
36 instance = sseq_type(range(20, 28, 2))
37 self.assertEqual(instance, (20, 22, 24, 26))
38 self.assertEqual(instance[2], 24)
39 self.assertEqual(instance.gr_gid, 24)
40
41 def test_new_with_min_len_returns_value(self):
42 sseq_type = time.struct_time
43 instance = sseq_type((9, 8, 7, 6, 5, 4, 3, 2, 1))
44 self.assertEqual(instance, (9, 8, 7, 6, 5, 4, 3, 2, 1))
45 self.assertIsNone(instance.tm_zone)
46 self.assertIsNone(instance.tm_gmtoff)
47
48 def test_new_with_more_than_min_len_returns_value(self):
49 sseq_type = time.struct_time
50 instance = sseq_type((10, 9, 8, 7, 6, 5, 4, 3, 2, 1))
51 self.assertEqual(instance, (10, 9, 8, 7, 6, 5, 4, 3, 2))
52 self.assertEqual(instance.tm_zone, 1)
53 self.assertIsNone(instance.tm_gmtoff)
54
55 def test_new_with_max_len_returns_value(self):
56 sseq_type = time.struct_time
57 instance = sseq_type((11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1))
58 self.assertEqual(instance, (11, 10, 9, 8, 7, 6, 5, 4, 3))
59 self.assertEqual(instance.tm_zone, 2)
60 self.assertEqual(instance.tm_gmtoff, 1)
61
62 def test_new_with_dict_returns_value(self):
63 sseq_type = time.struct_time
64 instance = sseq_type((1, 2, 3, 4, 5, 6, 7, 8, 9), {"tm_gmtoff": "foo"})
65 self.assertEqual(instance, (1, 2, 3, 4, 5, 6, 7, 8, 9))
66 self.assertIsNone(instance.tm_zone)
67 self.assertEqual(instance.tm_gmtoff, "foo")
68
69 def test_new_with_dict_with_extra_keys_returns_value(self):
70 sseq_type = time.struct_time
71 instance = sseq_type(
72 (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
73 {"tm_zone": "foo", "tm_gmtoff": "bar", "baz": 42, "tm_year": "ignored"},
74 )
75 self.assertEqual(instance, (1, 2, 3, 4, 5, 6, 7, 8, 9))
76 self.assertEqual(instance.tm_zone, 10)
77 self.assertEqual(instance.tm_gmtoff, "bar")
78
79 def test_new_with_non_sequence_raises_type_error(self):
80 sseq_type = grp.struct_group
81 with self.assertRaises(TypeError):
82 sseq_type(42)
83
84 def test_new_with_too_few_elements_raises_type_error(self):
85 sseq_type = grp.struct_group
86 with self.assertRaisesRegex(TypeError, ".*4-sequence.*(2-sequence given)"):
87 sseq_type((1, 2))
88
89 def test_new_with_too_many_elements_raises_type_error(self):
90 sseq_type = grp.struct_group
91 with self.assertRaisesRegex(TypeError, ".*4-sequence.*(5-sequence given)"):
92 sseq_type((1, 2, 3, 4, 5))
93
94 def test_descriptor_on_other_instance_raises_type_error(self):
95 sseq_type = grp.struct_group
96 descriptor = sseq_type.gr_name
97 self.assertRaisesRegex(
98 TypeError,
99 r"descriptor .* '(grp.)?struct_group' .* 'tuple' object",
100 descriptor.__get__,
101 (1, 2, 3, 4),
102 tuple,
103 )
104
105 def test_repr_returns_string(self):
106 sseq_type = grp.struct_group
107 instance = sseq_type(("foo", 42, "bar", (1, 2, 3)))
108 self.assertEqual(
109 sseq_type.__repr__(instance),
110 "grp.struct_group(gr_name='foo', gr_passwd=42, gr_gid='bar', "
111 "gr_mem=(1, 2, 3))",
112 )
113
114 def test_repr_unnamed_field_bug_matches_cptyhon(self):
115 if sys.implementation.name == "skybison":
116 # TODO(T64685580) This should use posix.stat_result, but the way
117 # posixmodule replaces new does not work yet.
118 stat_result = _structseq_new_type(
119 "os.stat_result",
120 (
121 "st_mode",
122 "st_ino",
123 "st_dev",
124 "st_nlink",
125 "st_uid",
126 "st_gid",
127 "st_size",
128 None,
129 None,
130 None,
131 "st_atime",
132 "st_mtime",
133 "st_ctime",
134 "st_atime_ns",
135 "st_mtime_ns",
136 "st_ctime_ns",
137 ),
138 num_in_sequence=10,
139 )
140 else:
141 stat_result = posix.stat_result
142 self.assertEqual(stat_result.n_sequence_fields, 10)
143 self.assertEqual(stat_result.n_unnamed_fields, 3)
144 self.assertTrue(stat_result.n_fields > 13)
145 instance = stat_result((1, 2, 3, 4, 5, 6, 7, 8, "nine", 10, 11))
146 self.assertEqual(
147 repr(instance),
148 "os.stat_result(st_mode=1, st_ino=2, st_dev=3, st_nlink=4, "
149 "st_uid=5, st_gid=6, st_size=7, st_atime=8, st_mtime='nine', "
150 "st_ctime=10)",
151 )
152
153 @pyro_only
154 def test_structseq_new_type_returns_type(self):
155 tp = _structseq_new_type("foo.bar", ("f0", "f1", "f2"), num_in_sequence=2)
156 self.assertIsInstance(tp, type)
157 self.assertTrue(issubclass(tp, tuple))
158 self.assertEqual(tp.__module__, "foo")
159 self.assertEqual(tp.__name__, "bar")
160 self.assertEqual(tp.__qualname__, "bar")
161 self.assertEqual(tp.n_sequence_fields, 2)
162 self.assertEqual(tp.n_unnamed_fields, 0)
163 self.assertEqual(tp.n_fields, 3)
164 self.assertEqual(tp._structseq_field_names, ("f0", "f1", "f2"))
165
166 @pyro_only
167 def test_structseq_new_type_without_dot_in_name_does_not_set_module(self):
168 tp = _structseq_new_type("foo", (), is_heaptype=False, num_in_sequence=0)
169 self.assertNotIn("__module__", tp.__dict__)
170
171 @pyro_only
172 def test_structseq_new_type_with_unnamed_fields_returns_structseq_type(self):
173 tp = _structseq_new_type(
174 "quux", ("foo", None, "bar", None, "baz"), num_in_sequence=4
175 )
176 self.assertIsInstance(tp, type)
177 self.assertTrue(issubclass(tp, tuple))
178 self.assertEqual(tp.__name__, "quux")
179 self.assertEqual(tp.n_sequence_fields, 4)
180 self.assertEqual(tp.n_unnamed_fields, 2)
181 self.assertEqual(tp.n_fields, 5)
182 self.assertEqual(tp._structseq_field_names, ("foo", None, "bar", None, "baz"))
183 instance = tp((1, 2, 3, 4, 5))
184 self.assertEqual(instance, (1, 2, 3, 4))
185 self.assertEqual(instance.bar, 3)
186 self.assertEqual(instance.baz, 5)
187
188 @pyro_only
189 def test_structseq_new_type_returns_heap_type(self):
190 tp = _structseq_new_type("foo", ("a",), is_heaptype=True)
191 self.assertTrue(tp.__flags__ & _TPFLAGS_HEAPTYPE)
192 tp.bar = 1
193
194 @pyro_only
195 def test_structseq_new_type_default_returns_heap_type(self):
196 tp = _structseq_new_type("foo", ("a",))
197 self.assertTrue(tp.__flags__ & _TPFLAGS_HEAPTYPE)
198 tp.bar = 1
199
200 @pyro_only
201 def test_structseq_new_type_default_returns_non_heap_type(self):
202 tp = _structseq_new_type("foo", ("a",), is_heaptype=False)
203 self.assertFalse(tp.__flags__ & _TPFLAGS_HEAPTYPE)
204 with self.assertRaises(TypeError):
205 tp.bar = 1
206
207
208if __name__ == "__main__":
209 unittest.main()