Пример #1
0
 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
Пример #2
0
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")
Пример #3
0
 def testMonitorOnNull(self):
     value = jpype.JObject(None)
     with self.assertRaises(TypeError):
         _jpype._JMonitor(value)
Пример #4
0
 def testMonitorStr(self):
     obj = jpype.java.lang.Object()
     monitor = _jpype._JMonitor(obj)
     self.assertIsInstance(str(monitor), str)
Пример #5
0
 def testMonitorInitBad2(self):
     with self.assertRaises(TypeError):
         _jpype._JMonitor(None)
Пример #6
0
 def testMonitorOnPrim(self):
     value = jpype.JInt(1)
     with self.assertRaises(TypeError):
         _jpype._JMonitor(value)
Пример #7
0
 def testMonitorOnString(self):
     value = jpype.JString("foo")
     with self.assertRaises(TypeError):
         _jpype._JMonitor(value)
Пример #8
0
 def testJPMonitor_str(self):
     jo = JClass("java.lang.Object")()
     jm = _jpype._JMonitor(jo)
     _jpype.fault("PyJPMonitor_str")
     with self.assertRaisesRegex(SystemError, "fault"):
         str(jm)