Skip to main content

Overview

Shed Skin translates Python code into standalone C++ code that can be compiled and integrated into C++ projects. Understanding the generated code structure helps with optimization, debugging, and integration.

Generated Code Structure

For a Python module mymodule.py, Shed Skin generates:
  • mymodule.hpp - Header file with declarations
  • mymodule.cpp - Implementation file with definitions
The generated code uses C++ templates, classes, and the Shed Skin runtime library.

Header File Structure

Key points:
  • Namespace __mymodule__ wraps all module contents
  • Classes inherit from pyobj base class
  • Template instantiations for polymorphic types

Implementation File Structure

Implementation: The code generation happens in shedskin/cpp.py via the GenerateVisitor class.

Type Mappings

Python types map to C++ types as follows: Most types are heap-allocated and use pointers, with automatic memory management via the Boehm GC.

Boehm Garbage Collector Integration

Shed Skin uses the Boehm-Demers-Weiser conservative garbage collector for automatic memory management.

Memory Allocation

All heap allocations go through GC-aware allocators:
The new operator is overridden to use GC_malloc() instead of standard malloc().

GC Configuration

The garbage collector is initialized in builtin.cpp:__shedskin__::__start():
Tuning GC behavior:

Disabling GC

For specialized use cases, compile without GC:
This generates code using manual memory management. Warning: Memory leaks are possible without GC.

Calling Conventions

Calling Shed Skin from C++

To use Shed Skin generated code from C++:
  1. Include the module header:
  1. Initialize the Shed Skin runtime:
  1. Handle Shed Skin types:

Calling C++ from Shed Skin

Extend Shed Skin with custom C++ code:
  1. Create a builtin module stub in ~/.shedskin/mycpp.py:
  1. Implement in C++ as mycpp.cpp and mycpp.hpp:
  1. Use in Python code:

Virtual Method Tables

For class hierarchies with inheritance, Shed Skin generates virtual method tables:
Implementation: Virtual methods are identified and generated by shedskin/virtual.py:virtuals() Virtual dispatch allows polymorphic behavior:
Generated C++:

Template Specialization

Polymorphic functions generate multiple template specializations:
Generated C++:
Implementation: Specializations are created by the CPA algorithm in shedskin/infer.py:cpa()

Exception Handling

Python exceptions map to C++ exceptions:
Generated C++:
All exception classes inherit from Exception * in the runtime.

Integrating with Existing C++ Projects

As a Static Library

  1. Compile Shed Skin code to object files:
  1. Link with your project:
  1. CMake integration:

As a Python Extension Module

Compile Shed Skin code as a Python extension:
This generates mymodule.so importable from Python:

Performance Considerations

Inlining

Small functions may be inlined for performance. Mark with inline:

Avoiding Virtual Calls

Virtual method calls have overhead. When possible, use final classes:
Generated methods will be non-virtual.

Memory Locality

Keep related data together for cache efficiency:

Debugging Generated Code

Enable debugging symbols:
Set breakpoints on generated functions:
Inspect Shed Skin data structures:

Further Reading

  • Shed Skin runtime source: shedskin/lib/builtin.cpp
  • Code generation implementation: shedskin/cpp.py
  • Boehm GC documentation: https://www.hboehm.info/gc/