Skip to content
Open
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
14 changes: 14 additions & 0 deletions src/_crypt_r.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,17 @@ crypt_r_crypt(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Py_ssize_t salt_length;

if (!PyUnicode_Check(args[0])) {
#if PY_VERSION_HEX >= 0x030d00a6
PyErr_Format(
PyExc_TypeError, "crypt argument 1 (word) must be string, not %T",
args[0]
);
#else
PyErr_Format(
PyExc_TypeError, "crypt argument 1 (word) must be string, not %s",
Py_TYPE(args[0])->tp_name
);
#endif
return NULL;
}
word = PyUnicode_AsUTF8AndSize(args[0], &word_length);
Expand All @@ -53,10 +60,17 @@ crypt_r_crypt(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
}

if (!PyUnicode_Check(args[1])) {
#if PY_VERSION_HEX >= 0x030d00a6
PyErr_Format(
PyExc_TypeError, "crypt argument 2 (salt) must be string, not %T",
args[1]
);
#else
PyErr_Format(
PyExc_TypeError, "crypt argument 2 (salt) must be string, not %s",
Py_TYPE(args[1])->tp_name
);
#endif
return NULL;
}
salt = PyUnicode_AsUTF8AndSize(args[1], &salt_length);
Expand Down
8 changes: 8 additions & 0 deletions tests/test_crypt_r.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ def test_invalid_rounds(self):
with self.assertRaisesRegex(ValueError, 'support'):
self.crypt.mksalt(method, rounds=4096)

def test_invalid_types(self):
with self.assertRaisesRegex(TypeError,
r'^crypt argument 1 \(word\) must be string, not int$'):
self.crypt.crypt(42)
with self.assertRaisesRegex(TypeError,
r'^crypt argument 2 \(salt\) must be string, not int$'):
self.crypt.crypt('mypassword', 42)


if sys.version_info >= (3, 13):
import crypt
Expand Down