X Tutup
Skip to content
This repository was archived by the owner on Jul 22, 2023. It is now read-only.

Commit 11445f5

Browse files
committed
Refactor utility functions
1 parent 429b516 commit 11445f5

File tree

9 files changed

+315
-401
lines changed

9 files changed

+315
-401
lines changed

src/tests/leaktest.py

Lines changed: 6 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66

77
import clr
88
import gc
9+
import sys
910

1011
import System
1112

1213
from _compat import range
14+
from utils import (CallableHandler, ClassMethodHandler, GenericHandler,
15+
HelloClass, StaticMethodHandler, VarCallableHandler,
16+
VariableArgsHandler, hello_func)
1317

1418

1519
class LeakTest(object):
@@ -53,7 +57,6 @@ def run(self):
5357
self.testDelegates()
5458

5559
def report(self):
56-
import sys, gc
5760
gc.collect()
5861
dicttype = type({})
5962
for item in gc.get_objects():
@@ -78,7 +81,6 @@ def testModules(self):
7881
def testClasses(self):
7982
from System.Collections import Hashtable
8083
from Python.Test import StringDelegate
81-
from System import Int32
8284

8385
self.notify("Running class leak check...")
8486

@@ -91,7 +93,7 @@ def testClasses(self):
9193
del x
9294

9395
# Value type
94-
x = Int32(99)
96+
x = System.Int32(99)
9597
del x
9698

9799
# Delegate type
@@ -101,7 +103,7 @@ def testClasses(self):
101103
self.end_test()
102104

103105
def testEnumerations(self):
104-
from Python import Test
106+
import Python.Test as Test
105107

106108
self.notify("Running enum leak check...")
107109

@@ -215,7 +217,6 @@ def handler(sender, args, dict=dict):
215217

216218
def testDelegates(self):
217219
from Python.Test import DelegateTest, StringDelegate
218-
import System
219220

220221
self.notify("Running delegate leak check...")
221222

@@ -326,92 +327,6 @@ def testDelegates(self):
326327
self.end_test()
327328

328329

329-
class GenericHandler(object):
330-
"""A generic handler to test event callbacks."""
331-
332-
def __init__(self):
333-
self.value = None
334-
335-
def handler(self, sender, args):
336-
self.value = args.value
337-
338-
339-
class VariableArgsHandler(object):
340-
"""A variable args handler to test event callbacks."""
341-
342-
def __init__(self):
343-
self.value = None
344-
345-
def handler(self, *args):
346-
ob, eventargs = args
347-
self.value = eventargs.value
348-
349-
350-
class CallableHandler(object):
351-
"""A callable handler to test event callbacks."""
352-
353-
def __init__(self):
354-
self.value = None
355-
356-
def __call__(self, sender, args):
357-
self.value = args.value
358-
359-
360-
class VarCallableHandler(object):
361-
"""A variable args callable handler to test event callbacks."""
362-
363-
def __init__(self):
364-
self.value = None
365-
366-
def __call__(self, *args):
367-
ob, eventargs = args
368-
self.value = eventargs.value
369-
370-
371-
class StaticMethodHandler(object):
372-
"""A static method handler to test event callbacks."""
373-
374-
value = None
375-
376-
def handler(sender, args):
377-
StaticMethodHandler.value = args.value
378-
379-
handler = staticmethod(handler)
380-
381-
382-
class ClassMethodHandler(object):
383-
"""A class method handler to test event callbacks."""
384-
385-
value = None
386-
387-
def handler(cls, sender, args):
388-
cls.value = args.value
389-
390-
handler = classmethod(handler)
391-
392-
393-
class HelloClass(object):
394-
def hello(self):
395-
return "hello"
396-
397-
def __call__(self):
398-
return "hello"
399-
400-
def s_hello():
401-
return "hello"
402-
403-
s_hello = staticmethod(s_hello)
404-
405-
def c_hello(cls):
406-
return "hello"
407-
408-
c_hello = classmethod(c_hello)
409-
410-
411-
def hello_func():
412-
return "hello"
413-
414-
415330
if __name__ == '__main__':
416331
test = LeakTest()
417332
test.run()

src/tests/test_class.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
from _compat import DictProxyType, range
99

1010

