Skip to content

Commit b166ccb

Browse files
mdboomddale
authored andcommitted
Miscellaneous fixes submitted by Christoph Gohlke in the mailing list thread "Patches for CTPUG / matplotlib-py3"
1 parent a24cae5 commit b166ccb

File tree

10 files changed

+75
-41
lines changed

10 files changed

+75
-41
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,8 +1176,7 @@ def __init__(self, name, canvas, x, y,guiEvent=None):
11761176
self._update_enter_leave()
11771177
return
11781178
elif (len(axes_list) > 1): # Overlap, get the highest zorder
1179-
axCmp = lambda _x,_y: cmp(_x.zorder, _y.zorder)
1180-
axes_list.sort(axCmp)
1179+
axes_list.sort(key=lambda x: x.zorder)
11811180
self.inaxes = axes_list[-1] # Use the highest zorder
11821181
else: # Just found one hit
11831182
self.inaxes = axes_list[0]

lib/matplotlib/cbook.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from the Python Cookbook -- hence the name cbook
44
"""
55
from __future__ import print_function
6+
67
import re, os, errno, sys, io, traceback, locale, threading, types
78
import time, datetime
89
import warnings
@@ -14,15 +15,24 @@
1415
import random
1516
import urllib2
1617
from functools import reduce
17-
if sys.version_info[0] >= 3:
18+
19+
major, minor1, minor2, s, tmp = sys.version_info
20+
21+
if major >= 3:
1822
import types
23+
import urllib.request
24+
def addinfourl(data, headers, url, code=None):
25+
return urllib.request.addinfourl(io.BytesIO(data),
26+
headers, url, code)
1927
else:
2028
import new
29+
import urllib2
30+
def addinfourl(data, headers, url, code=None):
31+
return urllib2.addinfourl(io.StringIO(data),
32+
headers, url, code)
2133

2234
import matplotlib
2335

24-
major, minor1, minor2, s, tmp = sys.version_info
25-
2636

2737
# On some systems, locale.getpreferredencoding returns None,
2838
# which can break unicode; and the sage project reports that
@@ -275,7 +285,7 @@ def process(self, s, *args, **kwargs):
275285
callbacks on *s* will be called with *\*args* and *\*\*kwargs*
276286
"""
277287
self._check_signal(s)
278-
for cid, proxy in self.callbacks[s].iteritems():
288+
for cid, proxy in self.callbacks[s].items():
279289
# Clean out dead references
280290
if proxy.inst is not None and proxy.inst() is None:
281291
del self.callbacks[s][cid]
@@ -571,7 +581,8 @@ def https_request(self, req):
571581
if url in self.cache:
572582
_, etag, lastmod = self.cache[url]
573583
req.add_header("If-None-Match", etag)
574-
req.add_header("If-Modified-Since", lastmod)
584+
if lastmod:
585+
req.add_header("If-Modified-Since", lastmod)
575586
return req
576587

577588
def https_error_304(self, req, fp, code, msg, hdrs):
@@ -584,7 +595,7 @@ def https_error_304(self, req, fp, code, msg, hdrs):
584595
'ViewVCCachedServer: reading data file from cache file "%s"' %fn,
585596
'debug')
586597
file = open(fn, 'rb')
587-
handle = urllib2.addinfourl(file, hdrs, url)
598+
handle = addinfourl(file, hdrs, url)
588599
handle.code = 304
589600
return handle
590601

@@ -599,9 +610,7 @@ def https_response(self, req, response):
599610
else:
600611
data = response.read()
601612
self.cache_file(req.get_full_url(), data, response.headers)
602-
result = urllib2.addinfourl(io.StringIO(data),
603-
response.headers,
604-
req.get_full_url())
613+
result = addinfourl(data, response.headers, req.get_full_url())
605614
result.code = response.code
606615
result.msg = response.msg
607616
return result
@@ -620,10 +629,7 @@ def get_sample_data(self, fname, asfileobj=True):
620629

621630
# quote is not in python2.4, so check for it and get it from
622631
# urllib if it is not available
623-
quote = getattr(urllib2, 'quote', None)
624-
if quote is None:
625-
import urllib
626-
quote = urllib.quote
632+
quote = urllib2.quote
627633

628634
# retrieve the URL for the side effect of refreshing the cache
629635
url = self.baseurl + quote(fname)

