How to preserve trailing blank lines in lstinputlisting with linerange option?

Here is my code in foo.tex:

\documentclass{article}

\usepackage{listings}
\begin{document}
\lstinputlisting[linerange={1-2,7}]{foo.tex}

\end{document}


This code in foo.tex reads itself and displays itself as code listing. But it tries to show only line 1, line 2 (blank line) and line 7 (the last line).

The output PDF contains only two lines:

\documentclass{article}
\end{document}


Line 2 (blank line) does not appear in the PDF output. It looks like trailing blank line was ignored. I tried adding the showlines=true option but that did not work too. How can I preserve the blank line in line 2 in the final PDF output?