[PATCH 1 of 7] util.h: Utility macros for handling different Python APIs

Renato Cunha renatoc at gmail.com
Thu Jun 10 08:39:50 CDT 2010


# HG changeset patch
# User Renato Cunha <renatoc at gmail.com>
# Date 1276177147 10800
# Node ID 8530f003b636e75a64eb8b4c7bb283697f829791
# Parent  800a49bb0e54e19f577e3e1cecbc9d6b3bb470c9
util.h: Utility macros for handling different Python APIs.

The macros are defined according to their meaning in Python 3. So a call to
PyString_AsString is now substituted to a call to PYBYTES_AS_STRING, for
example.

The macros were only defined for types/functions actually used by hg. This file
structure is very simple, yet, but, hopefully, the calls in the C modules won't
need more changes. Except for API changes, perhaps.

diff --git a/mercurial/util.h b/mercurial/util.h
new file mode 100644
--- /dev/null
+++ b/mercurial/util.h
@@ -0,0 +1,48 @@
+/*
+ util.h - utility functions for interfacing with the various python APIs.
+
+ Copyright 2010 Renato Cunha
+
+ This software may be used and distributed according to the terms of
+ the GNU General Public License, incorporated herein by reference.
+*/
+
+#ifndef _HG_UTIL_H_
+#define _HG_UTIL_H_
+
+/*
+ Functions affected:
+
+   - PyBytes_AsString vs. PyString_AsString
+   - PyBytes_Size vs. PyString_size
+   - PyBytes_FromStringAndSize vs. PyString_FromStringAndSize
+   - PyBytes_AS_STRING vs. PyString_AS_STRING
+   - PyBytes_GET_SIZE vs. PyString_GET_SIZE
+   - PyBytes_FromString vs. PyString_FromString
+*/
+
+#if PY_MAJOR_VERSION >= 3
+
+#define IS_PY3K
+#define PYBYTES_SIZE PyBytes_Size
+#define PYINT_FROMLONG PyLong_FromLong
+#define PYBYTES_RESIZE _PyBytes_Resize
+#define PYBYTES_GET_SIZE PyBytes_GET_SIZE
+#define PYBYTES_AS_STRING PyBytes_AsString
+#define PYBYTES_FROM_STRING PyBytes_FromString
+#define PYBYTES_FROM_STRING_AND_SIZE PyBytes_FromStringAndSize
+
+#else // Python version < 3.0
+
+#define PYBYTES_SIZE PyString_Size
+#define PYINT_FROMLONG PyInt_FromLong
+#define PYBYTES_RESIZE _PyString_Resize
+#define PYBYTES_GET_SIZE PyString_GET_SIZE
+#define PYBYTES_AS_STRING PyString_AsString
+#define PYBYTES_FROM_STRING PyString_FromString
+#define PYBYTES_FROM_STRING_AND_SIZE PyString_FromStringAndSize
+
+#endif // PY_MAJOR_VERSION
+
+#endif // _HG_UTIL_H_
+


More information about the Mercurial-devel mailing list