Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf9a68be

Browse files
[3.11]GH-99257: Check the owner's type when specializing slots (GH-99324)
(cherry picked from commit9d69284)Co-authored-by: Brandt Bucher <brandtbucher@microsoft.com>
1 parent1de088c commitf9a68be

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

‎Lib/test/test_opcache.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,73 @@ def f():
177177
for_inrange(1025):
178178
self.assertFalse(f())
179179

180+
deftest_load_shadowing_slot_should_raise_type_error(self):
181+
classClass:
182+
__slots__= ("slot",)
183+
184+
classSneaky:
185+
__slots__= ("shadowed",)
186+
shadowing=Class.slot
187+
188+
deff(o):
189+
o.shadowing
190+
191+
o=Sneaky()
192+
o.shadowed=42
193+
194+
for_inrange(1025):
195+
withself.assertRaises(TypeError):
196+
f(o)
197+
198+
deftest_store_shadowing_slot_should_raise_type_error(self):
199+
classClass:
200+
__slots__= ("slot",)
201+
202+
classSneaky:
203+
__slots__= ("shadowed",)
204+
shadowing=Class.slot
205+
206+
deff(o):
207+
o.shadowing=42
208+
209+
o=Sneaky()
210+
211+
for_inrange(1025):
212+
withself.assertRaises(TypeError):
213+
f(o)
214+
215+
deftest_load_borrowed_slot_should_not_crash(self):
216+
classClass:
217+
__slots__= ("slot",)
218+
219+
classSneaky:
220+
borrowed=Class.slot
221+
222+
deff(o):
223+
o.borrowed
224+
225+
o=Sneaky()
226+
227+
for_inrange(1025):
228+
withself.assertRaises(TypeError):
229+
f(o)
230+
231+
deftest_store_borrowed_slot_should_not_crash(self):
232+
classClass:
233+
__slots__= ("slot",)
234+
235+
classSneaky:
236+
borrowed=Class.slot
237+
238+
deff(o):
239+
o.borrowed=42
240+
241+
o=Sneaky()
242+
243+
for_inrange(1025):
244+
withself.assertRaises(TypeError):
245+
f(o)
246+
180247

181248
classTestLoadMethodCache(unittest.TestCase):
182249
deftest_descriptor_added_after_optimization(self):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix an issue where member descriptors (such as those for
2+
:attr:`~object.__slots__`) could behave incorrectly or crash instead of
3+
raising a:exc:`TypeError` when accessed via an instance of an invalid type.

‎Python/specialize.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,10 @@ _Py_Specialize_LoadAttr(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name)
688688
PyMemberDescrObject*member= (PyMemberDescrObject*)descr;
689689
structPyMemberDef*dmem=member->d_member;
690690
Py_ssize_toffset=dmem->offset;
691+
if (!PyObject_TypeCheck(owner,member->d_common.d_type)) {
692+
SPECIALIZATION_FAIL(LOAD_ATTR,SPEC_FAIL_EXPECTED_ERROR);
693+
gotofail;
694+
}
691695
if (dmem->flags&PY_AUDIT_READ) {
692696
SPECIALIZATION_FAIL(LOAD_ATTR,SPEC_FAIL_ATTR_AUDITED_SLOT);
693697
gotofail;
@@ -777,6 +781,10 @@ _Py_Specialize_StoreAttr(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name)
777781
PyMemberDescrObject*member= (PyMemberDescrObject*)descr;
778782
structPyMemberDef*dmem=member->d_member;
779783
Py_ssize_toffset=dmem->offset;
784+
if (!PyObject_TypeCheck(owner,member->d_common.d_type)) {
785+
SPECIALIZATION_FAIL(STORE_ATTR,SPEC_FAIL_EXPECTED_ERROR);
786+
gotofail;
787+
}
780788
if (dmem->flags&READONLY) {
781789
SPECIALIZATION_FAIL(STORE_ATTR,SPEC_FAIL_ATTR_READ_ONLY);
782790
gotofail;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp