~pythonregexp2.7/python/issue2636-01+09-01-01

« back to all changes in this revision

Viewing changes to Modules/posixmodule.c

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-09-22 00:02:12 UTC
  • mfrom: (39022.1.34 Regexp-2.7)
  • Revision ID: darklord@timehorse.com-20080922000212-7r0q4f4ugiq57jph
Merged in changes from the Atomic Grouping / Possessive Qualifiers branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
478
478
{
479
479
}
480
480
 
481
 
/* Function suitable for O& conversion */
482
481
static int
483
 
convert_to_unicode(PyObject *arg, void* _param)
 
482
convert_to_unicode(PyObject **param)
484
483
{
485
 
        PyObject **param = (PyObject**)_param;
486
 
        if (PyUnicode_CheckExact(arg)) {
487
 
                Py_INCREF(arg);
488
 
                *param = arg;
489
 
        } 
490
 
        else if (PyUnicode_Check(arg)) {
 
484
        if (PyUnicode_CheckExact(*param))
 
485
                Py_INCREF(*param);
 
486
        else if (PyUnicode_Check(*param))
491
487
                /* For a Unicode subtype that's not a Unicode object,
492
488
                   return a true Unicode object with the same data. */
493
 
                *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(arg),
494
 
                                               PyUnicode_GET_SIZE(arg));
495
 
                return *param != NULL;
496
 
        }
 
489
                *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
 
490
                                               PyUnicode_GET_SIZE(*param));
497
491
        else
498
 
                *param = PyUnicode_FromEncodedObject(arg,
 
492
                *param = PyUnicode_FromEncodedObject(*param,
499
493
                                                     Py_FileSystemDefaultEncoding,
500
494
                                                     "strict");
501
495
        return (*param) != NULL;
1956
1950
static PyObject *
1957
1951
posix_getcwd(PyObject *self, PyObject *noargs)
1958
1952
{
1959
 
        char buf[1026];
1960
 
        char *res;
 
1953
        int bufsize_incr = 1024;
 
1954
        int bufsize = 0;
 
1955
        char *tmpbuf = NULL;
 
1956
        char *res = NULL;
 
1957
        PyObject *dynamic_return;
1961
1958
 
1962
1959
        Py_BEGIN_ALLOW_THREADS
 
1960
        do {
 
1961
                bufsize = bufsize + bufsize_incr;
 
1962
                tmpbuf = malloc(bufsize);
 
1963
                if (tmpbuf == NULL) {
 
1964
                        break;
 
1965
                }
1963
1966
#if defined(PYOS_OS2) && defined(PYCC_GCC)
1964
 
        res = _getcwd2(buf, sizeof buf);
 
1967
                res = _getcwd2(tmpbuf, bufsize);
1965
1968
#else
1966
 
        res = getcwd(buf, sizeof buf);
 
1969
                res = getcwd(tmpbuf, bufsize);
1967
1970
#endif
 
1971
 
 
1972
                if (res == NULL) {
 
1973
                        free(tmpbuf);
 
1974
                }
 
1975
        } while ((res == NULL) && (errno == ERANGE));
1968
1976
        Py_END_ALLOW_THREADS
 
1977
 
1969
1978
        if (res == NULL)
1970
1979
                return posix_error();
1971
 
        return PyString_FromString(buf);
 
1980
 
 
1981
        dynamic_return = PyString_FromString(tmpbuf);
 
1982
        free(tmpbuf);
 
1983
 
 
1984
        return dynamic_return;
1972
1985
}
1973
1986
 
1974
1987
#ifdef Py_USING_UNICODE
2303
2316
                return NULL;
2304
2317
        }
