Skip to content

csv.get_dialect() and csv.unregister_dialect() raise csv.Error for an unhashable name instead of the hashing error #9507

Closed

Description

@jseop-lim

Feature

csv.get_dialect() and csv.unregister_dialect() raise csv.Error for any name that is not a str, but CPython looks the name up in a dict, so an unhashable name raises its hashing error.

Reproduction

import csv
csv.get_dialect([])

RustPython:

_csv.Error: argument 0 must be a string, not 'list'

CPython:

TypeError: cannot use 'list' as a dict key (unhashable type: 'list')

csv.unregister_dialect([]) behaves the same, and a name whose __hash__ raises ValueError gives _csv.Error instead of that ValueError in both functions.

Root cause

get_dialect and unregister_dialect downcast name to PyStr and turn every other object into _csv.Error without hashing it. CPython's get_dialect_from_registry and _csv_unregister_dialect_impl pass the name to PyDict_GetItemRef and PyDict_Pop, which propagate a hashing error and leave unknown dialect for a clean miss.

Fix

For a name that is not a str, call obj.hash(vm)? and then return _csv.Error("unknown dialect"), since the registry only holds str keys.

Environment

  • RustPython 713e14c (Python 3.15.0 alpha), also reproduced on 0.4.0 and 0.6.1
  • CPython v3.15.2, v3.14.4 and v3.12.10 (3.12 prints TypeError: unhashable type: 'list')
  • OS: macOS arm64 (713e14c), Debian 12 (0.4.0, 0.6.1)

Python Documentation or reference to CPython source code

  • CPython get_dialect_from_registry: Modules/_csv.c#L162-L170 reports unknown dialect only when the lookup returns 0, so a hashing error (-1) propagates:

    PyObject *dialect_obj;
    if (PyDict_GetItemRef(module_state->dialects, name_obj, &dialect_obj) == 0) {
        PyErr_SetString(module_state->error_obj, "unknown dialect");
    }
    return dialect_obj;
  • CPython _csv_unregister_dialect_impl: Modules/_csv.c#L1652-L1666 does the same with PyDict_Pop:

    int rc = PyDict_Pop(module_state->dialects, name, NULL);
    if (rc < 0) {
        return NULL;
    }
    if (rc == 0) {
        PyErr_Format(module_state->error_obj, "unknown dialect");
        return NULL;
    }
  • Documentation: csv.get_dialect and csv.unregister_dialect

AI disclosure: this report was drafted with Claude Code (claude-opus-5-5) and reviewed by me before submission. The divergence comes from a differential-testing run against CPython 3.12, and every source reference above was checked against the linked revisions.

Activity

  1. added
    C-compat
    on Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-compat

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions