Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-123446: Fix empty function names in TypeErrors in _csv module #123461

Merged
merged 1 commit into from
Aug 29, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix empty function name in :exc:`TypeError` when :func:`csv.reader`,
:func:`csv.writer`, or :func:`csv.register_dialect` are used without the
required args.
6 changes: 3 additions & 3 deletions Modules/_csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,7 @@ csv_reader(PyObject *module, PyObject *args, PyObject *keyword_args)
return NULL;
}

if (!PyArg_UnpackTuple(args, "", 1, 2, &iterator, &dialect)) {
if (!PyArg_UnpackTuple(args, "_csv.reader", 1, 2, &iterator, &dialect)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unnecessary exposes the implementation detail (the private module name).

>>> import csv
>>> csv.reader
<built-in function reader>
>>> csv.reader()
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    csv.reader()
    ~~~~~~~~~~^^
TypeError: _csv.reader expected at least 1 argument, got 0

Compare with:

>>> import os
>>> os.mkdir()
Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    os.mkdir()
    ~~~~~~~~^^
TypeError: mkdir() missing required argument 'path' (pos 1)

Copy link
Member Author

@sobolevn sobolevn Aug 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this was my open question. I will remove the module name.

@hauntsaninja sorry, I should have made this more clear :(

Py_DECREF(self);
return NULL;
}
Expand Down Expand Up @@ -1519,7 +1519,7 @@ csv_writer(PyObject *module, PyObject *args, PyObject *keyword_args)

self->error_obj = Py_NewRef(module_state->error_obj);

if (!PyArg_UnpackTuple(args, "", 1, 2, &output_file, &dialect)) {
if (!PyArg_UnpackTuple(args, "_csv.writer", 1, 2, &output_file, &dialect)) {
Py_DECREF(self);
return NULL;
}
Expand Down Expand Up @@ -1571,7 +1571,7 @@ csv_register_dialect(PyObject *module, PyObject *args, PyObject *kwargs)
_csvstate *module_state = get_csv_state(module);
PyObject *dialect;

if (!PyArg_UnpackTuple(args, "", 1, 2, &name_obj, &dialect_obj))
if (!PyArg_UnpackTuple(args, "_csv.register_dialect", 1, 2, &name_obj, &dialect_obj))
return NULL;
if (!PyUnicode_Check(name_obj)) {
PyErr_SetString(PyExc_TypeError,
Expand Down
Loading