Rechercher

PythonReactSEOPDF
GitHub Skill

polyglot-c-py

Guidance for creating polyglot files that are valid in both Python and C. This skill applies when tasked with writing code that must be parseable and executable by both the Python interpreter and C compiler. Covers polyglot syntax techniques, testing strategies, and critical cleanup requirements.

letta-ai☆ 46 Étoiles du dépôt7 Forks du dépôt

Installation

Suivez les instructions du dépôt pour votre agent. Consultez les fichiers et dépendances avant l’installation.

Voir la source ↗Guides

Instructions

Polyglot C-Python File Creation

This skill provides guidance for creating single source files that function correctly as both Python and C programs.

When to Use

This skill applies when:

  • Creating a file that must be valid syntax for both Python and C
  • The file needs to execute correctly under both python filename and gcc filename && ./output
  • Building polyglot programs that produce equivalent output in both languages

Polyglot Structure Approach

Core Technique

Use preprocessor directives and string literals to hide language-specific code:

  1. Hide C code from Python: Wrap C-specific code in triple-quoted strings that Python ignores
  2. Hide Python code from C: Use #if 0...#endif preprocessor blocks that C ignores but Python sees as comments
Typical File Structure
[Shared/polyglot preamble]
#if 0
[Python code block - C preprocessor skips this]
#endif
"""
[C code block - Python treats as string literal]
"""
Key Syntax Considerations
  • C-style // comments cause Python syntax errors - avoid in shared sections
  • Use #if 0 instead of /* */ comments for hiding Python code from C
  • Triple-quoted strings (""") effectively hide C code from Python
  • The file extension (e.g., .py.c) should indicate dual-language nature

Verification Strategy

Functional Testing
  1. Test Python execution: python <filename>
  2. Test C compilation and execution: gcc <filename> -o <output> && ./<output>
  3. Verify both produce identical or equivalent output
  4. Test multiple input cases including edge cases (e.g., boundary values, zero, negative inputs if applicable)
Environmental Verification (Critical)

Before declaring the task complete, verify the final directory state:

  1. List all files in the target directory: Ensure only the required source file(s) exist
  2. Check for leftover artifacts: Compiled binaries, object files (.o), temporary files
  3. Compare against requirements: Match the exact expected file structure

Common Pitfalls

Artifact Cleanup (Most Critical)

Problem: Leaving compiled binaries or test artifacts in the working directory.

Prevention:

  • Compile test binaries to /tmp or a separate test directory: gcc file.py.c -o /tmp/test_binary
  • If compiling in the working directory, delete binaries after testing: rm <binary_name>
  • Never leave .o files, executables, or other build artifacts in the final directory
Incomplete Task Analysis

Problem: Focusing only on functional correctness while ignoring implicit requirements like directory cleanliness.

Prevention:

  • Before starting, explicitly identify all success criteria:
    • What files should exist when done?
    • What files should NOT exist?
    • What state should the environment be in?
  • After completing functional work, verify the end state matches all requirements
Syntax Conflicts

Problem: Using syntax valid in one language but not the other in shared sections.

Prevention:

  • Avoid C-style // comments in any section Python will parse
  • Test both interpreters after each significant change
  • Keep shared sections minimal
Untested Edge Cases

Problem: Only testing happy-path inputs.

Prevention:

  • Test boundary values (0, 1, max values)
  • Test error conditions if the program should handle them
  • Compare outputs between Python and C for all test cases

Verification Checklist

Before declaring the task complete:

  1. Python execution produces correct output
  2. C compilation succeeds without warnings
  3. C execution produces correct output
  4. Multiple test inputs verified
  5. Target directory contains ONLY the required file(s)
  6. No compiled binaries left behind
  7. No temporary or intermediate files present
  8. File naming matches requirements exactly

Exemples

Consultez les exemples du dépôt source.

Voir la source ↗

Historique source

Date d’indexation
2026-01-20
Mise à jour de l’index
2026-02-04
Branch
main

Les dates concernent ce répertoire. Consultez le dépôt pour l’historique du code.

GitHub ↗

chat Comments (0)

chat_bubble_outline

No comments yet. Be the first to share your thoughts!