Skip to content

Instantly share code, notes, and snippets.

@moritz
Created April 4, 2022 19:19
Show Gist options
  • Select an option

  • Save moritz/04d38b1d0ec9330592ef74648b40f8b0 to your computer and use it in GitHub Desktop.

Select an option

Save moritz/04d38b1d0ec9330592ef74648b40f8b0 to your computer and use it in GitHub Desktop.

Revisions

  1. moritz created this gist Apr 4, 2022.
    9 changes: 9 additions & 0 deletions recalcitrant
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    #!/usr/bin/env python3
    # scrript being tested. Goal: patch out getpid
    from os import getpid

    def double_pid():
    return 2 * getpid()

    if __name__ == '__main__':
    print(double_pid())
    12 changes: 12 additions & 0 deletions testit.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    from importlib.util import spec_from_loader, module_from_spec
    from importlib.machinery import SourceFileLoader

    spec = spec_from_loader("recalcitrant", SourceFileLoader("recalcitrant", "./recalcitrant"))
    recalcitrant = module_from_spec(spec)
    spec.loader.exec_module(recalcitrant)

    from unittest.mock import patch

    with patch.object(recalcitrant, 'getpid') as mock:
    mock.return_value = 21
    print(recalcitrant.double_pid())