From c52c59f8581418934ac15a0510fcf3639125e508 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Sat, 19 Mar 2016 19:53:27 +0100 Subject: [PATCH] port mitmproxy.scripts to py3 --- .travis.yml | 4 ++-- mitmproxy/contentviews.py | 4 ++-- mitmproxy/contrib/wbxml/ASCommandResponse.py | 2 +- mitmproxy/contrib/wbxml/ASWBXML.py | 8 +++---- mitmproxy/contrib/wbxml/ASWBXMLByteQueue.py | 2 +- mitmproxy/controller.py | 10 ++++----- mitmproxy/models/http.py | 2 +- mitmproxy/script/concurrent.py | 4 ++-- mitmproxy/script/script.py | 23 +++++++++++++++----- test/mitmproxy/tutils.py | 2 +- 10 files changed, 37 insertions(+), 24 deletions(-) diff --git a/.travis.yml b/.travis.yml index ce48ab219..d0e5e62d1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,9 +20,9 @@ matrix: os: osx osx_image: xcode7.1 - python: 3.5 - env: SCOPE="netlib" + env: SCOPE="netlib ./test/mitmproxy/script" - python: 3.5 - env: SCOPE="netlib" NO_ALPN=1 + env: SCOPE="netlib ./test/mitmproxy/script" NO_ALPN=1 - python: 2.7 env: DOCS=1 script: 'cd docs && make html' diff --git a/mitmproxy/contentviews.py b/mitmproxy/contentviews.py index 5335b59c5..1b0f389fb 100644 --- a/mitmproxy/contentviews.py +++ b/mitmproxy/contentviews.py @@ -13,7 +13,7 @@ requests, the query parameters are passed as the ``query`` keyword argument. """ from __future__ import (absolute_import, print_function, division) -import cStringIO +from six.moves import cStringIO as StringIO import json import logging import subprocess @@ -397,7 +397,7 @@ class ViewImage(View): def __call__(self, data, **metadata): try: - img = Image.open(cStringIO.StringIO(data)) + img = Image.open(StringIO(data)) except IOError: return None parts = [ diff --git a/mitmproxy/contrib/wbxml/ASCommandResponse.py b/mitmproxy/contrib/wbxml/ASCommandResponse.py index 08d034456..f5f62e856 100644 --- a/mitmproxy/contrib/wbxml/ASCommandResponse.py +++ b/mitmproxy/contrib/wbxml/ASCommandResponse.py @@ -27,7 +27,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ''' -from ASWBXML import ASWBXML +from .ASWBXML import ASWBXML import logging class ASCommandResponse: diff --git a/mitmproxy/contrib/wbxml/ASWBXML.py b/mitmproxy/contrib/wbxml/ASWBXML.py index 926d18c03..009ba48f0 100644 --- a/mitmproxy/contrib/wbxml/ASWBXML.py +++ b/mitmproxy/contrib/wbxml/ASWBXML.py @@ -30,10 +30,10 @@ THE SOFTWARE. import xml.dom.minidom import logging -from ASWBXMLCodePage import ASWBXMLCodePage -from ASWBXMLByteQueue import ASWBXMLByteQueue -from GlobalTokens import GlobalTokens -from InvalidDataException import InvalidDataException +from .ASWBXMLCodePage import ASWBXMLCodePage +from .ASWBXMLByteQueue import ASWBXMLByteQueue +from .GlobalTokens import GlobalTokens +from .InvalidDataException import InvalidDataException class ASWBXML: versionByte = 0x03 diff --git a/mitmproxy/contrib/wbxml/ASWBXMLByteQueue.py b/mitmproxy/contrib/wbxml/ASWBXMLByteQueue.py index c7a9e0a55..ecc76efb5 100644 --- a/mitmproxy/contrib/wbxml/ASWBXMLByteQueue.py +++ b/mitmproxy/contrib/wbxml/ASWBXMLByteQueue.py @@ -27,7 +27,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ''' -from Queue import Queue +from six.moves.queue import Queue import logging class ASWBXMLByteQueue(Queue): diff --git a/mitmproxy/controller.py b/mitmproxy/controller.py index 9a059856e..fedfce628 100644 --- a/mitmproxy/controller.py +++ b/mitmproxy/controller.py @@ -1,5 +1,5 @@ from __future__ import absolute_import -import Queue +from six.moves import queue import threading @@ -27,7 +27,7 @@ class Reply: def __init__(self, obj): self.obj = obj - self.q = Queue.Queue() + self.q = queue.Queue() self.acked = False def __call__(self, msg=None): @@ -56,7 +56,7 @@ class Channel: try: # The timeout is here so we can handle a should_exit event. g = m.reply.q.get(timeout=0.5) - except Queue.Empty: # pragma: no cover + except queue.Empty: # pragma: no cover continue return g @@ -98,7 +98,7 @@ class Master(object): server may be None if no server is needed. """ self.server = server - self.masterq = Queue.Queue() + self.masterq = queue.Queue() self.should_exit = threading.Event() def tick(self, q, timeout): @@ -113,7 +113,7 @@ class Master(object): self.handle(*msg) q.task_done() changed = True - except Queue.Empty: + except queue.Empty: pass return changed diff --git a/mitmproxy/models/http.py b/mitmproxy/models/http.py index 0338945b1..21096a39e 100644 --- a/mitmproxy/models/http.py +++ b/mitmproxy/models/http.py @@ -1,5 +1,5 @@ from __future__ import (absolute_import, print_function, division) -import Cookie +from six.moves import http_cookies as Cookie import copy import warnings from email.utils import parsedate_tz, formatdate, mktime_tz diff --git a/mitmproxy/script/concurrent.py b/mitmproxy/script/concurrent.py index f0f5e3cd5..57ee37dec 100644 --- a/mitmproxy/script/concurrent.py +++ b/mitmproxy/script/concurrent.py @@ -47,7 +47,7 @@ class ScriptThread(threading.Thread): def concurrent(fn): - if fn.func_name in ( + if fn.__name__ in ( "request", "response", "error", @@ -60,4 +60,4 @@ def concurrent(fn): return _concurrent raise NotImplementedError( - "Concurrent decorator not supported for '%s' method." % fn.func_name) + "Concurrent decorator not supported for '%s' method." % fn.__name__) diff --git a/mitmproxy/script/script.py b/mitmproxy/script/script.py index edc17d43a..f142daca1 100644 --- a/mitmproxy/script/script.py +++ b/mitmproxy/script/script.py @@ -8,6 +8,9 @@ import os import shlex import traceback import sys + +import six + from ..exceptions import ScriptException @@ -40,7 +43,8 @@ class Script(object): def parse_command(command): if not command or not command.strip(): raise ScriptException("Empty script command.") - if os.name == "nt": # Windows: escape all backslashes in the path. + # Windows: escape all backslashes in the path. + if os.name == "nt": # pragma: no cover backslashes = shlex.split(command, posix=False)[0].count("\\") command = command.replace("\\", "\\\\", backslashes) args = shlex.split(command) # pragma: nocover @@ -71,10 +75,15 @@ class Script(object): self.ns = {'__file__': os.path.abspath(self.args[0])} sys.path.append(script_dir) try: - execfile(self.args[0], self.ns, self.ns) + with open(self.filename) as f: + code = compile(f.read(), self.filename, 'exec') + exec (code, self.ns, self.ns) except Exception as e: - # Python 3: use exception chaining, https://www.python.org/dev/peps/pep-3134/ - raise ScriptException(traceback.format_exc(e)) + six.reraise( + ScriptException, + ScriptException(str(e)), + sys.exc_info()[2] + ) finally: sys.path.pop() return self.run("start", self.args) @@ -103,6 +112,10 @@ class Script(object): try: return f(self.ctx, *args, **kwargs) except Exception as e: - raise ScriptException(traceback.format_exc(e)) + six.reraise( + ScriptException, + ScriptException(str(e)), + sys.exc_info()[2] + ) else: return None diff --git a/test/mitmproxy/tutils.py b/test/mitmproxy/tutils.py index 791db6d9e..191eb9665 100644 --- a/test/mitmproxy/tutils.py +++ b/test/mitmproxy/tutils.py @@ -3,7 +3,7 @@ import shutil import tempfile import argparse import sys -from cStringIO import StringIO +from six.moves import cStringIO as StringIO from contextlib import contextmanager from unittest.case import SkipTest