| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
import sys |
| 19 |
import _jpype |
| 20 |
import jpype |
| 21 |
from jpype.types import * |
| 22 |
import common |
| 23 |
import types |
| 24 |
import functools |
| 25 |
import inspect |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
def copy_func(f): |
| 32 |
"""Based on http://stackoverflow.com/a/6528148/190597 (Glenn Maynard)""" |
| 33 |
g = types.FunctionType(f.__code__, f.__globals__, name=f.__name__, |
| 34 |
argdefs=f.__defaults__, |
| 35 |
closure=f.__closure__) |
| 36 |
g.__kwdefaults__ = f.__kwdefaults__ |
| 37 |
g = functools.update_wrapper(g, f) |
| 38 |
return g |
| 39 |
|
| 40 |
|
| 41 |
class JMethodTestCase(common.JPypeTestCase): |
| 42 |
""" Test for methods of JMethod (_jpype._JMethod) |
| 43 |
|
| 44 |
This should test how well the object matchs a Python3 function. |
| 45 |
* __self__: should appear on a bound, None otherwise. |
| 46 |
* __name__: should be set |
| 47 |
* __qualname__: should be set |
| 48 |
* __doc__: should be set |
| 49 |
* __annotations__: should give return type |
| 50 |
* __defaults__, __kwdefaults__, __code__, __globals__, __closure__: should |
| 51 |
be enough to "clone" the method. |
| 52 |
|
| 53 |
It should also be callable as a method, class method. |
| 54 |
|
| 55 |
Further inspect should work |
| 56 |
* inspect.getdoc() should match __doc__ |
| 57 |
* inspect.signature() should work |
| 58 |
* inspect.isroutine() should be True |
| 59 |
|
| 60 |
We are not going to try to pretend to be Python2. |
| 61 |
""" |
| 62 |
|
| 63 |
def setUp(self): |
| 64 |
common.JPypeTestCase.setUp(self) |
| 65 |
self.cls = jpype.JClass('java.lang.String') |
| 66 |
self.obj = self.cls('foo') |
| 67 |
|
| 68 |
def testMethodSelf(self): |
| 69 |
self.assertEqual(self.cls.substring.__self__, None) |
| 70 |
self.assertEqual(self.obj.substring.__self__, self.obj) |
| 71 |
|
| 72 |
def testMethodName(self): |
| 73 |
self.assertEqual(self.cls.substring.__name__, "substring") |
| 74 |
self.assertEqual(self.obj.substring.__name__, "substring") |
| 75 |
|
| 76 |
def testMethodQualName(self): |
| 77 |
self.assertEqual(self.cls.substring.__qualname__, |
| 78 |
"java.lang.String.substring") |
| 79 |
self.assertEqual(self.obj.substring.__qualname__, |
| 80 |
"java.lang.String.substring") |
| 81 |
|
| 82 |
def testMethodDoc(self): |
| 83 |
self.assertIsInstance(self.cls.substring.__doc__, str) |
| 84 |
self.assertIsInstance(self.obj.substring.__doc__, str) |
| 85 |
d = self.cls.substring.__doc__ |
| 86 |
self.cls.substring.__doc__ = None |
| 87 |
self.assertIsNone(self.cls.substring.__doc__) |
| 88 |
self.assertIsNone(self.obj.substring.__doc__) |
| 89 |
self.cls.substring.__doc__ = d |
| 90 |
|
| 91 |
def testMethodInspectDoc(self): |
| 92 |
self.assertIsInstance(inspect.getdoc(self.cls.substring), str) |
| 93 |
self.assertIsInstance(inspect.getdoc(self.obj.substring), str) |
| 94 |
self.assertIsInstance(inspect.getdoc(self.obj.format), str) |
| 95 |
|
| 96 |
def testMethodAnnotations(self): |
| 97 |
self.assertIsInstance(self.cls.substring.__annotations__, dict) |
| 98 |
self.assertIsNotNone(self.obj.substring.__annotations__) |
| 99 |
a = self.cls.substring.__annotations__ |
| 100 |
d = {} |
| 101 |
self.cls.substring.__annotations__ = d |
| 102 |
self.assertEqual(self.cls.substring.__annotations__, d) |
| 103 |
self.cls.substring.__annotations__ = a |
| 104 |
self.assertIsNotNone(self.cls.substring.__annotations__) |
| 105 |
|
| 106 |
|
| 107 |
self.assertEqual(self.cls.substring.__annotations__[ |
| 108 |
"return"], self.cls) |
| 109 |
self.assertEqual(self.cls.trim.__annotations__[ |
| 110 |
"return"], self.cls) |
| 111 |
self.assertEqual(self.cls.getBytes.__annotations__, {}) |
| 112 |
|
| 113 |
def testMethodInspectSignature(self): |
| 114 |
self.assertIsInstance(inspect.signature( |
| 115 |
self.cls.substring), inspect.Signature) |
| 116 |
self.assertIsInstance(inspect.signature( |
| 117 |
self.obj.substring), inspect.Signature) |
| 118 |
self.assertEqual(inspect.signature( |
| 119 |
self.obj.substring).return_annotation, self.cls) |
| 120 |
|
| 121 |
def testMethodInspectFunction(self): |
| 122 |
self.assertTrue(inspect.isfunction(self.cls.substring)) |
| 123 |
self.assertTrue(inspect.isfunction(self.obj.substring)) |
| 124 |
|
| 125 |
def testMethodInspectRoutine(self): |
| 126 |
self.assertTrue(inspect.isroutine(self.cls.substring)) |
| 127 |
self.assertTrue(inspect.isroutine(self.obj.substring)) |
| 128 |
|
| 129 |
def testMethodClassCall(self): |
| 130 |
self.assertEqual(self.cls.substring(self.obj, 1), "oo") |
| 131 |
|
| 132 |
def testMethodClassCallWierd(self): |
| 133 |
self.assertEqual(self.cls.substring("foo", 1), "oo") |
| 134 |
|
| 135 |
def testMethodClassCallFail(self): |
| 136 |
with self.assertRaises(TypeError): |
| 137 |
self.cls.substring(1, 1) |
| 138 |
|
| 139 |
def testMethodCall(self): |
| 140 |
self.assertEqual(self.obj.substring(1), "oo") |
| 141 |
|
| 142 |
def testMethodClone(self): |
| 143 |
a = copy_func(self.cls.substring) |
| 144 |
self.assertEqual(a(self.obj, 1), "oo") |
| 145 |
a = copy_func(self.obj.substring) |
| 146 |
self.assertEqual(a(1), "oo") |
| 147 |
|
| 148 |
def testMethodDump(self): |
| 149 |
|
| 150 |
self.assertIsInstance(jpype.JString( |
| 151 |
"foo").substring.matchReport(), str) |
| 152 |
|
| 153 |
def testMethodHelp(self): |
| 154 |
import io |
| 155 |
import contextlib |
| 156 |
f = io.StringIO() |
| 157 |
with contextlib.redirect_stdout(f): |
| 158 |
help(jpype.JString("a").substring) |
| 159 |
s = f.getvalue() |
| 160 |
self.assertTrue("Java method dispatch" in s) |
| 161 |
self.assertTrue("substring(int)" in s) |
| 162 |
self.assertTrue("substring(int, int)" in s) |
| 163 |
|
| 164 |
@common.requireInstrumentation |
| 165 |
def testJMethod_get(self): |
| 166 |
fixture = JClass("jpype.common.Fixture")() |
| 167 |
_jpype.fault("PyJPMethod_get") |
| 168 |
with self.assertRaisesRegex(SystemError, "fault"): |
| 169 |
fixture.callInt(1) |
| 170 |
|
| 171 |
@common.requireInstrumentation |
| 172 |
def testJMethod_str(self): |
| 173 |
fixture = JClass("jpype.common.Fixture")() |
| 174 |
_jpype.fault("PyJPMethod_get") |
| 175 |
with self.assertRaisesRegex(SystemError, "fault"): |
| 176 |
str(fixture.callInt) |
| 177 |
|
| 178 |
@common.requireInstrumentation |
| 179 |
def testJMethod_str_2(self): |
| 180 |
Fixture = JClass("jpype.common.Fixture") |
| 181 |
fixture = Fixture() |
| 182 |
_jpype.fault("PyJPMethod_get") |
| 183 |
with self.assertRaisesRegex(SystemError, "fault"): |
| 184 |
repr(fixture.callInt) |
| 185 |
repr(Fixture.callInt) |
| 186 |
repr(fixture.callInt) |
| 187 |
|
| 188 |
@common.requireInstrumentation |
| 189 |
def testJMethod_selfFault(self): |
| 190 |
fixture = JClass("jpype.common.Fixture")() |
| 191 |
_jpype.fault("PyJPMethod_getSelf") |
| 192 |
with self.assertRaisesRegex(SystemError, "fault"): |
| 193 |
fixture.callInt.__self__ |
| 194 |
|
| 195 |
@common.requireInstrumentation |
| 196 |
def testJMethod_nameFault(self): |
| 197 |
fixture = JClass("jpype.common.Fixture")() |
| 198 |
_jpype.fault("PyJPMethod_getName") |
| 199 |
with self.assertRaisesRegex(SystemError, "fault"): |
| 200 |
fixture.callInt.__name__ |
| 201 |
|
| 202 |
@common.requireInstrumentation |
| 203 |
def testJMethod_qualnameFault(self): |
| 204 |
fixture = JClass("jpype.common.Fixture")() |
| 205 |
_jpype.fault("PyJPMethod_getQualName") |
| 206 |
with self.assertRaisesRegex(SystemError, "fault"): |
| 207 |
fixture.callInt.__qualname__ |
| 208 |
|
| 209 |
@common.requireInstrumentation |
| 210 |
def testJMethod_annotationsFault(self): |
| 211 |
fixture = JClass("jpype.common.Fixture")() |
| 212 |
_jpype.fault("PyJPMethod_getAnnotations") |
| 213 |
with self.assertRaisesRegex(SystemError, "fault"): |
| 214 |
fixture.callInt.__annotations__ |
| 215 |
|
| 216 |
@common.requireInstrumentation |
| 217 |
def testJMethod_docFault(self): |
| 218 |
fixture = JClass("jpype.common.Fixture")() |
| 219 |
_jpype.fault("PyJPMethod_getDoc") |
| 220 |
with self.assertRaisesRegex(SystemError, "fault"): |
| 221 |
fixture.callInt.__doc__ |
| 222 |
_jpype.fault("PyJPMethod_setDoc") |
| 223 |
with self.assertRaisesRegex(SystemError, "fault"): |
| 224 |
fixture.callInt.__doc__ = None |
| 225 |
|
| 226 |
@common.requireInstrumentation |
| 227 |
def testJMethod_docFault_2(self): |
| 228 |
fixture = JClass("jpype.common.Fixture")() |
| 229 |
_jpype.fault("PyJPMethod_getCodeAttr") |
| 230 |
with self.assertRaisesRegex(SystemError, "fault"): |
| 231 |
fixture.callFloat.__code__ |
| 232 |
|
| 233 |
@common.requireInstrumentation |
| 234 |
def testJMethod_beansFault(self): |
| 235 |
fixture = JClass("jpype.common.Fixture")() |
| 236 |
_jpype.fault("PyJPMethod_isBeanAccessor") |
| 237 |
with self.assertRaisesRegex(SystemError, "fault"): |
| 238 |
fixture.callInt._isBeanAccessor() |
| 239 |
_jpype.fault("PyJPMethod_isBeanMutator") |
| 240 |
with self.assertRaisesRegex(SystemError, "fault"): |
| 241 |
fixture.callInt._isBeanMutator() |
| 242 |
|
| 243 |
@common.requireInstrumentation |
| 244 |
def testJMethod_diagnosticsFault(self): |
| 245 |
fixture = JClass("jpype.common.Fixture")() |
| 246 |
_jpype.fault("PyJPMethod_matchReport") |
| 247 |
with self.assertRaisesRegex(SystemError, "fault"): |
| 248 |
fixture.callInt.matchReport() |
| 249 |
|
| 250 |
@common.requireInstrumentation |
| 251 |
def testJMethod_callFault(self): |
| 252 |
fixture = JClass("jpype.common.Fixture")() |
| 253 |
_jpype.fault("PyJPMethod_call") |
| 254 |
with self.assertRaisesRegex(SystemError, "fault"): |
| 255 |
fixture.callInt(1) |
| 256 |
|
| 257 |
def testJMethod_self(self): |
| 258 |
Fixture = JClass("jpype.common.Fixture") |
| 259 |
fixture = JClass("jpype.common.Fixture")() |
| 260 |
self.assertEqual(fixture.callInt.__self__, fixture) |
| 261 |
self.assertEqual(Fixture.callStaticInt.__self__, None) |
| 262 |
|
| 263 |
def testJMethod_name(self): |
| 264 |
fixture = JClass("jpype.common.Fixture")() |
| 265 |
self.assertIsInstance(fixture.callInt.__name__, str) |
| 266 |
self.assertEqual(fixture.callInt.__name__, 'callInt') |
| 267 |
|
| 268 |
def testJMethod_doc(self): |
| 269 |
fixture = JClass("jpype.common.Fixture")() |
| 270 |
self.assertIsInstance(fixture.callInt.__doc__, str) |
| 271 |
|
| 272 |
def testJMethod_annotations(self): |
| 273 |
fixture = JClass("jpype.common.Fixture")() |
| 274 |
self.assertIsInstance(fixture.callInt.__annotations__, dict) |
| 275 |
ann = fixture.callInt.__annotations__ |
| 276 |
expected = {'arg0': JInt, 'return': JInt} |
| 277 |
self.assertEqual(ann, expected) |
| 278 |
|
| 279 |
def testJMethod_closure(self): |
| 280 |
fixture = JClass("jpype.common.Fixture")() |
| 281 |
self.assertNotEqual(fixture.callInt.__closure__, None) |
| 282 |
|
| 283 |
def testJMethod_code(self): |
| 284 |
fixture = JClass("jpype.common.Fixture")() |
| 285 |
|
| 286 |
def f(): |
| 287 |
pass |
| 288 |
self.assertIsInstance(fixture.callInt.__code__, type(f.__code__)) |
| 289 |
|
| 290 |
def testJMethod_defaults(self): |
| 291 |
fixture = JClass("jpype.common.Fixture")() |
| 292 |
self.assertEqual(fixture.callInt.__defaults__, None) |
| 293 |
|
| 294 |
def testJMethod_kwdefaults(self): |
| 295 |
fixture = JClass("jpype.common.Fixture")() |
| 296 |
self.assertEqual(fixture.callInt.__kwdefaults__, None) |
| 297 |
|
| 298 |
def testJMethod_globals(self): |
| 299 |
fixture = JClass("jpype.common.Fixture")() |
| 300 |
self.assertIsInstance(fixture.callInt.__globals__, dict) |
| 301 |
|
| 302 |
def testJMethod_qualname(self): |
| 303 |
fixture = JClass("jpype.common.Fixture")() |
| 304 |
self.assertIsInstance(fixture.callInt.__qualname__, str) |
| 305 |
self.assertEqual(fixture.callInt.__qualname__, |
| 306 |
'jpype.common.Fixture.callInt') |
| 307 |
|
| 308 |
def testMatches(self): |
| 309 |
js = JClass("java.lang.String")() |
| 310 |
self.assertFalse(js.substring._matches(object())) |
| 311 |
self.assertTrue(js.substring._matches(1)) |
| 312 |
self.assertTrue(js.substring._matches(1, 2)) |
| 313 |
self.assertFalse(js.substring._matches(1, 2, 3)) |