Package: dynapyt
DynaPyt: A Dynamic Analysis Framework for PythonDynaPyt is a dynamic analysis framework designed and developed byAryaz Eghbali and Michael Pradel.
The framework provides hooks for a variety of runtime events in multiple layers of abstraction.
Users can create arbitrary dynamic analyses by implementing relevant hooks.
Beyond observing runtime behavior, DynaPyt also supports manipulation of behavior, e.g., to inject runtime values or modify branching decisions.InstallationInstallation with pipRunpip install dynapytInstallation from sourceDownload source:git clone https://github.com/sola-st/DynaPyt.gitInstall requirements:pip install libcst
# or
cd DynaPyt
pip install -r requirements.txtInstall DynaPyt:cd DynaPyt
pip install .Implementing an AnalysisAn analysis is a subclass of BaseAnalysis. See theanalysis folderfor examples. To add your own analysis, add a file with a new analysis class to this folder. The name of the class is refered to as <analysis name> below.Instrumenting Python CodeNote:DynaPyt instruments code in-place (it keeps a.py.origfor each file it instruments to keep the original code). But for more convenience in analyzing, we suggest to instrument a copy of the code under analysis.To run the instrumentation on a single file:python -m dynapyt.instrument.instrument --files <path to Python file> --analysis <analysis class full dotted path>To run the instrumentation on all files in a directory:python -m dynapyt.run_instrumentation --directory <path to directory> --analysis <analysis class full dotted path>Note that instrumented files might not be portable.Running an AnalysisTo run an analysis:python -m dynapyt.run_analysis --entry <entry file (python)> --analysis <analysis class full dotted path>Note:The analysis name should either match the analysis name used for the instrumentation, or the analysis should have a subset of hooks used in the instrumentation analysis.Single command to instrument and run an analysis on a project:python -m dynapyt.run_all --directory <directory of project> --entry <entry file (python)> --analysis <analysis class full dotted path>Available HooksCheck outthis auto-generated API referencefor available hooks.Reproducing the Results in the PaperTo reproduce results from the paper, follow these instructions:We suggest running each experiment in a fresh Python environment.First, install DynaPyt using the instructions above.Analyzing the Python Projects (RQ1, RQ2, and RQ4)Option 1-Run using theexperiment.shscript:First, download the desired project from GitHub (git clone ...), and put it under./test/PythonRepos/<package name>.Then, if the project can be installed with justpip install ., ignore this step and move to the next. Otherwise, place amyInstall.shscript in the root directory of the project with all steps required for installing the package.Finally, runbash ./experiment.sh <package name> <test directory> <analysis class full dotted path>
# E.g. for "rich" repository located at test/PythonRepos/rich:
bash ./experiment.sh rich test TraceAllto run the analysis, orbash ./experiment.sh <package name> <test directory> originalto run the original code (uninstrumented and unanalyzed).Option 2-Run manually:
For each project (for exampleTextualize/rich):Clone the repository (or download the zip):git clone https://github.com/Textualize/rich.git
cd richInstrument all files in the project (for original execution, skip this step):python -m dynapyt.run_instrumentation --directory . --analysis dynapyt.analyses.TraceAll.TraceAllAdd an entry file (run_all_tests.py) that runs all tests into the root directory:importpytestpytest.main(['-n','8','--import-mode=importlib','./tests'])Replace'./tests'with the path to test files in the project.Install the package of the project-under-analysis (may need extra steps for other packages):pip install .Run tests:For running the analysis:time python -m dynapyt.run_analysis --entry run_all_tests.py --analysis dynapyt.analyses.TraceAll.TraceAllTo replicate the results for the original execution (not instrumented and analyzed through DynaPyt), perform steps 1, 3, and 4. Then runpython run_all_tests.pyRunning Other Analyses (RQ3)DynaPyt AnalysesTo run other DynaPyt analyses, use the appropriate name (the class name) for--analysisin both the instrumentation and the analysis scripts.Python's built-in traceTo run Python'ssys.settrace(used as a baseline in the paper):From the above instructions, follow step 1, skip step 2 and 3, run step 4, and then put the following code inrun_all_tests.py:fromsysimportsettracefromtrcimport_trace_opcodes_# or _trace_assignments_ or _trace_all_importpytestsettrace(_trace_opcodes_)pytest.main(['-n','8','--import-mode=importlib','./tests'])Copysrc/nativetracer/trc.pybesiderun_all_tests.py.Then runtime python run_all_tests.pyCitationPlease refer to DynaPyt via our FSE'22 paper:@InProceedings{fse2022-DynaPyt,
  author    = {Aryaz Eghbali and Michael Pradel},
  title     = {Dyna{P}yt: A Dynamic Analysis Framework for {P}ython},
  booktitle = {{ESEC/FSE} '22: 30th {ACM} Joint European Software Engineering Conference and Symposium on the Foundations of Software Engineering},
  year      = {2022},
  publisher = {{ACM}},
}