Reference https://github.com/noxrepo/pox/blob/carp/pox.py
I'm trying to understand what does the 4 apostrophes means? It doesn't look like commenting and near the end of the code there were another 3 apostrophes. Can someone help to explain the code below?
#!/bin/sh -
''''true
#export OPT="-u -O"
export OPT="-u"
export FLG=""
if [ "$(basename $0)" = "debug-pox.py" ]; then
export OPT=""
export FLG="--debug"
fi
if [ -x pypy/bin/pypy ]; then
exec pypy/bin/pypy $OPT "$0" $FLG "$@"
fi
if type python2.7 > /dev/null 2> /dev/null; then
exec python2.7 $OPT "$0" $FLG "$@"
fi
exec python $OPT "$0" $FLG "$@"
'''
from pox.boot import boot
if __name__ == '__main__':
boot()
The first three apostrophes start a multi-line string. The next apostrophe is just part of the contents of the string.
The script stores the string in the __doc__ variable. After running the code interactively with python -i pox.py
, it is easy to see the parsed docstring directly:
>>> print __doc__
'true
#export OPT="-u -O"
export OPT="-u"
export FLG=""
if [ "$(basename $0)" = "debug-pox.py" ]; then
export OPT=""
export FLG="--debug"
fi
if [ -x pypy/bin/pypy ]; then
exec pypy/bin/pypy $OPT "$0" $FLG "$@"
fi
if type python2.7 > /dev/null 2> /dev/null; then
exec python2.7 $OPT "$0" $FLG "$@"
fi
exec python $OPT "$0" $FLG "$@"
Note how the fourth apostrophe was kept as part of the docstring.
According to the tokenize module, here is how Python views the above code:
NL : '\n'
COMMENT : '#!/bin/sh -'
NL : '\n'
NL : '\n'
STRING : '\'\'\'\'true\n#export OPT="-u -O"\nexport OPT="-u"\nexport FLG=""\nif [ "$(basename $0)" = "debug-pox.py" ]; then\n export OPT=""\n export FLG="--debug"\nfi\n\nif [ -x pypy/bin/pypy ]; then\n exec pypy/bin/pypy $OPT "$0" $FLG "$@"\nfi\n\nif type python2.7 > /dev/null 2> /dev/null; then\n exec python2.7 $OPT "$0" $FLG "$@"\nfi\nexec python $OPT "$0" $FLG "$@"\n\'\'\''
NEWLINE : '\n'
NAME : 'from'
NAME : 'pox'
OP : '.'
NAME : 'boot'
NAME : 'import'
NAME : 'boot'
NEWLINE : '\n'
NAME : 'if'
NAME : '__name__'
OP : '=='
STRING : "'__main__'"
OP : ':'
NEWLINE : '\n'
INDENT : ' '
NAME : 'boot'
OP : '('
OP : ')'
NEWLINE : '\n'
DEDENT : ''
ENDMARKER : ''
Here is a Python 2.7 script that tokenizes the pox.py script:
from __future__ import print_function
import tokenize
import token
with open('pox.py') as f:
for tok in tokenize.generate_tokens(f.readline):
tok_type, tok_str, (srow, scol), (erow, ecol), logical_lineno = tok
print('%-10s: %r' % (token.tok_name[tok_type], tok_str))