Skip to content

Commit cec3212

Browse files
mdboomddale
authored andcommitted
C/C++ compiling (not necessarily working) with Py3K
svn path=/branches/py3k/; revision=8551
1 parent 97a56f4 commit cec3212

File tree

16 files changed

+299
-71
lines changed

16 files changed

+299
-71
lines changed

lib/matplotlib/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,18 @@
130130

131131
import sys, os, tempfile
132132

133+
if sys.hexversion >= 0x03000000:
134+
def ascii(s): return bytes(s, 'ascii')
135+
else:
136+
ascii = str
137+
133138
from matplotlib.rcsetup import (defaultParams,
134139
validate_backend,
135140
validate_toolbar,
136141
validate_cairo_format)
137142

138143
major, minor1, minor2, s, tmp = sys.version_info
139-
_python24 = major>=2 and minor1>=4
144+
_python24 = (major == 2 and minor1 >= 4) or major >= 3
140145

141146
# the havedate check was a legacy from old matplotlib which preceeded
142147
# datetime support
@@ -172,7 +177,7 @@ def _is_writable_dir(p):
172177
except TypeError: return False
173178
try:
174179
t = tempfile.TemporaryFile(dir=p)
175-
t.write('1')
180+
t.write(ascii('1'))
176181
t.close()
177182
except OSError: return False
178183
else: return True

lib/matplotlib/backends/backend_ps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1269,7 +1269,7 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
12691269
else: angle = 0
12701270

12711271
if rcParams['text.latex.unicode']:
1272-
unicode_preamble = """\usepackage{ucs}
1272+
unicode_preamble = r"""\usepackage{ucs}
12731273
\usepackage[utf8x]{inputenc}"""
12741274
else:
12751275
unicode_preamble = ''

lib/matplotlib/cbook.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
import os.path
1414
import random
1515
import urllib2
16-
import new
16+
if sys.hexversion > 0x03000000:
17+
import types
18+
else:
19+
import new
1720

1821
import matplotlib
1922

@@ -183,7 +186,10 @@ def __call__(self, *args, **kwargs):
183186
raise ReferenceError
184187
elif self.inst is not None:
185188
# build a new instance method with a strong reference to the instance
186-
mtd = new.instancemethod(self.func, self.inst(), self.klass)
189+
if sys.hexversion >= 0x03000000:
190+
mtd = types.MethodType(self.func, self.inst(), self.klass)
191+
else:
192+
mtd = new.instancemethod(self.func, self.inst(), self.klass)
187193
else:
188194
# not a bound method, just return the func
189195
mtd = self.func

lib/matplotlib/delaunay/_delaunay.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,16 +726,40 @@ static PyMethodDef delaunay_methods[] = {
726726
{NULL, NULL, 0, NULL}
727727
};
728728

729+
#if PY_MAJOR_VERSION >= 3
730+
static PyModuleDef delaunay_module = {
731+
PyModuleDef_HEAD_INIT,
732+
"_delaunay",
733+
"Tools for computing the Delaunay triangulation and some operations on it.\n",
734+
-1,
735+
delaunay_methods,
736+
NULL, NULL, NULL, NULL
737+
};
738+
739+
PyMODINIT_FUNC
740+
PyInit__delaunay(void)
741+
{
742+
PyObject* m;
743+
// import_array():
729744

745+
m = PyModule_Create(&delaunay_module);
746+
if (m == NULL)
747+
return NULL;
748+
749+
return m;
750+
}
751+
#else
730752
PyMODINIT_FUNC init_delaunay(void)
731753
{
732754
PyObject* m;
755+
import_array();
756+
733757
m = Py_InitModule3("_delaunay", delaunay_methods,
734758
"Tools for computing the Delaunay triangulation and some operations on it.\n"
735759
);
736760
if (m == NULL)
737761
return;
738-
import_array();
739762
}
763+
#endif
740764

741765
} // extern "C"

lib/matplotlib/texmanager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def make_tex(self, tex, fontsize):
236236
tex = fontcmd % tex
237237

238238
if rcParams['text.latex.unicode']:
239-
unicode_preamble = """\usepackage{ucs}
239+
unicode_preamble = r"""\usepackage{ucs}
240240
\usepackage[utf8x]{inputenc}"""
241241
else:
242242
unicode_preamble = ''
@@ -288,7 +288,7 @@ def make_tex_preview(self, tex, fontsize):
288288
tex = fontcmd % tex
289289

290290
if rcParams['text.latex.unicode']:
291-
unicode_preamble = """\usepackage{ucs}
291+
unicode_preamble = r"""\usepackage{ucs}
292292
\usepackage[utf8x]{inputenc}"""
293293
else:
294294
unicode_preamble = ''

lib/matplotlib/tri/_tri.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -980,19 +980,21 @@ XY TriContourGenerator::interp(int point1,
980980

981981

982982

983-
984-
#if defined(_MSC_VER)
985-
DL_EXPORT(void)
986-
#elif defined(__cplusplus)
987-
extern "C" void
983+
#if PY_MAJOR_VERSION >= 3
984+
PyMODINIT_FUNC
985+
PyInit__tri(void)
988986
#else
989-
void
987+
PyMODINIT_FUNC
988+
init_tri(void)
990989
#endif
991-
init_tri()
992990
{
993991
static TriModule* triModule = NULL;
994992
triModule = new TriModule();
995993
import_array();
994+
995+
#if PY_MAJOR_VERSION >= 3
996+
return triModule->module().ptr();
997+
#endif
996998
}
997999

9981000
TriModule::TriModule()

setup.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333

3434
import glob
3535
from distutils.core import setup
36+
try:
37+
from distutils.command.build_py import build_py_2to3 as build_py
38+
except ImportError:
39+
from distutils.command.build_py import build_py
3640
from setupext import build_agg, build_gtkagg, build_tkagg,\
3741
build_macosx, build_ft2font, build_image, build_windowing, build_path, \
3842
build_contour, build_delaunay, build_nxutils, build_gdk, \
@@ -270,5 +274,6 @@ def add_dateutil():
270274
ext_modules = ext_modules,
271275
package_dir = {'': 'lib'},
272276
package_data = package_data,
277+
cmdclass = {'build_py': build_py},
273278
**additional_params
274279
)

setupext.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@
134134

135135
defines = [
136136
('PY_ARRAY_UNIQUE_SYMBOL', 'MPL_ARRAY_API'),
137-
('PYCXX_ISO_CPP_LIB', '1')]
137+
('PYCXX_ISO_CPP_LIB', '1'),
138+
('PYCXX_PYTHON_2TO3', '1')]
138139

139140
setup_cfg = os.environ.get('MPLSETUPCFG', 'setup.cfg')
140141
# Based on the contents of setup.cfg, determine the build options

src/_backend_agg.cpp

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ Py::Object
102102
BufferRegion::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);
@@ -200,7 +212,7 @@ void
200212
GCAgg::_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

23942429
extern "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

Comments
 (0)