| 1 |
import os |
| 2 |
import platform |
| 3 |
import random |
| 4 |
import warnings |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from gi.repository import Gio, GLib, GObject |
| 9 |
from gi import PyGIWarning |
| 10 |
|
| 11 |
|
| 12 |
class Item(GObject.Object): |
| 13 |
_id = 0 |
| 14 |
|
| 15 |
def __init__(self, **kwargs): |
| 16 |
super().__init__(**kwargs) |
| 17 |
Item._id += 1 |
| 18 |
self._id = self._id |
| 19 |
|
| 20 |
def __repr__(self): |
| 21 |
return str(self._id) |
| 22 |
|
| 23 |
|
| 24 |
class NamedItem(Item): |
| 25 |
name = GObject.Property(type=str, default="") |
| 26 |
|
| 27 |
def __repr__(self): |
| 28 |
return self.props.name |
| 29 |
|
| 30 |
|
| 31 |
def test_list_store_sort(): |
| 32 |
store = Gio.ListStore() |
| 33 |
items = [NamedItem(name=n) for n in "cabx"] |
| 34 |
sorted_items = sorted(items, key=lambda i: i.props.name) |
| 35 |
|
| 36 |
user_data = [object(), object()] |
| 37 |
|
| 38 |
def sort_func(a, b, *args): |
| 39 |
assert list(args) == user_data |
| 40 |
assert isinstance(a, NamedItem) |
| 41 |
assert isinstance(b, NamedItem) |
| 42 |
|
| 43 |
def cmp(a, b): |
| 44 |
return (a > b) - (a < b) |
| 45 |
|
| 46 |
return cmp(a.props.name, b.props.name) |
| 47 |
|
| 48 |
store[:] = items |
| 49 |
assert store[:] != sorted_items |
| 50 |
store.sort(sort_func, *user_data) |
| 51 |
assert store[:] == sorted_items |
| 52 |
|
| 53 |
|
| 54 |
def test_list_store_insert_sorted(): |
| 55 |
store = Gio.ListStore() |
| 56 |
items = [NamedItem(name=n) for n in "cabx"] |
| 57 |
sorted_items = sorted(items, key=lambda i: i.props.name) |
| 58 |
|
| 59 |
user_data = [object(), object()] |
| 60 |
|
| 61 |
def sort_func(a, b, *args): |
| 62 |
assert list(args) == user_data |
| 63 |
assert isinstance(a, NamedItem) |
| 64 |
assert isinstance(b, NamedItem) |
| 65 |
|
| 66 |
def cmp(a, b): |
| 67 |
return (a > b) - (a < b) |
| 68 |
|
| 69 |
return cmp(a.props.name, b.props.name) |
| 70 |
|
| 71 |
for item in items: |
| 72 |
index = store.insert_sorted(item, sort_func, *user_data) |
| 73 |
assert isinstance(index, int) |
| 74 |
assert store[:] == sorted_items |
| 75 |
|
| 76 |
|
| 77 |
def test_list_model_len(): |
| 78 |
model = Gio.ListStore.new(Item) |
| 79 |
assert len(model) == 0 |
| 80 |
assert not model |
| 81 |
for i in range(1, 10): |
| 82 |
model.append(Item()) |
| 83 |
assert len(model) == i |
| 84 |
assert model |
| 85 |
model.remove_all() |
| 86 |
assert not model |
| 87 |
assert len(model) == 0 |
| 88 |
|
| 89 |
|
| 90 |
def test_list_model_get_item_simple(): |
| 91 |
model = Gio.ListStore.new(Item) |
| 92 |
with pytest.raises(IndexError): |
| 93 |
model[0] |
| 94 |
first_item = Item() |
| 95 |
model.append(first_item) |
| 96 |
assert model[0] is first_item |
| 97 |
assert model[-1] is first_item |
| 98 |
second_item = Item() |
| 99 |
model.append(second_item) |
| 100 |
assert model[1] is second_item |
| 101 |
assert model[-1] is second_item |
| 102 |
assert model[-2] is first_item |
| 103 |
with pytest.raises(IndexError): |
| 104 |
model[-3] |
| 105 |
|
| 106 |
with pytest.raises(TypeError): |
| 107 |
model[object()] |
| 108 |
|
| 109 |
|
| 110 |
def test_list_model_get_item_slice(): |
| 111 |
model = Gio.ListStore.new(Item) |
| 112 |
source = [Item() for i in range(30)] |
| 113 |
for i in source: |
| 114 |
model.append(i) |
| 115 |
assert model[1:10] == source[1:10] |
| 116 |
assert model[1:-2] == source[1:-2] |
| 117 |
assert model[-4:-1] == source[-4:-1] |
| 118 |
assert model[-100:-1] == source[-100:-1] |
| 119 |
assert model[::-1] == source[::-1] |
| 120 |
assert model[:] == source[:] |
| 121 |
|
| 122 |
|
| 123 |
def test_list_model_contains(): |
| 124 |
model = Gio.ListStore.new(Item) |
| 125 |
item = Item() |
| 126 |
model.append(item) |
| 127 |
assert item in model |
| 128 |
assert Item() not in model |
| 129 |
with pytest.raises(TypeError): |
| 130 |
object() in model |
| 131 |
with pytest.raises(TypeError): |
| 132 |
None in model |
| 133 |
|
| 134 |
|
| 135 |
def test_list_model_iter(): |
| 136 |
model = Gio.ListStore.new(Item) |
| 137 |
item = Item() |
| 138 |
model.append(item) |
| 139 |
|
| 140 |
it = iter(model) |
| 141 |
assert next(it) is item |
| 142 |
repr(item) |
| 143 |
|
| 144 |
|
| 145 |
def test_list_store_delitem_simple(): |
| 146 |
store = Gio.ListStore.new(Item) |
| 147 |
store.append(Item()) |
| 148 |
del store[0] |
| 149 |
assert not store |
| 150 |
with pytest.raises(IndexError): |
| 151 |
del store[0] |
| 152 |
with pytest.raises(IndexError): |
| 153 |
del store[-1] |
| 154 |
|
| 155 |
store.append(Item()) |
| 156 |
with pytest.raises(IndexError): |
| 157 |
del store[-2] |
| 158 |
del store[-1] |
| 159 |
assert not store |
| 160 |
|
| 161 |
source = [Item(), Item()] |
| 162 |
store.append(source[0]) |
| 163 |
store.append(source[1]) |
| 164 |
del store[-1] |
| 165 |
assert store[:] == [source[0]] |
| 166 |
|
| 167 |
with pytest.raises(TypeError): |
| 168 |
del store[object()] |
| 169 |
|
| 170 |
|
| 171 |
def test_list_store_delitem_slice(): |
| 172 |
def do_del(count, key): |
| 173 |
events = [] |
| 174 |
|
| 175 |
def on_changed(m, *args): |
| 176 |
events.append(args) |
| 177 |
|
| 178 |
store = Gio.ListStore.new(Item) |
| 179 |
source = [Item() for i in range(count)] |
| 180 |
for item in source: |
| 181 |
store.append(item) |
| 182 |
store.connect("items-changed", on_changed) |
| 183 |
source.__delitem__(key) |
| 184 |
store.__delitem__(key) |
| 185 |
assert source == store[:] |
| 186 |
return events |
| 187 |
|
| 188 |
values = [None, 1, -15, 3, -2, 0, -3, 5, 7] |
| 189 |
variants = set() |
| 190 |
for i in range(500): |
| 191 |
start = random.choice(values) |
| 192 |
stop = random.choice(values) |
| 193 |
step = random.choice(values) |
| 194 |
length = abs(random.choice(values) or 0) |
| 195 |
if step == 0: |
| 196 |
step += 1 |
| 197 |
variants.add((length, start, stop, step)) |
| 198 |
|
| 199 |
for length, start, stop, step in variants: |
| 200 |
do_del(length, slice(start, stop, step)) |
| 201 |
|
| 202 |
# basics |
| 203 |
do_del(10, slice(None, None, None)) |
| 204 |
do_del(10, slice(None, None, None)) |
| 205 |
do_del(10, slice(None, None, -1)) |
| 206 |
do_del(10, slice(0, 5, None)) |
| 207 |
do_del(10, slice(0, 10, 1)) |
| 208 |
do_del(10, slice(0, 10, 2)) |
| 209 |
do_del(10, slice(14, 2, -1)) |
| 210 |
|
| 211 |
# test some fast paths |
| 212 |
assert do_del(100, slice(None, None, None)) == [(0, 100, 0)] |
| 213 |
assert do_del(100, slice(None, None, -1)) == [(0, 100, 0)] |
| 214 |
assert do_del(100, slice(0, 50, 1)) == [(0, 50, 0)] |
| 215 |
|
| 216 |
|
| 217 |
def test_list_store_setitem_simple(): |
| 218 |
store = Gio.ListStore.new(Item) |
| 219 |
first = Item() |
| 220 |
store.append(first) |
| 221 |
|
| 222 |
class Wrong(GObject.Object): |
| 223 |
pass |
| 224 |
|
| 225 |
with pytest.raises(TypeError): |
| 226 |
store[0] = object() |
| 227 |
with pytest.raises(TypeError): |
| 228 |
store[0] = None |
| 229 |
with pytest.raises(TypeError): |
| 230 |
store[0] = Wrong() |
| 231 |
|
| 232 |
assert store[:] == [first] |
| 233 |
|
| 234 |
new = Item() |
| 235 |
store[0] = new |
| 236 |
assert len(store) == 1 |
| 237 |
store[-1] = Item() |
| 238 |
assert len(store) == 1 |
| 239 |
|
| 240 |
with pytest.raises(IndexError): |
| 241 |
store[1] = Item() |
| 242 |
with pytest.raises(IndexError): |
| 243 |
store[-2] = Item() |
| 244 |
|
| 245 |
store = Gio.ListStore.new(Item) |
| 246 |
source = [Item(), Item(), Item()] |
| 247 |
for item in source: |
| 248 |
store.append(item) |
| 249 |
new = Item() |
| 250 |
store[1] = new |
| 251 |
assert store[:] == [source[0], new, source[2]] |
| 252 |
|
| 253 |
with pytest.raises(TypeError): |
| 254 |
store[object()] = Item() |
| 255 |
|
| 256 |
|
| 257 |
def test_list_store_setitem_slice(): |
| 258 |
def do_set(count, key, new_count): |
| 259 |
if ( |
| 260 |
count == 0 |
| 261 |
and key.step is not None |
| 262 |
and platform.python_implementation() == "PyPy" |
| 263 |
): |
| 264 |
# https://foss.heptapod.net/pypy/pypy/-/issues/2804 |
| 265 |
return |
| 266 |
store = Gio.ListStore.new(Item) |
| 267 |
source = [Item() for i in range(count)] |
| 268 |
new = [Item() for i in range(new_count)] |
| 269 |
for item in source: |
| 270 |
store.append(item) |
| 271 |
source_error = None |
| 272 |
try: |
| 273 |
source.__setitem__(key, new) |
| 274 |
except ValueError as e: |
| 275 |
source_error = type(e) |
| 276 |
|
| 277 |
store_error = None |
| 278 |
try: |
| 279 |
store.__setitem__(key, new) |
| 280 |
except Exception as e: |
| 281 |
store_error = type(e) |
| 282 |
|
| 283 |
assert source_error == store_error |
| 284 |
assert source == store[:] |
| 285 |
|
| 286 |
values = [None, 1, -15, 3, -2, 0, 3, 4, 100] |
| 287 |
variants = set() |
| 288 |
for i in range(500): |
| 289 |
start = random.choice(values) |
| 290 |
stop = random.choice(values) |
| 291 |
step = random.choice(values) |
| 292 |
length = abs(random.choice(values) or 0) |
| 293 |
new = random.choice(values) or 0 |
| 294 |
if step == 0: |
| 295 |
step += 1 |
| 296 |
variants.add((length, start, stop, step, new)) |
| 297 |
|
| 298 |
for length, start, stop, step, new in variants: |
| 299 |
do_set(length, slice(start, stop, step), new) |
| 300 |
|
| 301 |
# basics |
| 302 |
do_set(10, slice(None, None, None), 20) |
| 303 |
do_set(10, slice(None, None, None), 0) |
| 304 |
do_set(10, slice(None, None, -1), 20) |
| 305 |
do_set(10, slice(None, None, -1), 10) |
| 306 |
do_set(10, slice(0, 5, None), 20) |
| 307 |
do_set(10, slice(0, 10, 1), 0) |
| 308 |
|
| 309 |
# test iterators |
| 310 |
store = Gio.ListStore.new(Item) |
| 311 |
store[:] = iter([Item() for i in range(10)]) |
| 312 |
assert len(store) == 10 |
| 313 |
|
| 314 |
# make sure we do all or nothing |
| 315 |
store = Gio.ListStore.new(Item) |
| 316 |
with pytest.raises(TypeError): |
| 317 |
store[:] = [Item(), object()] |
| 318 |
assert len(store) == 0 |
| 319 |
|
| 320 |
|
| 321 |
def test_action_map_add_action_entries(): |
| 322 |
actionmap = Gio.SimpleActionGroup() |
| 323 |
|
| 324 |
test_data = [] |
| 325 |
|
| 326 |
def f(action, parameter, data): |
| 327 |
test_data.append("test back") |
| 328 |
|
| 329 |
actionmap.add_action_entries( |
| 330 |
( |
| 331 |
("simple", f), |
| 332 |
("with_type", f, "i"), |
| 333 |
("with_state", f, "s", "'left'", f), |
| 334 |
) |
| 335 |
) |
| 336 |
assert actionmap.has_action("simple") |
| 337 |
assert actionmap.has_action("with_type") |
| 338 |
assert actionmap.has_action("with_state") |
| 339 |
actionmap.add_action_entries((("with_user_data", f),), "user_data") |
| 340 |
assert actionmap.has_action("with_user_data") |
| 341 |
|
| 342 |
with pytest.raises(TypeError): |
| 343 |
actionmap.add_action_entries((("invaild_type_string", f, "asdf"),)) |
| 344 |
with pytest.raises(ValueError): |
| 345 |
actionmap.add_action_entries( |
| 346 |
(("stateless_with_change_state", f, None, None, f),) |
| 347 |
) |
| 348 |
|
| 349 |
actionmap.activate_action("simple") |
| 350 |
assert test_data[0] == "test back" |
| 351 |
|
| 352 |
|
| 353 |
def test_types_init_warn(): |
| 354 |
types = [ |
| 355 |
Gio.DBusAnnotationInfo, |
| 356 |
Gio.DBusArgInfo, |
| 357 |
Gio.DBusMethodInfo, |
| 358 |
Gio.DBusSignalInfo, |
| 359 |
Gio.DBusInterfaceInfo, |
| 360 |
Gio.DBusNodeInfo, |
| 361 |
] |
| 362 |
|
| 363 |
for t in types: |
| 364 |
with warnings.catch_warnings(record=True) as warn: |
| 365 |
warnings.simplefilter("always") |
| 366 |
t() |
| 367 |
assert issubclass(warn[0].category, PyGIWarning) |
| 368 |
|
| 369 |
|
| 370 |
def test_file_fspath(): |
| 371 |
file, stream = Gio.File.new_tmp("TestGFile.XXXXXX") |
| 372 |
content = b"hello\0world\x7f!" |
| 373 |
stream.get_output_stream().write_bytes(GLib.Bytes(content)) |
| 374 |
stream.close() |
| 375 |
path = file.peek_path() |
| 376 |
assert isinstance(path, str) |
| 377 |
fspath = file.__fspath__() |
| 378 |
assert isinstance(fspath, str) |
| 379 |
assert fspath == path |
| 380 |
|
| 381 |
with open(file, "rb") as file_like: |
| 382 |
assert file_like.read() == content |
| 383 |
|
| 384 |
|
| 385 |
def test_file_fspath_with_no_path(): |
| 386 |
file = Gio.File.new_for_path("") |
| 387 |
path = file.peek_path() |
| 388 |
# In older versions of GLib, creating a GFile for an empty path will result |
| 389 |
# in one representing the current pwd instead of a GDummyFile with no path. |
| 390 |
if path is None: |
| 391 |
with pytest.raises(TypeError): |
| 392 |
file.__fspath__() |
| 393 |
else: |
| 394 |
assert path == file.__fspath__() |
| 395 |
assert path == os.getcwd() |
| 396 |
|
| 397 |
|
| 398 |
@pytest.mark.skipif( |
| 399 |
not hasattr(Gio.ListStore, "find_with_equal_func_full"), |
| 400 |
reason="ListStore.find_with_equal_func_full() is available in Gio 2.74", |
| 401 |
) |
| 402 |
def test_list_store_find_with_equal_func(): |
| 403 |
def test(*user_data): |
| 404 |
class TestObject(GObject.Object): |
| 405 |
def __init__(self, val): |
| 406 |
super().__init__() |
| 407 |
self.val = val |
| 408 |
|
| 409 |
NUM = 5 |
| 410 |
data = [TestObject(i) for i in range(NUM)] |
| 411 |
list_store = Gio.ListStore() |
| 412 |
for d in data: |
| 413 |
list_store.append(d) |
| 414 |
|
| 415 |
def equal_func(a, b, *data): |
| 416 |
assert data == user_data |
| 417 |
return a.val == b.val |
| 418 |
|
| 419 |
for i in range(NUM): |
| 420 |
res, position = list_store.find_with_equal_func( |
| 421 |
data[i], equal_func, *user_data |
| 422 |
) |
| 423 |
assert res |
| 424 |
assert position == i |
| 425 |
|
| 426 |
not_in_list_store = TestObject(NUM + 1) |
| 427 |
res, position = list_store.find_with_equal_func( |
| 428 |
not_in_list_store, equal_func, *user_data |
| 429 |
) |
| 430 |
assert not res |
| 431 |
|
| 432 |
test() |
| 433 |
test((100, "data")) |