fixes #4277 this time in pure python (no calls to bash)