Skip to content

Commit

Permalink
Fix FSEntry's path type handling
Browse files Browse the repository at this point in the history
From the comments I understood that ``FSEntry`` aims to store the ``path``
using the binary form. This commit addresses two issues:

* In Py2, ``FSEntry`` was encoding ``path`` only with the `ascii` encoder
  raising ``UnicodeEncodeError`` when encoding certain Unicode strings.
* In Py3, it was not converting to bytes.
  • Loading branch information
sevein committed Mar 8, 2019
1 parent 1f4b7fd commit 1954131
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 2 additions & 3 deletions metsrw/fsentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,9 @@ def __init__(self, path=None, label=None, use='original', type=u'Item',
# path can validly be any encoding; if this value needs
# to be spliced later on, it's better to treat it as a
# bytestring than as actually being encoded text.
# TODO update this with six and bytes
if path:
path = str(path)
self.path = path
if isinstance(self.path, six.text_type):
self.path = self.path.encode('utf-8')
if label is None and path is not None:
label = os.path.basename(path)
self.label = label
Expand Down
8 changes: 8 additions & 0 deletions tests/test_fsentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@
import pytest
from unittest import TestCase
import uuid
import six

import metsrw


class TestFSEntry(TestCase):
""" Test FSEntry class. """

def test_path_is_binary(self):
"""It should store the ``path`` as a bytestring."""
assert isinstance(
metsrw.FSEntry(u'💜🎑💜', type='Directory').path, six.binary_type)
assert isinstance(
metsrw.FSEntry(b'💜🎑💜', type='Directory').path, six.binary_type)

def test_create_invalid_checksum_type(self):
""" It should only accept METS valid checksum types. """
metsrw.FSEntry(
Expand Down

0 comments on commit 1954131

Please sign in to comment.