forked from matplotlib/matplotlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_compat.h
135 lines (119 loc) · 2.9 KB
/
file_compat.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#ifndef __FILE_COMPAT_H__
#define __FILE_COMPAT_H__
#include "numpy/npy_3kcompat.h"
#if NPY_API_VERSION < 0x4 /* corresponds to Numpy 1.5 */
/*
* PyFile_* compatibility
*/
#if defined(NPY_PY3K)
/*
* Get a FILE* handle to the file represented by the Python object
*/
static NPY_INLINE FILE*
npy_PyFile_Dup(PyObject *file, char *mode)
{
int fd, fd2;
PyObject *ret, *os;
Py_ssize_t pos;
FILE *handle;
/* Flush first to ensure things end up in the file in the correct order */
ret = PyObject_CallMethod(file, "flush", "");
if (ret == NULL) {
return NULL;
}
Py_DECREF(ret);
fd = PyObject_AsFileDescriptor(file);
if (fd == -1) {
return NULL;
}
os = PyImport_ImportModule("os");
if (os == NULL) {
return NULL;
}
ret = PyObject_CallMethod(os, "dup", "i", fd);
Py_DECREF(os);
if (ret == NULL) {
return NULL;
}
fd2 = PyNumber_AsSsize_t(ret, NULL);
Py_DECREF(ret);
#ifdef _WIN32
handle = _fdopen(fd2, mode);
#else
handle = fdopen(fd2, mode);
#endif
if (handle == NULL) {
PyErr_SetString(PyExc_IOError,
"Getting a FILE* from a Python file object failed");
}
ret = PyObject_CallMethod(file, "tell", "");
if (ret == NULL) {
fclose(handle);
return NULL;
}
pos = PyNumber_AsSsize_t(ret, PyExc_OverflowError);
Py_DECREF(ret);
if (PyErr_Occurred()) {
fclose(handle);
return NULL;
}
npy_fseek(handle, pos, SEEK_SET);
return handle;
}
/*
* Close the dup-ed file handle, and seek the Python one to the current position
*/
static NPY_INLINE int
npy_PyFile_DupClose(PyObject *file, FILE* handle)
{
PyObject *ret;
Py_ssize_t position;
position = npy_ftell(handle);
fclose(handle);
ret = PyObject_CallMethod(file, "seek", NPY_SSIZE_T_PYFMT "i", position, 0);
if (ret == NULL) {
return -1;
}
Py_DECREF(ret);
return 0;
}
static NPY_INLINE int
npy_PyFile_Check(PyObject *file)
{
int fd;
fd = PyObject_AsFileDescriptor(file);
if (fd == -1) {
PyErr_Clear();
return 0;
}
return 1;
}
#else
#define npy_PyFile_Dup(file, mode) PyFile_AsFile(file)
#define npy_PyFile_DupClose(file, handle) (0)
#endif
static NPY_INLINE PyObject*
npy_PyFile_OpenFile(PyObject *filename, const char *mode)
{
PyObject *open;
open = PyDict_GetItemString(PyEval_GetBuiltins(), "open");
if (open == NULL) {
return NULL;
}
return PyObject_CallFunction(open, "Os", filename, mode);
}
#endif /* NPY_API_VERSION < 0x4 */
#if NPY_API_VERSION < 0x7 /* corresponds to Numpy 1.7 */
static NPY_INLINE int
npy_PyFile_CloseFile(PyObject *file)
{
PyObject *ret;
ret = PyObject_CallMethod(file, "close", NULL);
if (ret == NULL) {
return -1;
}
Py_DECREF(ret);
return 0;
}
#endif /* NPY_API_VERSION < 0x7 */
#endif /* ifndef __FILE_COMPAT_H__ */