Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1ebe784

Browse files
committed
Merge pull request #2705 from mdboom/fix-numpy-1.9
Build fails on OS X with NumPy 1.9 Conflicts: src/file_compat.h src/ft2font.cpp
1 parent 6bc1de6 commit 1ebe784

File tree

3 files changed

+118
-35
lines changed

3 files changed

+118
-35
lines changed

src/_backend_agg.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,13 +2125,14 @@ RendererAgg::write_rgba(const Py::Tuple& args)
21252125
args.verify_length(1);
21262126

21272127
FILE *fp = NULL;
2128+
mpl_off_t offset;
21282129
Py::Object py_fileobj = Py::Object(args[0]);
21292130
PyObject* py_file = NULL;
21302131
bool close_file = false;
21312132

21322133
if (py_fileobj.isString())
21332134
{
2134-
if ((py_file = npy_PyFile_OpenFile(py_fileobj.ptr(), (char *)"wb")) == NULL) {
2135+
if ((py_file = mpl_PyFile_OpenFile(py_fileobj.ptr(), (char *)"wb")) == NULL) {
21352136
throw Py::Exception();
21362137
}
21372138
}
@@ -2140,28 +2141,28 @@ RendererAgg::write_rgba(const Py::Tuple& args)
21402141
py_file = py_fileobj.ptr();
21412142
}
21422143

