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:
-
Hide C code from Python: Wrap C-specific code in triple-quoted strings that Python ignores
-
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
-
Test Python execution: python <filename>
-
Test C compilation and execution: gcc <filename> -o <output> && ./<output>
-
Verify both produce identical or equivalent output
-
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:
-
List all files in the target directory: Ensure only the required source file(s) exist
-
Check for leftover artifacts: Compiled binaries, object files (.o ), temporary files
-
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:
-
Python execution produces correct output
-
C compilation succeeds without warnings
-
C execution produces correct output
-
Multiple test inputs verified
-
Target directory contains ONLY the required file(s)
-
No compiled binaries left behind
-
No temporary or intermediate files present
-
File naming matches requirements exactly