Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(7)

Side by Side Diff: Modules/posixmodule.c

Issue 3055: combined patches from http://bugs.python.org/issue3187 (Closed) SVN Base: http://svn.python.org/view/*checkout*/python/branches/py3k/
Patch Set: One more tweak (fold some long lines) Created 1 year, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 1
2 /* POSIX module implementation */ 2 /* POSIX module implementation */
3 3
4 /* This file is also used for Windows NT/MS-Win and OS/2. In that case the 4 /* This file is also used for Windows NT/MS-Win and OS/2. In that case the
5 module actually calls itself 'nt' or 'os2', not 'posix', and a few 5 module actually calls itself 'nt' or 'os2', not 'posix', and a few
6 functions are either unimplemented or implemented differently. The source 6 functions are either unimplemented or implemented differently. The source
7 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent 7 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
8 of the compiler used. Different compilers define their own feature 8 of the compiler used. Different compilers define their own feature
9 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler 9 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. For OS/2, the compiler
10 independent macro PYOS_OS2 should be defined. On OS/2 the default 10 independent macro PYOS_OS2 should be defined. On OS/2 the default
(...skipping 1950 matching lines...) Expand 10 before | Expand all | Expand 10 after
1961 if (res < 0) 1961 if (res < 0)
1962 return posix_error_with_allocated_filename(path); 1962 return posix_error_with_allocated_filename(path);
1963 PyMem_Free(path); 1963 PyMem_Free(path);
1964 Py_INCREF(Py_None); 1964 Py_INCREF(Py_None);
1965 return Py_None; 1965 return Py_None;
1966 } 1966 }
1967 #endif /* HAVE_LCHOWN */ 1967 #endif /* HAVE_LCHOWN */
1968 1968
1969 1969
1970 #ifdef HAVE_GETCWD 1970 #ifdef HAVE_GETCWD
1971 PyDoc_STRVAR(posix_getcwd__doc__,
1972 "getcwd() -> path\n\n\
1973 Return a string representing the current working directory.");
1974
1975 static PyObject * 1971 static PyObject *
1976 posix_getcwd(PyObject *self, PyObject *noargs) 1972 posix_getcwd(int use_bytes)
1977 {
1978 » int bufsize_incr = 1024;
1979 » int bufsize = 0;
1980 » char *tmpbuf = NULL;
1981 » char *res = NULL;
1982 » PyObject *dynamic_return;
1983
1984 » Py_BEGIN_ALLOW_THREADS
1985 » do {
1986 » » bufsize = bufsize + bufsize_incr;
1987 » » tmpbuf = malloc(bufsize);
1988 » » if (tmpbuf == NULL) {
1989 » » » break;
1990 » » }
1991 #if defined(PYOS_OS2) && defined(PYCC_GCC)
1992 » » res = _getcwd2(tmpbuf, bufsize);
1993 #else
1994 » » res = getcwd(tmpbuf, bufsize);
1995 #endif
1996
1997 » » if (res == NULL) {
1998 » » » free(tmpbuf);
1999 » » }
2000 » } while ((res == NULL) && (errno == ERANGE));
2001 » Py_END_ALLOW_THREADS
2002
2003 » if (res == NULL)
2004 » » return posix_error();
2005
2006 » dynamic_return = PyUnicode_FromString(tmpbuf);
2007 » free(tmpbuf);
2008
2009 » return dynamic_return;
2010 }
2011
2012 PyDoc_STRVAR(posix_getcwdu__doc__,
2013 "getcwdu() -> path\n\n\
2014 Return a unicode string representing the current working directory.");
2015
2016 static PyObject *
2017 posix_getcwdu(PyObject *self, PyObject *noargs)
2018 { 1973 {
2019 char buf[1026]; 1974 char buf[1026];
2020 char *res; 1975 char *res;
2021 1976
2022 #ifdef Py_WIN_WIDE_FILENAMES 1977 #ifdef Py_WIN_WIDE_FILENAMES
2023 » DWORD len; 1978 » if (!use_bytes && unicode_file_names()) {
2024 » if (unicode_file_names()) {
2025 wchar_t wbuf[1026]; 1979 wchar_t wbuf[1026];
2026 wchar_t *wbuf2 = wbuf; 1980 wchar_t *wbuf2 = wbuf;
2027 PyObject *resobj; 1981 PyObject *resobj;
1982 DWORD len;
2028 Py_BEGIN_ALLOW_THREADS 1983 Py_BEGIN_ALLOW_THREADS
2029 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); 1984 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
2030 /* If the buffer is large enough, len does not include the 1985 /* If the buffer is large enough, len does not include the
2031 terminating \0. If the buffer is too small, len includes 1986 terminating \0. If the buffer is too small, len includes
2032 the space needed for the terminator. */ 1987 the space needed for the terminator. */
2033 if (len >= sizeof wbuf/ sizeof wbuf[0]) { 1988 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
2034 wbuf2 = malloc(len * sizeof(wchar_t)); 1989 wbuf2 = malloc(len * sizeof(wchar_t));
2035 if (wbuf2) 1990 if (wbuf2)
2036 len = GetCurrentDirectoryW(len, wbuf2); 1991 len = GetCurrentDirectoryW(len, wbuf2);
2037 } 1992 }
(...skipping 14 matching lines...) Expand all
2052 2007
2053 Py_BEGIN_ALLOW_THREADS 2008 Py_BEGIN_ALLOW_THREADS
2054 #if defined(PYOS_OS2) && defined(PYCC_GCC) 2009 #if defined(PYOS_OS2) && defined(PYCC_GCC)
2055 res = _getcwd2(buf, sizeof buf); 2010 res = _getcwd2(buf, sizeof buf);
2056 #else 2011 #else
2057 res = getcwd(buf, sizeof buf); 2012 res = getcwd(buf, sizeof buf);
2058 #endif 2013 #endif
2059 Py_END_ALLOW_THREADS 2014 Py_END_ALLOW_THREADS
2060 if (res == NULL) 2015 if (res == NULL)
2061 return posix_error(); 2016 return posix_error();
2017 if (use_bytes)
2018 return PyBytes_FromStringAndSize(buf, strlen(buf));
2062 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding," strict"); 2019 return PyUnicode_Decode(buf, strlen(buf), Py_FileSystemDefaultEncoding," strict");
2020 }
2021
2022 PyDoc_STRVAR(posix_getcwd__doc__,
2023 "getcwd() -> path\n\n\
2024 Return a unicode string representing the current working directory.");
2025
2026 static PyObject *
2027 posix_getcwd_unicode(PyObject *self)
2028 {
2029 return posix_getcwd(0);
2030 }
2031
2032 PyDoc_STRVAR(posix_getcwdb__doc__,
2033 "getcwdb() -> path\n\n\
2034 Return a bytes string representing the current working directory.");
2035
2036 static PyObject *
2037 posix_getcwd_bytes(PyObject *self)
2038 {
2039 return posix_getcwd(1);
2063 } 2040 }
2064 #endif 2041 #endif
2065 2042
2066 2043
2067 #ifdef HAVE_LINK 2044 #ifdef HAVE_LINK
2068 PyDoc_STRVAR(posix_link__doc__, 2045 PyDoc_STRVAR(posix_link__doc__,
2069 "link(src, dst)\n\n\ 2046 "link(src, dst)\n\n\
2070 Create a hard link to a file."); 2047 Create a hard link to a file.");
2071 2048
2072 static PyObject * 2049 static PyObject *
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after
2371 PyObject *w; 2348 PyObject *w;
2372 2349
2373 w = PyUnicode_FromEncodedObject(v, 2350 w = PyUnicode_FromEncodedObject(v,
2374 Py_FileSystemDefaultEncoding, 2351 Py_FileSystemDefaultEncoding,
2375 "strict"); 2352 "strict");
2376 if (w != NULL) { 2353 if (w != NULL) {
2377 Py_DECREF(v); 2354 Py_DECREF(v);
2378 v = w; 2355 v = w;
2379 } 2356 }
2380 else { 2357 else {
2381 » » » » /* fall back to the original byte string, as 2358 » » » » /* Ignore undecodable filenames, as discussed
2382 » » » » discussed in patch #683592 */ 2359 » » » » * in issue 3187. To include these,
2360 » » » » * use getcwdb(). */
2383 PyErr_Clear(); 2361 PyErr_Clear();
2362 Py_DECREF(v);
2363 continue;
2384 } 2364 }
2385 } 2365 }
2386 if (PyList_Append(d, v) != 0) { 2366 if (PyList_Append(d, v) != 0) {
2387 Py_DECREF(v); 2367 Py_DECREF(v);
2388 Py_DECREF(d); 2368 Py_DECREF(d);
2389 d = NULL; 2369 d = NULL;
2390 break; 2370 break;
2391 } 2371 }
2392 Py_DECREF(v); 2372 Py_DECREF(v);
2393 } 2373 }
(...skipping 2076 matching lines...) Expand 10 before | Expand all | Expand 10 after
4470 PyObject *w; 4450 PyObject *w;
4471 4451
4472 w = PyUnicode_FromEncodedObject(v, 4452 w = PyUnicode_FromEncodedObject(v,
4473 Py_FileSystemDefaultEncoding, 4453 Py_FileSystemDefaultEncoding,
4474 "strict"); 4454 "strict");
4475 if (w != NULL) { 4455 if (w != NULL) {
4476 Py_DECREF(v); 4456 Py_DECREF(v);
4477 v = w; 4457 v = w;
4478 } 4458 }
4479 else { 4459 else {
4480 » » » /* fall back to the original byte string, as 4460 » » » v = NULL;
4481 » » » discussed in patch #683592 */
4482 » » » PyErr_Clear();
4483 } 4461 }
4484 } 4462 }
4485 return v; 4463 return v;
4486 } 4464 }
4487 #endif /* HAVE_READLINK */ 4465 #endif /* HAVE_READLINK */
4488 4466
4489 4467
4490 #ifdef HAVE_SYMLINK 4468 #ifdef HAVE_SYMLINK
4491 PyDoc_STRVAR(posix_symlink__doc__, 4469 PyDoc_STRVAR(posix_symlink__doc__,
4492 "symlink(src, dst)\n\n\ 4470 "symlink(src, dst)\n\n\
(...skipping 2310 matching lines...) Expand 10 before | Expand all | Expand 10 after
6803 #ifdef HAVE_LCHOWN 6781 #ifdef HAVE_LCHOWN
6804 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__}, 6782 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
6805 #endif /* HAVE_LCHOWN */ 6783 #endif /* HAVE_LCHOWN */
6806 #ifdef HAVE_CHROOT 6784 #ifdef HAVE_CHROOT
6807 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__}, 6785 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
6808 #endif 6786 #endif
6809 #ifdef HAVE_CTERMID 6787 #ifdef HAVE_CTERMID
6810 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__}, 6788 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
6811 #endif 6789 #endif
6812 #ifdef HAVE_GETCWD 6790 #ifdef HAVE_GETCWD
6813 » {"getcwd",» posix_getcwd, METH_NOARGS, posix_getcwd__doc__}, 6791 » {"getcwd",» (PyCFunction)posix_getcwd_unicode,
6814 » {"getcwdu",» posix_getcwdu, METH_NOARGS, posix_getcwdu__doc__}, 6792 » METH_NOARGS, posix_getcwd__doc__},
6793 » {"getcwdb",» (PyCFunction)posix_getcwd_bytes,
6794 » METH_NOARGS, posix_getcwdb__doc__},
6815 #endif 6795 #endif
6816 #ifdef HAVE_LINK 6796 #ifdef HAVE_LINK
6817 {"link", posix_link, METH_VARARGS, posix_link__doc__}, 6797 {"link", posix_link, METH_VARARGS, posix_link__doc__},
6818 #endif /* HAVE_LINK */ 6798 #endif /* HAVE_LINK */
6819 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__}, 6799 {"listdir", posix_listdir, METH_VARARGS, posix_listdir__doc__},
6820 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__}, 6800 {"lstat", posix_lstat, METH_VARARGS, posix_lstat__doc__},
6821 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__}, 6801 {"mkdir", posix_mkdir, METH_VARARGS, posix_mkdir__doc__},
6822 #ifdef HAVE_NICE 6802 #ifdef HAVE_NICE
6823 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__}, 6803 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
6824 #endif /* HAVE_NICE */ 6804 #endif /* HAVE_NICE */
(...skipping 634 matching lines...) Expand 10 before | Expand all | Expand 10 after
7459 7439
7460 7440
7461 #endif /* __APPLE__ */ 7441 #endif /* __APPLE__ */
7462 return m; 7442 return m;
7463 7443
7464 } 7444 }
7465 7445
7466 #ifdef __cplusplus 7446 #ifdef __cplusplus
7467 } 7447 }
7468 #endif 7448 #endif
OLDNEW

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld r483