From db77c8c80b53d3983a253cd514e34295e55ce5a3 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sun, 10 Nov 2013 04:57:15 +0900 Subject: [PATCH] make sure simplejson always returns unicode --- anki/utils.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/anki/utils.py b/anki/utils.py index 0eeb3dfdd..06be1de67 100644 --- a/anki/utils.py +++ b/anki/utils.py @@ -3,12 +3,24 @@ # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html from __future__ import division -import re, os, random, time, math, htmlentitydefs, subprocess, \ - tempfile, shutil, string, httplib2, sys, locale +import re +import os +import random +import time +import math +import htmlentitydefs +import subprocess +import tempfile +import shutil +import string +import sys +import locale from hashlib import sha1 -from anki.lang import _, ngettext import platform +from anki.lang import _, ngettext + + if sys.version_info[1] < 5: def format_string(a, b): return a % b @@ -16,6 +28,14 @@ if sys.version_info[1] < 5: try: import simplejson as json + # make sure simplejson's loads() always returns unicode + # we don't try to support .load() + origLoads = json.loads + def loads(s, *args, **kwargs): + if not isinstance(s, unicode): + s = unicode(s, "utf8") + return origLoads(s, *args, **kwargs) + json.loads = loads except ImportError: import json