Skip to content

Commit

Permalink
Update backend_agg to use the Numpy file-handling compatibility funct…
Browse files Browse the repository at this point in the history
…ions.
  • Loading branch information
mdboom committed Jan 11, 2013
1 parent bee6479 commit c3d39b5
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions src/_backend_agg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

#include "numpy/arrayobject.h"
#include "agg_py_transforms.h"
#include "file_compat.h"

#ifndef M_PI
#define M_PI 3.14159265358979323846
Expand Down Expand Up @@ -2028,44 +2029,42 @@ RendererAgg::write_rgba(const Py::Tuple& args)

FILE *fp = NULL;
Py::Object py_fileobj = Py::Object(args[0]);

#if PY3K
int fd = PyObject_AsFileDescriptor(py_fileobj.ptr());
PyErr_Clear();
#endif
PyObject* py_file = NULL;
bool close_file = false;

if (py_fileobj.isString())
{
std::string fileName = Py::String(py_fileobj);
const char *file_name = fileName.c_str();
if ((fp = fopen(file_name, "wb")) == NULL)
throw Py::RuntimeError(
Printf("Could not open file %s", file_name).str());
if (fwrite(pixBuffer, 1, NUMBYTES, fp) != NUMBYTES)
{
fclose(fp);
throw Py::RuntimeError(
Printf("Error writing to file %s", file_name).str());
if ((py_file = npy_PyFile_OpenFile(py_fileobj.ptr(), (char *)"w")) == NULL) {
throw Py::Exception();
}
close_file = true;
}
#if PY3K
else if (fd != -1)
else
{
if (write(fd, pixBuffer, NUMBYTES) != (ssize_t)NUMBYTES)
{
throw Py::RuntimeError("Error writing to file");
}
py_file = py_fileobj.ptr();
}
#else
else if (PyFile_CheckExact(py_fileobj.ptr()))

if ((fp = npy_PyFile_Dup(py_file, (char *)"w")))
{
fp = PyFile_AsFile(py_fileobj.ptr());
if (fwrite(pixBuffer, 1, NUMBYTES, fp) != NUMBYTES)
{
npy_PyFile_DupClose(py_file, fp);

if (close_file) {
npy_PyFile_CloseFile(py_file);
Py_DECREF(py_file);
}

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

npy_PyFile_DupClose(py_file, fp);

if (close_file) {
npy_PyFile_CloseFile(py_file);
Py_DECREF(py_file);
}
}
#endif
else
{
PyObject* write_method = PyObject_GetAttrString(py_fileobj.ptr(),
Expand Down

0 comments on commit c3d39b5

Please sign in to comment.