Skip to content

Commit

Permalink
Tighten encoding and exception types in NameChooser
Browse files Browse the repository at this point in the history
  • Loading branch information
jamadden committed Jul 27, 2017
1 parent a533804 commit 0fadbfa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
- Attain 100% test coverage. See
https://github.com/zopefoundation/zope.container/issues/15

- Make the default ``NameChooser`` always decode bytes using ASCII instead of
whatever the current system codec happens to be.

- Make the default ``NameChooser`` stop catching ``KeyboardInterrupt``
and other ``BaseException`` types when it potentially calls
user-defined code to convert a name to a text string. Instead, just
catch ``Exception``.

4.1.0 (2015-05-22)
==================

Expand Down
8 changes: 4 additions & 4 deletions src/zope/container/contained.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ def checkName(self, name, object):
"""

if isinstance(name, bytes):
name = name.decode()
name = name.decode('ascii')
elif not isinstance(name, text_type):
raise TypeError("Invalid name type", type(name))

Expand Down Expand Up @@ -823,18 +823,18 @@ def chooseName(self, name, object):

# convert to unicode and remove characters that checkName does not allow
if isinstance(name, bytes):
name = name.decode()
name = name.decode('ascii')
if not isinstance(name, text_type):
try:
name = text_type(name)
except:
except Exception:
name = u''
name = name.replace('/', '-').lstrip('+@')

if not name:
name = object.__class__.__name__
if isinstance(name, bytes):
name = name.decode()
name = name.decode('ascii')

# for an existing name, append a number.
# We should keep client's os.path.extsep (not ours), we assume it's '.'
Expand Down

0 comments on commit 0fadbfa

Please sign in to comment.