Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1a2499b

Browse files
authored
Merge pull request #273 from ArvidJB/isinstance_refcount_leak
Calling isinstance(x,t) repeatedly when x or t is not actually a CLR …
2 parents 85db427 + b68e7a1 commit 1a2499b

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/runtime/metatype.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,10 @@ static IntPtr DoInstanceCheck(IntPtr tp, IntPtr args, bool checkType)
281281
{
282282
ClassBase cb = GetManagedObject(tp) as ClassBase;
283283

284-
if (cb == null)
284+
if (cb == null) {
285+
Runtime.Incref(Runtime.PyFalse);
285286
return Runtime.PyFalse;
287+
}
286288

287289
using (PyList argsObj = new PyList(args))
288290
{
@@ -296,12 +298,16 @@ static IntPtr DoInstanceCheck(IntPtr tp, IntPtr args, bool checkType)
296298
else
297299
otherType = arg.GetPythonType();
298300

299-
if (Runtime.PyObject_TYPE(otherType.Handle) != PyCLRMetaType)
301+
if (Runtime.PyObject_TYPE(otherType.Handle) != PyCLRMetaType) {
302+
Runtime.Incref(Runtime.PyFalse);
300303
return Runtime.PyFalse;
304+
}
301305

302306
ClassBase otherCb = GetManagedObject(otherType.Handle) as ClassBase;
303-
if (otherCb == null)
307+
if (otherCb == null) {
308+
Runtime.Incref(Runtime.PyFalse);
304309
return Runtime.PyFalse;
310+
}
305311

306312
return Converter.ToPython(cb.type.IsAssignableFrom(otherCb.type));
307313
}

src/tests/test_subclass.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,21 @@ def handler(self, x, args):
147147
self.assertEqual(event_handler.value, 3)
148148
self.assertEqual(len(d.event_handlers), 1)
149149

150+
def test_isinstance(self):
151+
from System import Object
152+
from System import String
153+
154+
a = [str(x) for x in range(0, 1000)]
155+
b = [String(x) for x in a]
156+
157+
for x in a:
158+
self.assertFalse(isinstance(x, Object))
159+
self.assertFalse(isinstance(x, String))
160+
161+
for x in b:
162+
self.assertTrue(isinstance(x, Object))
163+
self.assertTrue(isinstance(x, String))
164+
150165

151166
def test_suite():
152167
return unittest.makeSuite(SubClassTests)

0 commit comments

Comments
 (0)