Skip to content

Commit 4e31443

Browse files
author
Victor Stinner
committed
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
1 parent 7ae7c87 commit 4e31443

File tree

9 files changed

+832
-392
lines changed

9 files changed

+832
-392
lines changed

Include/Python.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
#include "pystrtod.h"
123123
#include "pystrcmp.h"
124124
#include "dtoa.h"
125+
#include "fileutils.h"
125126

126127
#ifdef __cplusplus
127128
extern "C" {
@@ -130,17 +131,6 @@ extern "C" {
130131
/* _Py_Mangle is defined in compile.c */
131132
PyAPI_FUNC(PyObject*) _Py_Mangle(PyObject *p, PyObject *name);
132133

133-
/* These functions live in main.c */
134-
PyAPI_FUNC(wchar_t *) _Py_char2wchar(char *);
135-
PyAPI_FUNC(char*) _Py_wchar2char(const wchar_t *text);
136-
PyAPI_FUNC(FILE *) _Py_wfopen(const wchar_t *path, const wchar_t *mode);
137-
138-
/* These functions live in import.c */
139-
PyAPI_FUNC(FILE*) _Py_fopen(PyObject *unicode, const char *mode);
140-
#ifdef HAVE_STAT
141-
int _Py_stat(PyObject *unicode, struct stat *statbuf);
142-
#endif
143-
144134
#ifdef __cplusplus
145135
}
146136
#endif

Include/fileutils.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#ifndef Py_FILEUTILS_H
2+
#define Py_FILEUTILS_H
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
PyAPI_FUNC(wchar_t *) _Py_char2wchar(
9+
char *arg);
10+
11+
PyAPI_FUNC(char*) _Py_wchar2char(
12+
const wchar_t *text);
13+
14+
#if defined(MS_WINDOWS) || defined(HAVE_STAT)
15+
PyAPI_FUNC(int) _Py_wstat(
16+
const wchar_t* path,
17+
struct stat *buf);
18+
#endif
19+
20+
#ifdef HAVE_STAT
21+
PyAPI_FUNC(int) _Py_stat(
22+
PyObject *unicode,
23+
struct stat *statbuf);
24+
#endif
25+
26+
PyAPI_FUNC(FILE *) _Py_wfopen(
27+
const wchar_t *path,
28+
const wchar_t *mode);
29+
30+
PyAPI_FUNC(FILE*) _Py_fopen(
31+
PyObject *unicode,
32+
const char *mode);
33+
34+
#ifdef HAVE_READLINK
35+
PyAPI_FUNC(int) _Py_wreadlink(
36+
const wchar_t *path,
37+
wchar_t *buf,
38+
size_t bufsiz);
39+
#endif
40+
41+
#ifdef HAVE_REALPATH
42+
PyAPI_FUNC(wchar_t*) _Py_wrealpath(
43+
const wchar_t *path,
44+
wchar_t *resolved_path);
45+
#endif
46+
47+
PyAPI_FUNC(wchar_t*) _Py_wgetcwd(
48+
wchar_t *buf,
49+
size_t size);
50+
51+
#ifdef __cplusplus
52+
}
53+
#endif
54+
55+
#endif /* !Py_FILEUTILS_H */

Makefile.pre.in

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ PYTHON_OBJS= \
325325
Python/pystrtod.o \
326326
Python/dtoa.o \
327327
Python/formatter_unicode.o \
328+
Python/fileutils.o \
328329
Python/$(DYNLOADFILE) \
329330
$(LIBOBJS) \
330331
$(MACHDEP_OBJS) \
@@ -454,7 +455,7 @@ libpython$(VERSION).so: $(LIBRARY_OBJS)
454455

455456
libpython$(VERSION).dylib: $(LIBRARY_OBJS)
456457
$(CC) -dynamiclib -Wl,-single_module $(PY_LDFLAGS) -undefined dynamic_lookup -Wl,-install_name,$(prefix)/lib/libpython$(VERSION).dylib -Wl,-compatibility_version,$(VERSION) -Wl,-current_version,$(VERSION) -o $@ $(LIBRARY_OBJS) $(SHLIBS) $(LIBC) $(LIBM) $(LDLAST); \
457-
458+
458459

459460
libpython$(VERSION).sl: $(LIBRARY_OBJS)
460461
$(LDSHARED) $(PY_LDFLAGS) -o $@ $(LIBRARY_OBJS) $(MODLIBS) $(SHLIBS) $(LIBC) $(LIBM) $(LDLAST)
@@ -620,7 +621,7 @@ BYTESTR_DEPS = \
620621

621622
Objects/bytesobject.o: $(srcdir)/Objects/bytesobject.c $(BYTESTR_DEPS)
622623

623-
Objects/bytearrayobject.o: $(srcdir)/Objects/bytearrayobject.c $(BYTESTR_DEPS)
624+
Objects/bytearrayobject.o: $(srcdir)/Objects/bytearrayobject.c $(BYTESTR_DEPS)
624625

