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

Gallopsled / pwntools / 1

29 May 2020 09:05AM UTC coverage: 0.0% (-72.4%) from 72.414%
1

push

github

layderv
__str__ to __bytes__ in python2

0 of 8 new or added lines in 2 files covered. (0.0%)

11301 existing lines in 133 files now uncovered.

0 of 15497 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
"""
UNCOV
6
from __future__ import division
×
7

UNCOV
8
import time
×
9

UNCOV
10
import pwnlib
×
11

12

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

UNCOV
17
_DummyContext = _DummyContextClass()
×
18

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

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

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

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

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

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

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

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

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

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

UNCOV
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
UNCOV
115
    default = TimeoutDefault()
×
116

117
    #: Value indicating that a timeout should not ever occur
UNCOV
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.
UNCOV
126
    maximum = maximum
×
127

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

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

UNCOV
140
        if not stop:
×
UNCOV
141
            return timeout
×
142

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

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

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

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

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

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

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

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

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

UNCOV
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
UNCOV
191
        if timeout is self.maximum:
×
192
            return _DummyContext
×
193

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

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

UNCOV
200
        return _countdown_handler(self, timeout)
×
201

UNCOV
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
        """
UNCOV
207
        if timeout is self.default or timeout == self.timeout:
×
UNCOV
208
            return _DummyContext
×
209

UNCOV
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