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

Skip to content

Commit 4d1e64b

Browse files
committed
posix_fildes(): New helper: run a function that takes a file descriptor
and returns None. This allows any object that supports the fileno() method to be passed as a file descriptor, not just an integer. posix_fchdir(): New exposed function: implements posix.fchdir(). This closes SF feature #536796. posix_fsync(), posix_fdatasync(): Convert to use posix_fildes() instead of posix_int(). This also changes them from METH_VARARGS to METH_O functions. setup_confname_table(): Remove unused variable. Change to take a module rather than a dict to save the resulting table into. setup_confname_tables(): Change to take a module instead of a dict to pass to setup_confname_table().
1 parent 0157276 commit 4d1e64b

1 file changed

Lines changed: 71 additions & 52 deletions

File tree

Modules/posixmodule.c

Lines changed: 71 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ posix_int(PyObject *args, char *format, int (*func)(int))
464464
{
465465
int fd;
466466
int res;
467-
if (!PyArg_ParseTuple(args, format, &fd))
467+
if (!PyArg_ParseTuple(args, format, &fd))
468468
return NULL;
469469
Py_BEGIN_ALLOW_THREADS
470470
res = (*func)(fd);
@@ -475,6 +475,22 @@ posix_int(PyObject *args, char *format, int (*func)(int))
475475
return Py_None;
476476
}
477477

478+
static PyObject *
479+
posix_fildes(PyObject *fdobj, int (*func)(int))
480+
{
481+
int fd;
482+
int res;
483+
fd = PyObject_AsFileDescriptor(fdobj);
484+
if (fd < 0)
485+
return NULL;
486+
Py_BEGIN_ALLOW_THREADS
487+
res = (*func)(fd);
488+
Py_END_ALLOW_THREADS
489+
if (res < 0)
490+
return posix_error();
491+
Py_INCREF(Py_None);
492+
return Py_None;
493+
}
478494

479495
static PyObject *
480496
posix_1str(PyObject *args, char *format, int (*func)(const char*))
@@ -823,6 +839,19 @@ posix_chdir(PyObject *self, PyObject *args)
823839
#endif
824840
}
825841

842+
#ifdef HAVE_FCHDIR
843+
static char posix_fchdir__doc__[] =
844+
"fchdir(fildes) -> None\n\
845+
Change to the directory of the given file descriptor. fildes must be\n\
846+
opened on a directory, not a file.";
847+
848+
static PyObject *
849+
posix_fchdir(PyObject *self, PyObject *fdobj)
850+
{
851+
return posix_fildes(fdobj, fchdir);
852+
}
853+
#endif /* HAVE_FCHDIR */
854+
826855

827856
static char posix_chmod__doc__[] =
828857
"chmod(path, mode) -> None\n\
@@ -866,9 +895,9 @@ static char posix_fsync__doc__[] =
866895
force write of file with filedescriptor to disk.";
867896

868897
static PyObject *
869-
posix_fsync(PyObject *self, PyObject *args)
898+
posix_fsync(PyObject *self, PyObject *fdobj)
870899
{
871-
return posix_int(args, "i:fsync", fsync);
900+
return posix_fildes(fdobj, fsync);
872901
}
873902
#endif /* HAVE_FSYNC */
874903

@@ -884,9 +913,9 @@ force write of file with filedescriptor to disk.\n\
884913
does not force update of metadata.";
885914

886915
static PyObject *
887-
posix_fdatasync(PyObject *self, PyObject *args)
916+
posix_fdatasync(PyObject *self, PyObject *fdobj)
888917
{
889-
return posix_int(args, "i:fdatasync", fdatasync);
918+
return posix_fildes(fdobj, fdatasync);
890919
}
891920
#endif /* HAVE_FDATASYNC */
892921

@@ -6088,11 +6117,10 @@ cmp_constdefs(const void *v1, const void *v2)
60886117

60896118
static int
60906119
setup_confname_table(struct constdef *table, size_t tablesize,
6091-
char *tablename, PyObject *moddict)
6120+
char *tablename, PyObject *module)
60926121
{
60936122
PyObject *d = NULL;
60946123
size_t i;
6095-
int status;
60966124

60976125
qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
60986126
d = PyDict_New();
@@ -6108,34 +6136,32 @@ setup_confname_table(struct constdef *table, size_t tablesize,
61086136
}
61096137
Py_DECREF(o);
61106138
}
6111-
status = PyDict_SetItemString(moddict, tablename, d);
6112-
Py_DECREF(d);
6113-
return status;
6139+
return PyModule_AddObject(module, tablename, d);
61146140
}
61156141

