[Python-checkins] r84030 - in python/branches/py3k: Include/Python.h Python/import.c
victor.stinner
python-checkins at python.org
Sat Aug 14 19:06:04 CEST 2010
Author: victor.stinner
Date: Sat Aug 14 19:06:04 2010
New Revision: 84030
Log:
Create _Py_fopen() for PyUnicodeObject path
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.
Modified:
python/branches/py3k/Include/Python.h
python/branches/py3k/Python/import.c
Modified: python/branches/py3k/Include/Python.h
==============================================================================
--- python/branches/py3k/Include/Python.h (original)
+++ python/branches/py3k/Include/Python.h Sat Aug 14 19:06:04 2010
@@ -135,7 +135,8 @@
PyAPI_FUNC(char*) _Py_wchar2char(const wchar_t *text);
PyAPI_FUNC(FILE *) _Py_wfopen(const wchar_t *path, const wchar_t *mode);
-/* _Py_stat lives in import.c */
+/* These functions live in import.c */
+PyAPI_FUNC(FILE*) _Py_fopen(PyObject *unicode, const char *mode);
#ifdef HAVE_STAT
int _Py_stat(PyObject *unicode, struct stat *statbuf);
#endif
Modified: python/branches/py3k/Python/import.c
==============================================================================
--- python/branches/py3k/Python/import.c (original)
+++ python/branches/py3k/Python/import.c Sat Aug 14 19:06:04 2010
@@ -1960,6 +1960,39 @@
#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[MAXPATHLEN+1];
+ wchar_t wmode[10];
+ Py_ssize_t len;
+ int usize;
+
+ len = PyUnicode_AsWideChar((PyUnicodeObject*)unicode, path, MAXPATHLEN);
+ if (len == -1)
+ return NULL;
+ path[len] = L'\0';
+
+ usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode));
+ if (usize == 0)
+ return NULL;
+
+ return _wfopen(path, wmode);
+#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
More information about the Python-checkins
mailing list