Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e4ad8aa

Browse files
Issue #4591: Uid and gid values larger than 2**31 are supported now.
1 parent 008deb7 commit e4ad8aa

8 files changed

Lines changed: 294 additions & 127 deletions

File tree

Lib/test/test_posix.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,20 @@ def test_mknod(self):
232232
else:
233233
self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))
234234

235-
def _test_all_chown_common(self, chown_func, first_param):
235+
def _test_all_chown_common(self, chown_func, first_param, stat_func):
236236
"""Common code for chown, fchown and lchown tests."""
237+
def check_stat():
238+
if stat_func is not None:
239+
stat = stat_func(first_param)
240+
self.assertEqual(stat.st_uid, os.getuid())
241+
self.assertEqual(stat.st_gid, os.getgid())
237242
# test a successful chown call
238243
chown_func(first_param, os.getuid(), os.getgid())
244+
check_stat()
245+
chown_func(first_param, -1, os.getgid())
246+
check_stat()
247+
chown_func(first_param, os.getuid(), -1)
248+
check_stat()
239249

240250
if os.getuid() == 0:
241251
try:
@@ -255,8 +265,12 @@ def _test_all_chown_common(self, chown_func, first_param):
255265
"behavior")
256266
else:
257267
# non-root cannot chown to root, raises OSError
258-
self.assertRaises(OSError, chown_func,
259-
first_param, 0, 0)
268+
self.assertRaises(OSError, chown_func, first_param, 0, 0)
269+
check_stat()
270+
self.assertRaises(OSError, chown_func, first_param, -1, 0)
271+
check_stat()
272+
self.assertRaises(OSError, chown_func, first_param, 0, -1)
273+
check_stat()
260274

261275
@unittest.skipUnless(hasattr(posix, 'chown'), "test needs os.chown()")
262276
def test_chown(self):
@@ -266,7 +280,8 @@ def test_chown(self):
266280

267281
# re-create the file
268282
open(support.TESTFN, 'w').close()
269-
self._test_all_chown_common(posix.chown, support.TESTFN)
283+
self._test_all_chown_common(posix.chown, support.TESTFN,
284+
getattr(posix, 'stat', None))
270285

271286
@unittest.skipUnless(hasattr(posix, 'fchown'), "test needs os.fchown()")
272287
def test_fchown(self):
@@ -276,7 +291,8 @@ def test_fchown(self):
276291
test_file = open(support.TESTFN, 'w')
277292
try:
278293
fd = test_file.fileno()
279-
self._test_all_chown_common(posix.fchown, fd)
294+
self._test_all_chown_common(posix.fchown, fd,
295+
getattr(posix, 'fstat', None))
280296
finally:
281297
test_file.close()
282298

@@ -285,7 +301,8 @@ def test_lchown(self):
285301
os.unlink(support.TESTFN)
286302
# create a symlink
287303
os.symlink(_DUMMY_SYMLINK, support.TESTFN)
288-
self._test_all_chown_common(posix.lchown, support.TESTFN)
304+
self._test_all_chown_common(posix.lchown, support.TESTFN,
305+
getattr(posix, 'lstat', None))
289306

290307
def test_chdir(self):
291308
if hasattr(posix, 'chdir'):

Lib/test/test_pwd.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ def test_values(self):
4949

5050
def test_errors(self):
5151
self.assertRaises(TypeError, pwd.getpwuid)
52+
self.assertRaises(TypeError, pwd.getpwuid, 3.14)
5253
self.assertRaises(TypeError, pwd.getpwnam)
54+
self.assertRaises(TypeError, pwd.getpwnam, 42)
5355
self.assertRaises(TypeError, pwd.getpwall, 42)
5456

5557
# try to get some errors
@@ -93,6 +95,13 @@ def test_errors(self):
9395
self.assertNotIn(fakeuid, byuids)
9496
self.assertRaises(KeyError, pwd.getpwuid, fakeuid)
9597

98+
# -1 shouldn't be a valid uid because it has a special meaning in many
99+
# uid-related functions
100+
self.assertRaises(KeyError, pwd.getpwuid, -1)
101+
# should be out of uid_t range
102+
self.assertRaises(KeyError, pwd.getpwuid, 2**128)
103+
self.assertRaises(KeyError, pwd.getpwuid, -2**128)
104+
96105
def test_main():
97106
support.run_unittest(PwdTest)
98107

Makefile.pre.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,12 @@ Modules/getpath.o: $(srcdir)/Modules/getpath.c Makefile
587587
Modules/python.o: $(srcdir)/Modules/python.c
588588
$(MAINCC) -c $(PY_CORE_CFLAGS) -o $@ $(srcdir)/Modules/python.c
589589

590+
Modules/posixmodule.o: $(srcdir)/Modules/posixmodule.c $(srcdir)/Modules/posixmodule.h
591+
592+
Modules/grpmodule.o: $(srcdir)/Modules/grpmodule.c $(srcdir)/Modules/posixmodule.h
593+
594+
Modules/pwdmodule.o: $(srcdir)/Modules/pwdmodule.c $(srcdir)/Modules/posixmodule.h
595+
590596
Python/dynload_shlib.o: $(srcdir)/Python/dynload_shlib.c Makefile
591597
$(CC) -c $(PY_CORE_CFLAGS) \
592598
-DSOABI='"$(SOABI)"' \

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ Library
226226

227227
- Issue #17052: unittest discovery should use self.testLoader.
228228

229+
- Issue #4591: Uid and gid values larger than 2**31 are supported now.
230+
229231
- Issue #17141: random.vonmisesvariate() no more hangs for large kappas.
230232

231233
- Issue #17149: Fix random.vonmisesvariate to always return results in

Modules/grpmodule.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
/* UNIX group file access module */
33

44
#include "Python.h"
5+
#include "posixmodule.h"
56

6-
#include <sys/types.h>
77
#include <grp.h>
88

99
static PyStructSequence_Field struct_group_type_fields[] = {
@@ -69,7 +69,7 @@ mkgrent(struct group *p)
6969
Py_INCREF(Py_None);
7070
}
7171
#endif
72-
SET(setIndex++, PyLong_FromLong((long) p->gr_gid));
72+
SET(setIndex++, _PyLong_FromGid(p->gr_gid));
7373
SET(setIndex++, w);
7474
#undef SET
7575

@@ -85,17 +85,24 @@ static PyObject *
8585
grp_getgrgid(PyObject *self, PyObject *pyo_id)
8686
{
8787
PyObject *py_int_id;
88-
unsigned int gid;
88+
gid_t gid;
8989
struct group *p;
9090

9191
py_int_id = PyNumber_Long(pyo_id);
9292
if (!py_int_id)
9393
return NULL;
94-
gid = PyLong_AS_LONG(py_int_id);
94+
if (!_Py_Gid_Converter(py_int_id, &gid)) {
95+
Py_DECREF(py_int_id);
96+
return NULL;
97+
}
9598
Py_DECREF(py_int_id);
9699

97100
if ((p = getgrgid(gid)) == NULL) {
98-
PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %d", gid);
101+
PyObject *gid_obj = _PyLong_FromGid(gid);
102+
if (gid_obj == NULL)
103+
return NULL;
104+
PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %S", gid_obj);
105+
Py_DECREF(gid_obj);
99106
return NULL;
100107
}
101108
return mkgrent(p);

0 commit comments

Comments
 (0)