Skip to content

Commit

Permalink
Fix GlobalObject breaking with just a .
Browse files Browse the repository at this point in the history
Fixes #35.
  • Loading branch information
jamadden committed Sep 26, 2018
1 parent abac053 commit beff591
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ Changes
4.2.1 (unreleased)
------------------

- Nothing changed yet.
- Fix ``GlobalObject`` (and ``GlobalInterface``) no longer allowing
just a single '.'. See `issue 35
<https://github.com/zopefoundation/zope.configuration/issues/35>`_.


4.2.0 (2018-09-26)
Expand Down
13 changes: 10 additions & 3 deletions src/zope/configuration/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@

class PythonIdentifier(schema_PythonIdentifier):
"""
This class is like `zope.schema.PythonIdentifier`, but does not allow empty strings.
This class is like `zope.schema.PythonIdentifier`.
.. versionchanged:: 4.2.0
Extend `zope.schema.PythonIdentifier`, which implies that `fromUnicode`
validates the strings.
"""

def _validate(self, value):
Expand Down Expand Up @@ -73,9 +77,12 @@ def fromUnicode(self, value):

try:
# Leading dots are allowed here to indicate current
# package.
# package, but not accepted by DottedName. Take care,
# though, because a single dot is valid to resolve, but
# not valid to pass to DottedName (as an empty string)
to_validate = name[1:] if name.startswith('.') else name
self._DOT_VALIDATOR.validate(to_validate)
if to_validate:
self._DOT_VALIDATOR.validate(to_validate)
except ValidationError as v:
v.with_field_and_value(self, name)
raise
Expand Down
16 changes: 15 additions & 1 deletion src/zope/configuration/tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,23 @@ def resolve(self, name):
context = Context()
bound = go.bind(context)
found = bound.fromUnicode('tried')
self.assertTrue(found is _target)
self.assertIs(found, _target)
self.assertEqual(context._resolved, 'tried')

def test_fromUnicode_w_resolve_dot(self):
_target = object()
class Context(object):
_resolved = None
def resolve(self, name):
self._resolved = name
return _target
go = self._makeOne()
context = Context()
bound = go.bind(context)
found = bound.fromUnicode('.')
self.assertIs(found, _target)
self.assertEqual(context._resolved, '.')

def test_fromUnicode_w_resolve_but_validation_fails(self):
from zope.schema import Text
from zope.schema import ValidationError
Expand Down

0 comments on commit beff591

Please sign in to comment.