Skip to content

Commit

Permalink
flake8, move tests into a tests module.
Browse files Browse the repository at this point in the history
  • Loading branch information
hannosch committed Dec 7, 2016
1 parent d58012b commit 6b526d4
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 33 deletions.
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ ignore =
[flake8]
ignore = N801,N802,N803,N805,N806,N812,E301
exclude = bootstrap.py
max-complexity = 70
2 changes: 2 additions & 0 deletions src/DocumentTemplate/DT_In.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ def __call__(self, blocks):
else:
return i.renderwob


In = InFactory()


Expand Down Expand Up @@ -869,6 +870,7 @@ def int_param(params, md, name, default=0, st=type('')):
def nocase(str1, str2):
return cmp(str1.lower(), str2.lower())


if 'locale' in sys.modules: # only if locale is already imported
from locale import strcoll

Expand Down
8 changes: 8 additions & 0 deletions src/DocumentTemplate/DT_Util.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def int_param(params, md, name, default=0, st=type('')):
v = int(v)
return v or 0


functype = type(int_param)


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


for name, f in list(safe_builtins.items()) + list(utility_builtins.items()):
if type(f) is functype:
f = NotBindable(f)
Expand Down Expand Up @@ -122,6 +124,7 @@ def __call__(self, *args, **kw):
retval = TaintedString(retval)
return retval


TemplateDict.string = StringModuleWrapper()
TemplateDict.__allow_access_to_unprotected_subobjects__ = 1

Expand Down Expand Up @@ -155,6 +158,7 @@ def careful_hasattr(md, inst, name):
else:
return 1


TemplateDict.getattr = careful_getattr
TemplateDict.hasattr = careful_hasattr

Expand All @@ -170,6 +174,7 @@ def namespace(self, **kw):
information may contain more details.)''')
return self(**kw)


TemplateDict.namespace = namespace


Expand All @@ -186,6 +191,7 @@ def render(self, v):
v = v()
return v


TemplateDict.render = render


Expand Down Expand Up @@ -224,6 +230,7 @@ def __call__(self, **kw):
md._push(kw)
return self.eval(md)


simple_name = re.compile('[a-z][a-z0-9_]*', re.I).match


Expand Down Expand Up @@ -300,6 +307,7 @@ def name_param(params, tag='', expr=0, attr='name', default_unnamed=1):

raise ParseError('No %s given' % attr, tag)


Expr_doc = """
Expand Down
1 change: 1 addition & 0 deletions src/DocumentTemplate/DT_Var.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ def spacify(val):
val = val.replace('_', ' ')
return val


modifiers = (
html_quote, url_quote, url_quote_plus, url_unquote,
url_unquote_plus, newline_to_br,
Expand Down
1 change: 1 addition & 0 deletions src/DocumentTemplate/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def SecurityCalledByExecutable(md):
class _dummy_class(object):
pass


templateDict = DT_Util.TemplateDict()
try:
dictInstance = templateDict(dummy=1)[0]
Expand Down
4 changes: 4 additions & 0 deletions src/DocumentTemplate/tests/testDTML.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,15 +314,18 @@ def __getitem__(self, i):
class testob(Base):

__roles__ = None # Public

def __init__(self, index):
self.index = index
self.value = 'item %s' % index

getValue__roles__ = None # Public

def getValue(self):
return self.value

puke__roles__ = None # Public

def puke(self):
raise PukeError('raaalf')

Expand Down Expand Up @@ -574,6 +577,7 @@ def __init__(self, **kw):
def __repr__(self):
return "Dummy(%r)" % self.__dict__


docutils_include_warning = (
'<p class="system-message-title">System Message: WARNING/2 '
'(<tt class="docutils">&lt;string&gt;</tt>, line 1)</p>\n'
Expand Down
34 changes: 1 addition & 33 deletions src/TreeDisplay/TreeTag.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def render(self, md):

__call__ = render


String.commands['tree'] = Tree

pyid = id # Copy builtin
Expand Down Expand Up @@ -772,36 +773,3 @@ def refuse_to_unpickle(self):
pass
del k
del v


def _should_succeed(x, binary=1):
if x != MiniUnpickler(StringIO(pickle.dumps(x, binary))).load():
raise ValueError(x)


def _should_fail(x, binary=1):
try:
MiniUnpickler(StringIO(pickle.dumps(x, binary))).load()
raise ValueError(x)
except pickle.UnpicklingError as e:
if e[0] != 'Refused':
raise ValueError(x)


class _junk_class(object):
pass


def _test():
_should_succeed('hello')
_should_succeed(1)
_should_succeed(1.0)
_should_succeed((1, 2, 3))
_should_succeed([1, 2, 3])
_should_succeed({1: 2, 3: 4})
_should_fail(open)
_should_fail(_junk_class)
_should_fail(_junk_class())

# Test MiniPickle on every import
_test()
50 changes: 50 additions & 0 deletions src/TreeDisplay/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
##############################################################################
#
# Copyright (c) 2002 Zope Foundation and Contributors.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################

from cStringIO import StringIO
import pickle
import unittest

from TreeDisplay.TreeTag import (
MiniUnpickler,
)


class _junk_class(object):
pass


class TestMiniPickle(unittest.TestCase):

def _should_succeed(self, x, binary=1):
if x != MiniUnpickler(StringIO(pickle.dumps(x, binary))).load():
raise ValueError(x)

def _should_fail(self, x, binary=1):
try:
MiniUnpickler(StringIO(pickle.dumps(x, binary))).load()
raise ValueError(x)
except pickle.UnpicklingError as e:
if e[0] != 'Refused':
raise ValueError(x)

def test_mini_pickle(self):
self._should_succeed('hello')
self._should_succeed(1)
self._should_succeed(1.0)
self._should_succeed((1, 2, 3))
self._should_succeed([1, 2, 3])
self._should_succeed({1: 2, 3: 4})
self._should_fail(open)
self._should_fail(_junk_class)
self._should_fail(_junk_class())

0 comments on commit 6b526d4

Please sign in to comment.