• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Gallopsled / pwntools / 1

16 Jun 2020 04:28AM UTC coverage: 0.0% (-69.4%) from 69.405%
1

push

github

heapcrash
Re-enable UI tests in Github Actions

I suspect this will not work because of curses failing to initialize in GHA

0 of 15570 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/pwnlib/timeout.py
1
#!/usr/bin/env python2
2
# -*- coding: utf-8 -*-
3
"""
4
Timeout encapsulation, complete with countdowns and scope managers.
5
"""
6
from __future__ import division
×
7

8
import time
×
9

10
import pwnlib
×
11

12

13
class _DummyContextClass(object):
×
14
    def __enter__(self):   pass
×
15
    def __exit__(self,*a): pass
×
16

17
_DummyContext = _DummyContextClass()
×
18

19
class _countdown_handler(object):
×
20
    def __init__(self, obj, timeout):
×
21
        self.obj     = obj
×
22
        self.timeout = timeout
×
23

24
    def __enter__(self):
×
25
        self.old_timeout  = self.obj._timeout
×
26
        self.old_stop     = self.obj._stop
×
27

28
        self.obj._stop    = time.time() + self.timeout
×
29

30
        if self.old_stop:
×
31
            self.obj._stop = min(self.obj._stop, self.old_stop)
×
32

33
        self.obj._timeout = self.timeout
×
34
    def __exit__(self, *a):
×
35
        self.obj._timeout = self.old_timeout
×
36
        self.obj._stop    = self.old_stop
×
37

38
class _local_handler(object):
×
39
    def __init__(self, obj, timeout):
×
40
        self.obj     = obj
×
41
        self.timeout = timeout
×
42
    def __enter__(self):
×
43
        self.old_timeout  = self.obj._timeout
×
44
        self.old_stop     = self.obj._stop
×
45

46
        self.obj._stop    = 0
×
47
        self.obj._timeout = self.timeout # leverage validation
×
48
        self.obj.timeout_change()
×
49

50
    def __exit__(self, *a):
×
51
        self.obj._timeout = self.old_timeout
×
52
        self.obj._stop    = self.old_stop
×
53
        self.obj.timeout_change()
×
54

55
class TimeoutDefault(object):
×
56
    def __repr__(self): return "pwnlib.timeout.Timeout.default"
×
57
    def __str__(self): return "<default timeout>"
×
58

59
class Maximum(float):
×
60
    def __repr__(self):
×
61
        return 'pwnlib.timeout.maximum'
×
62
maximum = Maximum(2**20)
×
63

64
class Timeout(object):
×
65
    """
66
    Implements a basic class which has a timeout, and support for
67
    scoped timeout countdowns.
68

69
    Valid timeout values are:
70

71
    - ``Timeout.default`` use the global default value (``context.default``)
72
    - ``Timeout.forever`` or :const:`None` never time out
73
    - Any positive float, indicates timeouts in seconds
74

75
    Example:
76

77
        >>> context.timeout = 30
78
        >>> t = Timeout()
79
        >>> t.timeout == 30
80
        True
81
        >>> t = Timeout(5)
82
        >>> t.timeout == 5
83
        True
84
        >>> i = 0
85
        >>> with t.countdown():
86
        ...     print(4 <= t.timeout and t.timeout <= 5)
87
        ...
88
        True
89
        >>> with t.countdown(0.5):
90
        ...     while t.timeout:
91
        ...         print(round(t.timeout,1))
92
        ...         time.sleep(0.1)
93
        0.5
94
        0.4
95
        0.3
96
        0.2
97
        0.1
98
        >>> print(t.timeout)
99
        5.0
100
        >>> with t.local(0.5):
101
        ...     for i in range(5):
102
        ...         print(round(t.timeout,1))
103
        ...         time.sleep(0.1)
104
        0.5
105
        0.5
106
        0.5
107
        0.5
108
        0.5
109
        >>> print(t.timeout)
110
        5.0
111
    """
112

113

114
    #: Value indicating that the timeout should not be changed
115
    default = TimeoutDefault()
×
116

117
    #: Value indicating that a timeout should not ever occur
118
    forever = None
×
119

120
    #: Maximum value for a timeout.  Used to get around platform issues
121
    #: with very large timeouts.
122
    #:
123
    #: OSX does not permit setting socket timeouts to 2**22.
124
    #: Assume that if we receive a timeout of 2**21 or greater,
125
    #: that the value is effectively infinite.
126
    maximum = maximum
×
127

128
    def __init__(self, timeout=default):
×
129
        self._stop    = 0
×
130
        self.timeout = self._get_timeout_seconds(timeout)
×
131

132
    @property
×
133
    def timeout(self):
×
134
        """
135
        Timeout for obj operations.  By default, uses ``context.timeout``.
136
        """
137
        timeout = self._timeout
×
138
        stop    = self._stop
×
139

140
        if not stop:
×
141
            return timeout
×
142

143
        return max(stop-time.time(), 0)
×
144

145
    @timeout.setter
×
146
    def timeout(self, value):
×
147
        assert not self._stop
×
148
        self._timeout = self._get_timeout_seconds(value)
×
149
        self.timeout_change()
×
150

151
    def _get_timeout_seconds(self, value):
×
152
        if value is self.default:
×
153
            value = pwnlib.context.context.timeout
×
154

155
        elif value is self.forever:
×
156
            value = self.maximum
×
157

158
        else:
159
            value = float(value)
×
160

161
            if value is value < 0:
×
162
                raise AttributeError("timeout: Timeout cannot be negative")
×
163

164
            if value > self.maximum:
×
165
                value = self.maximum
×
166
        return value
×
167

168
    def countdown_active(self):
×
169
        return (self._stop == 0) or (self._stop > time.time())
×
170

171
    def timeout_change(self):
×
172
        """
173
        Callback for subclasses to hook a timeout change.
174
        """
175
        pass
×
176

177
    def countdown(self, timeout = default):
×
178
        """
179
        Scoped timeout setter.  Sets the timeout within the scope,
180
        and restores it when leaving the scope.
181

182
        When accessing :attr:`timeout` within the scope, it will be
183
        calculated against the time when the scope was entered, in a
184
        countdown fashion.
185

186
        If :const:`None` is specified for ``timeout``, then the current
187
        timeout is used is made.  This allows :const:`None` to be specified
188
        as a default argument with less complexity.
189
        """
190
        # Don't count down from infinity
191
        if timeout is self.maximum:
×
192
            return _DummyContext
×
193

194
        if timeout is self.default and self.timeout is self.maximum:
×
195
            return _DummyContext
×
196

197
        if timeout is self.default:
×
198
            timeout = self._timeout
×
199

200
        return _countdown_handler(self, timeout)
×
201

202
    def local(self, timeout):
×
203
        """
204
        Scoped timeout setter.  Sets the timeout within the scope,
205
        and restores it when leaving the scope.
206
        """
207
        if timeout is self.default or timeout == self.timeout:
×
208
            return _DummyContext
×
209

210
        return _local_handler(self, timeout)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc