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

Gallopsled / pwntools / 12797806857

15 Jan 2025 10:04PM UTC coverage: 71.089% (-2.6%) from 73.646%
12797806857

push

github

peace-maker
Drop Python 2.7 support / Require Python 3.10

Only test on Python 3 and bump minimal required python version to 3.10.

3628 of 6402 branches covered (56.67%)

12848 of 18073 relevant lines covered (71.09%)

0.71 hits per line

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

46.3
/pwnlib/py2compat.py
1
"""
2
Compatibility layer with python 2, allowing us to write normal code.
3
Beware, some monkey-patching is done.
4
"""
5

6
import os
1✔
7
import shutil
1✔
8
import sys
1✔
9
try:
1✔
10
    import fcntl
1✔
11
    import termios
1✔
12
except ImportError:
×
13
    pass
×
14

15
from collections import namedtuple
1✔
16
from struct import Struct
1✔
17

18
def py2_monkey_patch(module):
1✔
19
    def decorator(f):
1✔
20
        if sys.version_info < (3,):
1!
21
            f.__module__ = module.__name__
×
22
            setattr(module, f.__name__, f)
×
23
    return decorator
1✔
24

25
# python3 -c 'import shutil,inspect; print(inspect.getsource(shutil.get_terminal_size))'
26
@py2_monkey_patch(shutil)
1✔
27
def get_terminal_size(fallback=(80, 24)):
1✔
28
    """Get the size of the terminal window.
29

30
    For each of the two dimensions, the environment variable, COLUMNS
31
    and LINES respectively, is checked. If the variable is defined and
32
    the value is a positive integer, it is used.
33

34
    When COLUMNS or LINES is not defined, which is the common case,
35
    the terminal connected to sys.__stdout__ is queried
36
    by invoking os.get_terminal_size.
37

38
    If the terminal size cannot be successfully queried, either because
39
    the system doesn't support querying, or because we are not
40
    connected to a terminal, the value given in fallback parameter
41
    is used. Fallback defaults to (80, 24) which is the default
42
    size used by many terminal emulators.
43

44
    The value returned is a named tuple of type os.terminal_size.
45
    """
46
    # columns, lines are the working values
47
    try:
×
48
        columns = int(os.environ['COLUMNS'])
×
49
    except (KeyError, ValueError):
×
50
        columns = 0
×
51

52
    try:
×
53
        lines = int(os.environ['LINES'])
×
54
    except (KeyError, ValueError):
×
55
        lines = 0
×
56

57
    # only query if necessary
58
    if columns <= 0 or lines <= 0:
×
59
        try:
×
60
            size = os.get_terminal_size(sys.__stdout__.fileno())
×
61
        except (AttributeError, ValueError, IOError):
×
62
            # stdout is None, closed, detached, or not a terminal, or
63
            # os.get_terminal_size() is unsupported
64
            size = os.terminal_size(fallback)
×
65
        if columns <= 0:
×
66
            columns = size.columns
×
67
        if lines <= 0:
×
68
            lines = size.lines
×
69

70
    return os.terminal_size((columns, lines))
×
71

72
@py2_monkey_patch(os)
1✔
73
class terminal_size(tuple):
1✔
74
    @property
1✔
75
    def columns(self):
1✔
76
        return self[0]
×
77

78
    @property
1✔
79
    def lines(self):
1✔
80
        return self[1]
×
81

82
    def __repr__(self):
1✔
83
        return 'os.terminal_size(columns=%r, lines=%r)' % self
×
84

85
terminal_size = namedtuple('terminal_size', 'columns lines')
1✔
86

87
termsize = Struct('HHHH')
1✔
88

89
@py2_monkey_patch(os)
1✔
90
def get_terminal_size(fd):  # pylint: disable=function-redefined
1✔
91
    arr = b'\0' * termsize.size
×
92
    arr = fcntl.ioctl(fd, termios.TIOCGWINSZ, arr)
×
93
    lines, columns, xpixel, ypixel = termsize.unpack(arr)
×
94
    return os.terminal_size((columns, lines))
×
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