2143-
if ((fp = npy_PyFile_Dup(py_file, (char *)"wb")))
2144+
if ((fp = mpl_PyFile_Dup(py_file, (char *)"wb", &offset)))
21442145
{
21452146
if (fwrite(pixBuffer, 1, NUMBYTES, fp) != NUMBYTES)
21462147
{
2147-
if (npy_PyFile_DupClose(py_file, fp)) {
2148+
if (mpl_PyFile_DupClose(py_file, fp, offset)) {
21482149
throw Py::RuntimeError("Error closing dupe file handle");
21492150
}
21502151

21512152
if (close_file) {
2152-
npy_PyFile_CloseFile(py_file);
2153+
mpl_PyFile_CloseFile(py_file);
21532154
Py_DECREF(py_file);
21542155
}
21552156

21562157
throw Py::RuntimeError("Error writing to file");
21572158
}
21582159

2159-
if (npy_PyFile_DupClose(py_file, fp)) {
2160+
if (mpl_PyFile_DupClose(py_file, fp, offset)) {
21602161
throw Py::RuntimeError("Error closing dupe file handle");
21612162
}
21622163

21632164
if (close_file) {
2164-
npy_PyFile_CloseFile(py_file);
2165+
mpl_PyFile_CloseFile(py_file);
21652166
Py_DECREF(py_file);
21662167
}
21672168
}

src/_png.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ Py::Object _png_module::write_png(const Py::Tuple& args)
105105
args.verify_length(4, 5);
106106

107107
FILE *fp = NULL;
108+
mpl_off_t offset;
108109
bool close_file = false;
109110
bool close_dup_file = false;
110111
Py::Object buffer_obj = Py::Object(args[0]);
@@ -134,7 +135,7 @@ Py::Object _png_module::write_png(const Py::Tuple& args)
134135
PyObject* py_file = NULL;
135136
if (py_fileobj.isString())
136137
{
137-
if ((py_file = npy_PyFile_OpenFile(py_fileobj.ptr(), (char *)"wb")) == NULL) {
138+
if ((py_file = mpl_PyFile_OpenFile(py_fileobj.ptr(), (char *)"wb")) == NULL) {
138139
throw Py::Exception();
139140
}
140141
close_file = true;
@@ -144,7 +145,7 @@ Py::Object _png_module::write_png(const Py::Tuple& args)
144145
py_file = py_fileobj.ptr();
145146
}
146147

147-
if ((fp = npy_PyFile_Dup(py_file, (char *)"wb")))
148+
if ((fp = mpl_PyFile_Dup(py_file, (char *)"wb", &offset)))
148149
{
149150
close_dup_file = true;
150151
}
@@ -240,14 +241,14 @@ Py::Object _png_module::write_png(const Py::Tuple& args)
240241

241242
if (close_dup_file)
242243
{
243-
if (npy_PyFile_DupClose(py_file, fp)) {
244+
if (mpl_PyFile_DupClose(py_file, fp, offset)) {
244245
throw Py::RuntimeError("Error closing dupe file handle");
245246
}
246247
}
247248

248249
if (close_file)
249250
{
250-
npy_PyFile_CloseFile(py_file);
251+
mpl_PyFile_CloseFile(py_file);
251252
Py_DECREF(py_file);
252253
}
253254
/* Changed calls to png_destroy_write_struct to follow
@@ -261,14 +262,14 @@ Py::Object _png_module::write_png(const Py::Tuple& args)
261262
delete [] row_pointers;
262263
if (close_dup_file)
263264
{
264-
if (npy_PyFile_DupClose(py_file, fp)) {
265+
if (mpl_PyFile_DupClose(py_file, fp, offset)) {
265266
throw Py::RuntimeError("Error closing dupe file handle");
266267
}
267268
}
268269

269270
if (close_file)
270271
{
271-
npy_PyFile_CloseFile(py_file);
272+
mpl_PyFile_CloseFile(py_file);
272273
Py_DECREF(py_file);
273274
}
274275

@@ -312,21 +313,22 @@ _png_module::_read_png(const Py::Object& py_fileobj, const bool float_result,
312313
{
313314
png_byte header[8]; // 8 is the maximum size that can be checked
314315
FILE* fp = NULL;
316+
mpl_off_t offset;
315317
bool close_file = false;
316318
bool close_dup_file = false;
317319
PyObject *py_file = NULL;
318320

319321
if (py_fileobj.isString())
320322
{
321-
if ((py_file = npy_PyFile_OpenFile(py_fileobj.ptr(), (char *)"rb")) == NULL) {
323+
if ((py_file = mpl_PyFile_OpenFile(py_fileobj.ptr(), (char *)"rb")) == NULL) {
322324
throw Py::Exception();
323325
}
324326
close_file = true;
325327
} else {
326328
py_file = py_fileobj.ptr();
327329
}
328330

329-
if ((fp = npy_PyFile_Dup(py_file, "rb")))
331+
if ((fp = mpl_PyFile_Dup(py_file, "rb", &offset)))
330332
{
331333
close_dup_file = true;
332334
}
@@ -574,14 +576,14 @@ _png_module::_read_png(const Py::Object& py_fileobj, const bool float_result,
574576
#endif
575577
if (close_dup_file)
576578
{
577-
if (npy_PyFile_DupClose(py_file, fp)) {
579+
if (mpl_PyFile_DupClose(py_file, fp, offset)) {
578580
throw Py::RuntimeError("Error closing dupe file handle");
579581
}
580582
}
581583

582584
if (close_file)
583585
{
584-
npy_PyFile_CloseFile(py_file);
586+
mpl_PyFile_CloseFile(py_file);
585587
Py_DECREF(py_file);
586588
}
587589

src/file_compat.h

Lines changed: 99 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,67 @@
11
#ifndef __FILE_COMPAT_H__
22
#define __FILE_COMPAT_H__
33

4-
#include "numpy/npy_3kcompat.h"
4+
#include <Python.h>
5+
#include <stdio.h>
6+
#include "numpy/npy_common.h"
7+
#include "numpy/ndarrayobject.h"
8+
#include "mplutils.h"
9+
10+
#ifdef __cplusplus
11+
extern "C" {
12+
#endif
13+
14+
#if defined(_MSC_VER) && defined(_WIN64) && (_MSC_VER > 1400)
15+
#include <io.h>
16+
#define npy_fseek _fseeki64
17+
#define npy_ftell _ftelli64
18+
#define npy_lseek _lseeki64
19+
#define mpl_off_t npy_int64
20+
21+
#if NPY_SIZEOF_INT == 8
22+
#define MPL_OFF_T_PYFMT "i"
23+
#elif NPY_SIZEOF_LONG == 8
24+
#define MPL_OFF_T_PYFMT "l"
25+
#elif NPY_SIZEOF_LONGLONG == 8
26+
#define MPL_OFF_T_PYFMT "L"
27+
#else
28+
#error Unsupported size for type off_t
29+
#endif
30+
#else
31+
#define npy_fseek fseek
32+
#define npy_ftell ftell
33+
#define npy_lseek lseek
34+
#define mpl_off_t off_t
35+
36+
#if NPY_SIZEOF_INT == NPY_SIZEOF_SHORT
37+
#define MPL_OFF_T_PYFMT "h"
38+
#elif NPY_SIZEOF_INT == NPY_SIZEOF_INT
39+
#define MPL_OFF_T_PYFMT "i"
40+
#elif NPY_SIZEOF_INT == NPY_SIZEOF_LONG
41+
#define MPL_OFF_T_PYFMT "l"
42+
#elif NPY_SIZEOF_INT == NPY_SIZEOF_LONGLONG
43+
#define MPL_OFF_T_PYFMT "L"
44+
#else
45+
#error Unsupported size for type off_t
46+
#endif
47+
#endif
548

6-
#if NPY_API_VERSION < 0x4 /* corresponds to Numpy 1.5 */
749
/*
850
* PyFile_* compatibility
951
*/
10-
#if defined(NPY_PY3K)
52+
#if PY3K
1153

1254
/*
1355
* Get a FILE* handle to the file represented by the Python object
1456
*/
1557
static NPY_INLINE FILE*
16-
npy_PyFile_Dup(PyObject *file, char *mode)
58+
mpl_PyFile_Dup(PyObject *file, char *mode, mpl_off_t *orig_pos)
1759
{
1860
int fd, fd2;
1961
PyObject *ret, *os;
20-
Py_ssize_t pos;
62+
mpl_off_t pos;
2163
FILE *handle;
64+
2265
/* Flush first to ensure things end up in the file in the correct order */
2366
ret = PyObject_CallMethod(file, "flush", "");
2467
if (ret == NULL) {
@@ -29,6 +72,9 @@ npy_PyFile_Dup(PyObject *file, char *mode)
2972
if (fd == -1) {
3073
return NULL;
3174
}
75+
76+
/* The handle needs to be dup'd because we have to call fclose
77+
at the end */
3278
os = PyImport_ImportModule("os");
3379
if (os == NULL) {
3480
return NULL;
@@ -40,6 +86,8 @@ npy_PyFile_Dup(PyObject *file, char *mode)
4086
}
4187
fd2 = PyNumber_AsSsize_t(ret, NULL);
4288
Py_DECREF(ret);
89+
90+
/* Convert to FILE* handle */
4391
#ifdef _WIN32
4492
handle = _fdopen(fd2, mode);
4593
#else
@@ -49,6 +97,15 @@ npy_PyFile_Dup(PyObject *file, char *mode)
4997
PyErr_SetString(PyExc_IOError,
5098
"Getting a FILE* from a Python file object failed");
5199
}
100+
101+
/* Record the original raw file handle position */
102+
*orig_pos = npy_ftell(handle);
103+
if (*orig_pos == -1) {
104+
PyErr_SetString(PyExc_IOError, "obtaining file position failed");
105+
return NULL;
106+
}
107+
108+
/* Seek raw handle to the Python-side position */
52109
ret = PyObject_CallMethod(file, "tell", "");
53110
if (ret == NULL) {
54111
fclose(handle);
@@ -60,22 +117,46 @@ npy_PyFile_Dup(PyObject *file, char *mode)
60117
fclose(handle);
61118
return NULL;
62119
}
63-
npy_fseek(handle, pos, SEEK_SET);
120+
if (npy_fseek(handle, pos, SEEK_SET) == -1) {
121+
PyErr_SetString(PyExc_IOError, "seeking file failed");
122+
return NULL;
123+
}
64124
return handle;
65125
}
66126

67127
/*
68128
* Close the dup-ed file handle, and seek the Python one to the current position
69129
*/
70130
static NPY_INLINE int
71-
npy_PyFile_DupClose(PyObject *file, FILE* handle)
131+
mpl_PyFile_DupClose(PyObject *file, FILE* handle, mpl_off_t orig_pos)
72132
{
133+
int fd;
73134
PyObject *ret;
74-
Py_ssize_t position;
135+
mpl_off_t position;
136+
75137
position = npy_ftell(handle);
138+
139+
/* Close the FILE* handle */
76140
fclose(handle);
77141

78-
ret = PyObject_CallMethod(file, "seek", NPY_SSIZE_T_PYFMT "i", position, 0);
142+
/* Restore original file handle position, in order to not confuse
143+
Python-side data structures */
144+
fd = PyObject_AsFileDescriptor(file);
145+
if (fd == -1) {
146+
return -1;
147+
}
148+
if (npy_lseek(fd, orig_pos, SEEK_SET) == -1) {
149+
PyErr_SetString(PyExc_IOError, "seeking file failed");
150+
return -1;
151+
}
152+
153+
if (position == -1) {
154+
PyErr_SetString(PyExc_IOError, "obtaining file position failed");
155+
return -1;
156+
}
157+
158+
/* Seek Python-side handle to the FILE* handle position */
159+
ret = PyObject_CallMethod(file, "seek", MPL_OFF_T_PYFMT "i", position, 0);
79160
if (ret == NULL) {
80161
return -1;
81162
}
@@ -84,7 +165,7 @@ npy_PyFile_DupClose(PyObject *file, FILE* handle)
84165
}
85166

86167
static NPY_INLINE int
87-
npy_PyFile_Check(PyObject *file)
168+
mpl_PyFile_Check(PyObject *file)
88169
{
89170
int fd;
90171
fd = PyObject_AsFileDescriptor(file);
@@ -97,13 +178,14 @@ npy_PyFile_Check(PyObject *file)
97178

98179
#else
99180

100-
#define npy_PyFile_Dup(file, mode) PyFile_AsFile(file)
101-
#define npy_PyFile_DupClose(file, handle) (0)
181+
#define mpl_PyFile_Dup(file, mode, orig_pos_p) PyFile_AsFile(file)
182+
#define mpl_PyFile_DupClose(file, handle, orig_pos) (0)
183+
#define mpl_PyFile_Check PyFile_Check
102184

103185
#endif
104186

105187
static NPY_INLINE PyObject*
106-
npy_PyFile_OpenFile(PyObject *filename, const char *mode)
188+
mpl_PyFile_OpenFile(PyObject *filename, const char *mode)
107189
{
108190
PyObject *open;
109191
open = PyDict_GetItemString(PyEval_GetBuiltins(), "open");
@@ -113,12 +195,8 @@ npy_PyFile_OpenFile(PyObject *filename, const char *mode)
113195
return PyObject_CallFunction(open, "Os", filename, mode);
114196
}
115197

116-
#endif /* NPY_API_VERSION < 0x4 */
117-
118-
#if NPY_API_VERSION < 0x7 /* corresponds to Numpy 1.7 */
119-
120198
static NPY_INLINE int
121-
npy_PyFile_CloseFile(PyObject *file)
199+
mpl_PyFile_CloseFile(PyObject *file)
122200
{
123201
PyObject *ret;
124202

@@ -130,6 +208,8 @@ npy_PyFile_CloseFile(PyObject *file)
130208
return 0;
131209
}
132210

133-
#endif /* NPY_API_VERSION < 0x7 */
211+
#ifdef __cplusplus
212+
}
213+
#endif
134214

135215
#endif /* ifndef __FILE_COMPAT_H__ */

0 commit comments

Comments
 (0)