This file is indexed.

/usr/lib/python2.7/dist-packages/preggy/core.py is in python-preggy 1.1.3-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# -*- coding: utf-8 -*-
'''preggy core: the `Expect` class, and the `@assertion` and `@create_assertions` decorators.
'''
# preggy assertions
# https://github.com/heynemann/preggy

# Licensed under the MIT license:
# http://www.opensource.org/licenses/mit-license
# Copyright (c) 2013 Bernardo Heynemann heynemann@gmail.com

from __future__ import absolute_import
import functools
from contextlib import contextmanager

from preggy import utils

_registered_assertions = utils.AssertionsMap()


def assertion(func):
    '''Function decorator.  Provides lower-level control for custom
    assertions than `@preggy.create_assertions`.

    This decorator is preferable over `@preggy.create_assertions`
    if you need to fine-tune your error message, or if your assertion
    doesn’t have a corresponding `not_`.

    Unlike `@preggy.create_assertions`, functions decorated with
    this shouldn’t return a boolean. Instead, they should check for
    undesirable conditions and raise an `AssertionError` when appropriate.

    Whenever possible, you should declare both the normal assertion as well
    as a `not_` counterpart, so they can be used like this:
    
    ... # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
    >>>
    >>> import preggy
    >>> from preggy.core import Expect as expect
    >>> 
    >>> @preggy.assertion
    ... def to_be_a_positive_integer(topic):
    ...     if not topic > 0:
    ...          raise AssertionError("Expected topic('{topic}') to be a positive integer".format(topic=topic))
    ...
    >>> @preggy.assertion
    ... def not_to_be_a_positive_integer(topic):
    ...     if not topic < 0:
    ...          raise AssertionError("Expected topic('{topic}') not to be a positive integer".format(topic=topic))
    ...
    >>> expect(5).to_be_a_positive_integer()
    >>> expect(-3).Not.to_be_a_positive_integer()

    '''
    if not hasattr(func, 'humanized'):
        setattr(func, 'humanized', utils.humanized_name(func.__name__))
        
    @functools.wraps(func)
    def wrapper(*args, **kw):
        return func(*args, **kw)

    _registered_assertions[wrapper.__name__] = wrapper
    return wrapper


def create_assertions(func):
    '''Function decorator.  Use to create custom assertions for your
    tests.
    ''' '''
    Creating new assertions for use with `expect` is as simple as using
    this decorator on a function. The function expects `topic` as the
    first parameter, and `expectation` second:

        @preggy.create_assertions
        def to_be_greater_than(topic, expected):
            return topic > expected

    This creates both the assertion AND its `not_*` counterpart.  
    
    ... # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
    >>> from preggy.core import (assertion, create_assertions, Expect as expect)  
    >>> from preggy.assertions import *
    
    >>> expect(2).to_be_greater_than(3)
    Traceback (most recent call last):
        ...
    AssertionError: Expected topic(2) to be greater than 3
    
    >>> expect(4).not_to_be_greater_than(3)
    Traceback (most recent call last):
        ...
    AssertionError: Expected topic(4) not to be greater than 3
    
    '''
    # set custom func attribute "humanized"
    setattr(func, 'humanized', utils.humanized_name(func.__name__))

    # modified functools.update_wrapper
    def _update_wrapper(wrapper, wrapped, not_assertion=True):
        '''A modified version of functools.update_wrapper. Auto-modifies the
        wrapper's __name__ and __doc__ to create a not_assertion.
        '''
        # begin as usual
        wrapper = functools.update_wrapper(wrapper, wrapped)
        
        # compute overrides for not_* assertions values
        if not_assertion:
            new_name = 'not_{0.__name__}'.format(wrapped)
            new_doc = ''#.format(wrapped)

            # set our overrides
            setattr(wrapper, '__name__', new_name)
            setattr(wrapper, '__doc__', new_doc)
            # update to reflect new __name__
            setattr(wrapper, 'humanized', utils.humanized_name(wrapper.__name__))

        # Return the wrapper so this can be used as a decorator via partial()
        return wrapper
    
    
    # First assertion
    @assertion
    @functools.wraps(func)
    def test_assertion(*args):
        raw_msg = utils.format_assertion_msg(func.humanized, *args)
        err_msg = raw_msg.format(*args)
        if not func(*args):
            raise AssertionError(err_msg)
    
    # Second assertion: prepare
    def test_not_assertion(*args):
        raw_msg = utils.format_assertion_msg('not {0!s}'.format(func.humanized), *args)
        err_msg = raw_msg.format(*args)
        if func(*args):
            raise AssertionError(err_msg)

    # Second assertion: update and register
    test_not_assertion = _update_wrapper(test_not_assertion, func)
    test_not_assertion = assertion(test_not_assertion)

class ErrorToHappenContext(object):
    def __init__(self, error_class, message=None):
        self.error_class = error_class
        self.message = message
        self._error = None

    @property
    def error(self):
        return self._error

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        has_exception = exc_type is not None
        is_subclass = has_exception and (exc_type is self.error_class or issubclass(exc_type, self.error_class)) or False
        if not has_exception:
            raise AssertionError('Expected "%s.%s" to happen but no errors happened during execution of with block.' % (
                self.error_class.__module__,
                self.error_class.__name__,
            ))

        if has_exception and not is_subclass:
            raise AssertionError('Expected "%s.%s" to happen but "%s.%s" happened during execution of with block.' % (
                self.error_class.__module__,
                self.error_class.__name__,
                exc_type.__module__,
                exc_type.__name__
            ))

        if self.message is not None:
            error_msg = getattr(exc_val, 'message', str(exc_val))
            if error_msg != self.message:
                raise AssertionError('Expected "%s.%s" to have a message of "%s", but the actual error was "%s".' % (
                    self.error_class.__module__,
                    self.error_class.__name__,
                    self.message,
                    error_msg
                ))

        if has_exception and is_subclass:
            self._error = exc_val
            return True

        return False


class Expect(object):
    '''This atypical class provides a key part of the preggy testing syntax.
    
    For example:
    
        >>> from preggy.core import (assertion, create_assertions, Expect as expect)
        >>> from preggy.assertions import *
        >>>
        >>> expect(True).to_be_true()
        >>> expect(False).to_be_false()
        
    '''

    def __init__(self, topic):
        self.topic = topic
        if isinstance(self.topic, ErrorToHappenContext):
            self.topic = self.topic.error

        self.not_assert = False

    @classmethod
    def not_to_be_here(self):
        raise AssertionError("Should not have gotten this far.")

    @classmethod
    def error_to_happen(self, error_class=Exception, message=None):
        return ErrorToHappenContext(error_class, message=message)

    def __getattr__(self, name):
        # common cases
        if name in ['topic', 'not_to_be_here']:
            return super(Expect, self).__getattr__(name)
        if name == 'Not':
            self.not_assert = not self.not_assert
            return self

        # determine whether assertion is of "not" form
        method_name = 'not_{name}'.format(name=name) if self.not_assert else name
        
        # if program gets this far, then it’s time to perform the assertion. (...FINALLY! ;D)
        def _assert_topic(*args, **kw):
            # Allows chained calls to assertions, such as `expect(topic).to_be_true()`.
            return _registered_assertions[method_name](self.topic, *args, **kw)
        
        return _assert_topic