| 1 |
from pypy.module.test_lib_pypy.support import import_lib_pypy |
| 2 |
|
| 3 |
|
| 4 |
class AppTestStructseq: |
| 5 |
|
| 6 |
spaceconfig = dict(usemodules=('binascii', 'struct',)) |
| 7 |
|
| 8 |
def setup_class(cls): |
| 9 |
cls.w__structseq = cls.space.appexec( |
| 10 |
[], "(): import _structseq; return _structseq") |
| 11 |
|
| 12 |
def w_get_mydata(self): |
| 13 |
_structseq = self._structseq |
| 14 |
ns = dict(_structseq=_structseq, |
| 15 |
ssfield=_structseq.structseqfield) |
| 16 |
|
| 17 |
exec("""class mydata(metaclass=_structseq.structseqtype): |
| 18 |
st_mode = ssfield(0, "protection bits") |
| 19 |
st_ino = ssfield(1) |
| 20 |
st_dev = ssfield(2) |
| 21 |
st_nlink = ssfield(3) |
| 22 |
st_uid = ssfield(4) |
| 23 |
st_gid = ssfield(5) |
| 24 |
st_size = ssfield(6) |
| 25 |
_st_atime_as_int = ssfield(7) |
| 26 |
_st_mtime_as_int = ssfield(8) |
| 27 |
_st_ctime_as_int = ssfield(9) |
| 28 |
# skip to higher numbers for fields not part of the sequence. |
| 29 |
# the numbers are only used to ordering |
| 30 |
st_rdev = ssfield(50, "device type (if inode device)") |
| 31 |
st_atime = ssfield(57, |
| 32 |
default=lambda self: self._st_atime_as_int) |
| 33 |
st_mtime = ssfield(58, |
| 34 |
default=lambda self: self._st_mtime_as_int) |
| 35 |
st_ctime = ssfield(59, |
| 36 |
default=lambda self: self._st_ctime_as_int) |
| 37 |
""", ns) |
| 38 |
return ns['mydata'] |
| 39 |
|
| 40 |
def w_get_small(self): |
| 41 |
_structseq = self._structseq |
| 42 |
ns = dict(_structseq=_structseq, |
| 43 |
ssfield=_structseq.structseqfield) |
| 44 |
|
| 45 |
exec("""class small(metaclass=_structseq.structseqtype): |
| 46 |
one = ssfield(0, "one") |
| 47 |
""", ns) |
| 48 |
return ns['small'] |
| 49 |
|
| 50 |
|
| 51 |
def test_class(self): |
| 52 |
mydata = self.get_mydata() |
| 53 |
assert mydata.st_mode.__doc__ == "protection bits" |
| 54 |
assert mydata.n_fields == 14 |
| 55 |
assert mydata.n_sequence_fields == 10 |
| 56 |
assert mydata.n_unnamed_fields == 3 |
| 57 |
|
| 58 |
def test_mydata(self): |
| 59 |
mydata = self.get_mydata() |
| 60 |
x = mydata(range(100, 111)) |
| 61 |
assert x.n_sequence_fields == type(x).n_sequence_fields == 10 |
| 62 |
assert x.n_fields == type(x).n_fields == 14 |
| 63 |
assert x.st_mode == 100 |
| 64 |
assert x.st_size == 106 |
| 65 |
assert x.st_ctime == 109 |
| 66 |
assert x.st_rdev == 110 |
| 67 |
assert len(x) == 10 |
| 68 |
assert list(x) == list(range(100, 110)) |
| 69 |
assert x + (5,) == tuple(range(100, 110)) + (5,) |
| 70 |
assert x[4:12:2] == (104, 106, 108) |
| 71 |
assert 104 in x |
| 72 |
assert 110 not in x |
| 73 |
|
| 74 |
def test_default_None(self): |
| 75 |
mydata = self.get_mydata() |
| 76 |
x = mydata(range(100, 110)) |
| 77 |
assert x.st_rdev is None |
| 78 |
|
| 79 |
def test_constructor(self): |
| 80 |
mydata = self.get_mydata() |
| 81 |
x = mydata(range(100, 111), {'st_mtime': 12.25}) |
| 82 |
assert x[8] == 108 |
| 83 |
assert x.st_mtime == 12.25 |
| 84 |
|
| 85 |
def test_compare_like_tuple(self): |
| 86 |
mydata = self.get_mydata() |
| 87 |
x = mydata(range(100, 111)) |
| 88 |
y = mydata(list(range(100, 110)) + [555]) |
| 89 |
assert x == tuple(range(100, 110)) |
| 90 |
assert x == y |
| 91 |
assert hash(x) == hash(y) == hash(tuple(range(100, 110))) |
| 92 |
|
| 93 |
def test_pickle(self): |
| 94 |
import pickle |
| 95 |
import sys |
| 96 |
import types |
| 97 |
sys.modules['mod'] = mod = types.ModuleType('mod') |
| 98 |
try: |
| 99 |
mod.mydata = mydata = self.get_mydata() |
| 100 |
mydata.__module__ = 'mod' |
| 101 |
x = mydata(range(100, 111)) |
| 102 |
s = pickle.dumps(x) |
| 103 |
y = pickle.loads(s) |
| 104 |
assert x == y |
| 105 |
assert x.st_rdev == y.st_rdev == 110 |
| 106 |
finally: |
| 107 |
del sys.modules['mod'] |
| 108 |
|
| 109 |
def test_readonly(self): |
| 110 |
mydata = self.get_mydata() |
| 111 |
x = mydata(range(100, 113)) |
| 112 |
raises((TypeError, AttributeError), "x.st_mode = 1") |
| 113 |
raises((TypeError, AttributeError), "x.st_mtime = 1") |
| 114 |
raises((TypeError, AttributeError), "x.st_rdev = 1") |
| 115 |
|
| 116 |
def test_no_extra_assignments(self): |
| 117 |
mydata = self.get_mydata() |
| 118 |
x = mydata(range(100, 113)) |
| 119 |
raises((TypeError, AttributeError), "x.some_random_attribute = 1") |
| 120 |
|
| 121 |
def test_dict_argument(self): |
| 122 |
mydata = self.get_mydata() |
| 123 |
with raises(TypeError) as data: |
| 124 |
mydata(range(100, 113), dict=None) |
| 125 |
assert "mydata() takes a dict as second arg, if any" |
| 126 |
|
| 127 |
def test_small(self): |
| 128 |
small = self.get_small() |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
x = small(0) |
| 133 |
assert x[0] == 0 |
| 134 |
x = small((0, 0, 0)) |
| 135 |
assert x[0] == (0, 0, 0) |
| 136 |
|
| 137 |
def test_match_margs(self): |
| 138 |
small = self.get_small() |
| 139 |
assert small.__match_args__ == ('one', ) |
| 140 |
|