2305
2318
        for (;;) {
 
2319
                errno = 0;
2306
2320
                Py_BEGIN_ALLOW_THREADS
2307
2321
                ep = readdir(dirp);
2308
2322
                Py_END_ALLOW_THREADS
2309
 
                if (ep == NULL)
2310
 
                        break;
 
2323
                if (ep == NULL) {
 
2324
                        if (errno == 0) {
 
2325
                                break;
 
2326
                        } else {
 
2327
                                closedir(dirp);
 
2328
                                Py_DECREF(d);
 
2329
                                return posix_error_with_allocated_filename(name);
 
2330
                        }
 
2331
                }
2311
2332
                if (ep->d_name[0] == '.' &&
2312
2333
                    (NAMLEN(ep) == 1 ||
2313
2334
                     (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
2344
2365
                }
2345
2366
                Py_DECREF(v);
2346
2367
        }
2347
 
        if (errno != 0 && d != NULL) {
2348
 
                /* readdir() returned NULL and set errno */
2349
 
                closedir(dirp);
2350
 
                Py_DECREF(d);
2351
 
                return posix_error_with_allocated_filename(name); 
2352
 
        }
2353
2368
        closedir(dirp);
2354
2369
        PyMem_Free(name);
2355
2370
 
2521
2536
        char *p1, *p2;
2522
2537
        BOOL result;
2523
2538
        if (unicode_file_names()) {
2524
 
            if (!PyArg_ParseTuple(args, "O&O&:rename", 
2525
 
                convert_to_unicode, &o1,
2526
 
                convert_to_unicode, &o2))
2527
 
                    PyErr_Clear();
2528
 
            else {
2529
 
                    Py_BEGIN_ALLOW_THREADS
2530
 
                    result = MoveFileW(PyUnicode_AsUnicode(o1),
2531
 
                                       PyUnicode_AsUnicode(o2));
2532
 
                    Py_END_ALLOW_THREADS
2533
 
                    Py_DECREF(o1);
2534
 
                    Py_DECREF(o2);
2535
 
                    if (!result)
2536
 
                            return win32_error("rename", NULL);
2537
 
                    Py_INCREF(Py_None);
2538
 
                    return Py_None;
 
2539
            if (!PyArg_ParseTuple(args, "OO:rename", &o1, &o2))
 
2540
                goto error;
 
2541
            if (!convert_to_unicode(&o1))
 
2542
                goto error;
 
2543
            if (!convert_to_unicode(&o2)) {
 
2544
                Py_DECREF(o1);
 
2545
                goto error;
2539
2546
            }
 
2547
            Py_BEGIN_ALLOW_THREADS
 
2548
            result = MoveFileW(PyUnicode_AsUnicode(o1),
 
2549
                               PyUnicode_AsUnicode(o2));
 
2550
            Py_END_ALLOW_THREADS
 
2551
            Py_DECREF(o1);
 
2552
            Py_DECREF(o2);
 
2553
            if (!result)
 
2554
                    return win32_error("rename", NULL);
 
2555
            Py_INCREF(Py_None);
 
2556
            return Py_None;
 
2557
error:
 
2558
            PyErr_Clear();
2540
2559
        }
2541
2560
        if (!PyArg_ParseTuple(args, "ss:rename", &p1, &p2))
2542
2561
                return NULL;
3578
3597
        pid_t pid = fork1();
3579
3598
        if (pid == -1)
3580
3599
                return posix_error();
3581
 
        PyOS_AfterFork();
 
3600
        if (pid == 0)
 
3601
                PyOS_AfterFork();
3582
3602
        return PyInt_FromLong(pid);
3583
3603
}
3584
3604
#endif
6312
6332
static PyObject *
6313
6333
posix_write(PyObject *self, PyObject *args)
6314
6334
{
 
6335
        Py_buffer pbuf;
6315
6336
        int fd;
6316
6337
        Py_ssize_t size;
6317
 
        char *buffer;
6318
6338
 
6319
 
        if (!PyArg_ParseTuple(args, "is#:write", &fd, &buffer, &size))
 
6339
        if (!PyArg_ParseTuple(args, "is*:write", &fd, &pbuf))
6320
6340
                return NULL;
6321
6341
        Py_BEGIN_ALLOW_THREADS
6322
 
        size = write(fd, buffer, (size_t)size);
 
6342
        size = write(fd, pbuf.buf, (size_t)pbuf.len);
6323
6343
        Py_END_ALLOW_THREADS
 
6344
                PyBuffer_Release(&pbuf);
6324
6345
        if (size < 0)
6325
6346
                return posix_error();
6326
6347
        return PyInt_FromSsize_t(size);
8228
8249
        result = PyString_FromStringAndSize(NULL, howMany);
8229
8250
        if (result != NULL) {
8230
8251
                /* Get random data */
 
8252
                memset(PyString_AS_STRING(result), 0, howMany); /* zero seed */
8231
8253
                if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
8232
8254
                                      PyString_AS_STRING(result))) {
8233
8255
                        Py_DECREF(result);