@@ -102,7 +102,11 @@ Py::Object
102102BufferRegion::to_string (const Py::Tuple &args)
103103{
104104 // owned=true to prevent memory leak
105+ #if PY_MAJOR_VERSION >= 3
106+ return Py::Bytes (PyBytes_FromStringAndSize ((const char *)data, height*stride), true );
107+ #else
105108 return Py::String (PyString_FromStringAndSize ((const char *)data, height*stride), true );
109+ #endif
106110}
107111
108112
@@ -152,12 +156,20 @@ BufferRegion::to_string_argb(const Py::Tuple &args)
152156 unsigned char tmp;
153157 size_t i, j;
154158
155- PyObject* str = PyString_FromStringAndSize (
156- (const char *)data, height * stride);
159+ #if PY_MAJOR_VERSION >= 3
160+ PyObject* str = PyBytes_FromStringAndSize ((const char *)data, height * stride);
161+ if (PyBytes_AsStringAndSize (str, (char **)&begin, &length))
162+ {
163+ throw Py::TypeError (" Could not create memory for blit" );
164+ }
165+ #else
166+ PyObject* str = PyString_FromStringAndSize ((const char *)data, height * stride);
157167 if (PyString_AsStringAndSize (str, (char **)&begin, &length))
158168 {
159169 throw Py::TypeError (" Could not create memory for blit" );
160170 }
171+ #endif
172+
161173
162174 pix = begin;
163175 end = begin + (height * stride);
200212GCAgg::_set_antialiased (const Py::Object& gc)
201213{
202214 _VERBOSE (" GCAgg::antialiased" );
203- isaa = Py::Int ( gc.getAttr (" _antialiased" ));
215+ isaa = gc.getAttr (" _antialiased" ). as_bool ( );
204216}
205217
206218
@@ -1509,7 +1521,7 @@ RendererAgg::_draw_path_collection_generic
15091521
15101522 if (check_snap)
15111523 {
1512- gc.isaa = bool ( Py::Int ( antialiaseds[i % Naa]) );
1524+ gc.isaa = antialiaseds[i % Naa]. as_bool ( );
15131525
15141526 transformed_path_t tpath (path, trans);
15151527 nan_removed_t nan_removed (tpath, true , has_curves);
@@ -1528,7 +1540,7 @@ RendererAgg::_draw_path_collection_generic
15281540 }
15291541 else
15301542 {
1531- gc.isaa = bool ( Py::Int ( antialiaseds[i % Naa]) );
1543+ gc.isaa = antialiaseds[i % Naa]. as_bool ( );
15321544
15331545 transformed_path_t tpath (path, trans);
15341546 nan_removed_t nan_removed (tpath, true , has_curves);
@@ -1741,8 +1753,8 @@ RendererAgg::draw_quad_mesh(const Py::Tuple& args)
17411753 Py::Object offsets_obj = args[5 ];
17421754 agg::trans_affine offset_trans = py_to_agg_transformation_matrix (args[6 ].ptr ());
17431755 Py::Object facecolors_obj = args[7 ];
1744- bool antialiased = ( bool ) Py::Int ( args[8 ]);
1745- bool showedges = ( bool ) Py::Int ( args[9 ]);
1756+ bool antialiased = args[8 ]. as_bool ( );
1757+ bool showedges = args[9 ]. as_bool ( );
17461758 bool free_edgecolors = false ;
17471759
17481760 QuadMeshGenerator path_generator (mesh_width, mesh_height, coordinates.ptr ());
@@ -1981,6 +1993,12 @@ RendererAgg::write_rgba(const Py::Tuple& args)
19811993 FILE *fp = NULL ;
19821994 bool close_file = false ;
19831995 Py::Object py_fileobj = Py::Object (args[0 ]);
1996+
1997+ #if PY_MAJOR_VERSION >= 3
1998+ int fd = PyObject_AsFileDescriptor (py_fileobj.ptr ());
1999+ PyErr_Clear ();
2000+ #endif
2001+
19842002 if (py_fileobj.isString ())
19852003 {
19862004 std::string fileName = Py::String (py_fileobj);
@@ -1996,6 +2014,15 @@ RendererAgg::write_rgba(const Py::Tuple& args)
19962014 }
19972015 close_file = true ;
19982016 }
2017+ #if PY_MAJOR_VERSION >= 3
2018+ else if (fd != -1 )
2019+ {
2020+ if (write (fd, pixBuffer, NUMBYTES) != (ssize_t )NUMBYTES)
2021+ {
2022+ throw Py::RuntimeError (" Error writing to file" );
2023+ }
2024+ }
2025+ #else
19992026 else if (PyFile_CheckExact (py_fileobj.ptr ()))
20002027 {
20012028 fp = PyFile_AsFile (py_fileobj.ptr ());
@@ -2004,6 +2031,7 @@ RendererAgg::write_rgba(const Py::Tuple& args)
20042031 throw Py::RuntimeError (" Error writing to file" );
20052032 }
20062033 }
2034+ #endif
20072035 else
20082036 {
20092037 PyObject* write_method = PyObject_GetAttrString (py_fileobj.ptr (),
@@ -2154,7 +2182,14 @@ RendererAgg::buffer_rgba(const Py::Tuple& args)
21542182 int starth = Py::Int (args[1 ]);
21552183 int row_len = width * 4 ;
21562184 int start = row_len * starth + startw * 4 ;
2185+ /* PY3KTODO: Buffers are different */
2186+ #if PY_MAJOR_VERSION >= 3
2187+ return Py::asObject (PyByteArray_FromStringAndSize (
2188+ (const char *)pixBuffer + start,
2189+ row_len*height - start));
2190+ #else
21572191 return Py::asObject (PyBuffer_FromMemory (pixBuffer + start, row_len*height - start));
2192+ #endif
21582193}
21592194
21602195
@@ -2298,8 +2333,8 @@ Py::Object _backend_agg_module::new_renderer(const Py::Tuple &args,
22982333 debug = 0 ;
22992334 }
23002335
2301- unsigned int width = (unsigned int )Py::Int (args[0 ]);
2302- unsigned int height = (unsigned int )Py::Int (args[1 ]);
2336+ unsigned int width = (int )Py::Int (args[0 ]);
2337+ unsigned int height = (int )Py::Int (args[1 ]);
23032338 double dpi = Py::Float (args[2 ]);
23042339
23052340 if (width > 1 << 15 || height > 1 << 15 )
@@ -2392,8 +2427,13 @@ void RendererAgg::init_type()
23922427}
23932428
23942429extern " C"
2395- DL_EXPORT (void )
2396- init_backend_agg(void )
2430+ #if PY_MAJOR_VERSION >= 3
2431+ PyMODINIT_FUNC
2432+ PyInit__backend_agg (void )
2433+ #else
2434+ PyMODINIT_FUNC
2435+ init_backend_agg (void )
2436+ #endif
23972437{
23982438 // static _backend_agg_module* _backend_agg = new _backend_agg_module;
23992439
@@ -2403,4 +2443,8 @@ extern "C"
24032443
24042444 static _backend_agg_module* _backend_agg = NULL ;
24052445 _backend_agg = new _backend_agg_module;
2446+
2447+ #if PY_MAJOR_VERSION >= 3
2448+ return _backend_agg->module ().ptr();
2449+ #endif
24062450}
0 commit comments