Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions slugify/_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ def _decode_entities(text: str, entities: bool, decimal: bool, hexadecimal: bool


def _transliterate(text: str, backend: Backend) -> str:
# Non-behavioral: every backend maps 7-bit ASCII to itself, so skip importing one.
if text.isascii():
return text
if backend == 'auto':
try:
module = import_module('unidecode')
Expand Down
3 changes: 3 additions & 0 deletions slugify/slugify.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ def _decode_entities(text: str, entities: bool, decimal: bool, hexadecimal: bool


def _transliterate(text: str, backend: Backend) -> str:
# Every backend maps 7-bit ASCII to itself; skip importing one (text-unidecode's table is ~3 MB).
if text.isascii():
return text
if backend == 'auto':
try:
module = import_module('unidecode')
Expand Down
13 changes: 10 additions & 3 deletions tests/test_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,20 +241,27 @@ def test_bounded_length_and_default_idempotence(self):
def test_backend_selection_and_no_fallback_on_broken_install(self):
with patch.object(core, 'import_module') as load:
load.return_value.unidecode.return_value = 'chosen'
self.assertEqual(slugify('x'), 'chosen')
self.assertEqual(slugify('é'), 'chosen')
load.assert_called_once_with('unidecode')
with patch.object(core, 'import_module', side_effect=ModuleNotFoundError(name='dependency')):
with self.assertRaises(ModuleNotFoundError) as error:
slugify('x')
slugify('é')
self.assertEqual(error.exception.name, 'dependency')
with patch.object(core, 'import_module', side_effect=ModuleNotFoundError(name='anyascii')):
with self.assertRaises(ModuleNotFoundError):
slugify('x', backend='anyascii')
slugify('é', backend='anyascii')
with patch.object(core, 'import_module', side_effect=AssertionError('must not import')):
self.assertEqual(slugify('影師嗎', allow_unicode=True, backend='anyascii'), '影師嗎')
with self.assertRaisesRegex(ValueError, 'backend'):
slugify('x', backend='invalid')

def test_ascii_input_skips_backend_import(self):
legacy = importlib.import_module('slugify._legacy')
for module, call in ((core, slugify), (legacy, public_slugify)):
with self.subTest(module=module.__name__):
with patch.object(module, 'import_module', side_effect=AssertionError('must not import')):
self.assertEqual(call('Living Room & Kitchen 2'), 'living-room-kitchen-2')

def test_auto_falls_back_only_when_unidecode_missing(self):
original = core.import_module

Expand Down