11+
class ClassicClass:
12+
def kind(self):
13+
return 'classic'
14+
15+
16+
class NewStyleClass(object):
17+
def kind(self):
18+
return 'new-style'
19+
20+
1121
class ClassTests(unittest.TestCase):
1222
"""Test CLR class support."""
1323

@@ -273,15 +283,5 @@ def PyCallback(self, self2):
273283
self.assertTrue(testobj.SameReference)
274284

275285

276-
class ClassicClass:
277-
def kind(self):
278-
return 'classic'
279-
280-
281-
class NewStyleClass(object):
282-
def kind(self):
283-
return 'new-style'
284-
285-
286286
def test_suite():
287287
return unittest.makeSuite(ClassTests)

src/tests/test_compat.py

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,18 @@
44
import unittest
55

66
from _compat import ClassType, PY2, PY3, range
7+
from utils import isCLRClass, isCLRModule, isCLRRootModule
78

89

910
class CompatibilityTests(unittest.TestCase):
10-
"""
11-
Backward-compatibility tests for deprecated features.
12-
"""
13-
14-
def isCLRModule(self, object):
15-
return type(object).__name__ == 'ModuleObject'
16-
17-
def isCLRRootModule(self, object):
18-
if PY3:
19-
# in Python 3 the clr module is a normal python module
20-
return object.__name__ == "clr"
21-
elif PY2:
22-
return type(object).__name__ == 'CLRModule'
23-
24-
def isCLRClass(self, object):
25-
return type(object).__name__ == 'CLR Metatype' # for now
11+
"""Backward-compatibility tests for deprecated features."""
2612

2713
# Tests for old-style CLR-prefixed module naming.
2814

2915
def testSimpleImport(self):
3016
"""Test simple import."""
3117
import CLR
32-
self.assertTrue(self.isCLRRootModule(CLR))
18+
self.assertTrue(isCLRRootModule(CLR))
3319
self.assertTrue(CLR.__name__ == 'clr')
3420

3521
import sys
@@ -49,7 +35,7 @@ def testSimpleImport(self):
4935
def testSimpleImportWithAlias(self):
5036
"""Test simple import with aliasing."""
5137
import CLR as myCLR
52-
self.assertTrue(self.isCLRRootModule(myCLR))
38+
self.assertTrue(isCLRRootModule(myCLR))
5339
self.assertTrue(myCLR.__name__ == 'clr')
5440

5541
import sys as mySys
@@ -69,11 +55,11 @@ def testSimpleImportWithAlias(self):
6955
def testDottedNameImport(self):
7056
"""Test dotted-name import."""
7157
import CLR.System
72-
self.assertTrue(self.isCLRModule(CLR.System))
58+
self.assertTrue(isCLRModule(CLR.System))
7359
self.assertTrue(CLR.System.__name__ == 'System')
7460

7561
import System
76-
self.assertTrue(self.isCLRModule(System))
62+
self.assertTrue(isCLRModule(System))
7763
self.assertTrue(System.__name__ == 'System')
7864

7965
self.assertTrue(System is CLR.System)
@@ -85,11 +71,11 @@ def testDottedNameImport(self):
8571
def testDottedNameImportWithAlias(self):
8672
"""Test dotted-name import with aliasing."""
8773
import CLR.System as myCLRSystem
88-
self.assertTrue(self.isCLRModule(myCLRSystem))
74+
self.assertTrue(isCLRModule(myCLRSystem))
8975
self.assertTrue(myCLRSystem.__name__ == 'System')
9076

9177
import System as mySystem
92-
self.assertTrue(self.isCLRModule(mySystem))
78+
self.assertTrue(isCLRModule(mySystem))
9379
self.assertTrue(mySystem.__name__ == 'System')
9480

9581
self.assertTrue(mySystem is myCLRSystem)
@@ -101,7 +87,7 @@ def testDottedNameImportWithAlias(self):
10187
def testSimpleImportFrom(self):
10288
"""Test simple 'import from'."""
10389
from CLR import System
104-
self.assertTrue(self.isCLRModule(System))
90+
self.assertTrue(isCLRModule(System))
10591
self.assertTrue(System.__name__ == 'System')
10692

