Skip to content

Instantly share code, notes, and snippets.

@Ivoz
Last active January 31, 2016 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ivoz/3de6ff284d327e49afd0 to your computer and use it in GitHub Desktop.
Save Ivoz/3de6ff284d327e49afd0 to your computer and use it in GitHub Desktop.
A self-checking python quine, of sorts
#!/usr/bin/env python3
'''
A "self-verifying" quine - `<quine> <source_file>` will only print itself
if `<quine>` has the same output as `cat <source_file>`
'''
source = r'''{foreword}
import sys, subprocess
if len(sys.argv) > 1:
with open(sys.argv[1], 'rb') as f:
quine = f.read()
output = subprocess.check_output("%s %s" % (sys.executable, sys.argv[1]))
if quine != output:
sys.exit(1)
foreword = """#!/usr/bin/env python3
\'\'\'
A "self-verifying" quine - `<quine> <source_file>` will only print itself
if `<quine>` has the same output as `cat <source_file>`
\'\'\'
source = r\'\'\'"""
last_line = 'source.format(foreword=foreword + source + "\'\'\'", program=last_line)'
sys.stdout.write({program})
'''
import sys, subprocess
if len(sys.argv) > 1:
with open(sys.argv[1], 'rb') as f:
quine = f.read()
output = subprocess.check_output("%s %s" % (sys.executable, sys.argv[1]))
if quine != output:
sys.exit(1)
foreword = """#!/usr/bin/env python3
\'\'\'
A "self-verifying" quine - `<quine> <source_file>` will only print itself
if `<quine>` has the same output as `cat <source_file>`
\'\'\'
source = r\'\'\'"""
last_line = 'source.format(foreword=foreword + source + "\'\'\'", program=last_line)'
sys.stdout.write(source.format(foreword=foreword + source + "'''", program=last_line))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment