[Python-checkins] r45505 - python/trunk/Modules/posixmodule.c

skip at pobox.com skip at pobox.com
Tue Apr 18 23:53:57 CEST 2006


    >> The return type is size_t and cannot return -1.  Do we know if the
    >> previous code ever worked?  Is there any system that does return -1?

    Martin> It's now broken in a different way. Previously, it was broken
    Martin> because it reset errno after the call. Now it is broken because
    Martin> it treats -1 as an error indication; the correct error
    Martin> indication is 0 (but 0 also indicates that there is no
    Martin> configuration-defined value, hence errno must be set to 0 before
    Martin> the call).

I was working from the confstr man page on my Mac which reads, in part:

SYNOPSIS
     #include <unistd.h>

     size_t
     confstr(int name, char *buf, size_t len);

DESCRIPTION
     This interface is specified by POSIX.  A more flexible (but non-portable)
     interface is provided by sysctl(3).

     ...

RETURN VALUES
     If the call to confstr() is not successful, -1 is returned and errno is
     set appropriately.  Otherwise, if the variable does not have a
     configuration defined value, 0 is returned and errno is not modified.
     Otherwise, the buffer size needed to hold the entire configuration-
     defined value is returned.  If this size is greater than the argument
     len, the string in buf was truncated.

I just tried this little program:

    #include <stdio.h>
    #include <errno.h>
    #include <unistd.h>

    main(int argc, char **argv) {
            char buf[80];
            errno = 0;
            printf("confstr(5) returned %u, errno == %d\n",
                   confstr(5, buf, 79), errno);
            errno = 0;
            printf("confstr(-1) returned %u, errno == %d\n",
                   confstr(-1, buf, 79), errno);
    }

which, when run, outputs:

    confstr(5) returned 1, errno == 0
    confstr(-1) returned 0, errno == 22

It appears the Mac's documentation is incorrect.

    Martin> Also, I suggest to use None as the return value for "no value
    Martin> available"; it might be that the configured value is an empty
    Martin> string (in which case confstr returns 1).

    Martin> Also, the PyString_FromStringAndSize is wrong: the confstr
    Martin> result includes the terminating \0, whereas
    Martin> PyString_FromStringAndSize doesn't.

    Martin> Also, for symmetry, it would be better if
    Martin> PyString_FromStringAndSize is also used in the case without
    Martin> resizing. This would also have the advantage of allowing for
    Martin> results that include \0 (although I doubt they are actually
    Martin> used).

I'll work on all of this.  Are you sure you want the API to change?

Skip


More information about the Python-checkins mailing list