def testJPMonitor_enter(self): jo = JClass("java.lang.Object")() _jpype.fault("PyJPMonitor_enter") with self.assertRaisesRegex(SystemError, "fault"): with _jpype._JMonitor(jo): pass _jpype.fault("PyJPModule_getContext") with self.assertRaisesRegex(SystemError, "fault"): with _jpype._JMonitor(jo): pass
def synchronized(obj): """ Creates a resource lock for a Java object. Produces a monitor object. During the lifespan of the monitor the Java will not be able to acquire a thread lock on the object. This will prevent multiple threads from modifying a shared resource. This should always be used as part of a Python ``with`` startment. Arguments: obj: A valid Java object shared by multiple threads. Example: .. code-block:: python with synchronized(obj): # modify obj values # lock is freed when with block ends """ try: return _jpype._JMonitor(obj) except AttributeError as ex: pass raise TypeError("synchronized only applies to java objects")
def testMonitorOnNull(self): value = jpype.JObject(None) with self.assertRaises(TypeError): _jpype._JMonitor(value)
def testMonitorStr(self): obj = jpype.java.lang.Object() monitor = _jpype._JMonitor(obj) self.assertIsInstance(str(monitor), str)
def testMonitorInitBad2(self): with self.assertRaises(TypeError): _jpype._JMonitor(None)
def testMonitorOnPrim(self): value = jpype.JInt(1) with self.assertRaises(TypeError): _jpype._JMonitor(value)
def testMonitorOnString(self): value = jpype.JString("foo") with self.assertRaises(TypeError): _jpype._JMonitor(value)
def testJPMonitor_str(self): jo = JClass("java.lang.Object")() jm = _jpype._JMonitor(jo) _jpype.fault("PyJPMonitor_str") with self.assertRaisesRegex(SystemError, "fault"): str(jm)