From f50b02dd642cef02d71af4a499a08525351c1270 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Sat, 4 Oct 2014 16:05:00 +0200 Subject: [PATCH] fix ox.api in python3 --- ox/api.py | 25 ++++++++++++++++++------- ox/form.py | 29 +++++++++++++++++++---------- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/ox/api.py b/ox/api.py index 52afbad..3b57ae2 100644 --- a/ox/api.py +++ b/ox/api.py @@ -5,7 +5,7 @@ from __future__ import with_statement from six.moves import http_cookiejar as cookielib import gzip -from six import BytesIO +from six import BytesIO, PY2 from six.moves import urllib from types import MethodType @@ -45,7 +45,10 @@ class API(object): def _add_method(self, method, name): if name is None: name = method.func_name - setattr(self, name, MethodType(method, self, type(self))) + if PY2: + setattr(self, name, MethodType(method, self, type(self))) + else: + setattr(self, name, MethodType(method, self)) def _add_action(self, action): def method(self, *args, **kw): @@ -57,18 +60,26 @@ class API(object): return self._request(action, kw) if 'doc' in self._properties[action]: method.__doc__ = self._properties[action]['doc'] - method.func_name = str(action) + if PY2: + method.func_name = str(action) + else: + method.func_name = action self._add_method(method, action) def _json_request(self, url, form): result = {} try: - body = str(form) - request = urllib.request.Request(str(url)) - request.add_header('Content-type', form.get_content_type()) + body = form.body() + if PY2: + if not isinstance(url, bytes): + url = url.encode('utf-8') + request = urllib.request.Request(url) + request.add_data(body) + else: + request = urllib.request.Request(url, data=body, method='POST') + request.add_header('Content-Type', form.get_content_type()) request.add_header('Content-Length', str(len(body))) request.add_header('Accept-Encoding', 'gzip, deflate') - request.add_data(body) f = self._opener.open(request) result = f.read() if f.headers.get('content-encoding', None) == 'gzip': diff --git a/ox/form.py b/ox/form.py index 10d17ab..8969e03 100644 --- a/ox/form.py +++ b/ox/form.py @@ -8,6 +8,8 @@ import mimetypes import random import sys +from six import PY2 + __all__ = ['MultiPartForm'] @@ -36,19 +38,19 @@ class MultiPartForm(object): def add_field(self, name, value): """Add a simple field to the form data.""" - if isinstance(name, unicode): - name = name.encode('utf-8') - if isinstance(value, unicode): - value = value.encode('utf-8') + if isinstance(name, bytes): + name = name.decode('utf-8') + if isinstance(value, bytes): + value = value.decode('utf-8') self.form_fields.append((name, value)) return def add_file(self, fieldname, filename, fileHandle, mimetype=None): """Add a file to be uploaded.""" - if isinstance(fieldname, unicode): - fieldname = fieldname.encode('utf-8') - if isinstance(filename, unicode): - filename = filename.encode('utf-8') + if isinstance(fieldname, bytes): + fieldname = fieldname.decode('utf-8') + if isinstance(filename, bytes): + filename = filename.decode('utf-8') if hasattr(fileHandle, 'read'): body = fileHandle.read() @@ -58,8 +60,14 @@ class MultiPartForm(object): mimetype = mimetypes.guess_type(filename)[0] or 'application/octet-stream' self.files.append((fieldname, filename, mimetype, body)) return - + def __str__(self): + body = self.body() + if not PY2: + body = body.decode('utf-8') + return body + + def body(self): """Return a string representing the form data, including attached files.""" # Build a list of lists, each containing "lines" of the # request. Each part is separated by a boundary string. @@ -95,5 +103,6 @@ class MultiPartForm(object): flattened = list(itertools.chain(*parts)) flattened.append('--' + self.boundary + '--') flattened.append('') - return '\r\n'.join(flattened) + flattened = [part if isinstance(part, bytes) else part.encode('utf-8') for part in flattened] + return b'\r\n'.join(flattened)