61166142
/* Return -1 on failure, 0 on success. */
61176143
static int
6118-
setup_confname_tables(PyObject *moddict)
6144+
setup_confname_tables(PyObject *module)
61196145
{
61206146
#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
61216147
if (setup_confname_table(posix_constants_pathconf,
61226148
sizeof(posix_constants_pathconf)
61236149
/ sizeof(struct constdef),
6124-
"pathconf_names", moddict))
6150+
"pathconf_names", module))
61256151
return -1;
61266152
#endif
61276153
#ifdef HAVE_CONFSTR
61286154
if (setup_confname_table(posix_constants_confstr,
61296155
sizeof(posix_constants_confstr)
61306156
/ sizeof(struct constdef),
6131-
"confstr_names", moddict))
6157+
"confstr_names", module))
61326158
return -1;
61336159
#endif
61346160
#ifdef HAVE_SYSCONF
61356161
if (setup_confname_table(posix_constants_sysconf,
61366162
sizeof(posix_constants_sysconf)
61376163
/ sizeof(struct constdef),
6138-
"sysconf_names", moddict))
6164+
"sysconf_names", module))
61396165
return -1;
61406166
#endif
61416167
return 0;
@@ -6384,11 +6410,14 @@ static PyMethodDef posix_methods[] = {
63846410
#ifdef HAVE_STRERROR
63856411
{"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
63866412
#endif
6413+
#ifdef HAVE_FCHDIR
6414+
{"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
6415+
#endif
63876416
#ifdef HAVE_FSYNC
6388-
{"fsync", posix_fsync, METH_VARARGS, posix_fsync__doc__},
6417+
{"fsync", posix_fsync, METH_O, posix_fsync__doc__},
63896418
#endif
63906419
#ifdef HAVE_FDATASYNC
6391-
{"fdatasync", posix_fdatasync, METH_VARARGS, posix_fdatasync__doc__},
6420+
{"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
63926421
#endif
63936422
#ifdef HAVE_SYS_WAIT_H
63946423
#ifdef WIFSTOPPED
@@ -6446,19 +6475,14 @@ static PyMethodDef posix_methods[] = {
64466475

64476476

64486477
static int
6449-
ins(PyObject *d, char *symbol, long value)
6478+
ins(PyObject *module, char *symbol, long value)
64506479
{
6451-
PyObject* v = PyInt_FromLong(value);
6452-
if (!v || PyDict_SetItemString(d, symbol, v) < 0)
6453-
return -1; /* triggers fatal error */
6454-
6455-
Py_DECREF(v);
6456-
return 0;
6480+
return PyModule_AddIntConstant(module, symbol, value);
64576481
}
64586482

64596483
#if defined(PYOS_OS2)
64606484
/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
6461-
static int insertvalues(PyObject *d)
6485+
static int insertvalues(PyObject *module)
64626486
{
64636487
APIRET rc;
64646488
ULONG values[QSV_MAX+1];
@@ -6474,13 +6498,13 @@ static int insertvalues(PyObject *d)
64746498
return -1;
64756499
}
64766500

6477-
if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
6478-
if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1;
6479-
if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
6480-
if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
6481-
if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
6482-
if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1;
6483-
if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1;
6501+
if (ins(module, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
6502+
if (ins(module, "memkernel", values[QSV_TOTRESMEM])) return -1;
6503+
if (ins(module, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
6504+
if (ins(module, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
6505+
if (ins(module, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
6506+
if (ins(module, "revision", values[QSV_VERSION_REVISION])) return -1;
6507+
if (ins(module, "timeslice", values[QSV_MIN_SLICE])) return -1;
64846508

64856509
switch (values[QSV_VERSION_MINOR]) {
64866510
case 0: ver = "2.00"; break;
@@ -6497,22 +6521,15 @@ static int insertvalues(PyObject *d)
64976521
}
64986522

64996523
/* Add Indicator of the Version of the Operating System */
6500-
v = PyString_FromString(ver);
6501-
if (!v || PyDict_SetItemString(d, "version", v) < 0)
6524+
if (PyModule_AddStringConstant(module, "version", tmp) < 0)
65026525
return -1;
6503-
Py_DECREF(v);
65046526

65056527
/* Add Indicator of Which Drive was Used to Boot the System */
65066528
tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
65076529
tmp[1] = ':';
65086530
tmp[2] = '\0';
65096531

6510-
v = PyString_FromString(tmp);
6511-
if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0)
6512-
return -1;
6513-
Py_DECREF(v);
6514-
6515-
return 0;
6532+
return PyModule_AddStringConstant(module, "bootdrive", tmp);
65166533
}
65176534
#endif
65186535

@@ -6680,28 +6697,27 @@ all_ins(PyObject *d)
66806697
DL_EXPORT(void)
66816698
INITFUNC(void)
66826699
{
6683-
PyObject *m, *d, *v;
6700+
PyObject *m, *v;
66846701

6685-
m = Py_InitModule4(MODNAME,
6702+
m = Py_InitModule3(MODNAME,
66866703
posix_methods,
6687-
posix__doc__,
6688-
(PyObject *)NULL,
6689-
PYTHON_API_VERSION);
6690-
d = PyModule_GetDict(m);
6704+
posix__doc__);
66916705

66926706
/* Initialize environ dictionary */
66936707
v = convertenviron();
6694-
if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
6708+
Py_XINCREF(v);
6709+
if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
66956710
return;
66966711
Py_DECREF(v);
66976712

6698-
if (all_ins(d))
6713+
if (all_ins(m))
66996714
return;
67006715

6701-
if (setup_confname_tables(d))
6716+
if (setup_confname_tables(m))
67026717
return;
67036718

6704-
PyDict_SetItemString(d, "error", PyExc_OSError);
6719+
Py_INCREF(PyExc_OSError);
6720+
PyModule_AddObject(m, "error", PyExc_OSError);
67056721

67066722
#ifdef HAVE_PUTENV
67076723
if (posix_putenv_garbage == NULL)
@@ -6710,9 +6726,12 @@ INITFUNC(void)
67106726

67116727
stat_result_desc.name = MODNAME ".stat_result";
67126728
PyStructSequence_InitType(&StatResultType, &stat_result_desc);
6713-
PyDict_SetItemString(d, "stat_result", (PyObject*) &StatResultType);
6729+
Py_INCREF((PyObject*) &StatResultType);
6730+
PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
67146731

67156732
statvfs_result_desc.name = MODNAME ".statvfs_result";
67166733
PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
6717-
PyDict_SetItemString(d, "statvfs_result", (PyObject*) &StatVFSResultType);
6734+
Py_INCREF((PyObject*) &StatVFSResultType);
6735+
PyModule_AddObject(m, "statvfs_result",
6736+
(PyObject*) &StatVFSResultType);
67186737
}

0 commit comments

Comments
 (0)