# How to put space before and after an \inputminted code block with bgcolor?

This is related to the question How to space before and after a minted code block with bgcolor? There it is explained that there is a bug with minted which causes the space above and below a code block to disappear when you set a background colour. A fix is given for the minted environment. I want to fix the \inputminted command as well! Unfortunately the same fix doesn't work as this is a command not an environment.

I thought I could fix it with \apptocmd and \pretocmd, but as the following minimal working example shows, \pretocmd does the right thing but \apptocmb gives the error "! Missing \endcsname inserted."

\documentclass{article}
\usepackage{lipsum}
\usepackage{filecontents}

\usepackage{minted}
\setminted{bgcolor=blue!8}

\usepackage{etoolbox}
\pretocmd{\inputminted}{\par\vspace{1em}}{}{}
% The following line doesn't work
\apptocmd{\inputminted}{\par\vspace{1em}}{}{}

% These are the fixes given for the minted environment
\BeforeBeginEnvironment{minted}{\vspace{1em}}
\AfterEndEnvironment{minted}{\par\vspace{1em}}

\begin{document}

\begin{filecontents*}{test.py}
n = 20
for i in range(n)
print("Hello World")
\end{filecontents*}

\lipsum*[1]

% Here is where the problem lies.
\inputminted{python}{test.py}

\lipsum*[1]

% This bit of code works as it should
\begin{minted}{python}
n = 10
for i in range(n)
print("Hello World")
\end{minted}

\lipsum*[1]
\end{document}


The \inputminted command is defined with an optional argument, so it's not patchable with etoolbox, unless you say
\expandafter\pretocmd\csname\string\inputminted\endcsname...

The package xpatch does the necessary bits by itself.
\usepackage{xpatch}