Create fileutils.c/.h

* _Py_fopen() and _Py_stat() come from Python/import.c
 * (_Py)_wrealpath() comes from Python/sysmodule.c
 * _Py_char2wchar(), _Py_wchar2char() and _Py_wfopen() come from Modules/main.c
 * (_Py)_wstat(), (_Py)_wgetcwd(), _Py_wreadlink() come from Modules/getpath.c
This commit is contained in:
Victor Stinner 2010-10-07 21:45:39 +00:00
parent 7ae7c87b05
commit 4e31443c4d
9 changed files with 832 additions and 392 deletions

View file

@ -1953,73 +1953,8 @@ case_ok(char *buf, Py_ssize_t len, Py_ssize_t namelen, char *name)
#endif
}
/* Call _wfopen() on Windows, or fopen() otherwise. Return the new file
object on success, or NULL if the file cannot be open or (if
PyErr_Occurred()) on unicode error */
FILE*
_Py_fopen(PyObject *unicode, const char *mode)
{
#ifdef MS_WINDOWS
wchar_t *path;
wchar_t wmode[10];
int usize;
FILE *f;
usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode));
if (usize == 0)
return NULL;
path = PyUnicode_AsWideCharString(unicode, NULL);
if (path == NULL)
return NULL;
f = _wfopen(path, wmode);
PyMem_Free(path);
return f;
#else
FILE *f;
PyObject *bytes = PyUnicode_EncodeFSDefault(unicode);
if (bytes == NULL)
return NULL;
f = fopen(PyBytes_AS_STRING(bytes), mode);
Py_DECREF(bytes);
return f;
#endif
}
#ifdef HAVE_STAT
/* Call _wstat() on Windows, or stat() otherwise. Only fill st_mode
attribute on Windows. Return 0 on success, -1 on stat error or (if
PyErr_Occurred()) unicode error. */
int
_Py_stat(PyObject *unicode, struct stat *statbuf)
{
#ifdef MS_WINDOWS
wchar_t *path;
int err;
struct _stat wstatbuf;
path = PyUnicode_AsWideCharString(unicode, NULL);
if (path == NULL)
return -1;
err = _wstat(path, &wstatbuf);
PyMem_Free(path);
if (!err)
statbuf->st_mode = wstatbuf.st_mode;
return err;
#else
int ret;
PyObject *bytes = PyUnicode_EncodeFSDefault(unicode);
if (bytes == NULL)
return -1;
ret = stat(PyBytes_AS_STRING(bytes), statbuf);
Py_DECREF(bytes);
return ret;
#endif
}
/* Helper to look for __init__.py or __init__.py[co] in potential package */
static int
find_init_module(char *buf)