A person who fails to comply with a writ of habeas corpus, or with a judgment rendered after a hearing on a petition for a writ of habeas corpus, mandamus, or quo warranto may be punished for contempt. When a sentence of imprisonment is imposed for contempt, imprisonment may continue until the defendant obeys the writ or judgment.