Skip to content

Commit

Permalink
Address Serhiy's review
Browse files Browse the repository at this point in the history
* Add more tests
* Add 'const'
  • Loading branch information
vstinner committed Jan 26, 2025
1 parent 075a205 commit 4fc2e10
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions Lib/test/test_capi/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,21 @@ def check_getmoduleattr(self, getmoduleattr):
self.assertIs(getmoduleattr('sys', 'argv'), sys.argv)
self.assertIs(getmoduleattr('types', 'ModuleType'), types.ModuleType)

# module name containing a dot
attr = getmoduleattr('email.message', 'Message')
from email.message import Message
self.assertIs(attr, Message)

with self.assertRaises(ImportError):
# nonexistent module
getmoduleattr('nonexistentmodule', 'attr')
with self.assertRaises(AttributeError):
# nonexistent attribute
getmoduleattr('sys', 'nonexistentattr')
with self.assertRaises(AttributeError):
# attribute name containing a dot
getmoduleattr('sys', 'implementation.name')

def test_getmoduleattr(self):
# Test PyImport_GetModuleAttr()
getmoduleattr = _testcapi.PyImport_GetModuleAttr
Expand Down
2 changes: 1 addition & 1 deletion Modules/_testcapi/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pyimport_getmoduleattr(PyObject *self, PyObject *args)
static PyObject *
pyimport_getmoduleattrstring(PyObject *self, PyObject *args)
{
char *mod_name, *attr_name;
const char *mod_name, *attr_name;
Py_ssize_t len;
if (!PyArg_ParseTuple(args, "z#z#", &mod_name, &len, &attr_name, &len)) {
return NULL;
Expand Down

0 comments on commit 4fc2e10

Please sign in to comment.