diff options
-rw-r--r-- | AUTHORS | 4 | ||||
-rw-r--r-- | NEWS | 9 | ||||
-rw-r--r-- | RELEASING | 34 | ||||
-rw-r--r-- | doc/reference/context.rst | 8 | ||||
-rw-r--r-- | doc/reference/text.rst | 4 | ||||
-rwxr-xr-x | examples/cairo_snippets/snippets/__init__.py | 4 | ||||
-rwxr-xr-x | examples/cairo_snippets/snippets_pdf.py | 68 | ||||
-rwxr-xr-x | examples/cairo_snippets/snippets_png.py | 6 | ||||
-rwxr-xr-x | examples/cairo_snippets/snippets_ps.py | 68 | ||||
-rwxr-xr-x | examples/cairo_snippets/snippets_svg.py | 68 | ||||
-rwxr-xr-x | examples/hering.py | 44 | ||||
-rwxr-xr-x | examples/spiral.py | 24 | ||||
-rwxr-xr-x | examples/warpedtext.py | 68 | ||||
-rwxr-xr-x | src/__init__.py | 2 | ||||
-rw-r--r-- | src/cairomodule.c | 129 | ||||
-rw-r--r-- | src/context.c | 135 | ||||
-rw-r--r-- | src/font.c | 101 | ||||
-rw-r--r-- | src/matrix.c | 84 | ||||
-rw-r--r-- | src/path.c | 33 | ||||
-rw-r--r-- | src/pattern.c | 37 | ||||
-rw-r--r-- | src/surface.c | 249 | ||||
-rw-r--r-- | src/wscript | 14 | ||||
-rwxr-xr-x | test/isurface_create_for_data1.py | 2 | ||||
-rwxr-xr-x | waf | bin | 0 -> 92546 bytes | |||
-rw-r--r-- | wscript | 101 |
25 files changed, 711 insertions, 585 deletions
@@ -1,10 +1,10 @@ Original Author --------------- -James Henstridge <james@daa.com.au> +James Henstridge Maintainer ---------- -Steve Chaplin <stevech1097 # yahoo.com.au> +Steve Chaplin Contributors ------------ @@ -0,0 +1,9 @@ +pycairo 1.8.10 (2010-09-12) +=========================== +A new version of pycairo which requires and supports Python 3. +The current pycairo for Python 2 has been renamed to py2cairo. + +Waf is the tool used to build and install the package. +It is licensed under the LGPL version 3. +The functions image_surface_create_for_data, image_surface_get_data, and +xpyb2struct use the old Python 2.x buffer protocol, and have been disabled. @@ -5,29 +5,26 @@ Sift through the information in 'git log' since the last release. Summarize major changes briefly in a style similar to other entries in NEWS. 2) Increment pycairo and cairo version numbers in: - configure.ac doc/conf.py README - setup.py wscript -Increment pycairo_major/minor/micro_version in configure.ac -Increase the cairo_required_version - to an even numbered version. -Increment pycairo_major/minor version to match the cairo major/minor version -that pycairo requires (and increment the micro version if necessary for any -subsequent pycairo updates). +3) Create the archive +Delete *.pdf *.png *.ps *.svg files in + examples/ + examples/cairo_snippets/snippets/ -3) Run "make distcheck" and verify that it gives no warnings or errors and - ends with a message of the form: +$ ./waf distcheck # ensure there are no errors - =============================================== - pycairo-X.Y.Z archives ready for distribution: - pycairo-X.Y.Z.tar.gz - =============================================== +$ ./waf clean distclean +$ ./waf dist # to create pycairo-x.x.x.tar.bz2 -Install from the tar.gz archive, run tests and examples. +create sha1 sum +$ sha1sum pycairo-x.x.x.tar.bz2 > pycairo-x.x.x.tar.bz2.sha1 -4) Commit the changes to NEWS, configure.ac etc +Copy to /tmp, install from the archive, run tests and examples. + +4) git commit # the changes to NEWS, etc It's especially important to mention the new version number in the git commit comment. Add a '=== Pycairo x.x.x ===' header. @@ -54,11 +51,8 @@ $ git push origin master Update the pycairo webpage to reference the new release. Update http://cheeseshop.python.org entry for pycairo. -7) Edit: - configure.ac - setup.py - wscript - to increment pycairo_version_micro to the next larger (odd) number. +7) Edit wscript to increment pycairo_version_micro to the next larger (odd) +number. $ git push origin master 8) Send a message to cairo-announce@cairographics.org diff --git a/doc/reference/context.rst b/doc/reference/context.rst index 277bd16..dfcd78a 100644 --- a/doc/reference/context.rst +++ b/doc/reference/context.rst @@ -927,7 +927,7 @@ safely be changed, without loosing the current state. Use .. method:: select_font_face(family[, slant[, weight]]) :param family: a font family name - :type family: str or unicode + :type family: str :param slant: the :ref:`FONT_SLANT <constants_FONT_SLANT>` of the font, defaults to :data:`cairo.FONT_SLANT_NORMAL`. :param weight: the :ref:`FONT_WEIGHT <constants_FONT_WEIGHT>` of the @@ -1295,7 +1295,7 @@ safely be changed, without loosing the current state. Use .. method:: show_text(text) :param text: text - :type text: str or unicode + :type text: str A drawing operator that generates the shape from a string of text, rendered according to the current font_face, font_size (font_matrix), @@ -1389,7 +1389,7 @@ safely be changed, without loosing the current state. Use .. method:: text_extents(text) :param text: text to get extents for - :type text: string or unicode + :type text: str :returns: x_bearing, y_bearing, width, height, x_advance, y_advance :rtype: 6-tuple of float @@ -1409,7 +1409,7 @@ safely be changed, without loosing the current state. Use .. method:: text_path(text) :param text: text - :type text: string or unicode + :type text: string Adds closed paths for text to the current path. The generated path if filled, achieves an effect similar to that of :meth:`Context.show_text`. diff --git a/doc/reference/text.rst b/doc/reference/text.rst index b0a261f..f7bb854 100644 --- a/doc/reference/text.rst +++ b/doc/reference/text.rst @@ -65,7 +65,7 @@ The *cairo.ToyFontFace* class can be used instead of :meth:`Context.select_font_ .. class:: ToyFontFace(family[, slant[, weight]]) :param family: a font family name - :type family: str or unicode + :type family: str :param slant: the :ref:`FONT_SLANT <constants_FONT_SLANT>` of the font, defaults to :data:`cairo.FONT_SLANT_NORMAL`. :param weight: the :ref:`FONT_WEIGHT <constants_FONT_WEIGHT>` of the font, @@ -186,7 +186,7 @@ use. .. method:: text_extents(text) :param text: text - :type text: str or unicode + :type text: str :returns: (x_bearing, y_bearing, width, height, x_advance, y_advance) :rtype: 6-tuple of float diff --git a/examples/cairo_snippets/snippets/__init__.py b/examples/cairo_snippets/snippets/__init__.py index 70b4813..a3bc4fa 100755 --- a/examples/cairo_snippets/snippets/__init__.py +++ b/examples/cairo_snippets/snippets/__init__.py @@ -8,5 +8,5 @@ snip_list.sort() # function used by some or all snippets def snippet_normalize (ctx, width, height): - ctx.scale (width, height) - ctx.set_line_width (0.04) + ctx.scale (width, height) + ctx.set_line_width (0.04) diff --git a/examples/cairo_snippets/snippets_pdf.py b/examples/cairo_snippets/snippets_pdf.py index 0606804..65b9c3e 100755 --- a/examples/cairo_snippets/snippets_pdf.py +++ b/examples/cairo_snippets/snippets_pdf.py @@ -9,7 +9,7 @@ import sys import cairo if not cairo.HAS_PDF_SURFACE: - raise SystemExit ('cairo was not compiled with PDF support') + raise SystemExit ('cairo was not compiled with PDF support') from snippets import snip_list, snippet_normalize @@ -20,37 +20,39 @@ width, height = width_in_points, height_in_points # used by snippet_normalize() def do_snippet (snippet): - if verbose_mode: - print 'processing %s' % snippet, - - filename = 'snippets/%s.pdf' % snippet - surface = cairo.PDFSurface (filename, width_in_points, height_in_points) - cr = cairo.Context (surface) - - cr.save() - try: - execfile ('snippets/%s.py' % snippet, globals(), locals()) - except: - exc_type, exc_value = sys.exc_info()[:2] - print >> sys.stderr, exc_type, exc_value - else: - cr.restore() - cr.show_page() - surface.finish() - - if verbose_mode: - print + if verbose_mode: + print('processing %s' % snippet) + + filename = 'snippets/%s.pdf' % snippet + surface = cairo.PDFSurface (filename, width_in_points, height_in_points) + cr = cairo.Context (surface) + + cr.save() + try: + fName = 'snippets/%s.py' % snippet + code = open(fName).read() + exec (code, globals(), locals()) + except: + exc_type, exc_value = sys.exc_info()[:2] + print(exc_type, exc_value, file=sys.stderr) + else: + cr.restore() + cr.show_page() + surface.finish() + + if verbose_mode: + print if __name__ == '__main__': - verbose_mode = True - if len(sys.argv) > 1 and sys.argv[1] == '-s': - verbose_mode = False - del sys.argv[1] - - if len(sys.argv) > 1: # do specified snippets - snippet_list = sys.argv[1:] - else: # do all snippets - snippet_list = snip_list - - for s in snippet_list: - do_snippet (s) + verbose_mode = True + if len(sys.argv) > 1 and sys.argv[1] == '-s': + verbose_mode = False + del sys.argv[1] + + if len(sys.argv) > 1: # do specified snippets + snippet_list = sys.argv[1:] + else: # do all snippets + snippet_list = snip_list + + for s in snippet_list: + do_snippet (s) diff --git a/examples/cairo_snippets/snippets_png.py b/examples/cairo_snippets/snippets_png.py index 5341979..94b5fb9 100755 --- a/examples/cairo_snippets/snippets_png.py +++ b/examples/cairo_snippets/snippets_png.py @@ -18,14 +18,16 @@ width, height = 256, 256 # used by snippet_normalize() def do_snippet (snippet): if verbose_mode: - print 'processing %s' % snippet, + print('processing %s' % snippet) surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, width, height) cr = cairo.Context (surface) cr.save() try: - execfile ('snippets/%s.py' % snippet, globals(), locals()) + fName = 'snippets/%s.py' % snippet + code = open(fName).read() + exec (code, globals(), locals()) except: # exc_type, exc_value = sys.exc_info()[:2] # print >> sys.stderr, exc_type, exc_value diff --git a/examples/cairo_snippets/snippets_ps.py b/examples/cairo_snippets/snippets_ps.py index 364402a..0015973 100755 --- a/examples/cairo_snippets/snippets_ps.py +++ b/examples/cairo_snippets/snippets_ps.py @@ -9,7 +9,7 @@ import sys import cairo if not cairo.HAS_PS_SURFACE: - raise SystemExit ('cairo was not compiled with PS support') + raise SystemExit ('cairo was not compiled with PS support') from snippets import snip_list, snippet_normalize @@ -20,37 +20,39 @@ width, height = width_in_points, height_in_points # used by snippet_normalize() def do_snippet (snippet): - if verbose_mode: - print 'processing %s' % snippet, - - filename = 'snippets/%s.ps' % snippet - surface = cairo.PSSurface (filename, width_in_points, height_in_points) - cr = cairo.Context (surface) - - cr.save() - try: - execfile ('snippets/%s.py' % snippet, globals(), locals()) - except: - exc_type, exc_value = sys.exc_info()[:2] - print >> sys.stderr, exc_type, exc_value - else: - cr.restore() - cr.show_page() - surface.finish() - - if verbose_mode: - print + if verbose_mode: + print('processing %s' % snippet) + + filename = 'snippets/%s.ps' % snippet + surface = cairo.PSSurface (filename, width_in_points, height_in_points) + cr = cairo.Context (surface) + + cr.save() + try: + fName = 'snippets/%s.py' % snippet + code = open(fName).read() + exec (code, globals(), locals()) + except: + exc_type, exc_value = sys.exc_info()[:2] + print >> sys.stderr, exc_type, exc_value + else: + cr.restore() + cr.show_page() + surface.finish() + + if verbose_mode: + print if __name__ == '__main__': - verbose_mode = True - if len(sys.argv) > 1 and sys.argv[1] == '-s': - verbose_mode = False - del sys.argv[1] - - if len(sys.argv) > 1: # do specified snippets - snippet_list = sys.argv[1:] - else: # do all snippets - snippet_list = snip_list - - for s in snippet_list: - do_snippet (s) + verbose_mode = True + if len(sys.argv) > 1 and sys.argv[1] == '-s': + verbose_mode = False + del sys.argv[1] + + if len(sys.argv) > 1: # do specified snippets + snippet_list = sys.argv[1:] + else: # do all snippets + snippet_list = snip_list + + for s in snippet_list: + do_snippet (s) diff --git a/examples/cairo_snippets/snippets_svg.py b/examples/cairo_snippets/snippets_svg.py index 3620ee7..e9c9d8f 100755 --- a/examples/cairo_snippets/snippets_svg.py +++ b/examples/cairo_snippets/snippets_svg.py @@ -6,7 +6,7 @@ import sys import cairo if not cairo.HAS_SVG_SURFACE: - raise SystemExit ('cairo was not compiled with SVG support') + raise SystemExit ('cairo was not compiled with SVG support') from snippets import snip_list, snippet_normalize @@ -17,37 +17,39 @@ width, height = width_in_points, height_in_points # used by snippet_normalize() def do_snippet (snippet): - if verbose_mode: - print 'processing %s' % snippet, - - filename = 'snippets/%s.svg' % snippet - surface = cairo.SVGSurface (filename, width_in_points, height_in_points) - cr = cairo.Context (surface) - - cr.save() - try: - execfile ('snippets/%s.py' % snippet, globals(), locals()) - except: - exc_type, exc_value = sys.exc_info()[:2] - print >> sys.stderr, exc_type, exc_value - else: - cr.restore() - cr.show_page() - surface.finish() - - if verbose_mode: - print + if verbose_mode: + print('processing %s' % snippet) + + filename = 'snippets/%s.svg' % snippet + surface = cairo.SVGSurface (filename, width_in_points, height_in_points) + cr = cairo.Context (surface) + + cr.save() + try: + fName = 'snippets/%s.py' % snippet + code = open(fName).read() + exec (code, globals(), locals()) + except: + exc_type, exc_value = sys.exc_info()[:2] + print >> sys.stderr, exc_type, exc_value + else: + cr.restore() + cr.show_page() + surface.finish() + + if verbose_mode: + print if __name__ == '__main__': - verbose_mode = True - if len(sys.argv) > 1 and sys.argv[1] == '-s': - verbose_mode = False - del sys.argv[1] - - if len(sys.argv) > 1: # do specified snippets - snippet_list = sys.argv[1:] - else: # do all snippets - snippet_list = snip_list - - for s in snippet_list: - do_snippet (s) + verbose_mode = True + if len(sys.argv) > 1 and sys.argv[1] == '-s': + verbose_mode = False + del sys.argv[1] + + if len(sys.argv) > 1: # do specified snippets + snippet_list = sys.argv[1:] + else: # do all snippets + snippet_list = snip_list + + for s in snippet_list: + do_snippet (s) diff --git a/examples/hering.py b/examples/hering.py index e400e15..1d3cf60 100755 --- a/examples/hering.py +++ b/examples/hering.py @@ -10,37 +10,37 @@ WIDTH = 300 HEIGHT = 600 def draw_hering (ctx, width, height): - LINES= 32 - MAX_THETA = .80 * math.pi * 2 - THETA_INC = 2.0 * MAX_THETA / (LINES-1) + LINES= 32 + MAX_THETA = .80 * math.pi * 2 + THETA_INC = 2.0 * MAX_THETA / (LINES-1) - ctx.set_source_rgb (0, 0, 0) - ctx.set_line_width (2.0) + ctx.set_source_rgb (0, 0, 0) + ctx.set_line_width (2.0) - ctx.save() + ctx.save() - ctx.translate (width / 2, height / 2) - ctx.rotate (MAX_THETA) + ctx.translate (width / 2, height / 2) + ctx.rotate (MAX_THETA) - for i in range (LINES): - ctx.move_to (-2 * width, 0) - ctx.line_to (2 * width, 0) - ctx.stroke() + for i in range (LINES): + ctx.move_to (-2 * width, 0) + ctx.line_to (2 * width, 0) + ctx.stroke() - ctx.rotate (- THETA_INC) + ctx.rotate (- THETA_INC) - ctx.restore() + ctx.restore() - ctx.set_line_width (6) - ctx.set_source_rgb (1, 0, 0) + ctx.set_line_width (6) + ctx.set_source_rgb (1, 0, 0) - ctx.move_to (width / 4.0, 0) - ctx.rel_line_to (0, height) - ctx.stroke() + ctx.move_to (width / 4.0, 0) + ctx.rel_line_to (0, height) + ctx.stroke() - ctx.move_to (3 * width / 4.0, 0) - ctx.rel_line_to (0, height) - ctx.stroke() + ctx.move_to (3 * width / 4.0, 0) + ctx.rel_line_to (0, height) + ctx.stroke() surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT) diff --git a/examples/spiral.py b/examples/spiral.py index 7be9af2..b0f3952 100755 --- a/examples/spiral.py +++ b/examples/spiral.py @@ -7,21 +7,21 @@ import cairo WIDTH, HEIGHT = 600, 600 def draw_spiral (ctx, width, height): - wd = .02 * width - hd = .02 * height + wd = .02 * width + hd = .02 * height - width -= 2 - height -= 2 + width -= 2 + height -= 2 - ctx.move_to (width + 1, 1-hd) - for i in range(9): - ctx.rel_line_to (0, height - hd * (2 * i - 1)) - ctx.rel_line_to (- (width - wd * (2 *i)), 0) - ctx.rel_line_to (0, - (height - hd * (2*i))) - ctx.rel_line_to (width - wd * (2 * i + 1), 0) + ctx.move_to (width + 1, 1-hd) + for i in range(9): + ctx.rel_line_to (0, height - hd * (2 * i - 1)) + ctx.rel_line_to (- (width - wd * (2 *i)), 0) + ctx.rel_line_to (0, - (height - hd * (2*i))) + ctx.rel_line_to (width - wd * (2 * i + 1), 0) - ctx.set_source_rgb (0, 0, 1) - ctx.stroke() + ctx.set_source_rgb (0, 0, 1) + ctx.stroke() surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, WIDTH, HEIGHT) diff --git a/examples/warpedtext.py b/examples/warpedtext.py index cd83ee7..0c8257d 100755 --- a/examples/warpedtext.py +++ b/examples/warpedtext.py @@ -4,44 +4,44 @@ import cairo import math def warpPath(ctx, function): - first = True - - for type, points in ctx.copy_path(): - if type == cairo.PATH_MOVE_TO: - if first: - ctx.new_path() - first = False - x, y = function(*points) - ctx.move_to(x, y) - - elif type == cairo.PATH_LINE_TO: - x, y = function(*points) - ctx.line_to(x, y) - - elif type == cairo.PATH_CURVE_TO: - x1, y1, x2, y2, x3, y3 = points - x1, y1 = function(x1, y1) - x2, y2 = function(x2, y2) - x3, y3 = function(x3, y3) - ctx.curve_to(x1, y1, x2, y2, x3, y3) - - elif type == cairo.PATH_CLOSE_PATH: - ctx.close_path() + first = True + + for type, points in ctx.copy_path(): + if type == cairo.PATH_MOVE_TO: + if first: + ctx.new_path() + first = False + x, y = function(*points) + ctx.move_to(x, y) + + elif type == cairo.PATH_LINE_TO: + x, y = function(*points) + ctx.line_to(x, y) + + elif type == cairo.PATH_CURVE_TO: + x1, y1, x2, y2, x3, y3 = points + x1, y1 = function(x1, y1) + x2, y2 = function(x2, y2) + x3, y3 = function(x3, y3) + ctx.curve_to(x1, y1, x2, y2, x3, y3) + + elif type == cairo.PATH_CLOSE_PATH: + ctx.close_path() def spiral(x, y): - theta0 = -math.pi * 3 / 4 - theta = x / Width * math.pi * 2 + theta0 - radius = y + 200 - x/7 - xnew = radius*math.cos(theta) - ynew = radius*math.sin(-theta) - return xnew + Width/2, ynew + Height/2 + theta0 = -math.pi * 3 / 4 + theta = x / Width * math.pi * 2 + theta0 + radius = y + 200 - x/7 + xnew = radius*math.cos(theta) + ynew = radius*math.sin(-theta) + return xnew + Width/2, ynew + Height/2 def curl(x, y): - xn = x - Textwidth/2 - #yn = y - Textheight/2 - xnew = xn - ynew = y + xn ** 3 / ((Textwidth/2)**3) * 70 - return xnew + Width/2, ynew + Height*2/5 + xn = x - Textwidth/2 + #yn = y - Textheight/2 + xnew = xn + ynew = y + xn ** 3 / ((Textwidth/2)**3) * 70 + return xnew + Width/2, ynew + Height*2/5 Width, Height = 512, 512 diff --git a/src/__init__.py b/src/__init__.py index 85e07e2..b0f3bf6 100755 --- a/src/__init__.py +++ b/src/__init__.py @@ -15,4 +15,4 @@ more details. You should have received a copy of the GNU Lesser General Public License along with pycairo. If not, see <http://www.gnu.org/licenses/>. ''' -from _cairo import * +from ._cairo import * diff --git a/src/cairomodule.c b/src/cairomodule.c index 02da659..58e665c 100644 --- a/src/cairomodule.c +++ b/src/cairomodule.c @@ -145,15 +145,16 @@ static Pycairo_CAPI_t CAPI = { static PyObject * pycairo_cairo_version (PyObject *self) { - return PyInt_FromLong (cairo_version()); + return PyLong_FromLong (cairo_version()); } static PyObject * pycairo_cairo_version_string (PyObject *self) { - return PyString_FromString (cairo_version_string()); + return PyUnicode_FromString (cairo_version_string()); } -static PyMethodDef cairo_functions[] = { +//static PyMethodDef cairo_functions[] = { +static PyMethodDef cairo_methods[] = { {"cairo_version", (PyCFunction)pycairo_cairo_version, METH_NOARGS}, {"cairo_version_string", (PyCFunction)pycairo_cairo_version_string, METH_NOARGS}, @@ -161,77 +162,133 @@ static PyMethodDef cairo_functions[] = { }; -DL_EXPORT(void) -init_cairo(void) +// Module initialization +struct cairo_state { + PyObject *ErrorObject; +}; + +#define GETSTATE(m) ((struct cairo_state*)PyModule_GetState(m)) + +static int +cairo_traverse(PyObject *m, visitproc v, void *arg) +{ + // Py_VISIT(GETSTATE(m)->ErrorObject); + /* gives error + ImportError: ./_cairo.so: undefined symbol: visit + */ + return 0; +} + +static int +cairo_clear(PyObject *m) { - PyObject *m; + Py_CLEAR(GETSTATE(m)->ErrorObject); + return 0; +} +static struct PyModuleDef cairomodule = { + PyModuleDef_HEAD_INIT, + "cairo", + NULL, + sizeof(struct cairo_state), + cairo_methods, + 0, /* m_reload */ + cairo_traverse, + cairo_clear, + 0, /* m_free - not needed, since all is done in m_clear */ +}; + +PyObject * +PyInit__cairo(void) +{ if (PyType_Ready(&PycairoContext_Type) < 0) - return; + return NULL; if (PyType_Ready(&PycairoFontFace_Type) < 0) - return; + return NULL; if (PyType_Ready(&PycairoToyFontFace_Type) < 0) - return; + return NULL; if (PyType_Ready(&PycairoFontOptions_Type) < 0) - return; + return NULL; if (PyType_Ready(&PycairoMatrix_Type) < 0) - return; + return NULL; if (PyType_Ready(&PycairoPath_Type) < 0) - return; + return NULL; PycairoPathiter_Type.tp_iter=&PyObject_SelfIter; if (PyType_Ready(&PycairoPathiter_Type) < 0) - return; + return NULL; if (PyType_Ready(&PycairoPattern_Type) < 0) - return; + return NULL; if (PyType_Ready(&PycairoSolidPattern_Type) < 0) - return; + return NULL; if (PyType_Ready(&PycairoSurfacePattern_Type) < 0) - return; + return NULL; if (PyType_Ready(&PycairoGradient_Type) < 0) - return; + return NULL; if (PyType_Ready(&PycairoLinearGradient_Type) < 0) - return; + return NULL; if (PyType_Ready(&PycairoRadialGradient_Type) < 0) - return; + return NULL; if (PyType_Ready(&PycairoScaledFont_Type) < 0) - return; + return NULL; if (PyType_Ready(&PycairoSurface_Type) < 0) - return; + return NULL; #ifdef CAIRO_HAS_IMAGE_SURFACE if (PyType_Ready(&PycairoImageSurface_Type) < 0) - return; + return NULL; #endif #ifdef CAIRO_HAS_PDF_SURFACE if (PyType_Ready(&PycairoPDFSurface_Type) < 0) - return; + return NULL; #endif #ifdef CAIRO_HAS_PS_SURFACE if (PyType_Ready(&PycairoPSSurface_Type) < 0) - return; + return NULL; #endif #ifdef CAIRO_HAS_SVG_SURFACE if (PyType_Ready(&PycairoSVGSurface_Type) < 0) - return; + return NULL; #endif #ifdef CAIRO_HAS_WIN32_SURFACE if (PyType_Ready(&PycairoWin32Surface_Type) < 0) - return; + return NULL; if (PyType_Ready(&PycairoWin32PrintingSurface_Type) < 0) - return; + return NULL; #endif #ifdef CAIRO_HAS_XCB_SURFACE if (PyType_Ready(&PycairoXCBSurface_Type) < 0) - return; + return NULL; #endif #ifdef CAIRO_HAS_XLIB_SURFACE if (PyType_Ready(&PycairoXlibSurface_Type) < 0) - return; + return NULL; #endif - m = Py_InitModule("cairo._cairo", cairo_functions); + + PyObject *m = PyModule_Create(&cairomodule); + //PyObject *m; + //m = Py_InitModule("cairo._cairo", cairo_functions); + if (m==NULL) + return NULL; + GETSTATE(m)->ErrorObject = PyErr_NewException("cairo.Error", NULL, NULL); + if (GETSTATE(m)->ErrorObject == NULL) { + Py_DECREF(m); + return NULL; + } + /* Add 'cairo.Error' to the module */ + // if (CairoError == NULL) { + // CairoError = PyErr_NewException("cairo.Error", NULL, NULL); + // if (CairoError == NULL) + // return NULL; + //} + //Py_INCREF(CairoError); + // not needed ? + //if (PyModule_AddObject(m, "Error", CairoError) < 0) + // return NULL; + + PyModule_AddStringConstant(m, "version", VERSION); PyModule_AddObject(m, "version_info", @@ -322,16 +379,6 @@ init_cairo(void) PyModule_AddObject(m, "CAPI", PyCObject_FromVoidPtr(&CAPI, NULL)); - /* Add 'cairo.Error' to the module */ - if (CairoError == NULL) { - CairoError = PyErr_NewException("cairo.Error", NULL, NULL); - if (CairoError == NULL) - return; - } - Py_INCREF(CairoError); - if (PyModule_AddObject(m, "Error", CairoError) < 0) - return; - /* constants */ #if CAIRO_HAS_ATSUI_FONT PyModule_AddIntConstant(m, "HAS_ATSUI_FONT", 1); @@ -493,4 +540,6 @@ init_cairo(void) CONSTANT(SUBPIXEL_ORDER_VRGB); CONSTANT(SUBPIXEL_ORDER_VBGR); #undef CONSTANT + + return m; } diff --git a/src/context.c b/src/context.c index cf56f5d..60cb0d7 100644 --- a/src/context.c +++ b/src/context.c @@ -65,8 +65,8 @@ pycairo_dealloc(PycairoContext *o) { o->ctx = NULL; } Py_CLEAR(o->base); - - o->ob_type->tp_free((PyObject *)o); + //o->ob_type->tp_free((PyObject *)o); + Py_TYPE(o)->tp_free(o); } static PyObject * @@ -286,7 +286,7 @@ pycairo_font_extents (PycairoContext *o) { static PyObject * pycairo_get_antialias (PycairoContext *o) { - return PyInt_FromLong (cairo_get_antialias (o->ctx)); + return PyLong_FromLong (cairo_get_antialias (o->ctx)); } static PyObject * @@ -328,12 +328,12 @@ pycairo_get_dash (PycairoContext *o) { static PyObject * pycairo_get_dash_count (PycairoContext *o) { - return PyInt_FromLong (cairo_get_dash_count (o->ctx)); + return PyLong_FromLong (cairo_get_dash_count (o->ctx)); } static PyObject * pycairo_get_fill_rule (PycairoContext *o) { - return PyInt_FromLong(cairo_get_fill_rule (o->ctx)); + return PyLong_FromLong(cairo_get_fill_rule (o->ctx)); } static PyObject * @@ -368,12 +368,12 @@ pycairo_get_group_target (PycairoContext *o) { static PyObject * pycairo_get_line_cap (PycairoContext *o) { - return PyInt_FromLong(cairo_get_line_cap (o->ctx)); + return PyLong_FromLong(cairo_get_line_cap (o->ctx)); } static PyObject * pycairo_get_line_join (PycairoContext *o) { - return PyInt_FromLong(cairo_get_line_join (o->ctx)); + return PyLong_FromLong(cairo_get_line_join (o->ctx)); } static PyObject * @@ -395,7 +395,7 @@ pycairo_get_miter_limit (PycairoContext *o) { static PyObject * pycairo_get_operator (PycairoContext *o) { - return PyInt_FromLong (cairo_get_operator (o->ctx)); + return PyLong_FromLong (cairo_get_operator (o->ctx)); } static PyObject * @@ -457,7 +457,7 @@ _PyGlyphs_AsGlyphs (PyObject *py_object, int *num_glyphs) "each glyph item must be an (i,x,y) sequence"); goto error; } - glyph->index = PyInt_AsLong(PySequence_Fast_GET_ITEM(py_seq, 0)); + glyph->index = PyLong_AsLong(PySequence_Fast_GET_ITEM(py_seq, 0)); glyph->x = PyFloat_AsDouble(PySequence_Fast_GET_ITEM(py_seq, 1)); glyph->y = PyFloat_AsDouble(PySequence_Fast_GET_ITEM(py_seq, 2)); if (PyErr_Occurred()) @@ -784,32 +784,21 @@ pycairo_scale (PycairoContext *o, PyObject *args) { static PyObject * pycairo_select_font_face (PycairoContext *o, PyObject *args) { PyObject *obj; - PyObject *pyUTF8 = NULL; - const char *utf8family = NULL; cairo_font_slant_t slant = CAIRO_FONT_SLANT_NORMAL; cairo_font_weight_t weight = CAIRO_FONT_WEIGHT_NORMAL; - if (!PyArg_ParseTuple(args, "O!|ii:Context.select_font_face", - &PyBaseString_Type, &obj, &slant, &weight)) + if (!PyArg_ParseTuple(args, "U|ii:Context.select_font_face", + &obj, &slant, &weight)) return NULL; - /* accept str and unicode family, auto convert to utf8 as required */ - if (PyString_Check(obj)) { - /* A plain ASCII string is also a valid UTF-8 string */ - utf8family = PyString_AS_STRING(obj); - } else if (PyUnicode_Check(obj)) { - pyUTF8 = PyUnicode_AsUTF8String(obj); - if (pyUTF8 != NULL) { - utf8family = PyString_AS_STRING(pyUTF8); - } - } else { - PyErr_SetString(PyExc_TypeError, - "Context.select_font_face: family must be str or unicode"); - } - if (utf8family == NULL) + PyObject *pyUTF8 = PyUnicode_AsUTF8String(obj); + if (pyUTF8 == NULL) + return NULL; + const char *utf8 = PyBytes_AS_STRING(pyUTF8); + if (utf8 == NULL) return NULL; - cairo_select_font_face (o->ctx, utf8family, slant, weight); + cairo_select_font_face (o->ctx, utf8, slant, weight); Py_XDECREF(pyUTF8); RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx); Py_RETURN_NONE; @@ -1109,23 +1098,16 @@ pycairo_show_page (PycairoContext *o) { } static PyObject * -pycairo_show_text (PycairoContext *o, PyObject *obj) { - PyObject *pyUTF8 = NULL; - const char *utf8 = NULL; +pycairo_show_text (PycairoContext *o, PyObject *args) { + PyObject *obj; - /* accept str and unicode text, auto convert to utf8 as required */ - if (PyString_Check(obj)) { - /* A plain ASCII string is also a valid UTF-8 string */ - utf8 = PyString_AS_STRING(obj); - } else if (PyUnicode_Check(obj)) { - pyUTF8 = PyUnicode_AsUTF8String(obj); - if (pyUTF8 != NULL) { - utf8 = PyString_AS_STRING(pyUTF8); - } - } else { - PyErr_SetString(PyExc_TypeError, - "Context.show_text: text must be str or unicode"); - } + if (!PyArg_ParseTuple(args, "U:Context.show_text", &obj)) + return NULL; + + PyObject *pyUTF8 = PyUnicode_AsUTF8String(obj); + if (pyUTF8 == NULL) + return NULL; + const char *utf8 = PyBytes_AS_STRING(pyUTF8); if (utf8 == NULL) return NULL; @@ -1164,27 +1146,20 @@ pycairo_stroke_preserve (PycairoContext *o) { } static PyObject * -pycairo_text_extents (PycairoContext *o, PyObject *obj) { - cairo_text_extents_t extents; - PyObject *pyUTF8 = NULL; - const char *utf8 = NULL; - - /* accept str and unicode text, auto convert to utf8 as required */ - if (PyString_Check(obj)) { - /* A plain ASCII string is also a valid UTF-8 string */ - utf8 = PyString_AS_STRING(obj); - } else if (PyUnicode_Check(obj)) { - pyUTF8 = PyUnicode_AsUTF8String(obj); - if (pyUTF8 != NULL) { - utf8 = PyString_AS_STRING(pyUTF8); - } - } else { - PyErr_SetString(PyExc_TypeError, - "Context.text_extents: text must be str or unicode"); - } +pycairo_text_extents (PycairoContext *o, PyObject *args) { + PyObject *obj; + + if (!PyArg_ParseTuple(args, "U:Context.text_extents", &obj)) + return NULL; + + PyObject *pyUTF8 = PyUnicode_AsUTF8String(obj); + if (pyUTF8 == NULL) + return NULL; + const char *utf8 = PyBytes_AS_STRING(pyUTF8); if (utf8 == NULL) return NULL; + cairo_text_extents_t extents; cairo_text_extents (o->ctx, utf8, &extents); Py_XDECREF(pyUTF8); RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx); @@ -1194,23 +1169,16 @@ pycairo_text_extents (PycairoContext *o, PyObject *obj) { } static PyObject * -pycairo_text_path (PycairoContext *o, PyObject *obj) { - PyObject *pyUTF8 = NULL; - const char *utf8 = NULL; +pycairo_text_path (PycairoContext *o, PyObject *args) { + PyObject *obj; - /* accept str and unicode text, auto convert to utf8 as required */ - if (PyString_Check(obj)) { - /* A plain ASCII string is also a valid UTF-8 string */ - utf8 = PyString_AS_STRING(obj); - } else if (PyUnicode_Check(obj)) { - pyUTF8 = PyUnicode_AsUTF8String(obj); - if (pyUTF8 != NULL) { - utf8 = PyString_AS_STRING(pyUTF8); - } - } else { - PyErr_SetString(PyExc_TypeError, - "Context.text_path: text must be str or unicode"); - } + if (!PyArg_ParseTuple(args, "U:Context.text_path", &obj)) + return NULL; + + PyObject *pyUTF8 = PyUnicode_AsUTF8String(obj); + if (pyUTF8 == NULL) + return NULL; + const char *utf8 = PyBytes_AS_STRING(pyUTF8); if (utf8 == NULL) return NULL; @@ -1373,12 +1341,12 @@ static PyMethodDef pycairo_methods[] = { {"set_tolerance", (PyCFunction)pycairo_set_tolerance, METH_VARARGS}, {"show_glyphs", (PyCFunction)pycairo_show_glyphs, METH_VARARGS}, {"show_page", (PyCFunction)pycairo_show_page, METH_NOARGS}, - {"show_text", (PyCFunction)pycairo_show_text, METH_O}, + {"show_text", (PyCFunction)pycairo_show_text, METH_VARARGS}, {"stroke", (PyCFunction)pycairo_stroke, METH_NOARGS}, {"stroke_extents", (PyCFunction)pycairo_stroke_extents, METH_NOARGS}, {"stroke_preserve", (PyCFunction)pycairo_stroke_preserve, METH_NOARGS}, - {"text_extents", (PyCFunction)pycairo_text_extents, METH_O}, - {"text_path", (PyCFunction)pycairo_text_path, METH_O}, + {"text_extents", (PyCFunction)pycairo_text_extents, METH_VARARGS}, + {"text_path", (PyCFunction)pycairo_text_path, METH_VARARGS}, {"transform", (PyCFunction)pycairo_transform, METH_VARARGS}, {"translate", (PyCFunction)pycairo_translate, METH_VARARGS}, {"user_to_device", (PyCFunction)pycairo_user_to_device, METH_VARARGS}, @@ -1388,8 +1356,9 @@ static PyMethodDef pycairo_methods[] = { }; PyTypeObject PycairoContext_Type = { - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ + PyVarObject_HEAD_INIT(&PyType_Type, 0) + // PyObject_HEAD_INIT(NULL) + //0, /* ob_size */ "cairo.Context", /* tp_name */ sizeof(PycairoContext), /* tp_basicsize */ 0, /* tp_itemsize */ @@ -66,7 +66,8 @@ font_face_dealloc (PycairoFontFace *o) { cairo_font_face_destroy (o->font_face); o->font_face = NULL; } - o->ob_type->tp_free((PyObject *) o); + //o->ob_type->tp_free((PyObject *) o); + Py_TYPE(o)->tp_free(o); } static PyObject * @@ -89,8 +90,9 @@ static PyMethodDef font_face_methods[] = { */ PyTypeObject PycairoFontFace_Type = { - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ + PyVarObject_HEAD_INIT(&PyType_Type, 0) + //PyObject_HEAD_INIT(NULL) + //0, /* ob_size */ "cairo.FontFace", /* tp_name */ sizeof(PycairoFontFace), /* tp_basicsize */ 0, /* tp_itemsize */ @@ -139,50 +141,39 @@ PyTypeObject PycairoFontFace_Type = { static PyObject * toy_font_face_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { PyObject *obj; - PyObject *pyUTF8 = NULL; - const char *utf8family = NULL; cairo_font_slant_t slant = CAIRO_FONT_SLANT_NORMAL; cairo_font_weight_t weight = CAIRO_FONT_WEIGHT_NORMAL; - if (!PyArg_ParseTuple(args, "O!|ii:ToyFontFace.__new__", - &PyBaseString_Type, &obj, &slant, &weight)) + if (!PyArg_ParseTuple(args, "U|ii:ToyFontFace.__new__", + &obj, &slant, &weight)) return NULL; - /* accept str and unicode family, auto convert to utf8 as required */ - if (PyString_Check(obj)) { - /* A plain ASCII string is also a valid UTF-8 string */ - utf8family = PyString_AS_STRING(obj); - } else if (PyUnicode_Check(obj)) { - pyUTF8 = PyUnicode_AsUTF8String(obj); - if (pyUTF8 != NULL) { - utf8family = PyString_AS_STRING(pyUTF8); - } - } else { - PyErr_SetString(PyExc_TypeError, - "ToyFontFace.__new__: family must be str or unicode"); - } - if (utf8family == NULL) + PyObject *pyUTF8 = PyUnicode_AsUTF8String(obj); + if (pyUTF8 == NULL) + return NULL; + const char *utf8 = PyBytes_AS_STRING(pyUTF8); + if (utf8 == NULL) return NULL; PyObject *o = PycairoFontFace_FromFontFace ( - cairo_toy_font_face_create (utf8family, slant, weight)); + cairo_toy_font_face_create (utf8, slant, weight)); Py_XDECREF(pyUTF8); return o; } static PyObject * toy_font_get_family (PycairoToyFontFace *o) { - return PyString_FromString (cairo_toy_font_face_get_family (o->font_face)); + return PyUnicode_FromString (cairo_toy_font_face_get_family (o->font_face)); } static PyObject * toy_font_get_slant (PycairoToyFontFace *o) { - return PyInt_FromLong (cairo_toy_font_face_get_slant (o->font_face)); + return PyLong_FromLong (cairo_toy_font_face_get_slant (o->font_face)); } static PyObject * toy_font_get_weight (PycairoToyFontFace *o) { - return PyInt_FromLong (cairo_toy_font_face_get_weight (o->font_face)); + return PyLong_FromLong (cairo_toy_font_face_get_weight (o->font_face)); } static PyMethodDef toy_font_face_methods[] = { @@ -194,8 +185,9 @@ static PyMethodDef toy_font_face_methods[] = { PyTypeObject PycairoToyFontFace_Type = { - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ + PyVarObject_HEAD_INIT(&PyType_Type, 0) + //PyObject_HEAD_INIT(NULL) + //0, /* ob_size */ "cairo.ToyFontFace", /* tp_name */ sizeof(PycairoToyFontFace), /* tp_basicsize */ 0, /* tp_itemsize */ @@ -272,7 +264,8 @@ scaled_font_dealloc(PycairoScaledFont *o) { cairo_scaled_font_destroy (o->scaled_font); o->scaled_font = NULL; } - o->ob_type->tp_free((PyObject *) o); + //o->ob_type->tp_free((PyObject *) o); + Py_TYPE(o)->tp_free(o); } static PyObject * @@ -317,27 +310,20 @@ scaled_font_get_scale_matrix (PycairoScaledFont *o) { } static PyObject * -scaled_font_text_extents (PycairoScaledFont *o, PyObject *obj) { - cairo_text_extents_t extents; - PyObject *pyUTF8 = NULL; - const char *utf8 = NULL; - - /* accept str and unicode text, auto convert to utf8 as required */ - if (PyString_Check(obj)) { - /* A plain ASCII string is also a valid UTF-8 string */ - utf8 = PyString_AS_STRING(obj); - } else if (PyUnicode_Check(obj)) { - pyUTF8 = PyUnicode_AsUTF8String(obj); - if (pyUTF8 != NULL) { - utf8 = PyString_AS_STRING(pyUTF8); - } - } else { - PyErr_SetString(PyExc_TypeError, - "ScaledFont.text_extents: text must be str or unicode"); - } +scaled_font_text_extents (PycairoScaledFont *o, PyObject *args) { + PyObject *obj; + + if (!PyArg_ParseTuple(args, "U:ScaledFont.text_extents", &obj)) + return NULL; + + PyObject *pyUTF8 = PyUnicode_AsUTF8String(obj); + if (pyUTF8 == NULL) + return NULL; + const char *utf8 = PyBytes_AS_STRING(pyUTF8); if (utf8 == NULL) return NULL; + cairo_text_extents_t extents; cairo_scaled_font_text_extents (o->scaled_font, utf8, &extents); Py_XDECREF(pyUTF8); RETURN_NULL_IF_CAIRO_SCALED_FONT_ERROR(o->scaled_font); @@ -362,14 +348,15 @@ static PyMethodDef scaled_font_methods[] = { {"extents", (PyCFunction)scaled_font_extents, METH_NOARGS}, {"get_font_face", (PyCFunction)scaled_font_get_font_face, METH_NOARGS}, {"get_scale_matrix", (PyCFunction)scaled_font_get_scale_matrix, METH_VARARGS}, - {"text_extents", (PyCFunction)scaled_font_text_extents, METH_O}, + {"text_extents", (PyCFunction)scaled_font_text_extents, METH_VARARGS}, {NULL, NULL, 0, NULL}, }; PyTypeObject PycairoScaledFont_Type = { - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ + PyVarObject_HEAD_INIT(&PyType_Type, 0) + //PyObject_HEAD_INIT(NULL) + //0, /* ob_size */ "cairo.ScaledFont", /* tp_name */ sizeof(PycairoScaledFont), /* tp_basicsize */ 0, /* tp_itemsize */ @@ -446,7 +433,8 @@ font_options_dealloc(PycairoFontOptions *o) { cairo_font_options_destroy (o->font_options); o->font_options = NULL; } - o->ob_type->tp_free((PyObject *) o); + //o->ob_type->tp_free((PyObject *) o); + Py_TYPE(o)->tp_free(o); } static PyObject * @@ -456,24 +444,24 @@ font_options_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { static PyObject * font_options_get_antialias (PycairoFontOptions *o) { - return PyInt_FromLong (cairo_font_options_get_antialias (o->font_options)); + return PyLong_FromLong (cairo_font_options_get_antialias (o->font_options)); } static PyObject * font_options_get_hint_metrics (PycairoFontOptions *o) { - return PyInt_FromLong (cairo_font_options_get_hint_metrics + return PyLong_FromLong (cairo_font_options_get_hint_metrics (o->font_options)); } static PyObject * font_options_get_hint_style (PycairoFontOptions *o) { - return PyInt_FromLong (cairo_font_options_get_hint_style + return PyLong_FromLong (cairo_font_options_get_hint_style (o->font_options)); } static PyObject * font_options_get_subpixel_order (PycairoFontOptions *o) { - return PyInt_FromLong (cairo_font_options_get_subpixel_order + return PyLong_FromLong (cairo_font_options_get_subpixel_order (o->font_options)); } @@ -553,8 +541,9 @@ static PyMethodDef font_options_methods[] = { PyTypeObject PycairoFontOptions_Type = { - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ + PyVarObject_HEAD_INIT(&PyType_Type, 0) + //PyObject_HEAD_INIT(NULL) + //0, /* ob_size */ "cairo.FontOptions", /* tp_name */ sizeof(PycairoFontOptions), /* tp_basicsize */ 0, /* tp_itemsize */ diff --git a/src/matrix.c b/src/matrix.c index 98ccd9f..122e700 100644 --- a/src/matrix.c +++ b/src/matrix.c @@ -41,7 +41,8 @@ PycairoMatrix_FromMatrix (const cairo_matrix_t *matrix) { static void matrix_dealloc (PycairoMatrix *o) { - o->ob_type->tp_free((PyObject *)o); + //o->ob_type->tp_free((PyObject *)o); + Py_TYPE(o)->tp_free(o); } static PyObject * @@ -108,7 +109,7 @@ matrix_repr (PycairoMatrix *o) { o->matrix.xx, o->matrix.yx, o->matrix.xy, o->matrix.yy, o->matrix.x0, o->matrix.y0); - return PyString_FromString(buf); + return PyUnicode_FromString(buf); } static PyObject * @@ -213,45 +214,41 @@ matrix_item (PycairoMatrix *o, Py_ssize_t i) { } static PyNumberMethods matrix_as_number = { - (binaryfunc)0, /*nb_add*/ - (binaryfunc)0, /*nb_subtract*/ - (binaryfunc)matrix_operator_multiply, /*nb_multiply*/ - (binaryfunc)0, /*nb_divide*/ - (binaryfunc)0, /*nb_remainder*/ - (binaryfunc)0, /*nb_divmod*/ - (ternaryfunc)0, /*nb_power*/ - (unaryfunc)0, /*nb_negative*/ - (unaryfunc)0, /*nb_positive*/ - (unaryfunc)0, /*nb_absolute*/ - (inquiry)0, /*nb_nonzero*/ - (unaryfunc)0, /*nb_invert*/ - (binaryfunc)0, /*nb_lshift*/ - (binaryfunc)0, /*nb_rshift*/ - (binaryfunc)0, /*nb_and*/ - (binaryfunc)0, /*nb_xor*/ - (binaryfunc)0, /*nb_or*/ - (coercion)0, /*nb_coerce*/ - (unaryfunc)0, /*nb_int*/ - (unaryfunc)0, /*nb_long*/ - (unaryfunc)0, /*nb_float*/ - (unaryfunc)0, /*nb_oct*/ - (unaryfunc)0, /*nb_hex*/ - 0, /*nb_inplace_add*/ - 0, /*nb_inplace_subtract*/ - 0, /*nb_inplace_multiply*/ - 0, /*nb_inplace_divide*/ - 0, /*nb_inplace_remainder*/ - 0, /*nb_inplace_power*/ - 0, /*nb_inplace_lshift*/ - 0, /*nb_inplace_rshift*/ - 0, /*nb_inplace_and*/ - 0, /*nb_inplace_xor*/ - 0, /*nb_inplace_or*/ - (binaryfunc)0, /* nb_floor_divide */ - 0, /* nb_true_divide */ - 0, /* nb_inplace_floor_divide */ - 0, /* nb_inplace_true_divide */ - (unaryfunc)0, /* nb_index */ + 0, /* nb_add*/ + 0, /* nb_subtract*/ + (binaryfunc)matrix_operator_multiply, /* nb_multiply*/ + 0, /* nb_remainder*/ + 0, /* nb_divmod*/ + 0, /* nb_power*/ + 0, /* nb_negative*/ + 0, /* nb_positive*/ + 0, /* nb_absolute*/ + 0, /* nb_bool*/ + 0, /* nb_invert*/ + 0, /* nb_lshift*/ + 0, /* nb_rshift*/ + 0, /* nb_and*/ + 0, /* nb_xor*/ + 0, /* nb_or*/ + 0, /* nb_int*/ + 0, /* nb_reserved*/ + 0, /* nb_float*/ + 0, /* nb_inplace_add*/ + 0, /* nb_inplace_subtract*/ + 0, /* nb_inplace_multiply*/ + 0, /* nb_inplace_divide*/ + 0, /* nb_inplace_remainder*/ + 0, /* nb_inplace_power*/ + 0, /* nb_inplace_lshift*/ + 0, /* nb_inplace_rshift*/ + 0, /* nb_inplace_and*/ + 0, /* nb_inplace_xor*/ + 0, /* nb_inplace_or*/ + 0, /* nb_floor_divide */ + 0, /* nb_true_divide */ + 0, /* nb_inplace_floor_divide */ + 0, /* nb_inplace_true_divide */ + // 0, /* nb_index */ }; static PySequenceMethods matrix_as_sequence = { @@ -287,8 +284,9 @@ static PyMethodDef matrix_methods[] = { }; PyTypeObject PycairoMatrix_Type = { - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ + PyVarObject_HEAD_INIT(&PyType_Type, 0) + //PyObject_HEAD_INIT(NULL) + //0, /* ob_size */ "cairo.Matrix", /* tp_name */ sizeof(PycairoMatrix), /* tp_basicsize */ 0, /* tp_itemsize */ @@ -55,15 +55,16 @@ PycairoPath_FromPath (cairo_path_t *path) { } static void -path_dealloc(PycairoPath *p) { +path_dealloc(PycairoPath *o) { #ifdef DEBUG printf("path_dealloc start\n"); #endif - if (p->path) { - cairo_path_destroy(p->path); - p->path = NULL; + if (o->path) { + cairo_path_destroy(o->path); + o->path = NULL; } - p->ob_type->tp_free((PyObject *)p); + //o->ob_type->tp_free((PyObject *)o); + Py_TYPE(o)->tp_free(o); #ifdef DEBUG printf("path_dealloc end\n"); #endif @@ -102,7 +103,7 @@ path_str(PycairoPath *p) { case CAIRO_PATH_MOVE_TO: PyOS_snprintf(buf, sizeof(buf), "move_to %f %f", data[1].point.x, data[1].point.y); - s = PyString_FromString(buf); + s = PyUnicode_FromString(buf); if (!s) goto Done; ret = PyList_Append(pieces, s); @@ -114,7 +115,7 @@ path_str(PycairoPath *p) { case CAIRO_PATH_LINE_TO: PyOS_snprintf(buf, sizeof(buf), "line_to %f %f", data[1].point.x, data[1].point.y); - s = PyString_FromString(buf); + s = PyUnicode_FromString(buf); if (!s) goto Done; ret = PyList_Append(pieces, s); @@ -128,7 +129,7 @@ path_str(PycairoPath *p) { data[1].point.x, data[1].point.y, data[2].point.x, data[2].point.y, data[3].point.x, data[3].point.y); - s = PyString_FromString(buf); + s = PyUnicode_FromString(buf); if (!s) goto Done; ret = PyList_Append(pieces, s); @@ -138,7 +139,7 @@ path_str(PycairoPath *p) { break; case CAIRO_PATH_CLOSE_PATH: - s = PyString_FromString("close path"); + s = PyUnicode_FromString("close path"); if (!s) goto Done; ret = PyList_Append(pieces, s); @@ -149,10 +150,10 @@ path_str(PycairoPath *p) { } } /* result = "\n".join(pieces) */ - s = PyString_FromString("\n"); + s = PyUnicode_FromString("\n"); if (s == NULL) goto Done; - result = _PyString_Join(s, pieces); + result = PyUnicode_Join(s, pieces); Py_DECREF(s); Done: @@ -164,8 +165,9 @@ static PyObject * path_iter(PyObject *seq); /* forward declaration */ PyTypeObject PycairoPath_Type = { - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ + PyVarObject_HEAD_INIT(&PyType_Type, 0) + //PyObject_HEAD_INIT(NULL) + //0, /* ob_size */ "cairo.Path", /* tp_name */ sizeof(PycairoPath), /* tp_basicsize */ 0, /* tp_itemsize */ @@ -284,8 +286,9 @@ pathiter_next(PycairoPathiter *it) { } PyTypeObject PycairoPathiter_Type = { - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ + PyVarObject_HEAD_INIT(&PyType_Type, 0) + //PyObject_HEAD_INIT(NULL) + //0, /* ob_size */ "cairo.Pathiter", /* tp_name */ sizeof(PycairoPathiter), /* tp_basicsize */ 0, /* tp_itemsize */ diff --git a/src/pattern.c b/src/pattern.c index 0676114..61ae1c5 100644 --- a/src/pattern.c +++ b/src/pattern.c @@ -90,7 +90,8 @@ pattern_dealloc (PycairoPattern *o) { } Py_CLEAR(o->base); - o->ob_type->tp_free((PyObject *)o); + //o->ob_type->tp_free((PyObject *)o); + Py_TYPE(o)->tp_free(o); } static PyObject * @@ -102,7 +103,7 @@ pattern_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { static PyObject * pattern_get_extend (PycairoPattern *o) { - return PyInt_FromLong (cairo_pattern_get_extend (o->pattern)); + return PyLong_FromLong (cairo_pattern_get_extend (o->pattern)); } static PyObject * @@ -152,8 +153,9 @@ static PyMethodDef pattern_methods[] = { }; PyTypeObject PycairoPattern_Type = { - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ + PyVarObject_HEAD_INIT(&PyType_Type, 0) + //PyObject_HEAD_INIT(NULL) + //0, /* ob_size */ "cairo.Pattern", /* tp_name */ sizeof(PycairoPattern), /* tp_basicsize */ 0, /* tp_itemsize */ @@ -221,8 +223,9 @@ static PyMethodDef solid_pattern_methods[] = { }; PyTypeObject PycairoSolidPattern_Type = { - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ + PyVarObject_HEAD_INIT(&PyType_Type, 0) + //PyObject_HEAD_INIT(NULL) + //0, /* ob_size */ "cairo.SolidPattern", /* tp_name */ sizeof(PycairoSolidPattern), /* tp_basicsize */ 0, /* tp_itemsize */ @@ -279,7 +282,7 @@ surface_pattern_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { static PyObject * surface_pattern_get_filter (PycairoSurfacePattern *o) { - return PyInt_FromLong (cairo_pattern_get_filter (o->pattern)); + return PyLong_FromLong (cairo_pattern_get_filter (o->pattern)); } static PyObject * @@ -313,8 +316,9 @@ static PyMethodDef surface_pattern_methods[] = { }; PyTypeObject PycairoSurfacePattern_Type = { - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ + PyVarObject_HEAD_INIT(&PyType_Type, 0) + //PyObject_HEAD_INIT(NULL) + //0, /* ob_size */ "cairo.SurfacePattern", /* tp_name */ sizeof(PycairoSurfacePattern), /* tp_basicsize */ 0, /* tp_itemsize */ @@ -398,8 +402,9 @@ static PyMethodDef gradient_methods[] = { }; PyTypeObject PycairoGradient_Type = { - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ + PyVarObject_HEAD_INIT(&PyType_Type, 0) + //PyObject_HEAD_INIT(NULL) + //0, /* ob_size */ "cairo.Gradient", /* tp_name */ sizeof(PycairoGradient), /* tp_basicsize */ 0, /* tp_itemsize */ @@ -468,8 +473,9 @@ static PyMethodDef linear_gradient_methods[] = { }; PyTypeObject PycairoLinearGradient_Type = { - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ + PyVarObject_HEAD_INIT(&PyType_Type, 0) + //PyObject_HEAD_INIT(NULL) + //0, /* ob_size */ "cairo.LinearGradient", /* tp_name */ sizeof(PycairoLinearGradient), /* tp_basicsize */ 0, /* tp_itemsize */ @@ -540,8 +546,9 @@ static PyMethodDef radial_gradient_methods[] = { }; PyTypeObject PycairoRadialGradient_Type = { - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ + PyVarObject_HEAD_INIT(&PyType_Type, 0) + //PyObject_HEAD_INIT(NULL) + //0, /* ob_size */ "cairo.RadialGradient", /* tp_name */ sizeof(PycairoRadialGradient), /* tp_basicsize */ 0, /* tp_itemsize */ diff --git a/src/surface.c b/src/surface.c index 2e19f14..50480df 100644 --- a/src/surface.c +++ b/src/surface.c @@ -117,7 +117,9 @@ PycairoSurface_FromSurface (cairo_surface_t *surface, PyObject *base) { static cairo_status_t _write_func (void *closure, const unsigned char *data, unsigned int length) { PyGILState_STATE gstate = PyGILState_Ensure(); - PyObject *res = PyObject_CallMethod ((PyObject *)closure, "write", "(s#)", + // PyObject *res = PyObject_CallMethod ((PyObject *)closure, "write", "(s#)", + // data, (Py_ssize_t)length); + PyObject *res = PyObject_CallMethod ((PyObject *)closure, "write", "(y#)", data, (Py_ssize_t)length); if (res == NULL) { /* an exception has occurred, it will be picked up later by @@ -139,7 +141,8 @@ surface_dealloc (PycairoSurface *o) { } Py_CLEAR(o->base); - o->ob_type->tp_free((PyObject *)o); + //o->ob_type->tp_free((PyObject *)o); + Py_TYPE(o)->tp_free(o); } static PyObject * @@ -190,7 +193,7 @@ surface_flush (PycairoSurface *o) { static PyObject * surface_get_content (PycairoSurface *o) { - return PyInt_FromLong (cairo_surface_get_content (o->surface)); + return PyLong_FromLong (cairo_surface_get_content (o->surface)); } static PyObject * @@ -271,18 +274,27 @@ surface_show_page (PycairoSurface *o) { #ifdef CAIRO_HAS_PNG_FUNCTIONS /* METH_O */ static PyObject * -surface_write_to_png (PycairoSurface *o, PyObject *file) { +surface_write_to_png (PycairoSurface *o, PyObject *obj) { cairo_status_t status; - if (PyObject_TypeCheck (file, &PyString_Type)) { - /* string (filename) argument */ + if (PyObject_TypeCheck (obj, &PyUnicode_Type)) { + /* unicode (filename) argument */ + + PyObject *pyUTF8 = PyUnicode_AsUTF8String(obj); + if (pyUTF8 == NULL) + return NULL; + const char *utf8 = PyBytes_AS_STRING(pyUTF8); + if (utf8 == NULL) + return NULL; + Py_BEGIN_ALLOW_THREADS; - status = cairo_surface_write_to_png (o->surface, - PyString_AsString(file)); + status = cairo_surface_write_to_png (o->surface, utf8); Py_END_ALLOW_THREADS; + Py_XDECREF(pyUTF8); + } else { /* file or file-like object argument */ - PyObject* writer = PyObject_GetAttrString (file, "write"); + PyObject* writer = PyObject_GetAttrString (obj, "write"); if (writer == NULL || !PyCallable_Check (writer)) { Py_XDECREF(writer); PyErr_SetString(PyExc_TypeError, @@ -293,7 +305,7 @@ surface_write_to_png (PycairoSurface *o, PyObject *file) { Py_DECREF(writer); Py_BEGIN_ALLOW_THREADS; status = cairo_surface_write_to_png_stream (o->surface, _write_func, - file); + obj); Py_END_ALLOW_THREADS; } RETURN_NULL_IF_CAIRO_ERROR(status); @@ -334,8 +346,9 @@ static PyMethodDef surface_methods[] = { PyTypeObject PycairoSurface_Type = { - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ + PyVarObject_HEAD_INIT(&PyType_Type, 0) + //PyObject_HEAD_INIT(NULL) + //0, /* ob_size */ "cairo.Surface", /* tp_name */ sizeof(PycairoSurface), /* tp_basicsize */ 0, /* tp_itemsize */ @@ -404,6 +417,10 @@ image_surface_create_for_data (PyTypeObject *type, PyObject *args) { Py_ssize_t buffer_len; PyObject *obj; + // buffer function disabled + PyErr_SetString(PyExc_NotImplementedError, "Surface.create_for_data: Not Implemented yet."); + return NULL; + if (!PyArg_ParseTuple(args, "Oiii|i:Surface.create_for_data", &obj, &format, &width, &height, &stride)) return NULL; @@ -448,15 +465,15 @@ _read_func (void *closure, unsigned char *data, unsigned int length) { Py_ssize_t str_length; cairo_status_t status = CAIRO_STATUS_READ_ERROR; PyGILState_STATE gstate = PyGILState_Ensure(); - PyObject *pystr = PyObject_CallMethod ((PyObject *)closure, "read", "(i)", + PyObject *pyBytes = PyObject_CallMethod ((PyObject *)closure, "read", "(i)", length); - if (pystr == NULL) { + if (pyBytes == NULL) { /* an exception has occurred, it will be picked up later by * Pycairo_Check_Status() */ goto end; } - int ret = PyString_AsStringAndSize(pystr, &buffer, &str_length); + int ret = PyBytes_AsStringAndSize(pyBytes, &buffer, &str_length); if (ret == -1 || str_length < length) { goto end; } @@ -464,26 +481,38 @@ _read_func (void *closure, unsigned char *data, unsigned int length) { memcpy (data, buffer, str_length); status = CAIRO_STATUS_SUCCESS; end: - Py_XDECREF(pystr); + Py_XDECREF(pyBytes); PyGILState_Release(gstate); return status; } /* METH_O | METH_CLASS */ static PyObject * -image_surface_create_from_png (PyTypeObject *type, PyObject *file) { +image_surface_create_from_png (PyTypeObject *type, PyObject *obj) { PyObject* reader; cairo_surface_t *is; - if (PyObject_TypeCheck (file, &PyBaseString_Type)) { + // filename (str) + if (PyObject_TypeCheck (obj, &PyUnicode_Type)) { + + PyObject *pyUTF8 = PyUnicode_AsUTF8String(obj); + if (pyUTF8 == NULL) + return NULL; + const char *utf8 = PyBytes_AS_STRING(pyUTF8); + if (utf8 == NULL) + return NULL; + Py_BEGIN_ALLOW_THREADS; - is = cairo_image_surface_create_from_png (PyString_AsString(file)); + is = cairo_image_surface_create_from_png (utf8); Py_END_ALLOW_THREADS; + + Py_XDECREF(pyUTF8); + return PycairoSurface_FromSurface (is, NULL); } /* file or file-like object argument */ - reader = PyObject_GetAttrString (file, "read"); + reader = PyObject_GetAttrString (obj, "read"); if (reader == NULL || !PyCallable_Check (reader)) { Py_XDECREF(reader); PyErr_SetString(PyExc_TypeError, @@ -494,7 +523,7 @@ image_surface_create_from_png (PyTypeObject *type, PyObject *file) { Py_DECREF(reader); Py_BEGIN_ALLOW_THREADS; - is = cairo_image_surface_create_from_png_stream (_read_func, file); + is = cairo_image_surface_create_from_png_stream (_read_func, obj); Py_END_ALLOW_THREADS; return PycairoSurface_FromSurface (is, NULL); } @@ -507,36 +536,39 @@ image_surface_format_stride_for_width (PyObject *self, PyObject *args) { int width; if (!PyArg_ParseTuple(args, "ii:format_stride_for_width", &format, &width)) return NULL; - return PyInt_FromLong (cairo_format_stride_for_width (format, width)); + return PyLong_FromLong (cairo_format_stride_for_width (format, width)); } static PyObject * image_surface_get_data (PycairoImageSurface *o) { - return PyBuffer_FromReadWriteObject((PyObject *)o, 0, Py_END_OF_BUFFER); + PyErr_SetString(PyExc_NotImplementedError, "Surface.get_data: Not Implemented yet."); + return NULL; + // return PyBuffer_FromReadWriteObject((PyObject *)o, 0, Py_END_OF_BUFFER); } static PyObject * image_surface_get_format (PycairoImageSurface *o) { - return PyInt_FromLong (cairo_image_surface_get_format (o->surface)); + return PyLong_FromLong (cairo_image_surface_get_format (o->surface)); } static PyObject * image_surface_get_height (PycairoImageSurface *o) { - return PyInt_FromLong (cairo_image_surface_get_height (o->surface)); + return PyLong_FromLong (cairo_image_surface_get_height (o->surface)); } static PyObject * image_surface_get_stride (PycairoImageSurface *o) { - return PyInt_FromLong (cairo_image_surface_get_stride (o->surface)); + return PyLong_FromLong (cairo_image_surface_get_stride (o->surface)); } static PyObject * image_surface_get_width (PycairoImageSurface *o) { - return PyInt_FromLong (cairo_image_surface_get_width (o->surface)); + return PyLong_FromLong (cairo_image_surface_get_width (o->surface)); } /* Buffer interface functions, used by ImageSurface.get_data() */ +/* static int image_surface_buffer_getreadbuf (PycairoImageSurface *o, int segment, const void **ptr) { @@ -574,22 +606,25 @@ image_surface_buffer_getwritebuf (PycairoImageSurface *o, int segment, static int image_surface_buffer_getsegcount (PycairoImageSurface *o, int *lenp) { if (lenp) { - /* report the sum of the sizes (in bytes) of all segments */ + // report the sum of the sizes (in bytes) of all segments cairo_surface_t *surface = o->surface; int height = cairo_image_surface_get_height (surface); int stride = cairo_image_surface_get_stride (surface); *lenp = height * stride; } - return 1; /* surface data is all in one segment */ + return 1; // surface data is all in one segment } +*/ /* See Python C API Manual 10.7 */ +/* static PyBufferProcs image_surface_as_buffer = { (readbufferproc) image_surface_buffer_getreadbuf, (writebufferproc)image_surface_buffer_getwritebuf, (segcountproc) image_surface_buffer_getsegcount, (charbufferproc) NULL, }; +*/ static PyMethodDef image_surface_methods[] = { {"create_for_data",(PyCFunction)image_surface_create_for_data, @@ -611,8 +646,9 @@ static PyMethodDef image_surface_methods[] = { PyTypeObject PycairoImageSurface_Type = { - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ + PyVarObject_HEAD_INIT(&PyType_Type, 0) + // PyObject_HEAD_INIT(NULL) + //0, /* ob_size */ "cairo.ImageSurface", /* tp_name */ sizeof(PycairoImageSurface), /* tp_basicsize */ 0, /* tp_itemsize */ @@ -630,7 +666,8 @@ PyTypeObject PycairoImageSurface_Type = { 0, /* tp_str */ 0, /* tp_getattro */ 0, /* tp_setattro */ - &image_surface_as_buffer, /* tp_as_buffer */ + // &image_surface_as_buffer, /* tp_as_buffer */ + 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT, /* tp_flags */ 0, /* tp_doc */ 0, /* tp_traverse */ @@ -664,29 +701,39 @@ PyTypeObject PycairoImageSurface_Type = { static PyObject * pdf_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { double width_in_points, height_in_points; - PyObject *file, *writer; + PyObject *obj, *writer; cairo_surface_t *sfc; if (!PyArg_ParseTuple(args, "Odd:PDFSurface.__new__", - &file, &width_in_points, &height_in_points)) + &obj, &width_in_points, &height_in_points)) return NULL; - if (file == Py_None) { + if (obj == Py_None) { Py_BEGIN_ALLOW_THREADS; - sfc = cairo_pdf_surface_create (NULL, - width_in_points, height_in_points); + sfc = cairo_pdf_surface_create (NULL, width_in_points, height_in_points); Py_END_ALLOW_THREADS; return PycairoSurface_FromSurface (sfc, NULL); - }else if (PyObject_TypeCheck (file, &PyBaseString_Type)) { + + }else if (PyObject_TypeCheck (obj, &PyUnicode_Type)) { /* string (filename) argument */ + + PyObject *pyUTF8 = PyUnicode_AsUTF8String(obj); + if (pyUTF8 == NULL) + return NULL; + const char *utf8 = PyBytes_AS_STRING(pyUTF8); + if (utf8 == NULL) + return NULL; + Py_BEGIN_ALLOW_THREADS; - sfc = cairo_pdf_surface_create (PyString_AsString(file), - width_in_points, height_in_points); + sfc = cairo_pdf_surface_create (utf8, width_in_points, height_in_points); Py_END_ALLOW_THREADS; + + Py_XDECREF(pyUTF8); return PycairoSurface_FromSurface (sfc, NULL); } + /* file or file-like object argument */ - writer = PyObject_GetAttrString (file, "write"); + writer = PyObject_GetAttrString (obj, "write"); if (writer == NULL || !PyCallable_Check (writer)) { Py_XDECREF(writer); PyErr_SetString(PyExc_TypeError, @@ -701,10 +748,10 @@ pdf_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { Py_DECREF(writer); Py_BEGIN_ALLOW_THREADS; - sfc = cairo_pdf_surface_create_for_stream (_write_func, file, + sfc = cairo_pdf_surface_create_for_stream (_write_func, obj, width_in_points, height_in_points); Py_END_ALLOW_THREADS; - return PycairoSurface_FromSurface (sfc, file); + return PycairoSurface_FromSurface (sfc, obj); } static PyObject * @@ -725,8 +772,9 @@ static PyMethodDef pdf_surface_methods[] = { }; PyTypeObject PycairoPDFSurface_Type = { - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ + PyVarObject_HEAD_INIT(&PyType_Type, 0) + // PyObject_HEAD_INIT(NULL) + //0, /* ob_size */ "cairo.PDFSurface", /* tp_name */ sizeof(PycairoPDFSurface), /* tp_basicsize */ 0, /* tp_itemsize */ @@ -778,29 +826,38 @@ PyTypeObject PycairoPDFSurface_Type = { static PyObject * ps_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { double width_in_points, height_in_points; - PyObject *file, *writer; + PyObject *obj, *writer; cairo_surface_t *sfc; if (!PyArg_ParseTuple(args, "Odd:PSSurface.__new__", - &file, &width_in_points, &height_in_points)) + &obj, &width_in_points, &height_in_points)) return NULL; - if (file == Py_None) { + if (obj == Py_None) { Py_BEGIN_ALLOW_THREADS; - sfc = cairo_ps_surface_create (NULL, - width_in_points, height_in_points); + sfc = cairo_ps_surface_create (NULL, width_in_points, height_in_points); Py_END_ALLOW_THREADS; return PycairoSurface_FromSurface (sfc, NULL); - }else if (PyObject_TypeCheck (file, &PyBaseString_Type)) { + + }else if (PyObject_TypeCheck (obj, &PyUnicode_Type)) { /* string (filename) argument */ + + PyObject *pyUTF8 = PyUnicode_AsUTF8String(obj); + if (pyUTF8 == NULL) + return NULL; + const char *utf8 = PyBytes_AS_STRING(pyUTF8); + if (utf8 == NULL) + return NULL; + Py_BEGIN_ALLOW_THREADS; - sfc = cairo_ps_surface_create (PyString_AsString(file), - width_in_points, height_in_points); + sfc = cairo_ps_surface_create (utf8, width_in_points, height_in_points); Py_END_ALLOW_THREADS; + + Py_XDECREF(pyUTF8); return PycairoSurface_FromSurface (sfc, NULL); } /* else: file or file-like object argument */ - writer = PyObject_GetAttrString (file, "write"); + writer = PyObject_GetAttrString (obj, "write"); if (writer == NULL || !PyCallable_Check (writer)) { Py_XDECREF(writer); PyErr_SetString(PyExc_TypeError, @@ -815,10 +872,10 @@ ps_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { Py_DECREF(writer); Py_BEGIN_ALLOW_THREADS; - sfc = cairo_ps_surface_create_for_stream (_write_func, file, + sfc = cairo_ps_surface_create_for_stream (_write_func, obj, width_in_points, height_in_points); Py_END_ALLOW_THREADS; - return PycairoSurface_FromSurface (sfc, file); + return PycairoSurface_FromSurface (sfc, obj); } static PyObject * @@ -865,7 +922,8 @@ ps_surface_ps_level_to_string (PyObject *self, PyObject *args) { "invalid level argument"); return NULL; } - return PyString_FromString(s); + //return PyUnicode_FromString(s); + return PyUnicode_DecodeASCII(s, strlen(s), NULL); } static PyObject * @@ -918,8 +976,9 @@ static PyMethodDef ps_surface_methods[] = { }; PyTypeObject PycairoPSSurface_Type = { - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ + PyVarObject_HEAD_INIT(&PyType_Type, 0) + //PyObject_HEAD_INIT(NULL) + //0, /* ob_size */ "cairo.PSSurface", /* tp_name */ sizeof(PycairoPSSurface), /* tp_basicsize */ 0, /* tp_itemsize */ @@ -971,29 +1030,38 @@ PyTypeObject PycairoPSSurface_Type = { static PyObject * svg_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { double width_in_points, height_in_points; - PyObject *file, *writer; + PyObject *obj, *writer; cairo_surface_t *sfc; if (!PyArg_ParseTuple(args, "Odd:SVGSurface.__new__", - &file, &width_in_points, &height_in_points)) + &obj, &width_in_points, &height_in_points)) return NULL; - if (file == Py_None) { + if (obj == Py_None) { Py_BEGIN_ALLOW_THREADS; - sfc = cairo_svg_surface_create (NULL, - width_in_points, height_in_points); + sfc = cairo_svg_surface_create (NULL, width_in_points, height_in_points); Py_END_ALLOW_THREADS; return PycairoSurface_FromSurface (sfc, NULL); - }else if (PyObject_TypeCheck (file, &PyBaseString_Type)) { + + }else if (PyObject_TypeCheck (obj, &PyUnicode_Type)) { /* string (filename) argument */ + + PyObject *pyUTF8 = PyUnicode_AsUTF8String(obj); + if (pyUTF8 == NULL) + return NULL; + const char *utf8 = PyBytes_AS_STRING(pyUTF8); + if (utf8 == NULL) + return NULL; + Py_BEGIN_ALLOW_THREADS; - sfc = cairo_svg_surface_create (PyString_AsString(file), - width_in_points, height_in_points); + sfc = cairo_svg_surface_create (utf8, width_in_points, height_in_points); Py_END_ALLOW_THREADS; + + Py_XDECREF(pyUTF8); return PycairoSurface_FromSurface (sfc, NULL); } /* else: file or file-like object argument */ - writer = PyObject_GetAttrString (file, "write"); + writer = PyObject_GetAttrString (obj, "write"); if (writer == NULL || !PyCallable_Check (writer)) { Py_XDECREF(writer); PyErr_SetString(PyExc_TypeError, @@ -1008,10 +1076,10 @@ svg_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { Py_DECREF(writer); Py_BEGIN_ALLOW_THREADS; - sfc = cairo_svg_surface_create_for_stream (_write_func, file, + sfc = cairo_svg_surface_create_for_stream (_write_func, obj, width_in_points, height_in_points); Py_END_ALLOW_THREADS; - return PycairoSurface_FromSurface (sfc, file); + return PycairoSurface_FromSurface (sfc, obj); } static PyMethodDef svg_surface_methods[] = { @@ -1024,8 +1092,9 @@ static PyMethodDef svg_surface_methods[] = { }; PyTypeObject PycairoSVGSurface_Type = { - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ + PyVarObject_HEAD_INIT(&PyType_Type, 0) + //PyObject_HEAD_INIT(NULL) + //0, /* ob_size */ "cairo.SVGSurface", /* tp_name */ sizeof(PycairoSVGSurface), /* tp_basicsize */ 0, /* tp_itemsize */ @@ -1089,8 +1158,9 @@ static PyMethodDef win32_surface_methods[] = { }; PyTypeObject PycairoWin32Surface_Type = { - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ + PyVarObject_HEAD_INIT(&PyType_Type, 0) + //PyObject_HEAD_INIT(NULL) + //0, /* ob_size */ "cairo.Win32Surface", /* tp_name */ sizeof(PycairoWin32Surface), /* tp_basicsize */ 0, /* tp_itemsize */ @@ -1151,8 +1221,9 @@ static PyMethodDef win32_printing_surface_methods[] = { }; PyTypeObject PycairoWin32PrintingSurface_Type = { - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ + PyVarObject_HEAD_INIT(&PyType_Type, 0) + //PyObject_HEAD_INIT(NULL) + //0, /* ob_size */ "cairo.Win32PrintingSurface", /* tp_name */ sizeof(PycairoWin32PrintingSurface), /* tp_basicsize */ 0, /* tp_itemsize */ @@ -1212,12 +1283,14 @@ PyTypeObject PycairoWin32PrintingSurface_Type = { const void * xpyb2struct(PyObject *obj, Py_ssize_t *len) { - const void *data; + const void *data; - if (PyObject_AsReadBuffer(obj, &data, len) < 0) - return NULL; + // buffer function disabled + return NULL; + if (PyObject_AsReadBuffer(obj, &data, len) < 0) + return NULL; - return data; + return data; } static int @@ -1298,8 +1371,9 @@ static PyMethodDef xcb_surface_methods[] = { }; PyTypeObject PycairoXCBSurface_Type = { - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ + PyVarObject_HEAD_INIT(&PyType_Type, 0) + //PyObject_HEAD_INIT(NULL) + //0, /* ob_size */ "cairo.XCBSurface", /* tp_name */ sizeof(PycairoXCBSurface), /* tp_basicsize */ 0, /* tp_itemsize */ @@ -1357,17 +1431,17 @@ xlib_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { static PyObject * xlib_surface_get_depth (PycairoXlibSurface *o) { - return PyInt_FromLong (cairo_xlib_surface_get_depth (o->surface)); + return PyLong_FromLong (cairo_xlib_surface_get_depth (o->surface)); } static PyObject * xlib_surface_get_height (PycairoXlibSurface *o) { - return PyInt_FromLong (cairo_xlib_surface_get_height (o->surface)); + return PyLong_FromLong (cairo_xlib_surface_get_height (o->surface)); } static PyObject * xlib_surface_get_width (PycairoXlibSurface *o) { - return PyInt_FromLong (cairo_xlib_surface_get_width (o->surface)); + return PyLong_FromLong (cairo_xlib_surface_get_width (o->surface)); } static PyMethodDef xlib_surface_methods[] = { @@ -1378,8 +1452,9 @@ static PyMethodDef xlib_surface_methods[] = { }; PyTypeObject PycairoXlibSurface_Type = { - PyObject_HEAD_INIT(NULL) - 0, /* ob_size */ + PyVarObject_HEAD_INIT(&PyType_Type, 0) + //PyObject_HEAD_INIT(NULL) + //0, /* ob_size */ "cairo.XlibSurface", /* tp_name */ sizeof(PycairoXlibSurface), /* tp_basicsize */ 0, /* tp_itemsize */ diff --git a/src/wscript b/src/wscript index 41b89c7..7fdeffc 100644 --- a/src/wscript +++ b/src/wscript @@ -5,18 +5,18 @@ import os d = 'src' -def build(bld): - print(' %s/build' %d) +def build(ctx): + print(' %s/build()' %d) # .py files - bld.new_task_gen( - features = 'py', - source = '__init__.py', + ctx.new_task_gen( + features = 'py', + source = '__init__.py', install_path = '${PYTHONDIR}/cairo', ) # C extension module - bld.new_task_gen( + ctx.new_task_gen( features = 'cc cshlib pyext', source = 'cairomodule.c context.c font.c path.c pattern.c matrix.c surface.c', target = '_cairo', @@ -26,7 +26,7 @@ def build(bld): ) # C API - bld.install_files(os.path.join(bld.env['PREFIX'], 'include', 'pycairo'), + ctx.install_files(os.path.join(ctx.env['PREFIX'], 'include', 'pycairo'), 'py3cairo.h') # how to strip binaries ? diff --git a/test/isurface_create_for_data1.py b/test/isurface_create_for_data1.py index 867fde4..7ad5496 100755 --- a/test/isurface_create_for_data1.py +++ b/test/isurface_create_for_data1.py @@ -29,4 +29,4 @@ surface = cairo.ImageSurface.create_for_data(data, cairo.FORMAT_ARGB32, width, height) ctx = cairo.Context(surface) surface.write_to_png(fileName) -print "see %s output file" % fileName +print("see %s output file" % fileName) Binary files differ@@ -2,71 +2,96 @@ import os +top = '.' +out = 'build_directory' +d = top + APPNAME='pycairo' VERSION='1.8.10' cairo_version_required = '1.8.10' -srcdir = '.' -blddir = '../%s-build' % APPNAME -d = srcdir +def set_options(ctx): + print(' %s/set_options()' %d) + ctx.tool_options('compiler_cc') + ctx.tool_options('python') # options for disabling pyc or pyo compilation -def set_options(opt): - print(' %s/set_options' %d) - opt.tool_options('compiler_cc') - opt.tool_options('python') # options for disabling pyc or pyo compilation +def init(): # run at start of any waf invocation + print(' %s/init()' %d) -def init(): - print(' %s/init' %d) +def shutdown(): # run at end of any waf invocation + print(' %s/shutdown()' %d) -def configure(conf): - print(' %s/configure' %d) +def configure(ctx): + print(' %s/configure()' %d) - env = conf.env - conf.check_tool('misc') - conf.check_tool('compiler_cc') - conf.check_tool('python') - conf.check_python_version((2,6,0)) - conf.check_python_headers() - conf.check_cfg(package='cairo', atleast_version=cairo_version_required, - args='--cflags --libs') + env = ctx.env + ctx.check_tool('misc') + ctx.check_tool('compiler_cc') + ctx.check_tool('python') + ctx.check_python_version((3,1,0)) + ctx.check_python_headers() + ctx.check_cfg(package='cairo', atleast_version=cairo_version_required, + args='--cflags --libs') # add gcc options if env['CC_NAME'] == 'gcc': - for opt in ('-std=c99', '-Wall'): - if opt not in env['CCFLAGS']: - env.append_value('CCFLAGS', opt) + env.append_unique('CCFLAGS', ['-std=c99', '-Wall']) version = [int(s) for s in VERSION.split('.')] - conf.define('VERSION', VERSION) - conf.define('PYCAIRO_VERSION_MAJOR', version[0]) - conf.define('PYCAIRO_VERSION_MINOR', version[1]) - conf.define('PYCAIRO_VERSION_MICRO', version[2]) + ctx.define('VERSION', VERSION) + ctx.define('PYCAIRO_VERSION_MAJOR', version[0]) + ctx.define('PYCAIRO_VERSION_MINOR', version[1]) + ctx.define('PYCAIRO_VERSION_MICRO', version[2]) + + ctx.write_config_header('src/config.h') - conf.write_config_header('src/config.h') + import Options + print("%-40s : %s" % ('Prefix', Options.options.prefix)) -def build(bld): - print(' %s/build' %d) - bld.add_subdirs('src') +def build(ctx): + print(' %s/build()' %d) + ctx.add_subdirs('src') # generate and install the .pc file - obj = bld.new_task_gen('subst') + obj = ctx.new_task_gen('subst') obj.source = 'py3cairo.pc.in' obj.target = 'py3cairo.pc' obj.dict = { 'VERSION' : VERSION, - 'prefix' : bld.env['PREFIX'], - 'includedir': os.path.join(bld.env['PREFIX'], 'include'), + 'prefix' : ctx.env['PREFIX'], + 'includedir': os.path.join(ctx.env['PREFIX'], 'include'), } - obj.install_path = os.path.join(bld.env['PREFIX'], 'lib', 'pkgconfig') + obj.install_path = os.path.join(ctx.env['PREFIX'], 'lib', 'pkgconfig') + + +#def dist(): # create archives of project +# print(' %s/dist()' %d) +# dist is predefined + +def dist_hook(): + # remove unwanted files from the archive -def dist(): # create archives of project - print(' %s/dist' %d) + # individual files + for f in [ + 'RELEASING', + 'examples/cairo_snippets/c_to_python.py', + 'doc/html_docs_create.sh', + 'doc/html_docs_upload.sh', + ]: + os.remove(f) + # rm examples/*.{pdf,png,ps,svg} + D='examples' + for f in os.listdir(D): + if f.endswith(('.pdf', '.png', '.ps', '.svg')): + os.remove(os.path.join(D, f)) -def shutdown(): - print(' %s/shutdown' %d) + D='examples/cairo_snippets/snippets' + for f in os.listdir(D): + if f.endswith(('.pdf', '.png', '.ps', '.svg')): + os.remove(os.path.join(D, f)) |