[Python-Dev] a few strdup() questions...

Ulrich Eckhardt doomster at knuut.de
Wed Jan 7 12:30:35 CET 2009


Greetings!

MS Windows CE doesn't provide strdup(), so where should I put it? I guess I 
should just compile in Python/strdup.c, right?

However, where should I declare it? My approach would be to declare it in 
PC/pyconfig.h. I see that RISCOS also seems to lack that function, which is 
why it is declared locally in _localemodule.c, but I guess this isn't really 
the best of all possible ways.

Also, there is HAVE_STRDUP. I would actually expect that #undef HAVE_STRDUP 
would do the trick to at least declare this, but it doesn't. I guess that 
most modern OS have this so this will probably just be bitrot ... right? But 
where should I put the declaration?

BTW: there is another implementation (called my_strdup) in 
Modules/_ctypes/_ctypes_test.c, why not use the one in Python/strdup.c there?

Lastly: I would have written the thing a bit differently:

char* strdup(char const* s) {
    char* res;
    size_t len;

    assert(s);

    len = strlen(s);
    res = malloc(len+1);
    if(res)
        memcpy(res, s, len+1);
    return res;
}

First difference is that I wouldn't accept NULL as valid input, e.g. the glibc 
implementation doesn't either and GCC even warns you if you call 
strdup(NULL). Secondly, I would have used memcpy(), since the length is 
already known and then potentially quicker. Should I write a patch?

thanks

Uli


More information about the Python-Dev mailing list