Commit 60ef19a2 authored by Tres Seaver's avatar Tres Seaver

- Raise more "Pythonic" errors from ``__setitem__``, losing the dependency

  on ``zope.exceptions``:

  o ``zope.exceptions.DuplicationError`` -> ``KeyError``

  o ``zope.exceptions.UserError`` -> ``ValueError``
parent c8107b86
...@@ -5,6 +5,13 @@ CHANGES ...@@ -5,6 +5,13 @@ CHANGES
3.7.1 (unreleased) 3.7.1 (unreleased)
------------------ ------------------
- Raise more "Pythonic" errors from ``__setitem__``, losing the dependency
on ``zope.exceptions``:
o ``zope.exceptions.DuplicationError`` -> ``KeyError``
o ``zope.exceptions.UserError`` -> ``ValueError``
- Moved import of ``IBroken`` interface to use new ``zope.broken`` - Moved import of ``IBroken`` interface to use new ``zope.broken``
package, which has no dependencies beyond ``zope.interface``. package, which has no dependencies beyond ``zope.interface``.
......
...@@ -68,7 +68,6 @@ setup(name='zope.container', ...@@ -68,7 +68,6 @@ setup(name='zope.container',
'zope.component', 'zope.component',
'zope.event', 'zope.event',
'zope.location', 'zope.location',
'zope.exceptions',
'zope.security', 'zope.security',
'zope.lifecycleevent', 'zope.lifecycleevent',
'zope.i18nmessageid', 'zope.i18nmessageid',
......
...@@ -25,7 +25,6 @@ from zope.interface.declarations import ObjectSpecification ...@@ -25,7 +25,6 @@ from zope.interface.declarations import ObjectSpecification
from zope.event import notify from zope.event import notify
from zope.component.interfaces import ObjectEvent from zope.component.interfaces import ObjectEvent
from zope.location.interfaces import ILocation, ISublocations from zope.location.interfaces import ILocation, ISublocations
from zope.exceptions.interfaces import DuplicationError, UserError
from zope.security.checker import selectChecker, CombinedChecker from zope.security.checker import selectChecker, CombinedChecker
from zope.lifecycleevent import ObjectModifiedEvent from zope.lifecycleevent import ObjectModifiedEvent
...@@ -478,7 +477,7 @@ def setitem(container, setitemf, name, object): ...@@ -478,7 +477,7 @@ def setitem(container, setitemf, name, object):
>>> setitem(container, container.__setitem__, u'c', []) >>> setitem(container, container.__setitem__, u'c', [])
Traceback (most recent call last): Traceback (most recent call last):
... ...
DuplicationError: c KeyError: u'c'
>>> del container[u'c'] >>> del container[u'c']
...@@ -582,7 +581,7 @@ def setitem(container, setitemf, name, object): ...@@ -582,7 +581,7 @@ def setitem(container, setitemf, name, object):
if old is object: if old is object:
return return
if old is not None: if old is not None:
raise DuplicationError(name) raise KeyError(name)
object, event = containedEvent(object, container, name) object, event = containedEvent(object, container, name)
setitemf(name, object) setitemf(name, object)
...@@ -718,19 +717,19 @@ class NameChooser(object): ...@@ -718,19 +717,19 @@ class NameChooser(object):
>>> NameChooser(container).checkName('+foo', object()) >>> NameChooser(container).checkName('+foo', object())
Traceback (most recent call last): Traceback (most recent call last):
... ...
UserError: Names cannot begin with '+' or '@' or contain '/' ValueError: Names cannot begin with '+' or '@' or contain '/'
>>> NameChooser(container).checkName('@foo', object()) >>> NameChooser(container).checkName('@foo', object())
Traceback (most recent call last): Traceback (most recent call last):
... ...
UserError: Names cannot begin with '+' or '@' or contain '/' ValueError: Names cannot begin with '+' or '@' or contain '/'
>>> NameChooser(container).checkName('f/oo', object()) >>> NameChooser(container).checkName('f/oo', object())
Traceback (most recent call last): Traceback (most recent call last):
... ...
UserError: Names cannot begin with '+' or '@' or contain '/' ValueError: Names cannot begin with '+' or '@' or contain '/'
>>> NameChooser(container).checkName('foo', object()) >>> NameChooser(container).checkName('foo', object())
Traceback (most recent call last): Traceback (most recent call last):
... ...
UserError: The given name is already being used KeyError: u'The given name is already being used'
>>> NameChooser(container).checkName(2, object()) >>> NameChooser(container).checkName(2, object())
Traceback (most recent call last): Traceback (most recent call last):
... ...
...@@ -745,7 +744,7 @@ class NameChooser(object): ...@@ -745,7 +744,7 @@ class NameChooser(object):
""" """
if not name: if not name:
raise UserError( raise ValueError(
_("An empty name was provided. Names cannot be empty.") _("An empty name was provided. Names cannot be empty.")
) )
...@@ -755,12 +754,12 @@ class NameChooser(object): ...@@ -755,12 +754,12 @@ class NameChooser(object):
raise TypeError("Invalid name type", type(name)) raise TypeError("Invalid name type", type(name))
if name[:1] in '+@' or '/' in name: if name[:1] in '+@' or '/' in name:
raise UserError( raise ValueError(
_("Names cannot begin with '+' or '@' or contain '/'") _("Names cannot begin with '+' or '@' or contain '/'")
) )
if name in self.context: if name in self.context:
raise UserError( raise KeyError(
_("The given name is already being used") _("The given name is already being used")
) )
......
...@@ -21,7 +21,6 @@ from BTrees.OOBTree import OOBTree ...@@ -21,7 +21,6 @@ from BTrees.OOBTree import OOBTree
from persistent import Persistent from persistent import Persistent
from zope.container.interfaces import IContainer, IContentContainer from zope.container.interfaces import IContainer, IContentContainer
from zope.container.contained import Contained, setitem, uncontained from zope.container.contained import Contained, setitem, uncontained
from zope.exceptions import DuplicationError
from zope.interface import implements, directlyProvides from zope.interface import implements, directlyProvides
# XXX This container implementation is really only used by # XXX This container implementation is really only used by
...@@ -93,7 +92,7 @@ class Folder(Persistent, Contained): ...@@ -93,7 +92,7 @@ class Folder(Persistent, Contained):
raise TypeError("Name must not be empty") raise TypeError("Name must not be empty")
if name in self.data: if name in self.data:
raise DuplicationError("name, %s, is already in use" % name) raise KeyError("name, %s, is already in use" % name)
setitem(self, self.data.__setitem__, name, object) setitem(self, self.data.__setitem__, name, object)
......
...@@ -64,7 +64,11 @@ class IWriteContainer(Interface): ...@@ -64,7 +64,11 @@ class IWriteContainer(Interface):
"""Add the given `object` to the container under the given name. """Add the given `object` to the container under the given name.
Raises a ``TypeError`` if the key is not a unicode or ascii string. Raises a ``TypeError`` if the key is not a unicode or ascii string.
Raises a ``ValueError`` if key is empty.
Raises a ``ValueError`` if the key is empty, or if the key contains
a character which is not allowed in an object name.
Raises a ``KeyError`` if the key violates a uniqueness constraint.
The container might choose to add a different object than the The container might choose to add a different object than the
one passed to this method. one passed to this method.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment