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

zopefoundation / RestrictedPython / 16550884845

20 Mar 2025 07:39AM UTC coverage: 98.772%. Remained the same
16550884845

push

github

icemac
Back to development: 8.1

213 of 231 branches covered (92.21%)

2493 of 2524 relevant lines covered (98.77%)

0.99 hits per line

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

98.44
/src/RestrictedPython/Utilities.py
1
##############################################################################
2
#
3
# Copyright (c) 2002 Zope Foundation and Contributors.
4
#
5
# This software is subject to the provisions of the Zope Public License,
6
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
7
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10
# FOR A PARTICULAR PURPOSE
11
#
12
##############################################################################
13

14
import math
1✔
15
import random
1✔
16
import string
1✔
17

18

19
utility_builtins = {}
1✔
20

21

22
class _AttributeDelegator:
1✔
23
    def __init__(self, mod, *excludes):
1✔
24
        """delegate attribute lookups outside *excludes* to module *mod*."""
25
        self.__mod = mod
1✔
26
        self.__excludes = excludes
1✔
27

28
    def __getattr__(self, attr):
1✔
29
        if attr in self.__excludes:
1✔
30
            raise NotImplementedError(
31
                f"{self.__mod.__name__}.{attr} is not safe")
32
        try:
1✔
33
            return getattr(self.__mod, attr)
1✔
34
        except AttributeError as e:
1✔
35
            e.obj = self
1✔
36
            raise
1✔
37

38

39
utility_builtins['string'] = _AttributeDelegator(string, "Formatter")
1✔
40
utility_builtins['math'] = math
1✔
41
utility_builtins['random'] = random
1✔
42
utility_builtins['whrandom'] = random
1✔
43
utility_builtins['set'] = set
1✔
44
utility_builtins['frozenset'] = frozenset
1✔
45

46
try:
1✔
47
    import DateTime
1✔
48
    utility_builtins['DateTime'] = DateTime.DateTime
×
49
except ImportError:
50
    pass
51

52

53
def same_type(arg1, *args):
1✔
54
    """Compares the class or type of two or more objects."""
55
    t = getattr(arg1, '__class__', type(arg1))
1✔
56
    for arg in args:
1✔
57
        if getattr(arg, '__class__', type(arg)) is not t:
1✔
58
            return False
1✔
59
    return True
1✔
60

61

62
utility_builtins['same_type'] = same_type
1✔
63

64

65
def test(*args):
1✔
66
    length = len(args)
1✔
67
    for i in range(1, length, 2):
1✔
68
        if args[i - 1]:
1✔
69
            return args[i]
1✔
70

71
    if length % 2:
1✔
72
        return args[-1]
1✔
73

74

75
utility_builtins['test'] = test
1✔
76

77

78
def reorder(s, with_=None, without=()):
1✔
79
    # s, with_, and without are sequences treated as sets.
80
    # The result is subtract(intersect(s, with_), without),
81
    # unless with_ is None, in which case it is subtract(s, without).
82
    if with_ is None:
1✔
83
        with_ = s
1✔
84
    orig = {}
1✔
85
    for item in s:
1✔
86
        if isinstance(item, tuple) and len(item) == 2:
1✔
87
            key, value = item
1✔
88
        else:
89
            key = value = item
1✔
90
        orig[key] = value
1✔
91

92
    result = []
1✔
93

94
    for item in without:
1✔
95
        if isinstance(item, tuple) and len(item) == 2:
1✔
96
            key, ignored = item
1✔
97
        else:
98
            key = item
1✔
99
        if key in orig:
1✔
100
            del orig[key]
1✔
101

102
    for item in with_:
1✔
103
        if isinstance(item, tuple) and len(item) == 2:
1✔
104
            key, ignored = item
1✔
105
        else:
106
            key = item
1✔
107
        if key in orig:
1✔
108
            result.append((key, orig[key]))
1✔
109
            del orig[key]
1✔
110

111
    return result
1✔
112

113

114
utility_builtins['reorder'] = reorder
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