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

Gallopsled / pwntools / 7250412654

18 Dec 2023 03:44PM UTC coverage: 74.547% (+0.1%) from 74.452%
7250412654

push

github

web-flow
Merge branch 'dev' into retguard

4565 of 7244 branches covered (0.0%)

350 of 507 new or added lines in 17 files covered. (69.03%)

13 existing lines in 5 files now uncovered.

12843 of 17228 relevant lines covered (74.55%)

0.75 hits per line

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

94.44
/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✔
NEW
12
except ImportError:
×
NEW
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__
1✔
22
            setattr(module, f.__name__, f)
1✔
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:
1✔
48
        columns = int(os.environ['COLUMNS'])
1✔
49
    except (KeyError, ValueError):
1✔
50
        columns = 0
1✔
51

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

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

70
    return os.terminal_size((columns, lines))
1✔
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]
1✔
77

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

82
    def __repr__(self):
1✔
NEW
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
1✔
92
    arr = fcntl.ioctl(fd, termios.TIOCGWINSZ, arr)
1✔
93
    lines, columns, xpixel, ypixel = termsize.unpack(arr)
1✔
94
    return os.terminal_size((columns, lines))
1✔
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

© 2025 Coveralls, Inc