Issue #850728: Add a *timeout* parameter to the `acquire()` method of
`threading.Semaphore` objects.  Original patch by Torsten Landschoff.
diff --git a/Lib/test/lock_tests.py b/Lib/test/lock_tests.py
index 74db3e4..a288176 100644
--- a/Lib/test/lock_tests.py
+++ b/Lib/test/lock_tests.py
@@ -521,6 +521,19 @@
         # ordered.
         self.assertEqual(sorted(results), [False] * 7 + [True] *  3 )
 
+    def test_acquire_timeout(self):
+        sem = self.semtype(2)
+        self.assertRaises(ValueError, sem.acquire, False, timeout=1.0)
+        self.assertTrue(sem.acquire(timeout=0.005))
+        self.assertTrue(sem.acquire(timeout=0.005))
+        self.assertFalse(sem.acquire(timeout=0.005))
+        sem.release()
+        self.assertTrue(sem.acquire(timeout=0.005))
+        t = time.time()
+        self.assertFalse(sem.acquire(timeout=0.5))
+        dt = time.time() - t
+        self.assertTimeout(dt, 0.5)
+
     def test_default_value(self):
         # The default initial value is 1.
         sem = self.semtype()
diff --git a/Lib/threading.py b/Lib/threading.py
index b4d07fe..2ca224e 100644
--- a/Lib/threading.py
+++ b/Lib/threading.py
@@ -290,8 +290,11 @@
         self._cond = Condition(Lock())
         self._value = value
 
-    def acquire(self, blocking=True):
+    def acquire(self, blocking=True, timeout=None):
+        if not blocking and timeout is not None:
+            raise ValueError("can't specify timeout for non-blocking acquire")
         rc = False
+        endtime = None
         self._cond.acquire()
         while self._value == 0:
             if not blocking:
@@ -299,7 +302,14 @@
             if __debug__:
                 self._note("%s.acquire(%s): blocked waiting, value=%s",
                            self, blocking, self._value)
-            self._cond.wait()
+            if timeout is not None:
+                if endtime is None:
+                    endtime = _time() + timeout
+                else:
+                    timeout = endtime - _time()
+                    if timeout <= 0:
+                        break
+            self._cond.wait(timeout)
         else:
             self._value = self._value - 1
             if __debug__: