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

Skip to content

Commit 3b9af38

Browse files
committed
Modified setup to build array-package-specific extensions for those
extensions which are array-aware. Setup builds extensions automatically for either Numeric, numarray, or both, depending on what you have installed. Python proxy modules for the array-aware extensions import the version optimized for numarray or Numeric as determined by numerix. svn path=/trunk/matplotlib/; revision=532
1 parent 6532e5a commit 3b9af38

5 files changed

Lines changed: 126 additions & 52 deletions

File tree

MANIFEST

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,10 @@ license/LICENSE_PAINT
402402
license/LICENSE_PIL
403403
license/LICENSE_TTFQUERY
404404
matplotlib/__init__.py
405+
matplotlib/_image.py
405406
matplotlib/_mathtext_data.py
406407
matplotlib/_matlab_helpers.py
408+
matplotlib/_transforms.py
407409
matplotlib/afm.py
408410
matplotlib/artist.py
409411
matplotlib/axes.py

setup.py

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,11 @@
1010
respectively; set them to 0 if you do not want to build them
1111
"""
1212

13-
# The NUMERIX
14-
#NUMERIX = 'Numeric' # or numarray
15-
NUMERIX = 'numarray' # or numarray
16-
1713
# build the image support module - requires agg and Numeric or
1814
# numarray. You can build the image module with either Numeric or
19-
# numarray. Whichever way you build it, the code will work with both
20-
# Numeric or numarray arrays, but you will get the best performance if
21-
# you build with the type of array you use most
22-
BUILD_IMAGE = 1
15+
# numarray or both. By default, matplotlib will build support for
16+
# whatever array packages you have installed.
17+
BUILD_IMAGE = 1
2318

2419
# Build the antigrain geometry toolkit. Agg makes heavy use of
2520
# templates, so it probably requires a fairly recent compiler to build
@@ -45,13 +40,32 @@
4540
## You shouldn't need to customize below this point
4641

4742

43+
# Figure out which array packages to provide binary support for
44+
# and define the NUMERIX value: Numeric, numarray, or both.
45+
try:
46+
import Numeric
47+
HAVE_NUMERIC=1
48+
except ImportError:
49+
HAVE_NUMERIC=0
50+
try:
51+
import numarray
52+
HAVE_NUMARRAY=1
53+
except ImportError:
54+
HAVE_NUMARRAY=0
55+
56+
NUMERIX=["neither", "Numeric","numarray","both"][HAVE_NUMARRAY*2+HAVE_NUMERIC]
57+
58+
if NUMERIX == "neither":
59+
raise RuntimeError("You must install Numeric, numarray, or both to build matplotlib")
60+
61+
print "Compiling matplotlib for:", NUMERIX
4862

4963
from distutils.core import setup
5064
import sys,os
5165
import glob
5266
from distutils.core import Extension
5367
from setupext import build_agg, build_gtkagg, build_tkagg, \
54-
build_ft2font, build_image, build_windowing
68+
build_ft2font, build_image, build_windowing, build_transforms
5569
import distutils.sysconfig
5670

5771
for line in file('matplotlib/__init__.py').readlines():
@@ -71,27 +85,17 @@
7185

7286
data_files=[('share/matplotlib', data),]
7387

74-
cxx = glob.glob('CXX/*.cxx')
75-
cxx.extend(glob.glob('CXX/*.c'))
76-
77-
modtrans = Extension('matplotlib._transforms',
78-
['src/_transforms.cpp', 'src/mplutils.cpp'] + cxx,
79-
libraries = ['stdc++', 'm'],
80-
include_dirs = ['src', '.'],
81-
)
82-
83-
ext_modules = [modtrans]
84-
88+
ext_modules = []
8589

8690
BUILD_FT2FONT = 1
8791
packages = [
8892
'matplotlib',
8993
'matplotlib/backends',
9094
]
9195

96+
build_transforms(ext_modules, packages, NUMERIX)
9297

9398
if BUILD_GTKAGG:
94-
9599
try: import gtk
96100
except ImportError: print 'GTKAgg requires pygtk'
97101
else:
@@ -105,10 +109,7 @@
105109
BUILD_AGG = 1
106110
build_tkagg(ext_modules, packages)
107111

108-
109-
110112
if BUILD_AGG:
111-
112113
build_agg(ext_modules, packages)
113114

114115
if BUILD_FT2FONT:
@@ -123,10 +124,6 @@
123124
for mod in ext_modules:
124125
if VERBOSE:
125126
mod.extra_compile_args.append('-DVERBOSE')
126-
if NUMERIX.lower().find('numarray')>=0:
127-
mod.extra_compile_args.append('-DNUMARRAY')
128-
129-
130127

131128
setup(name="matplotlib",
132129
version= __version__,

setupext.py

Lines changed: 80 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
'sunos5' : [os.getenv('MPLIB_BASE') or '/usr/local',],
4141
}
4242

43-
import sys, os
43+
import sys, os, stat
4444
from distutils.core import Extension
4545
import glob
4646

@@ -59,6 +59,26 @@
5959
BUILT_TKAGG = False
6060
BUILT_WINDOWING = False
6161

62+
class CleanUpFile:
63+
"""CleanUpFile deletes the specified filename when self is destroyed."""
64+
def __init__(self, name):
65+
self.name = name
66+
def __del__(self):
67+
os.remove(self.name)
68+
69+
def temp_copy(_from, _to):
70+
"""temp_copy copies a named file into a named temporary file.
71+
The temporary will be deleted when the setupext module is destructed.
72+
"""
73+
# Copy the file data from _from to _to
74+
s = open(_from).read()
75+
open(_to,"w+").write(s)
76+
# Suppress object rebuild by preserving time stamps.
77+
stats = os.stat(_from)
78+
os.utime(_to, (stats.st_atime, stats.st_mtime))
79+
# Make an object to eliminate the temporary file at exit time.
80+
globals()["_cleanup_"+_to] = CleanUpFile(_to)
81+
6282
def add_base_flags(module):
6383
incdirs = [os.path.join(p, 'include') for p in basedir[sys.platform]
6484
if os.path.exists(p)]
@@ -343,17 +363,13 @@ def build_tkagg(ext_modules, packages):
343363
# add agg flags before pygtk because agg only supports freetype1
344364
# and pygtk includes freetype2. This is a bit fragile.
345365

346-
347366
add_tk_flags(module) # do this first
348367
add_agg_flags(module)
349-
add_ft2font_flags(module)
350-
351-
368+
add_ft2font_flags(module)
352369
ext_modules.append(module)
353370
BUILT_TKAGG = True
354371

355372

356-
357373
def build_agg(ext_modules, packages):
358374
global BUILT_AGG
359375
if BUILT_AGG: return # only build it if you you haven't already
@@ -377,20 +393,64 @@ def build_agg(ext_modules, packages):
377393
def build_image(ext_modules, packages, numerix):
378394
global BUILT_IMAGE
379395
if BUILT_IMAGE: return # only build it if you you haven't already
380-
381-
deps = ['src/_image.cpp', 'src/mplutils.cpp']
382-
deps.extend(glob.glob('agg2/src/*.cpp'))
383-
deps.extend(glob.glob('CXX/*.cxx'))
384-
deps.extend(glob.glob('CXX/*.c'))
385396

386-
module = Extension(
387-
'matplotlib._image',
388-
deps
389-
,
390-
)
391-
if numerix.lower().find('numarray')>=0:
392-
module.extra_compile_args.append('-DNUMARRAY')
393-
add_agg_flags(module)
394-
ext_modules.append(module)
397+
if numerix in ["numarray","both"]: # Build for numarray
398+
temp_copy('src/_image.cpp', 'src/_na_image.cpp')
399+
deps = ['src/_na_image.cpp', 'src/mplutils.cpp']
400+
deps.extend(glob.glob('agg2/src/*.cpp'))
401+
deps.extend(glob.glob('CXX/*.cxx'))
402+
deps.extend(glob.glob('CXX/*.c'))
403+
module = Extension(
404+
'matplotlib._na_image',
405+
deps
406+
,
407+
)
408+
module.extra_compile_args.append('-DNUMARRAY=1')
409+
add_agg_flags(module)
410+
ext_modules.append(module)
411+
412+
if numerix in ["Numeric","both"]: # Build for Numeric
413+
temp_copy('src/_image.cpp', 'src/_nc_image.cpp')
414+
deps = ['src/_nc_image.cpp', 'src/mplutils.cpp']
415+
deps.extend(glob.glob('agg2/src/*.cpp'))
416+
deps.extend(glob.glob('CXX/*.cxx'))
417+
deps.extend(glob.glob('CXX/*.c'))
418+
module = Extension(
419+
'matplotlib._nc_image',
420+
deps
421+
,
422+
)
423+
module.extra_compile_args.append('-DNUMERIC=1')
424+
add_agg_flags(module)
425+
ext_modules.append(module)
426+
395427
BUILT_IMAGE = True
396428

429+
def build_transforms(ext_modules, packages, numerix):
430+
if numerix in ["numarray","both"]: # Build for numarray
431+
cxx = glob.glob('CXX/*.cxx')
432+
cxx.extend(glob.glob('CXX/*.c'))
433+
temp_copy("src/_transforms.cpp","src/_na_transforms.cpp")
434+
module = Extension('matplotlib._na_transforms',
435+
['src/_na_transforms.cpp',
436+
'src/mplutils.cpp'] + cxx,
437+
libraries = ['stdc++', 'm'],
438+
include_dirs = ['src', '.'],
439+
)
440+
module.extra_compile_args.append("-DNUMARRAY=1")
441+
ext_modules.append(module)
442+
443+
if numerix in ["Numeric","both"]: # Build for Numeric
444+
cxx = glob.glob('CXX/*.cxx')
445+
cxx.extend(glob.glob('CXX/*.c'))
446+
temp_copy("src/_transforms.cpp","src/_nc_transforms.cpp")
447+
module = Extension('matplotlib._nc_transforms',
448+
['src/_nc_transforms.cpp',
449+
'src/mplutils.cpp'] + cxx,
450+
libraries = ['stdc++', 'm'],
451+
include_dirs = ['src', '.'],
452+
)
453+
module.extra_compile_args.append("-DNUMERIC=1")
454+
ext_modules.append(module)
455+
456+

src/_image.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -844,8 +844,14 @@ DL_EXPORT(void)
844844
#else
845845
void
846846
#endif
847-
init_image(void) {
848-
_VERBOSE("init_image");
847+
848+
#if defined(NUMARRAY)
849+
init_na_image(void) {
850+
_VERBOSE("init_na_image");
851+
#else
852+
init_nc_image(void) {
853+
_VERBOSE("init_nc_image");
854+
#endif
849855

850856
static _image_module* _image = new _image_module;
851857

src/_transforms.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,12 +1469,21 @@ SeparableTransformation::init_type()
14691469

14701470
extern "C"
14711471
DL_EXPORT(void)
1472-
init_transforms(void)
1472+
#if defined(NUMARRAY)
1473+
init_na_transforms(void)
1474+
#else
1475+
init_nc_transforms(void)
1476+
#endif
14731477
{
14741478
static _transforms_module* _transforms = new _transforms_module;
14751479

1476-
import_array();
1480+
#if defined(NUMARRAY)
1481+
_VERBOSE("init_na_transforms");
1482+
#else
1483+
_VERBOSE("init_nc_transforms");
1484+
#endif
14771485

1486+
import_array();
14781487

14791488
Py::Dict d = _transforms->moduleDictionary();
14801489
d["LOG10"] = Py::Int((int)Func::LOG10);

0 commit comments

Comments
 (0)