625626
Objects/unicodeobject.o: $(srcdir)/Objects/unicodeobject.c \
626627
$(BYTESTR_DEPS)
@@ -665,6 +666,7 @@ PYTHON_HEADERS= \
665666
Include/errcode.h \
666667
Include/eval.h \
667668
Include/fileobject.h \
669+
Include/fileutils.h \
668670
Include/floatobject.h \
669671
Include/frameobject.h \
670672
Include/funcobject.h \
@@ -1283,7 +1285,7 @@ Python/thread.o: @THREADHEADERS@
12831285
.PHONY: maninstall libinstall inclinstall libainstall sharedinstall
12841286
.PHONY: frameworkinstall frameworkinstallframework frameworkinstallstructure
12851287
.PHONY: frameworkinstallmaclib frameworkinstallapps frameworkinstallunixtools
1286-
.PHONY: frameworkaltinstallunixtools recheck autoconf clean clobber distclean
1288+
.PHONY: frameworkaltinstallunixtools recheck autoconf clean clobber distclean
12871289
.PHONY: smelly funny patchcheck
12881290
.PHONY: gdbhooks
12891291

Modules/getpath.c

Lines changed: 4 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -136,78 +136,6 @@ static wchar_t progpath[MAXPATHLEN+1];
136136
static wchar_t *module_search_path = NULL;
137137
static wchar_t *lib_python = L"lib/python" VERSION;
138138

139-
/* In principle, this should use HAVE__WSTAT, and _wstat
140-
should be detected by autoconf. However, no current
141-
POSIX system provides that function, so testing for
142-
it is pointless.
143-
Not sure whether the MS_WINDOWS guards are necessary:
144-
perhaps for cygwin/mingw builds?
145-
*/
146-
#ifndef MS_WINDOWS
147-
static int
148-
_wstat(const wchar_t* path, struct stat *buf)
149-
{
150-
int err;
151-
char *fname;
152-
fname = _Py_wchar2char(path);
153-
if (fname == NULL) {
154-
errno = EINVAL;
155-
return -1;
156-
}
157-
err = stat(fname, buf);
158-
PyMem_Free(fname);
159-
return err;
160-
}
161-
#endif
162-
163-
#ifndef MS_WINDOWS
164-
static wchar_t*
165-
_wgetcwd(wchar_t *buf, size_t size)
166-
{
167-
char fname[PATH_MAX];
168-
if (getcwd(fname, PATH_MAX) == NULL)
169-
return NULL;
170-
if (mbstowcs(buf, fname, size) >= size) {
171-
errno = ERANGE;
172-
return NULL;
173-
}
174-
return buf;
175-
}
176-
#endif
177-
178-
#ifdef HAVE_READLINK
179-
int
180-
_Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
181-
{
182-
char *cpath;
183-
char cbuf[PATH_MAX];
184-
int res;
185-
size_t r1;
186-
187-
cpath = _Py_wchar2char(path);
188-
if (cpath == NULL) {
189-
errno = EINVAL;
190-
return -1;
191-
}
192-
res = (int)readlink(cpath, cbuf, PATH_MAX);
193-
PyMem_Free(cpath);
194-
if (res == -1)
195-
return -1;
196-
if (res == PATH_MAX) {
197-
errno = EINVAL;
198-
return -1;
199-
}
200-
cbuf[res] = '\0'; /* buf will be null terminated */
201-
r1 = mbstowcs(buf, cbuf, bufsiz);
202-
if (r1 == -1) {
203-
errno = EINVAL;
204-
return -1;
205-
}
206-
return (int)r1;
207-
208-
}
209-
#endif
210-
211139
static void
212140
reduce(wchar_t *dir)
213141
{
@@ -217,12 +145,11 @@ reduce(wchar_t *dir)
217145
dir[i] = '\0';
218146
}
219147

220-
221148
static int
222149
isfile(wchar_t *filename) /* Is file, not directory */
223150
{
224151
struct stat buf;
225-
if (_wstat(filename, &buf) != 0)
152+
if (_Py_wstat(filename, &buf) != 0)
226153
return 0;
227154
if (!S_ISREG(buf.st_mode))
228155
return 0;
@@ -250,7 +177,7 @@ static int
250177
isxfile(wchar_t *filename) /* Is executable file */
251178
{
252179
struct stat buf;
253-
if (_wstat(filename, &buf) != 0)
180+
if (_Py_wstat(filename, &buf) != 0)
254181
return 0;
255182
if (!S_ISREG(buf.st_mode))
256183
return 0;
@@ -264,7 +191,7 @@ static int
264191
isdir(wchar_t *filename) /* Is directory */
265192
{
266193
struct stat buf;
267-
if (_wstat(filename, &buf) != 0)
194+
if (_Py_wstat(filename, &buf) != 0)
268195
return 0;
269196
if (!S_ISDIR(buf.st_mode))
270197
return 0;
@@ -309,7 +236,7 @@ copy_absolute(wchar_t *path, wchar_t *p)
309236
if (p[0] == SEP)
310237
wcscpy(path, p);
311238
else {
312-
_wgetcwd(path, MAXPATHLEN);
239+
_Py_wgetcwd(path, MAXPATHLEN);
313240
if (p[0] == '.' && p[1] == SEP)
314241
p += 2;
315242
joinpath(path, p);

0 commit comments

Comments
 (0)