PyCG: Practical Python Call Graphs

Overview

PyCG - Practical Python Call Graphs

PyCG generates call graphs for Python code using static analysis. It efficiently supports

  • Higher order functions
  • Twisted class inherritance schemes
  • Automatic discovery of imported modules for further analysis
  • Nested definitions

You can read the full methodology as well as a complete evaluation on the ICSE 2021 paper.

Abstract:

Call graphs play an important role in different contexts, such as profiling and vulnerability propagation analysis. Generating call graphs in an efficient manner can be a challenging task when it comes to high-level languages that are modular and incorporate dynamic features and higher-order functions. Despite the language's popularity, there have been very few tools aiming to generate call graphs for Python programs. Worse, these tools suffer from several effectiveness issues that limit their practicality in realistic programs. We propose a pragmatic, static approach for call graph generation in Python. We compute all assignment relations between program identifiers of functions, variables, classes, and modules through an inter-procedural analysis. Based on these assignment relations, we produce the resulting call graph by resolving all calls to potentially invoked functions. Notably, the underlying analysis is designed to be efficient and scalable, handling several Python features, such as modules, generators, function closures, and multiple inheritance. We have evaluated our prototype implementation, which we call PyCG, using two benchmarks: a micro-benchmark suite containing small Python programs and a set of macro-benchmarks with several popular real-world Python packages. Our results indicate that PyCG can efficiently handle thousands of lines of code in less than a second (0.34 seconds for 1k LoC on average). Further, it outperforms the state-of-the-art for Python in both precision and recall: PyCG achieves high rates of precision ~99, and adequate recall ~69.3. Finally, we demonstrate how PyCG can aid dependency impact analysis by showcasing a potential enhancement to GitHub's security advisory notification service using a real-world example.

Installation

PyCG is implemented in Python3 and has no dependencies. Simply:

pip install pycg

Usage

~ >>> pycg -h
usage: pycg [-h] [--package PACKAGE] [--fasten] [--product PRODUCT]
            [--forge FORGE] [--version VERSION] [--timestamp TIMESTAMP]
            [-o OUTPUT]
            [entry_point [entry_point ...]]

positional arguments:
  entry_point           Entry points to be processed

optional arguments:
  -h, --help            show this help message and exit
  --package PACKAGE     Package containing the code to be analyzed
  --fasten              Produce call graph using the FASTEN format
  --product PRODUCT     Package name
  --forge FORGE         Source the product was downloaded from
  --version VERSION     Version of the product
  --timestamp TIMESTAMP
                        Timestamp of the package's version
  -o OUTPUT, --output OUTPUT
                        Output path

where the command line arguments are:

  • entry_point: A list of paths to Python modules that PyCG will analyze. It is suggested that this list of paths contains only entry points since PyCG automatically discovers all other (local) imported modules.
  • --package: The unix path to the module's namespace (i.e. the path from which the module would be executed). This parameter is really important for the correct resolving of imports.
  • --fasten: Output the callgraph in FASTEN format.
  • -output: The unix path where the output call graph will be stored in JSON format.

The following command line arguments should used only when --fasten is provied:

  • --product: The name of the package.
  • --forge: Source the package was downloaded from.
  • --version: The version of the package.
  • --timestamp : The timestamp of the package's version.

Output

Simple JSON format

The call edges are in the form of an adjacency list where an edge (src, dst) is represented as an entry of dst in the list assigned to key src:

{
    "node1": ["node2", "node3"],
    "node2": ["node3"],
    "node3": []
}

FASTEN Format

For an up-to-date description of the FASTEN format refer to the FASTEN wiki.

Examples

All the entry points are known and we want the simple JSON format

~ >>> pycg --package pkg_root pkg_root/module1.py pkg_root/subpackage/module2.py -o cg.json

All entry points are not known and we want the simple JSON format

~ >>> pycg --package django $(find django -type f -name "*.py") -o django.json

We want the FASTEN format:

~ >>> pycg --package pypi_pkg --fasten --product "pypipkg" --forge "PyPI" \
        --version "0.1" --timestamp 42 \
        pypi_pkg/module1.py pkg_root/subpackage/module2.py -o cg.json

Running Tests

From the root directory:

make test
Owner
Vitalis Salis
ECE undergraduate @NTUA Researcher @AUEB-BALab
Vitalis Salis
Project made in Qt Designer + Python, for evaluation in the subject Introduction to Programming in IFPE - Paulista campus.

Project made in Qt Designer + Python, for evaluation in the subject Introduction to Programming in IFPE - Paulista campus.

