File: __init__.py

package info (click to toggle)
python-xlib 0.33-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,040 kB
  • sloc: python: 23,022; awk: 89; makefile: 60; sh: 10
file content (79 lines) | stat: -rw-r--r-- 1,862 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

import unittest
import binascii
import difflib
import struct
import array

import Xlib.protocol.event
import Xlib.protocol.rq


class CmpArray(object):

    def __init__(self, *args, **kws):
        self.array = array.array(*args, **kws)

    def __len__(self):
        return len(self.array)

    def __getitem__(self, key):
        if isinstance(key, slice):
            x = key.start
            y = key.stop
            return list(self.array[x:y])
        else:
            return self.array[key]

    def __getattr__(self, attr):
        return getattr(self.array, attr)

    def __eq__(self, other):
        return self.array.tolist() == other

Xlib.protocol.rq.array = CmpArray


class DummyDisplay(object):

    def get_resource_class(self, x):
        return None

    event_classes = Xlib.protocol.event.event_class


class TestCase(unittest.TestCase):

    def assertBinaryEqual(self, bin1, bin2):
        if bin1 != bin2:
            self.fail('binary contents differ:\n' + bindiff(bin1, bin2))

    def assertBinaryEmpty(self, bin):
        if len(bin) != 0:
            self.fail('binary content not empty:\n' + ''.join(tohex(bin)))

class BigEndianTest(TestCase):

    @classmethod
    def setUpClass(cls):
        if struct.unpack('BB', struct.pack('H', 0x0100))[0] != 1:
            raise unittest.SkipTest('big-endian tests, skipping on this system')

class LittleEndianTest(TestCase):

    @classmethod
    def setUpClass(cls):
        if struct.unpack('BB', struct.pack('H', 0x0100))[0] != 0:
            raise unittest.SkipTest('little-endian tests, skipping on this system')


def tohex(bin):
    hex = []
    for i in range(0, len(bin), 16):
        hex.append(str(binascii.hexlify(bin[i:i+16])) + '\n')
    return hex

def bindiff(bin1, bin2):
    hex1 = tohex(bin1)
    hex2 = tohex(bin2)
    return ''.join(difflib.ndiff(hex1, hex2))