BUG: core: ensure file handle positions are in sync

The Python3 file handles keep records of current file positions, so that
the raw handle needs to be restored to the original position in order
to not confuse the mechanism.

Fixes gh-4118