lib/matplotlib/contour.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import matplotlib.ticker as ticker
1313
import matplotlib.cm as cm
1414
import matplotlib.colors as colors
15-
import matplotlib.collections as collections
15+
import matplotlib.collections as mcoll
1616
import matplotlib.font_manager as font_manager
1717
import matplotlib.text as text
1818
import matplotlib.cbook as cbook
@@ -703,9 +703,9 @@ def __init__(self, ax, *args, **kwargs):
703703
ncolors -= 1
704704
cmap = colors.ListedColormap(self.colors, N=ncolors)
705705
if self.filled:
706-
self.collections = cbook.silent_list('collections.PathCollection')
706+
self.collections = cbook.silent_list('mcoll.PathCollection')
707707
else:
708-
self.collections = cbook.silent_list('collections.LineCollection')
708+
self.collections = cbook.silent_list('mcoll.LineCollection')
709709
# label lists must be initialized here
710710
self.labelTexts = []
711711
self.labelCValues = []
@@ -734,7 +734,7 @@ def __init__(self, ax, *args, **kwargs):
734734
paths = self._make_paths(segs, kinds)
735735
# Default zorder taken from Collection
736736
zorder = kwargs.get('zorder', 1)
737-
col = collections.PathCollection(paths,
737+
col = mcoll.PathCollection(paths,
738738
antialiaseds = (self.antialiased,),
739739
edgecolors= 'none',
740740
alpha=self.alpha,
@@ -752,7 +752,7 @@ def __init__(self, ax, *args, **kwargs):
752752
zip(self.levels, tlinewidths, tlinestyles, self.allsegs):
753753
# Default zorder taken from LineCollection
754754
zorder = kwargs.get('zorder', 2)
755-
col = collections.LineCollection(segs,
755+
col = mcoll.LineCollection(segs,
756756
antialiaseds = aa,
757757
linewidths = width,
758758
linestyle = lstyle,

lib/matplotlib/delaunay/_delaunay.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ static PyMethodDef delaunay_methods[] = {
727727
};
728728

729729
#if PY_MAJOR_VERSION >= 3
730-
static PyModuleDef delaunay_module = {
730+
static struct PyModuleDef delaunay_module = {
731731
PyModuleDef_HEAD_INIT,
732732
"_delaunay",
733733
"Tools for computing the Delaunay triangulation and some operations on it.\n",
@@ -740,7 +740,7 @@ PyMODINIT_FUNC
740740
PyInit__delaunay(void)
741741
{
742742
PyObject* m;
743-
// import_array():
743+
import_array();
744744

745745
m = PyModule_Create(&delaunay_module);
746746
if (m == NULL)

lib/matplotlib/finance.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
55
"""
66
#from __future__ import division
7-
import os, warnings
7+
import sys, os, warnings
88
from urllib2 import urlopen
99

10-
try:
10+
if sys.version_info[0] < 3:
1111
from hashlib import md5
12-
except ImportError:
13-
from md5 import md5 #Deprecated in 2.5
12+
else:
13+
import hashlib
14+
md5 = lambda x: hashlib.md5(x.encode())
15+
1416
import datetime
1517

1618
import numpy as np
@@ -177,7 +179,7 @@ def fetch_historical_yahoo(ticker, date1, date2, cachename=None):
177179
os.mkdir(cachedir)
178180
urlfh = urlopen(url)
179181

180-
fh = open(cachename, 'w')
182+
fh = open(cachename, 'wb')
181183
fh.write(urlfh.read())
182184
fh.close()
183185
verbose.report('Saved %s data to cache file %s'%(ticker, cachename))

lib/matplotlib/tri/_tri.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,9 +988,10 @@ PyMODINIT_FUNC
988988
init_tri(void)
989989
#endif
990990
{
991+
import_array();
992+
991993
static TriModule* triModule = NULL;
992994
triModule = new TriModule();
993-
import_array();
994995

995996
#if PY_MAJOR_VERSION >= 3
996997
return triModule->module().ptr();

lib/mpl_toolkits/mplot3d/art3d.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ def do_3d_projection(self, renderer):
439439
if self._zsort:
440440
z_segments_2d = [(self._zsortfunc(zs), zip(xs, ys), fc, ec) for
441441
(xs, ys, zs), fc, ec in zip(xyzlist, cface, cedge)]
442-
z_segments_2d.sort(cmp=lambda x, y: cmp(y[0], x[0]))
442+
z_segments_2d.sort(key=lambda x: x[0], reverse=True)
443443
else:
444444
raise ValueError, "whoops"
445445

setupext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ def add_tk_flags(module):
928928
message = None
929929
if sys.platform == 'win32':
930930
major, minor1, minor2, s, tmp = sys.version_info
931-
if major == 2 and minor1 in [6, 7]:
931+
if (2, 6) <= (major, minor1) <= (3, 2):
932932
module.include_dirs.extend(['win32_static/include/tcl85'])
933933
module.libraries.extend(['tk85', 'tcl85'])
934934
elif major == 2 and minor1 in [3, 4, 5]:

src/_tkagg.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ extern "C"
3434
#endif
3535
}
3636

37-
37+
#if defined(_MSC_VER)
38+
# define SIZE_T_FORMAT "%Iu"
39+
#else
40+
# define SIZE_T_FORMAT "%zu"
41+
#endif
3842

3943
typedef struct
4044
{
@@ -53,7 +57,7 @@ PyAggImagePhoto(ClientData clientdata, Tcl_Interp* interp,
5357
// vars for blitting
5458
PyObject* bboxo;
5559

56-
unsigned long aggl, bboxl;
60+
size_t aggl, bboxl;
5761
bool has_bbox;
5862
agg::int8u *destbuffer;
5963
double l, b, r, t;
@@ -83,7 +87,7 @@ PyAggImagePhoto(ClientData clientdata, Tcl_Interp* interp,
8387
return TCL_ERROR;
8488
}
8589
/* get array (or object that can be converted to array) pointer */
86-
if (sscanf(argv[2], "%lu", &aggl) != 1)
90+
if (sscanf(argv[2], SIZE_T_FORMAT, &aggl) != 1)
8791
{
8892
Tcl_AppendResult(interp, "error casting pointer", (char *) NULL);
8993
return TCL_ERROR;
@@ -109,7 +113,7 @@ PyAggImagePhoto(ClientData clientdata, Tcl_Interp* interp,
109113
}
110114

111115
/* check for bbox/blitting */
112-
if (sscanf(argv[4], "%lu", &bboxl) != 1)
116+
if (sscanf(argv[4], SIZE_T_FORMAT, &bboxl) != 1)
113117
{
114118
Tcl_AppendResult(interp, "error casting pointer", (char *) NULL);
115119
return TCL_ERROR;
@@ -261,6 +265,7 @@ static PyMethodDef functions[] =
261265
{NULL, NULL} /* sentinel */
262266
};
263267

268+
extern "C"
264269
#if PY3K
265270
static PyModuleDef _tkagg_module = {
266271
PyModuleDef_HEAD_INIT,
@@ -278,11 +283,12 @@ PyInit__tkagg(void)
278283

279284
m = PyModule_Create(&_tkagg_module);
280285

286+
import_array();
287+
281288
return m;
282289
}
283290
#else
284-
extern "C"
285-
DL_EXPORT(void) init_tkagg(void)
291+
PyMODINIT_FUNC init_tkagg(void)
286292
{
287293
import_array();
288294

src/_windowing.cpp

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* -*- mode: c++; c-basic-offset: 4 -*- */
2-
31
#include "Python.h"
42
#include <windows.h>
53

@@ -11,14 +9,14 @@ _GetForegroundWindow(PyObject *module, PyObject *args)
119
{
1210
return NULL;
1311
}
14-
return PyInt_FromLong((long) handle);
12+
return PyLong_FromSize_t((size_t)handle);
1513
}
1614

1715
static PyObject *
1816
_SetForegroundWindow(PyObject *module, PyObject *args)
1917
{
2018
HWND handle;
21-
if (!PyArg_ParseTuple(args, "l:SetForegroundWindow", &handle))
19+
if (!PyArg_ParseTuple(args, "n:SetForegroundWindow", &handle))
2220
{
2321
return NULL;
2422
}
@@ -38,7 +36,29 @@ static PyMethodDef _windowing_methods[] =
3836
{NULL, NULL}
3937
};
4038

41-
extern "C" DL_EXPORT(void) init_windowing()
39+
#if PY_MAJOR_VERSION >= 3
40+
41+
static struct PyModuleDef moduledef = {
42+
PyModuleDef_HEAD_INIT,
43+
"_windowing",
44+
"",
45+
-1,
46+
_windowing_methods,
47+
NULL,
48+
NULL,
49+
NULL,
50+
NULL
51+
};
52+
53+
PyMODINIT_FUNC PyInit__windowing(void)
54+
{
55+
PyObject *module = PyModule_Create(&moduledef);
56+
return module;
57+
}
58+
59+
#else
60+
PyMODINIT_FUNC init_windowing()
4261
{
4362
Py_InitModule("_windowing", _windowing_methods);
4463
}
64+
#endif

0 commit comments

Comments
 (0)