10793
from xml import dom
@@ -111,7 +97,7 @@ def testSimpleImportFrom(self):
11197
def testSimpleImportFromWithAlias(self):
11298
"""Test simple 'import from' with aliasing."""
11399
from CLR import System as mySystem
114-
self.assertTrue(self.isCLRModule(mySystem))
100+
self.assertTrue(isCLRModule(mySystem))
115101
self.assertTrue(mySystem.__name__ == 'System')
116102

117103
from xml import dom as myDom
@@ -121,11 +107,11 @@ def testSimpleImportFromWithAlias(self):
121107
def testDottedNameImportFrom(self):
122108
"""Test dotted-name 'import from'."""
123109
from CLR.System import Xml
124-
self.assertTrue(self.isCLRModule(Xml))
110+
self.assertTrue(isCLRModule(Xml))
125111
self.assertTrue(Xml.__name__ == 'System.Xml')
126112

127113
from CLR.System.Xml import XmlDocument
128-
self.assertTrue(self.isCLRClass(XmlDocument))
114+
self.assertTrue(isCLRClass(XmlDocument))
129115
self.assertTrue(XmlDocument.__name__ == 'XmlDocument')
130116

131117
from xml.dom import pulldom
@@ -139,11 +125,11 @@ def testDottedNameImportFrom(self):
139125
def testDottedNameImportFromWithAlias(self):
140126
"""Test dotted-name 'import from' with aliasing."""
141127
from CLR.System import Xml as myXml
142-
self.assertTrue(self.isCLRModule(myXml))
128+
self.assertTrue(isCLRModule(myXml))
143129
self.assertTrue(myXml.__name__ == 'System.Xml')
144130

145131
from CLR.System.Xml import XmlDocument as myXmlDocument
146-
self.assertTrue(self.isCLRClass(myXmlDocument))
132+
self.assertTrue(isCLRClass(myXmlDocument))
147133
self.assertTrue(myXmlDocument.__name__ == 'XmlDocument')
148134

149135
from xml.dom import pulldom as myPulldom
@@ -159,12 +145,12 @@ def testFromModuleImportStar(self):
159145
count = len(locals().keys())
160146
m = __import__('CLR.System.Management', globals(), locals(), ['*'])
161147
self.assertTrue(m.__name__ == 'System.Management')
162-
self.assertTrue(self.isCLRModule(m))
148+
self.assertTrue(isCLRModule(m))
163149
self.assertTrue(len(locals().keys()) > count + 1)
164150

165151
m2 = __import__('System.Management', globals(), locals(), ['*'])
166152
self.assertTrue(m2.__name__ == 'System.Management')
167-
self.assertTrue(self.isCLRModule(m2))
153+
self.assertTrue(isCLRModule(m2))
168154
self.assertTrue(len(locals().keys()) > count + 1)
169155

170156
self.assertTrue(m is m2)
@@ -193,7 +179,7 @@ def testImplicitLoadAlreadyValidNamespace(self):
193179
# Python runtime to "do the right thing", allowing types from both
194180
# assemblies to be found in the CLR.System module implicitly.
195181
import CLR.System
196-
self.assertTrue(self.isCLRClass(CLR.System.UriBuilder))
182+
self.assertTrue(isCLRClass(CLR.System.UriBuilder))
197183

198184
def testImportNonExistantModule(self):
199185
"""Test import failure for a non-existant module."""
@@ -211,7 +197,7 @@ def testLookupNoNamespaceType(self):
211197
"""Test lookup of types without a qualified namespace."""
212198
import CLR.Python.Test
213199
import CLR
214-
self.assertTrue(self.isCLRClass(CLR.NoNamespaceType))
200+
self.assertTrue(isCLRClass(CLR.NoNamespaceType))
215201

216202
def testModuleLookupRecursion(self):
217203
"""Test for recursive lookup handling."""
@@ -232,10 +218,10 @@ def testModuleGetAttr(self):
232218
import CLR.System as System
233219

234220
int_type = System.Int32
235-
self.assertTrue(self.isCLRClass(int_type))
221+
self.assertTrue(isCLRClass(int_type))
236222

237223
module = System.Xml
238-
self.assertTrue(self.isCLRModule(module))
224+
self.assertTrue(isCLRModule(module))
239225

240226
def test():
241227
spam = System.Spam

0 commit comments

Comments
 (0)
X Tutup