Skip to content
This repository has been archived by the owner on Feb 10, 2023. It is now read-only.

Commit

Permalink
Python 3 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ale-rt committed Oct 14, 2018
1 parent 539f824 commit 18aad64
Show file tree
Hide file tree
Showing 50 changed files with 320 additions and 205 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -11,3 +11,4 @@ dist/
eggs/
lib/
parts/
/.tox
3 changes: 3 additions & 0 deletions .travis.yml
Expand Up @@ -2,6 +2,9 @@ language: python
sudo: false
python:
- 2.7
- 3.5
- 3.6
- 3.7
install:
- pip install -U setuptools==39.1.0 zc.buildout==2.11.4 six==1.11.0 coveralls
- buildout
Expand Down
2 changes: 1 addition & 1 deletion CHANGES.rst
Expand Up @@ -4,7 +4,7 @@ Changelog
4.0b4 (unreleased)
------------------

- Nothing changed yet.
- Python 3 compatibility


4.0b3 (2018-10-05)
Expand Down
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -42,6 +42,7 @@
],
install_requires=[
'setuptools',
'six',
'AccessControl',
'Acquisition',
'ExtensionClass',
Expand Down
9 changes: 5 additions & 4 deletions src/Signals/threads.py
@@ -1,15 +1,16 @@
import sys
import thread
import traceback
from six.moves import _thread
import time
from cStringIO import StringIO
import traceback

from six.moves import cStringIO as StringIO


def dump_threads():
"""Dump running threads. Returns a string with the tracebacks."""

frames = sys._current_frames()
this_thread_id = thread.get_ident()
this_thread_id = _thread.get_ident()
now = time.strftime("%Y-%m-%d %H:%M:%S")
res = ["Threads traceback dump at %s\n" % now]
for thread_id, frame in frames.iteritems():
Expand Down
17 changes: 14 additions & 3 deletions src/ZServer/ClockServer.py
Expand Up @@ -19,8 +19,13 @@
import os
import socket
import time
import StringIO
import asyncore
import six
try:
from base64 import encodebytes
except ImportError:
# PY2
from base64 import encodestring as encodebytes

from ZServer.medusa.http_server import http_request
from ZServer.medusa.default_handler import unquote
Expand All @@ -29,6 +34,9 @@
from ZPublisher.HTTPRequest import HTTPRequest


from io import StringIO


def timeslice(period, when=None, t=time.time):
if when is None:
when = t()
Expand Down Expand Up @@ -87,7 +95,10 @@ def __init__(self, method, period=60, user=None, password=None,
h.append('Host: %s' % host)
auth = False
if user and password:
encoded = ('%s:%s' % (user, password)).encode('base64')
user_password = '%s:%s' % (user, password)
if not isinstance(user_password, six.binary_type):
user_password = user_password.encode()
encoded = encodebytes(user_password)
h.append('Authorization: Basic %s' % encoded)
auth = True

Expand All @@ -102,7 +113,7 @@ def __init__(self, method, period=60, user=None, password=None,
self.zhandler = handler

def get_requests_and_response(self):
out = StringIO.StringIO()
out = StringIO()
s_req = '%s %s HTTP/%s' % ('GET', self.method, '1.0')
req = http_request(DummyChannel(self), s_req, 'GET', self.method,
'1.0', self.headers)
Expand Down
8 changes: 5 additions & 3 deletions src/ZServer/FCGIServer.py
Expand Up @@ -48,16 +48,18 @@

from ZServer import DebugLogger

from cStringIO import StringIO
from tempfile import TemporaryFile
import socket
import string
import os
import sys
import time
import thread
from six.moves import _thread
import base64

from io import StringIO


tz_for_log = compute_timezone_for_log()

# Set various FastCGI constants
Expand Down Expand Up @@ -689,7 +691,7 @@ def write(self, data):
l = string.atoi(l)
if l > 128000:
self._tempfile = TemporaryFile()
self._templock = thread.allocate_lock()
self._templock = _thread.allocate_lock()
except Exception:
pass

Expand Down
12 changes: 9 additions & 3 deletions src/ZServer/FTPRequest.py
Expand Up @@ -20,12 +20,18 @@

from ZPublisher.HTTPRequest import HTTPRequest

from cStringIO import StringIO
import os
from base64 import encodestring
try:
from base64 import encodebytes
except ImportError:
# PY2
from base64 import encodestring as encodebytes
import re


from io import StringIO


class FTPRequest(HTTPRequest):

def __init__(self, path, command, channel, response, stdin=None,
Expand Down Expand Up @@ -74,7 +80,7 @@ def _get_env(self, path, command, channel, stdin, size):
if channel.userid != 'anonymous':
env['HTTP_AUTHORIZATION'] = 'Basic %s' % re.sub(
'\012', '',
encodestring('%s:%s' % (channel.userid, channel.password)))
encodebytes('%s:%s' % (channel.userid, channel.password)))
env['SERVER_NAME'] = channel.server.hostname
env['SERVER_PORT'] = str(channel.server.port)
env['REMOTE_ADDR'] = channel.client_addr[0]
Expand Down
6 changes: 4 additions & 2 deletions src/ZServer/FTPResponse.py
Expand Up @@ -15,11 +15,13 @@
"""

from ZServer.HTTPResponse import ZServerHTTPResponse
from PubCore.ZEvent import Wakeup
from cStringIO import StringIO
from ZServer.PubCore.ZEvent import Wakeup
import marshal


from six.moves import cStringIO as StringIO


class FTPResponse(ZServerHTTPResponse):
"""
Response to an FTP command
Expand Down
14 changes: 10 additions & 4 deletions src/ZServer/HTTPResponse.py
Expand Up @@ -19,10 +19,10 @@
from __future__ import absolute_import

import asyncore
from cStringIO import StringIO
import re
import tempfile
import thread
import six
from six.moves import _thread
import time

from zope.event import notify
Expand All @@ -45,6 +45,9 @@
from ZServer.Zope2.Startup import config


from io import StringIO


class ZServerHTTPResponse(HTTPResponse):
"Used to push data into a channel's producer fifo"

Expand Down Expand Up @@ -73,6 +76,9 @@ def __str__(self):

headers = self.headers
body = self.body
# native string here
if not six.PY2 and isinstance(body, six.binary_type):
body = body.decode()

# set 204 (no content) status if 200 and response is empty
# and not streaming
Expand Down Expand Up @@ -119,7 +125,7 @@ def __str__(self):
self.setHeader('Transfer-Encoding', 'chunked')
self._chunking = 1

headers = headers.items()
headers = list(headers.items())
headers.extend(self.accumulated_headers)

for key, val in headers:
Expand Down Expand Up @@ -179,7 +185,7 @@ def write(self, data):
l = int(l)
if l > 128000:
self._tempfile = tempfile.TemporaryFile()
self._templock = thread.allocate_lock()
self._templock = _thread.allocate_lock()
except Exception:
pass

Expand Down
6 changes: 4 additions & 2 deletions src/ZServer/HTTPServer.py
Expand Up @@ -39,7 +39,6 @@
import posixpath
import time
import socket
from cStringIO import StringIO

from ZServer.PubCore import handle
from ZServer.HTTPResponse import make_response
Expand All @@ -60,6 +59,9 @@
from ZServer import DebugLogger


from six.moves import cStringIO as StringIO


CONTENT_LENGTH = re.compile('Content-Length: ([0-9]+)', re.I)
CONNECTION = re.compile('Connection: (.*)', re.I)
USER_AGENT = re.compile('User-Agent: (.*)', re.I)
Expand Down Expand Up @@ -176,7 +178,7 @@ def handle_request(self, request):

def get_environment(self, request,
# These are strictly performance hackery...
h2ehas=header2env.has_key,
h2ehas=lambda key: key in header2env,
h2eget=header2env.get,
workdir=os.getcwd(),
ospath=os.path,
Expand Down
4 changes: 3 additions & 1 deletion src/ZServer/PCGIServer.py
Expand Up @@ -31,7 +31,6 @@

import asynchat
import asyncore
from cStringIO import StringIO
from tempfile import TemporaryFile
import socket
import string
Expand All @@ -56,6 +55,9 @@
from ZServer import DebugLogger
from ZServer.Zope2.Startup import config

from six.moves import cStringIO as StringIO


tz_for_log = compute_timezone_for_log()


Expand Down
10 changes: 5 additions & 5 deletions src/ZServer/PubCore/ZRendezvous.py
Expand Up @@ -11,8 +11,8 @@
#
##############################################################################

import thread
from ZServerPublisher import ZServerPublisher
from six.moves import _thread
from .ZServerPublisher import ZServerPublisher


class ZRendevous(object):
Expand All @@ -23,7 +23,7 @@ class ZRendevous(object):
"""

def __init__(self, n=1):
sync = thread.allocate_lock()
sync = _thread.allocate_lock()
self._acquire = sync.acquire
self._release = sync.release
pool = []
Expand All @@ -38,10 +38,10 @@ def __init__(self, n=1):
self._acquire() # callers will block
try:
while n > 0:
l = thread.allocate_lock()
l = _thread.allocate_lock()
l.acquire()
pool.append(l)
thread.start_new_thread(ZServerPublisher,
_thread.start_new_thread(ZServerPublisher,
(self.accept,))
n = n - 1
finally:
Expand Down
11 changes: 8 additions & 3 deletions src/ZServer/Testing/doctest_functional.py
Expand Up @@ -13,14 +13,19 @@
"""Support for (functional) doc tests
"""

import base64
import doctest
import re
import sys
import warnings

import transaction

try:
from base64 import encodebytes
except ImportError:
# PY2
from base64 import encodestring as encodebytes

from Testing.ZopeTestCase import ZopeTestCase
from Testing.ZopeTestCase import FunctionalTestCase
from Testing.ZopeTestCase import Functional
Expand Down Expand Up @@ -109,7 +114,7 @@ def auth_header(header):
u = ''
if p is None:
p = ''
auth = base64.encodestring('%s:%s' % (u, p))
auth = encodebytes('%s:%s' % (u, p))
return 'Basic %s' % auth[:-1]
return header

Expand Down Expand Up @@ -294,7 +299,7 @@ def setup_globs(self):
globs['http'] = http
globs['getRootFolder'] = getRootFolder
globs['sync'] = sync
globs['user_auth'] = base64.encodestring(
globs['user_auth'] = encodebytes(
'%s:%s' % (user_name, user_password))

def setup_test_class(self):
Expand Down
9 changes: 7 additions & 2 deletions src/ZServer/Testing/functional.py
Expand Up @@ -15,10 +15,15 @@
After Marius Gedminas' functional.py module for Zope3.
"""

import base64
import re
import sys

try:
from base64 import encodebytes
except ImportError:
# PY2
from base64 import encodestring as encodebytes

import transaction
from zope.interface import implementer

Expand Down Expand Up @@ -87,7 +92,7 @@ def publish(self, path, basic=None, env=None, extra=None,
raise TypeError('')

if basic:
env['HTTP_AUTHORIZATION'] = "Basic %s" % base64.encodestring(basic)
env['HTTP_AUTHORIZATION'] = "Basic %s" % encodebytes(basic)

if stdin is None:
stdin = StringIO()
Expand Down
2 changes: 1 addition & 1 deletion src/ZServer/Testing/threadutils.py
Expand Up @@ -17,9 +17,9 @@
from __future__ import absolute_import

from threading import Thread
from StringIO import StringIO

from Lifetime import loop
from io import StringIO

dummyLOG = StringIO()

Expand Down
4 changes: 2 additions & 2 deletions src/ZServer/ZPublisher/Publish.py
Expand Up @@ -14,9 +14,9 @@
"""
import os
import sys
from thread import allocate_lock
from six.moves._thread import allocate_lock
import transaction
from urlparse import urlparse
from six.moves.urllib.parse import urlparse

from AccessControl.SecurityManagement import newSecurityManager
from AccessControl.SecurityManagement import noSecurityManager
Expand Down

0 comments on commit 18aad64

Please sign in to comment.