Skip to content

Unhandled TypeError when sorting strings containing >4,300 digits on Python 3.11+ #193

Description

@Abhinav-143x

Bug Description

In Python 3.11 and newer, calling natsort.natsorted() on a collection containing normal numeric strings alongside a string with more than 4,300 consecutive digits raises an unhandled TypeError: '<' not supported between instances of 'str' and 'int'.

Because key generation functions return tuples specifically to avoid TypeErrors during heterogeneous sorting, this unhandled exception breaks sorting routines on valid string inputs without warning.

Root Cause

In Python 3.11+, CPython enforces an integer string conversion boundary (sys.get_int_max_str_digits(), defaulting to 4,300 digits) to mitigate algorithmic complexity attacks.

When the optional fastnumbers C extension is not installed, numeric sequences are processed through the fallback routine in natsort/compat/fake_fastnumbers.py (lines 110–117):

    if x[0] in _first_char:
        try:
            return int(x)
        except ValueError:
            try:
                return _uni(x, key(x)) if len(x) == 1 else key(x)

When x exceeds 4,300 digits, int(x) raises ValueError: Exceeds the limit (4300) for integer string conversion. The exception block catches this error and returns key(x), defaulting the token to a raw str.

When Python's sorting engine evaluates a normal integer token against this fallback string token in the same tuple position, attempting an int < str comparison terminates execution with a TypeError.

Reproduction

import sys
import natsort

filenames = [
    "item_10.png",
    "item_2.png",
    "item_" + ("9" * 5000) + ".png"
]

try:
    sorted_files = natsort.natsorted(filenames)
    print("Sort succeeded:", sorted_files[:2])
except TypeError as e:
    print(f"CRASH: {e!r}")

Output on CPython 3.11+:

CRASH: TypeError('< not supported between instances of \'str\' and \'int\'')

cc @SethMMorton

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions