Skip to content

Commit

Permalink
Fixes a JSON related issue. The interface of the json module is
Browse files Browse the repository at this point in the history
different in Python 2.5 and 2.6. The dumps() method, for instance,
is missing in Python 2.5.

git-svn-id: svn://cherokee-project.com/CTK/trunk@5010 5dc97367-97f1-0310-9951-d761b3857238
  • Loading branch information
alobbs committed May 3, 2010
1 parent d69701d commit cb4fc25
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
10 changes: 3 additions & 7 deletions CTK/Server.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,11 @@
import threading
import traceback

try:
import json
except ImportError:
import json_embedded as json

import pyscgi
import Cookie
import Config

from util import json_dump
from Post import Post
from HTTP import HTTP_Response, HTTP_Error

Expand Down Expand Up @@ -108,7 +104,7 @@ def _do_handle (self):
validator = PostValidator (post, published._validation)
errors = validator.Validate()
if errors:
resp = HTTP_Response(200, body=json.dumps(errors))
resp = HTTP_Response(200, body=json_dump(errors))
resp['Content-Type'] = "application/json"
return resp

Expand All @@ -121,7 +117,7 @@ def _do_handle (self):
return self.response

elif type(ret) == dict:
info = json.dumps(ret)
info = json_dump(ret)
self.response += info
self.response['Content-Type'] = "application/json"
return self.response
Expand Down
13 changes: 5 additions & 8 deletions CTK/Widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@
#

from consts import *
from util import json_dump
from PageCleaner import Postprocess

try:
import json
except ImportError:
import json_embedded as json

widget_uniq_id = 1;

Expand Down Expand Up @@ -79,10 +76,10 @@ def toJSON (self):
else:
help = []

return json.dumps({'html': self.html,
'js': Postprocess(self.js),
'headers': self.headers,
'helps': help})
return json_dump({'html': self.html,
'js': Postprocess(self.js),
'headers': self.headers,
'helps': help})


class Widget:
Expand Down
16 changes: 16 additions & 0 deletions CTK/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@

import re

try:
import json
except ImportError:
import json_embedded as json

#
# Strings
#
Expand Down Expand Up @@ -83,3 +88,14 @@ def find_copy_name (orig, names):
return '%s Copy' %(orig)

return '%s Copy %d' %(orig, higher+1)

#
# JSon
#
def json_dump (obj):
# Python 2.6, and json_embeded
if hasattr (json, 'dumps'):
return json.dumps (obj)

# Python 2.5
return json.write(obj)

0 comments on commit cb4fc25

Please sign in to comment.