linux - piping a string to gcc with python's subprocess.Popen -
i have c++ style text file i'm trying pipe gcc remove comments. initially, tried regex approach, had trouble handling things nested comments, string literals , eol issues.
so i'm trying like:
strip_comments(test_file.c) def strip_comments(text): p = popen(['gcc', '-w', '-e', text], stdin=pipe, stdout=pipe, stderr=stdout) p.stdin.write(text) p.stdin.close() print p.stdout.read()
but instead of passing file, i'd pipe contents because files i'm trying preprocess don't have .c extension
has had success this?
this 1 works subprocess.pipe
(os.popen() deprecated), , need pass , additional -
argument gcc make process stdin:
import os subprocess import popen, pipe def strip_comments(text): p = popen(['gcc', '-fpreprocessed', '-dd', '-e', '-x', 'c++', '-'], stdin=pipe, stdout=pipe, stderr=pipe) p.stdin.write(text) p.stdin.close() return_code = p.wait() print p.stdout.read()
Comments
Post a Comment