diff roundup/rest.py @ 5655:207e0f5d551c

Merge in non-conflicting changes from ba67e397f063 including workaround for: https://bugs.python.org/issue27777 1) cgi/client.py: override cgi.FieldStorage's make_file so that file is always created in binary/byte mode. This means that json (and xml) are bytes not strings. 2) rest.py: try harder to find dicttoxml in roundup directory or on sys.path. This just worked under python 2 but python 3 only searches sys.path by default and does not search relative like python 2. 3) test/rest_common.py: workaround for issue27777 Also removed an unneeded case insensitive dict implementation.
author John Rouillard <rouilj@ieee.org>
date Mon, 18 Mar 2019 21:42:33 -0400
parents e8ca7072c629 ba67e397f063
children 3401daf8613b
line wrap: on
line diff
--- a/roundup/rest.py	Sun Mar 17 21:47:45 2019 -0400
+++ b/roundup/rest.py	Mon Mar 18 21:42:33 2019 -0400
@@ -20,14 +20,20 @@
 import re
 
 try:
-    from dicttoxml import dicttoxml
+    # if dicttoxml installed in roundup directory, use it
+    from .dicttoxml import dicttoxml
 except ImportError:
-    dicttoxml = None
+    try:
+        # else look in sys.path
+        from dicttoxml import dicttoxml
+    except ImportError:
+        # else not supported
+        dicttoxml = None
 
 from roundup import hyperdb
 from roundup import date
 from roundup import actions
-from roundup.anypy.strings import bs2b, s2b
+from roundup.anypy.strings import bs2b, b2s
 from roundup.exceptions import *
 from roundup.cgi.exceptions import *
 
@@ -1346,12 +1352,12 @@
             "Access-Control-Allow-Methods",
             "HEAD, OPTIONS, GET, PUT, DELETE, PATCH"
         )
-
         # Is there an input.value with format json data?
         # If so turn it into an object that emulates enough
         # of the FieldStorge methods/props to allow a response.
         content_type_header = headers.get('Content-Type', None)
-        if type(input.value) == str and content_type_header:
+        # python2 is str type, python3 is bytes
+        if type(input.value) in ( str, bytes ) and content_type_header:
             parsed_content_type_header = content_type_header
             # the structure of a content-type header
             # is complex: mime-type; options(charset ...)
@@ -1366,7 +1372,7 @@
             # for example.
             if content_type_header.lower() == "application/json":
                 try:
-                    input = SimulateFieldStorageFromJson(input.value)
+                    input = SimulateFieldStorageFromJson(b2s(input.value))
                 except ValueError as msg:
                     output = self.error_obj(400, msg)
 
@@ -1404,7 +1410,7 @@
 
         # Make output json end in a newline to
         # separate from following text in logs etc..
-        return s2b(output + "\n")
+        return bs2b(output + "\n")
 
 
 class RoundupJSONEncoder(json.JSONEncoder):

Roundup Issue Tracker: http://roundup-tracker.org/