Skip to content

Commit

Permalink
Merge branch 'py3' of github.com:zopefoundation/Zope into py3
Browse files Browse the repository at this point in the history
  • Loading branch information
dwt committed May 5, 2017
2 parents 0dcfb5f + f9dbc5e commit 68d1fab
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
4 changes: 0 additions & 4 deletions buildout.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ auto-checkout =
Products.ZCatalog
RestrictedPython

[sources]
DocumentTemplate = git ${remotes:github}/DocumentTemplate pushurl=${remotes:github_push}/DocumentTemplate branch=py3-pure-python
RestrictedPython = git ${remotes:github}/RestrictedPython pushurl=${remotes:github_push}/RestrictedPython branch=Python3_update


[tox]
env = py27
Expand Down
2 changes: 1 addition & 1 deletion src/OFS/tests/testAppInitializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def configure(self, text):
# of the directory is checked. This handles this in a
# platform-independent way.
schema = self.schema
sio = io.StringIO(text.replace("<<INSTANCE_HOME>>", TEMPNAME))
sio = io.StringIO(text.replace(u"<<INSTANCE_HOME>>", TEMPNAME))
conf, handler = ZConfig.loadConfigFile(schema, sio)
self.assertEqual(conf.instancehome, TEMPNAME)
setConfiguration(conf)
Expand Down
21 changes: 21 additions & 0 deletions src/ZTUtils/Zope.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,22 @@ def __init__(self, sequence, size, start=0, end=0,
# "make_query(bstart=batch.end)" to the other.


#Do not do this at import time.
#Call '_default_encoding()' at run time to retrieve it from config, if present
#If not configured, will be 'utf8' by default.
_DEFAULT_ENCODING = None
def _default_encoding():
''' Retreive default encoding from config '''
global _DEFAULT_ENCODING
if _DEFAULT_ENCODING is None:
from App.config import getConfiguration
config = getConfiguration()
try:
_DEFAULT_ENCODING = config.zpublisher_default_encoding
except AttributeError:
_DEFAULT_ENCODING = 'utf8'
return _DEFAULT_ENCODING

def make_query(*args, **kwargs):
'''Construct a URL query string, with marshalling markup.
Expand All @@ -188,6 +204,8 @@ def make_query(*args, **kwargs):
qlist = complex_marshal(list(d.items()))
for i in range(len(qlist)):
k, m, v = qlist[i]
if isinstance(v, unicode):
v = v.encode(_default_encoding())
qlist[i] = '%s%s=%s' % (quote(k), m, quote(str(v)))

return '&'.join(qlist)
Expand Down Expand Up @@ -275,6 +293,9 @@ def complex_marshal(pairs):
def simple_marshal(v):
if isinstance(v, str):
return ''
if isinstance(v, unicode):
encoding = _default_encoding()
return ':%s:ustring' % (encoding,)
if isinstance(v, bool):
return ':boolean'
if isinstance(v, int):
Expand Down
41 changes: 35 additions & 6 deletions src/ZTUtils/tests/testZope.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,61 @@

from ZTUtils.Zope import (
complex_marshal,
simple_marshal,
make_hidden_input,
make_query,
)


class QueryTests(unittest.TestCase):


def testMarshalString(self):
self.assertEqual(simple_marshal('string'), '')

def testMarshalBool(self):
self.assertEqual(simple_marshal(True), ':boolean')

def testMarshalInt(self):
self.assertEqual(simple_marshal(42), ":int")

def testMarshalFloat(self):
self.assertEqual(simple_marshal(3.1415), ":float")

def testMarshalDate(self):
self.assertEqual(simple_marshal(DateTime()), ":date")

def testMarshalUnicode(self):
self.assertEqual(simple_marshal(u'unic\xF3de'), ":utf8:ustring")

def testMarshallLists(self):
'''Test marshalling lists'''
test_date = DateTime()
list_ = [1, test_date, 'str']
list_ = [1, test_date, 'str', u'unic\xF3de']
result = complex_marshal([('list', list_), ])
assert result == [('list', ':int:list', 1),
('list', ':date:list', test_date),
('list', ':list', 'str')]
('list', ':list', 'str'),
('list', ':utf8:ustring:list', u'unic\xF3de')]

def testMarshallRecords(self):
'''Test marshalling records'''
test_date = DateTime()
record = {'arg1': 1, 'arg2': test_date, 'arg3': 'str'}
record = {'arg1': 1, 'arg2': test_date, 'arg3': 'str', 'arg4': u'unic\xF3de'}
result = complex_marshal([('record', record), ])
assert result == [('record.arg1', ':int:record', 1),
('record.arg2', ':date:record', test_date),
('record.arg3', ':record', 'str')]
('record.arg3', ':record', 'str'),
('record.arg4', ':utf8:ustring:record', u'unic\xF3de' )]

def testMarshallListsInRecords(self):
'''Test marshalling lists inside of records'''
test_date = DateTime()
record = {'arg1': [1, test_date, 'str'], 'arg2': 1}
record = {'arg1': [1, test_date, 'str', u'unic\xF3de'], 'arg2': 1}
result = complex_marshal([('record', record), ])
assert result == [('record.arg1', ':int:list:record', 1),
('record.arg1', ':date:list:record', test_date),
('record.arg1', ':list:record', 'str'),
('record.arg1', ':utf8:ustring:list:record', u'unic\xF3de'),
('record.arg2', ':int:record', 1)]

def testMakeComplexQuery(self):
Expand All @@ -56,6 +78,13 @@ def testMakeComplexQuery(self):
'record.arg1:int:list:record=1&record.arg1:date:list:record=%s&'
'record.arg1:list:record=str&record.arg2:int:record=1' % (
quote_date, quote_date, quote_date))

def testMakeQueryUnicode(self):
''' Test makequery against Github issue 15
https://github.com/zopefoundation/Zope/issues/15
'''
query = make_query(search_text=u'unic\xF3de')
self.assertEqual('search_text:utf8:ustring=unic%C3%B3de', query)

def testMakeHiddenInput(self):
tag = make_hidden_input(foo='bar')
Expand Down

0 comments on commit 68d1fab

Please sign in to comment.