We had to revert #3041 because it called `resetwarnings` which disturbs current warnings filters. Fix #2831 without disturbing current warning filters.