Issue2649

I rewrite it and add docstring as well as a test.

[This was gh-3196, see there for the initial discussion. The bug is gh-2649]