Esempio n. 1
0
def test_replace_mutable_default_arg():
    def foo(append=None, mutable=[]):  # noqa: B006
        if append is not None:
            mutable.append(append)
        return len(mutable)

    assert foo() == 0
    assert foo("v1") == 1
    assert foo("v2") == 2
    assert foo(mutable=[]) == 0

    patchy.replace(
        foo,
        """\
        def foo(append=None, mutable=[]):
            if append is not None:
                mutable.append(append)
            return len(mutable)
        """,
        """\
        def foo(append=None, mutable=[]):
            len(mutable)
            if append is not None:
                mutable.append(append)
            return len(mutable)
        """,
    )

    assert foo() == 2
    assert foo("v3") == 3
    assert foo(mutable=[]) == 0
Esempio n. 2
0
def test_replace_only_cares_about_ast():
    def sample():
        return 1

    patchy.replace(sample, "def sample(): return 1", "def sample(): return 42")

    assert sample() == 42
Esempio n. 3
0
def code_replace(func, *replacements):
    """
    Slight modification to original replace function to allow replacing only
     part(s) of code and not necessarily using whole function's code.
    
    :param func:         function object to be modified
    :param replacements: list of replacements - pairs (str_to_repl, replac)
    :return:             whether code was modified or not
    """
    __validate(func)
    try:
        patchy.replace(func, *replacements)
        old_code, new_code = replacements
    # if normal handling fails, get whole function's code first, apply
    #  replacement then retry
    except:
        if len(replacements) % 2 != 0:
            raise PatchError("Bad code replacement")
        old_code = code_source(func)
        new_code = old_code
        for i in range(0, len(replacements), 2):
            new_code = new_code.replace(replacements[i], replacements[i + 1])
        patchy.replace(func, old_code, new_code)
    # report whether the code was changed or not
    return old_code != new_code
Esempio n. 4
0
def test_replace_mutable_default_arg():
    def foo(append=None, mutable=[]):
        if append is not None:
            mutable.append(append)
        return len(mutable)

    assert foo() == 0
    assert foo('v1') == 1
    assert foo('v2') == 2
    assert foo(mutable=[]) == 0

    patchy.replace(
        foo,
        """\
        def foo(append=None, mutable=[]):
            if append is not None:
                mutable.append(append)
            return len(mutable)
        """,
        """\
        def foo(append=None, mutable=[]):
            len(mutable)
            if append is not None:
                mutable.append(append)
            return len(mutable)
        """
    )

    assert foo() == 2
    assert foo('v3') == 3
    assert foo(mutable=[]) == 0
Esempio n. 5
0
def test_replace_twice():
    def sample():
        return 1

    patchy.replace(sample, "def sample(): return 1", "def sample(): return 2")
    patchy.replace(sample, "def sample(): return 2", "def sample(): return 3")

    assert sample() == 3
Esempio n. 6
0
def test_replace_only_cares_about_ast():
    def sample():
        return 1

    patchy.replace(
        sample,
        "def sample(): return 1",
        "def sample(): return 42"
    )

    assert sample() == 42
Esempio n. 7
0
def test_replace_no_expected_source():
    def sample():
        return 2

    patchy.replace(
        sample,
        None,
        """\
        def sample():
            return 42
        """
    )

    assert sample() == 42
Esempio n. 8
0
def test_replace_no_expected_source():
    def sample():
        return 2

    patchy.replace(
        sample,
        None,
        """\
        def sample():
            return 42
        """,
    )

    assert sample() == 42
Esempio n. 9
0
def test_replace_twice():
    def sample():
        return 1

    patchy.replace(
        sample,
        "def sample(): return 1",
        "def sample(): return 2",
    )
    patchy.replace(
        sample,
        "def sample(): return 2",
        "def sample(): return 3",
    )

    assert sample() == 3
Esempio n. 10
0
def test_replace():
    def sample():
        return 1

    patchy.replace(
        sample,
        """\
        def sample():
            return 1
        """,
        """\
        def sample():
            return 42
        """,
    )

    assert sample() == 42
Esempio n. 11
0
def test_replace():
    def sample():
        return 1

    patchy.replace(
        sample,
        """\
        def sample():
            return 1
        """,
        """\
        def sample():
            return 42
        """
    )

    assert sample() == 42
Esempio n. 12
0
def test_replace_instancemethod():
    class Artist:
        def method(self):
            return "Chalk"

    patchy.replace(
        Artist.method,
        """\
        def method(self):
            return 'Chalk'
        """,
        """\
        def method(self):
            return 'Cheese'
        """,
    )

    assert Artist().method() == "Cheese"
Esempio n. 13
0
def test_replace_instancemethod():
    class Artist(object):
        def method(self):
            return 'Chalk'

    patchy.replace(
        Artist.method,
        """\
        def method(self):
            return 'Chalk'
        """,
        """\
        def method(self):
            return 'Cheese'
        """
    )

    assert Artist().method() == "Cheese"
Esempio n. 14
0
def test_replace_unexpected_source():
    def sample():
        return 2

    with pytest.raises(ValueError) as excinfo:
        patchy.replace(
            sample,
            """\
            def sample():
                return 1
            """,
            """\
            def sample():
                return 42
            """,
        )

    msg = str(excinfo.value)
    assert "The code of 'sample' has changed from expected" in msg
    assert "return 2" in msg
    assert "return 1" in msg
Esempio n. 15
0
def test_replace_unexpected_source():
    def sample():
        return 2

    with pytest.raises(ValueError) as excinfo:
        patchy.replace(
            sample,
            """\
            def sample():
                return 1
            """,
            """\
            def sample():
                return 42
            """
        )

    msg = str(excinfo.value)
    assert "The code of 'sample' has changed from expected" in msg
    assert 'return 2' in msg
    assert 'return 1' in msg
Esempio n. 16
0
from .types import *
from ..compat import ensure_str

__all__ = __features__ = [
    "BitArray", "entropy", "entropy_bits", "pad", "unpad"
]

PAD = ["ansic9.23", "incremental", "iso7816-4", "pkcs5", "pkcs7", "w3c"]

patchy.replace(
    bitstring.Bits._getlength, """
    def _getlength(self):
        \"\"\"Return the length of the bitstring in bits.\"\"\"
        return self._datastore.bitlength
""", """
    def _getlength(self):
        \"\"\"Return the length of the bitstring in bits.\"\"\"
        l = self._datastore.bitlength
        return l + (8 - l % 8) % 8 if getattr(Bits, "_padding", True) else l
""")
patchy.replace(
    bitstring.Bits._getbin, """
    def _getbin(self):
        \"\"\"Return interpretation as a binary string.\"\"\"
        return self._readbin(self.len, 0)
""", """
    def _getbin(self):
        \"\"\"Return interpretation as a binary string.\"\"\"
        Bits._padding = False
        r = self._readbin(self.len, 0)
Esempio n. 17
0
def _patch_keras_engine():
    from keras.layers import Layer
    layer_call_fn = Layer.__call__
    patch_source = inspect.getsource(Dummy.__call__)
    patchy.replace(layer_call_fn, None, patch_source)