FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
matplotlib/src/file_compat.h at boxplot-stat-transforms · phobson/matplotlib · GitHub
phobson
/
matplotlib
Public
forked from
matplotlib/matplotlib
Notifications
You must be signed in to change notification settings
Fork
1
Star
1
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
matplotlib
/
src
/
file_compat.h
Copy path
More file actions
More file actions
Latest commit
History
History
History
217 lines (190 loc) · 5.18 KB
Breadcrumbs
matplotlib
/
src
/
file_compat.h
Copy path
File metadata and controls
217 lines (190 loc) · 5.18 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#ifndef
__FILE_COMPAT_H__
#define
__FILE_COMPAT_H__
#include
<Python.h>
#include
<stdio.h>
#include
"numpy/npy_common.h"
#include
"numpy/ndarrayobject.h"
#include
"mplutils.h"
#ifdef
__cplusplus
extern
"C"
{
#endif
#if
defined(
_MSC_VER
)
&&
defined(
_WIN64
)
&&
(
_MSC_VER
>
1400
)
#include
<io.h>
#define
mpl_fseek
_fseeki64
#define
mpl_ftell
_ftelli64
#define
mpl_lseek
_lseeki64
#define
mpl_off_t
npy_int64
#if
NPY_SIZEOF_INT
==
8
#define
MPL_OFF_T_PYFMT
"i"
#elif
NPY_SIZEOF_LONG
==
8
#define
MPL_OFF_T_PYFMT
"l"
#elif
NPY_SIZEOF_LONGLONG
==
8
#define
MPL_OFF_T_PYFMT
"L"
#else
#error
Unsupported size for type off_t
#endif
#else
#define
mpl_fseek
fseek
#define
mpl_ftell
ftell
#define
mpl_lseek
lseek
#define
mpl_off_t
off_t
#if
NPY_SIZEOF_INT
==
NPY_SIZEOF_SHORT
#define
MPL_OFF_T_PYFMT
"h"
#elif
NPY_SIZEOF_INT
==
NPY_SIZEOF_INT
#define
MPL_OFF_T_PYFMT
"i"
#elif
NPY_SIZEOF_INT
==
NPY_SIZEOF_LONG
#define
MPL_OFF_T_PYFMT
"l"
#elif
NPY_SIZEOF_INT
==
NPY_SIZEOF_LONGLONG
#define
MPL_OFF_T_PYFMT
"L"
#else
#error
Unsupported size for type off_t
#endif
#endif
/*
* PyFile_* compatibility
*/
#if
PY3K
/*
* Get a FILE* handle to the file represented by the Python object
*/
static
NPY_INLINE
FILE
*
mpl_PyFile_Dup
(
PyObject
*
file
,
char
*
mode
,
mpl_off_t
*
orig_pos
)
{
int
fd
,
fd2
;
PyObject
*
ret
,
*
os
;
mpl_off_t
pos
;
FILE
*
handle
;
/* Flush first to ensure things end up in the file in the correct order */
ret
=
PyObject_CallMethod
(
file
, (
char
*
)
"flush"
, (
char
*
)
""
);
if
(
ret
==
NULL
) {
return
NULL
;
}
Py_DECREF
(
ret
);
fd
=
PyObject_AsFileDescriptor
(
file
);
if
(
fd
==
-1
) {
return
NULL
;
}
/* The handle needs to be dup'd because we have to call fclose
at the end */
os
=
PyImport_ImportModule
(
"os"
);
if
(
os
==
NULL
) {
return
NULL
;
}
ret
=
PyObject_CallMethod
(
os
, (
char
*
)
"dup"
, (
char
*
)
"i"
,
fd
);
Py_DECREF
(
os
);
if
(
ret
==
NULL
) {
return
NULL
;
}
fd2
=
PyNumber_AsSsize_t
(
ret
,
NULL
);
Py_DECREF
(
ret
);
/* Convert to FILE* handle */
#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"
);
}
/* Record the original raw file handle position */
*
orig_pos
=
mpl_ftell
(
handle
);
if
(
*
orig_pos
==
-1
) {
// handle is a stream, so we don't have to worry about this
return
handle
;
}
/* Seek raw handle to the Python-side position */
ret
=
PyObject_CallMethod
(
file
, (
char
*
)
"tell"
, (
char
*
)
""
);
if
(
ret
==
NULL
) {
fclose
(
handle
);
return
NULL
;
}
pos
=
PyNumber_AsSsize_t
(
ret
,
PyExc_OverflowError
);
Py_DECREF
(
ret
);
if
(
PyErr_Occurred
()) {
fclose
(
handle
);
return
NULL
;
}
if
(
mpl_fseek
(
handle
,
pos
,
SEEK_SET
)
==
-1
) {
PyErr_SetString
(
PyExc_IOError
,
"seeking file failed"
);
return
NULL
;
}
return
handle
;
}
/*
* Close the dup-ed file handle, and seek the Python one to the current position
*/
static
NPY_INLINE
int
mpl_PyFile_DupClose
(
PyObject
*
file
,
FILE
*
handle
,
mpl_off_t
orig_pos
)
{
int
fd
;
PyObject
*
ret
;
mpl_off_t
position
;
position
=
mpl_ftell
(
handle
);
/* Close the FILE* handle */
fclose
(
handle
);
/* Restore original file handle position, in order to not confuse
Python-side data structures */
fd
=
PyObject_AsFileDescriptor
(
file
);
if
(
fd
==
-1
) {
return
-1
;
}
if
(
mpl_lseek
(
fd
,
orig_pos
,
SEEK_SET
)
!=
-1
) {
if
(
position
==
-1
) {
PyErr_SetString
(
PyExc_IOError
,
"obtaining file position failed"
);
return
-1
;
}
/* Seek Python-side handle to the FILE* handle position */
ret
=
PyObject_CallMethod
(
file
, (
char
*
)
"seek"
, (
char
*
)(
MPL_OFF_T_PYFMT
"i"
),
position
,
0
);
if
(
ret
==
NULL
) {
return
-1
;
}
Py_DECREF
(
ret
);
}
return
0
;
}
static
NPY_INLINE
int
mpl_PyFile_Check
(
PyObject
*
file
)
{
int
fd
;
fd
=
PyObject_AsFileDescriptor
(
file
);
if
(
fd
==
-1
) {
PyErr_Clear
();
return
0
;
}
return
1
;
}
#else
static
NPY_INLINE
FILE
*
mpl_PyFile_Dup
(
PyObject
*
file
,
const
char
*
mode
,
mpl_off_t
*
orig_pos
)
{
return
PyFile_AsFile
(
file
);
}
static
NPY_INLINE
int
mpl_PyFile_DupClose
(
PyObject
*
file
,
FILE
*
handle
,
mpl_off_t
orig_pos
)
{
// deliberately nothing
return
0
;
}
static
NPY_INLINE
int
mpl_PyFile_Check
(
PyObject
*
file
)
{
return
PyFile_Check
(
file
);
}
#endif
static
NPY_INLINE
PyObject
*
mpl_PyFile_OpenFile
(
PyObject
*
filename
,
const
char
*
mode
)
{
PyObject
*
open
;
open
=
PyDict_GetItemString
(
PyEval_GetBuiltins
(),
"open"
);
if
(
open
==
NULL
) {
return
NULL
;
}
return
PyObject_CallFunction
(
open
, (
char
*
)
"Os"
,
filename
,
mode
);
}
static
NPY_INLINE
int
mpl_PyFile_CloseFile
(
PyObject
*
file
)
{
PyObject
*
ret
;
ret
=
PyObject_CallMethod
(
file
, (
char
*
)
"close"
,
NULL
);
if
(
ret
==
NULL
) {
return
-1
;
}
Py_DECREF
(
ret
);
return
0
;
}
#ifdef
__cplusplus
}
#endif
#endif
/* ifndef __FILE_COMPAT_H__ */
Back
|
FazBrowse Home
|
New Git URL