Skip to content

Commit

Permalink
Add Python 3 support boilerplate and make some easy changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
hannosch committed Sep 23, 2016
1 parent 9648512 commit c30adaf
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 41 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ parts
*.pyc
*.pyo
.installed.cfg
.tox
develop-eggs/
eggs/
*.egg-info/
9 changes: 7 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ language: python
sudo: false
python:
- 2.7
# - 3.3
# - 3.4
# - 3.5
# - pypy
install:
- python bootstrap.py
- bin/buildout
- pip install zc.buildout
- buildout bootstrap
- buildout
script:
- bin/test -v1
notifications:
Expand Down
6 changes: 5 additions & 1 deletion buildout.cfg
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
[buildout]
extends = https://raw.githubusercontent.com/zopefoundation/Zope/master/versions.cfg
develop = .
parts = interpreter test
parts = tox interpreter test

[versions]
DocumentTemplate =

[tox]
recipe = zc.recipe.egg
eggs = tox

[interpreter]
recipe = zc.recipe.egg
interpreter = python
Expand Down
7 changes: 6 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,20 @@
"License :: OSI Approved :: Zope Public License",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 2 :: Only",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.7",
# "Programming Language :: Python :: 3.3",
# "Programming Language :: Python :: 3.4",
# "Programming Language :: Python :: 3.5",
"Programming Language :: Python :: Implementation :: CPython",
# "Programming Language :: Python :: Implementation :: PyPy",
],
install_requires=[
'AccessControl',
'Acquisition',
'ExtensionClass>=4.1a1',
'RestrictedPython',
'six',
'zExceptions',
'zope.sequencesort',
'zope.structuredtext',
Expand Down
3 changes: 3 additions & 0 deletions src/DocumentTemplate/DT_In.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@
if sys.version_info > (3, 0):
unicode = str

def cmp(a, b):
return (a > b) - (a < b)

TupleType = tuple
StringTypes = (str, unicode)

Expand Down
48 changes: 17 additions & 31 deletions src/DocumentTemplate/DT_String.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,17 @@

import os
import re
import sys
import thread
from threading import Lock

import six

from DocumentTemplate.DT_Util import ParseError, InstanceDict
from DocumentTemplate.DT_Util import TemplateDict, render_blocks, str
from DocumentTemplate.DT_Util import TemplateDict, render_blocks
from DocumentTemplate.DT_Var import Var, Call, Comment
from DocumentTemplate.DT_Return import ReturnTag, DTReturn


PY3 = False
if sys.version_info > (3, 0):
PY3 = True

_marker = [] # Create a new marker object.
COOKLOCK = Lock()


class String(object):
Expand All @@ -53,13 +50,13 @@ class String(object):
class func_code:
pass

func_code = func_code()
func_code.co_varnames = 'self', 'REQUEST'
func_code.co_argcount = 2
func_code.__roles__ = ()
__code__ = func_code = func_code()
__code__.co_varnames = 'self', 'REQUEST'
__code__.co_argcount = 2
__code__.__roles__ = ()

func_defaults__roles__ = ()
func_defaults = ()
__defaults__ = func_defaults = ()
__defaults____roles__ = func_defaults__roles__ = ()

errQuote__roles__ = ()
def errQuote(self, s):
Expand Down Expand Up @@ -111,17 +108,10 @@ def _parseTag(self, match_ob, command=None, sargs='', tt=type(())):
cname, module, name = command
d = {}
try:
if PY3:
exec('from %s import %s' % (module, name), d)
else:
exec 'from %s import %s' % (module, name) in d
six.exec_('from %s import %s' % (module, name), d)
except ImportError:
if PY3:
exec('from DocumentTemplate.%s import %s' % (
module, name), d)
else:
exec 'from DocumentTemplate.%s import %s' % (
module, name) in d
six.exec_('from DocumentTemplate.%s import %s' % (
module, name), d)
command = d[name]
self.commands[cname] = command
return tag, args, command, coname
Expand Down Expand Up @@ -379,14 +369,10 @@ def read(self, raw=None):
return self.read_raw()

cook__roles__ = ()
def cook(self,
cooklock=thread.allocate_lock()):
cooklock.acquire()
try:
def cook(self):
with COOKLOCK:
self._v_blocks = self.parse(self.read())
self._v_cooked = None
finally:
cooklock.release()

initvars__roles__ = ()
def initvars(self, globals, vars):
Expand Down Expand Up @@ -577,7 +563,7 @@ def read_raw(self):
if self.edited_source:
return self.edited_source
if not os.path.exists(self.raw):
print 'file not found: %s' % self.raw
print('file not found: %s' % self.raw)

if self.raw:
return open(self.raw, 'r').read()
Expand Down
9 changes: 4 additions & 5 deletions src/DocumentTemplate/DT_Util.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from RestrictedPython.Eval import RestrictionCapableEval
from zExceptions import Unauthorized as ValidationError

# for import by other modules, dont remove!
# backwards compatibility
from DocumentTemplate.html_quote import html_quote, ustr # NOQA
from DocumentTemplate._DocumentTemplate import ( # NOQA
InstanceDict,
Expand All @@ -41,14 +41,13 @@
from RestrictedPython.Utilities import test
utility_builtins['test'] = test

test = utility_builtins['test'] # for backwards compatibility, dont remove!
test = utility_builtins['test'] # backwards compatibility
utility_builtins['sequence'] = sequence
safe_builtins['sequence'] = sequence
_safe_globals['sequence'] = sequence

LIMITED_BUILTINS = 1

str = __builtins__['str'] # Waaaaa, waaaaaaaa needed for pickling waaaaa
str = str # NOQA - backwards compatibility for pickling


class ParseError(Exception):
Expand All @@ -74,7 +73,7 @@ class NotBindable:
def __init__(self, f):
self.__call__ = f

for name, f in safe_builtins.items() + utility_builtins.items():
for name, f in list(safe_builtins.items()) + list(utility_builtins.items()):
if type(f) is functype:
f = NotBindable(f)
setattr(TemplateDict, name, f)
Expand Down
3 changes: 2 additions & 1 deletion src/DocumentTemplate/DT_Var.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@
import re
import string
import sys
from urllib import quote, quote_plus, unquote, unquote_plus

from six.moves.urllib.parse import quote, quote_plus, unquote, unquote_plus

from Acquisition import aq_base
from AccessControl.tainted import TaintedString
Expand Down
10 changes: 10 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[tox]
envlist =
py27
# py33,py34,py35,pypy

[testenv]
commands =
zope-testrunner --test-path={envsitepackagesdir} -s DocumentTemplate
deps =
zope.testrunner

0 comments on commit c30adaf

Please sign in to comment.