Paola Rodrigues 2 Apr 13, 2022
Advanced Zola Cabs integrated with tkinter Graphical User Interface (GUI) made for ZOHO Corp .

ZolaCabs Advanced Zola Cabs integrated with tkinter Graphical User Interface (GUI) made for ZOHO Corp. Logs username : zoho password : zoho [ Deve

Mastermind 9 Nov 18, 2021
GUI for Volatility forensics tool written in PyQT5

Volatility GUI This is a GUI for Volatility forensics tool written in PyQT5 Prerequisites: 1- Installed version of Volatility. 2- Install PyQT5. sudo

Hamza Megahed 52 Jun 18, 2022
This program is written in python. It will help you find a valid solution for a sudoku puzzle.

Sudoku-Solver-Using-Tkinter This program is written in python. It will help you find a valid solution for a sudoku puzzle. Requirements: Python3 IDLE

Ankan Mahapatra 3 Oct 02, 2021
A html canvas based screencasting server with occasional ground-truth updates via screenshots and very fast input drawing

rm2canvas A html canvas based screencasting server for the reMarkable 1/2 digital paper systems. It draws live on the canvas from the remarkables touc

45 Sep 08, 2022
A desktop application developed in Python with PyQt5 to predict demand and help monitor and schedule brewing processes for Barnaby's Brewhouse.

brewhouse-management A desktop application developed in Python with PyQt5 to predict demand and help monitor and schedule brewing processes for Barnab

Isaac Cheng 2 Jul 09, 2022
A Simple GUI application to organize and store accounts/passwords.

PyssView A Simple GUI application to organize and store accounts/passwords. Install/Run (Linux) Via script This way will install a binary version and

Jefferson Richard Dias 1 Nov 24, 2021
A small manager/GUI Frontend for QEMU written in Python

qemu-manager A small manager/GUI Frontend for QEMU written in Python Requirements You'll need to have the following tools installed to run this: QEMU

yeppiidev 15 Dec 21, 2022
Build GUI for your Python program with JavaScript, HTML, and CSS

https://pywebview.flowrl.com pywebview is a lightweight cross-platform wrapper around a webview component that allows to display HTML content in its o

Roman 3.3k Jan 01, 2023
A Windows Dock Widget Written In Pure Python

VEПUS A Windows Dock Widget Written In Pure Python What is Venus? Venus is a Dock Widget for your desktops interface. It adds a couple of really cool

Secrets 18 Dec 30, 2022
AutoCalypsoBTS - A simple GUI CalypsoBTS for DragonOS by CrTh

AutoCalypsoBTS 📱 📞 A simple GUI CalypsoBTS for DragonOS by CrTh Download Drago

CrTx0 18 Dec 15, 2022
Python Web Version 3.0 Using PyQty module

Python-Web-Version-3.0 Python Web Version 3.0 Using PyQty module you have to install pyinstaller module then install PyQt5 module and install PyQtwebE

JehanKandy 9 Jul 13, 2022
Software com funçoes de A a Z feito no Python

Introdução Iniciante em programação Python, decidi criar um programa com diversas ferramentas de A a Z. Funções Ferramenta de Gerenciamento e Manutenç

João Pedro 1 Jan 26, 2022
A lightweight file-copying interface.

autosort A lightweight file-copying interface. Preview What is autosort? Autosort is a lightweight file-copying interface. It allows you to copy sever

32 Jan 02, 2023
This repository contains some projects that I have done using Python + Tkinter.

This repository contains some projects that I have done using Python + Tkinter.

João Victor Vilela dos Santos 1 Nov 10, 2021
Make desktop applications using HTML and CSS with python

Neutron Make desktop applications using HTML and CSS with python What is Neutron Neutron will allow developers to design modern applications in python

Ian Baldelli 284 Dec 29, 2022
GUI based app made in python using tkinter

Virtual Keyboard A GUI application made in python using tkinter This is my first ever proper GUI based application project after learning tkinter rece

AbhineetK 10 Dec 10, 2022
Tkinker GUI for a college project.

GuiUtilities Pequeña suite de utileria con Tkinter enfocada en sistemas Linux. Lista de trabajo Crear archivo Copiar archivo a otro dir Editar con un

1hiking 2 Nov 25, 2021
Tukaan is the new framework that aims to replace Tkinter

Tukaan is the new, pythonic and colorful (like a keel-billed toucan) framework that aims to replace Tkinter. It has everything (on my computer, not at GitHub) that you need to develop cross-platform

Tukaan 101 Jan 08, 2023