File tree Expand file tree Collapse file tree 2 files changed +28
-9
lines changed
Expand file tree Collapse file tree 2 files changed +28
-9
lines changed Original file line number Diff line number Diff line change 99 "cchardet" ,
1010]
1111
12- test_deps = [
13- # Test timeouts
14- "wrapt-timeout-decorator" ,
15- ]
16-
1712extras = {
1813 'speed' : speed_deps ,
19- 'test' : test_deps ,
2014}
2115
2216# Adapted from https://github.com/pypa/pip/blob/master/setup.py
@@ -54,7 +48,6 @@ def find_version(*file_paths):
5448 "lxml-html-clean; python_version < '3.11'" ,
5549 "cssselect"
5650 ],
57- tests_require = test_deps ,
5851 extras_require = extras ,
5952 classifiers = [
6053 "Environment :: Web Environment" ,
Original file line number Diff line number Diff line change 11import os
2+ import time
23import unittest
34
45from readability import Document
5- from wrapt_timeout_decorator import *
6+ from functools import wraps
7+
8+
9+ class TimeoutException (Exception ):
10+ """Exception raised when a function exceeds its time limit."""
11+ pass
12+
13+
14+ def timeout (seconds ):
15+ """Decorator to enforce a timeout on function execution."""
16+ def decorator (func ):
17+ @wraps (func )
18+ def wrapper (* args , ** kwargs ):
19+ start_time = time .perf_counter ()
20+ result = func (* args , ** kwargs )
21+ end_time = time .perf_counter ()
22+ elapsed_time = end_time - start_time
23+ if elapsed_time > seconds :
24+ raise TimeoutException (
25+ f"Function '{ func .__name__ } ' exceeded time limit of { seconds } seconds "
26+ f"with an execution time of { elapsed_time :.4f} seconds"
27+ )
28+ return result
29+ return wrapper
30+ return decorator
31+
632
733SAMPLES = os .path .join (os .path .dirname (__file__ ), "samples" )
834
@@ -100,7 +126,7 @@ def test_correct_cleanup(self):
100126 assert not "aside" in s
101127
102128 # Many spaces make some regexes run forever
103- @timeout (3 , use_signals = False )
129+ @timeout (3 )
104130 def test_many_repeated_spaces (self ):
105131 long_space = " " * 1000000
106132 sample = "<html><body><p>foo" + long_space + "</p></body></html>"
You can’t perform that action at this time.
0 commit comments