update printing code from upstream

Fix #1545