1
+ import sys
2
+
1
3
import pytest
2
4
3
5
import env # noqa: F401
4
6
from pybind11_tests import ConstructorStats
5
7
from pybind11_tests import methods_and_attributes as m
6
8
9
+ NO_GETTER_MSG = (
10
+ "unreadable attribute" if sys .version_info < (3 , 11 ) else "object has no getter"
11
+ )
12
+ NO_SETTER_MSG = (
13
+ "can't set attribute" if sys .version_info < (3 , 11 ) else "object has no setter"
14
+ )
15
+ NO_DELETER_MSG = (
16
+ "can't delete attribute" if sys .version_info < (3 , 11 ) else "object has no deleter"
17
+ )
18
+
7
19
8
20
def test_methods_and_attributes ():
9
21
instance1 = m .ExampleMandA ()
@@ -102,47 +114,47 @@ def test_properties():
102
114
103
115
with pytest .raises (AttributeError ) as excinfo :
104
116
dummy = instance .def_property_writeonly # unused var
105
- assert "unreadable attribute" in str (excinfo .value )
117
+ assert NO_GETTER_MSG in str (excinfo .value )
106
118
107
119
instance .def_property_writeonly = 4
108
120
assert instance .def_property_readonly == 4
109
121
110
122
with pytest .raises (AttributeError ) as excinfo :
111
123
dummy = instance .def_property_impossible # noqa: F841 unused var
112
- assert "unreadable attribute" in str (excinfo .value )
124
+ assert NO_GETTER_MSG in str (excinfo .value )
113
125
114
126
with pytest .raises (AttributeError ) as excinfo :
115
127
instance .def_property_impossible = 5
116
- assert "can't set attribute" in str (excinfo .value )
128
+ assert NO_SETTER_MSG in str (excinfo .value )
117
129
118
130
119
131
def test_static_properties ():
120
132
assert m .TestProperties .def_readonly_static == 1
121
133
with pytest .raises (AttributeError ) as excinfo :
122
134
m .TestProperties .def_readonly_static = 2
123
- assert "can't set attribute" in str (excinfo .value )
135
+ assert NO_SETTER_MSG in str (excinfo .value )
124
136
125
137
m .TestProperties .def_readwrite_static = 2
126
138
assert m .TestProperties .def_readwrite_static == 2
127
139
128
140
with pytest .raises (AttributeError ) as excinfo :
129
141
dummy = m .TestProperties .def_writeonly_static # unused var
130
- assert "unreadable attribute" in str (excinfo .value )
142
+ assert NO_GETTER_MSG in str (excinfo .value )
131
143
132
144
m .TestProperties .def_writeonly_static = 3
133
145
assert m .TestProperties .def_readonly_static == 3
134
146
135
147
assert m .TestProperties .def_property_readonly_static == 3
136
148
with pytest .raises (AttributeError ) as excinfo :
137
149
m .TestProperties .def_property_readonly_static = 99
138
- assert "can't set attribute" in str (excinfo .value )
150
+ assert NO_SETTER_MSG in str (excinfo .value )
139
151
140
152
m .TestProperties .def_property_static = 4
141
153
assert m .TestProperties .def_property_static == 4
142
154
143
155
with pytest .raises (AttributeError ) as excinfo :
144
156
dummy = m .TestProperties .def_property_writeonly_static
145
- assert "unreadable attribute" in str (excinfo .value )
157
+ assert NO_GETTER_MSG in str (excinfo .value )
146
158
147
159
m .TestProperties .def_property_writeonly_static = 5
148
160
assert m .TestProperties .def_property_static == 5
@@ -160,7 +172,7 @@ def test_static_properties():
160
172
161
173
with pytest .raises (AttributeError ) as excinfo :
162
174
dummy = instance .def_property_writeonly_static # noqa: F841 unused var
163
- assert "unreadable attribute" in str (excinfo .value )
175
+ assert NO_GETTER_MSG in str (excinfo .value )
164
176
165
177
instance .def_property_writeonly_static = 4
166
178
assert instance .def_property_static == 4
@@ -180,7 +192,7 @@ def test_static_properties():
180
192
properties_override = m .TestPropertiesOverride ()
181
193
with pytest .raises (AttributeError ) as excinfo :
182
194
del properties_override .def_readonly
183
- assert "can't delete attribute" in str (excinfo .value )
195
+ assert NO_DELETER_MSG in str (excinfo .value )
184
196
185
197
186
198
def test_static_cls ():
0 commit comments