6.2.4.4. Marc-Andre's Standard Macros

Comienzo C section to interscript/core/mx.h[1 /1 ]
     1: #line 2332 "mxTools.pak"
     2: #ifndef MX_H
     3: #define MX_H
     4: 
     5: /*
     6:   mx -- Marc's eXtension modules for Python: basic macros
     7: 
     8:   This file is only meant to be included by the extension modules.
     9:   DO NOT include it in the extension module's header file, since ixmap
    10:   will definitely cause troubles then.
    11: 
    12:   To enable debugging ceratin things, define one of these before
    13:   including this file:
    14: 
    15:   MAL_REF_DEBUG -- debug reference counts (Py_MY_xxx) [this file]
    16:   MAL_DEBUG     -- enable debug output (DPRINTF) [mxstdlib.h]
    17:   MAL_MEM_DEBUG -- enable malloc output (new,cnew,free,...) [mxstdlib.h]
    18: 
    19:   (c) Marc-Andre Lemburg; All Rights Reserved; mailto: mal@lemburg.com
    20:   See the documentation for further copyright information or contact
    21:   the author.
    22: 
    23: */
    24: 
    25: /* Include the generic mx header file */
    26: #include "mxh.h"
    27: 
    28: /* Add some platform specific symbols to enable work-arounds */
    29: #if defined(NeXT) || defined(sgi)
    30: # define BAD_STATIC_FORWARD
    31: #endif
    32: 
    33: /* Include all Python symbols & definitions */
    34: #include "Python.h"
    35: 
    36: /* Include other standard stuff */
    37: #include "mxstdlib.h"
    38: 
    39: /* --- Declare macros ----------------------------------------------------- */
    40: 
    41: #define Py_NONE (Py_INCREF(Py_None),Py_None)
    42: 
    43: #ifdef MAL_REF_DEBUG
    44: # define printref(x) printf("* refcount for "#x" = %i\n",(long) x->ob_refcnt);
    45: #else
    46: # define printref(x)
    47: #endif
    48: 
    49: /* --- Error handling ----------------------------------------------------- */
    50: 
    51: #define Py_Do(x) {if (!(x)) goto onError;}
    52: #define Py_ReturnOnError(errortype,errorstr) {PyErr_SetString(errortype,errorstr);return NULL;}
    53: 
    54: #define Py_Assert(x,errortype,errorstr) {if (!(x)) {PyErr_SetString(errortype,errorstr);goto onError;}}
    55: #define Py_AssertWithArg(x,errortype,errorstr,a1) {if (!(x)) {PyErr_Format(errortype,errorstr,a1);goto onError;}}
    56: #define Py_AssertWith2Args(x,errortype,errorstr,a1,a2) {if (!(x)) {PyErr_Format(errortype,errorstr,a1,a2);goto onError;}}
    57: #define Py_AssertWith3Args(x,errortype,errorstr,a1,a2,a3) {if (!(x)) {PyErr_Format(errortype,errorstr,a1,a2,a3);goto onError;}}
    58: 
    59: #define Py_Error(errortype,errorstr) {PyErr_SetString(errortype,errorstr);goto onError;}
    60: #define Py_ErrorWithArg(errortype,errorstr,a1) {PyErr_Format(errortype,errorstr,a1);goto onError;}
    61: #define Py_ErrorWith2Args(errortype,errorstr,a1,a2) {PyErr_Format(errortype,errorstr,a1,a2);goto onError;}
    62: #define Py_ErrorWith3Args(errortype,errorstr,a1,a2,a3) {PyErr_Format(errortype,errorstr,a1,a2,a3);goto onError;}
    63: 
    64: /* --- Reference counting ------------------------------------------------- */
    65: 
    66: #ifdef MAL_REF_DEBUG
    67: # define Py_MY_INCREF(x) Py_INCREF(x);printf("* refcount for "#x" = %i (after INCREF at %s:%i)\n",(long) x->ob_refcnt, __FILE__,__LINE__);
    68: # define Py_MY_DECREF(x) Py_DECREF(x);printf("* refcount for "#x" = %i (after DECREF at %s:%i)\n",(long) x->ob_refcnt, __FILE__,__LINE__);
    69: # define Py_MY_DELETE(x) if ((long) x->ob_refcnt > 1) printf("* Warning: refcount for "#x" = %i (NOT deleted in %s:%i)\n",(long) x->ob_refcnt-1, __FILE__,__LINE__);Py_DECREF(x);
    70: # define Py_MY_REFCOUNT(x) printf("* refcount for "#x" = %i (REFCOUNT at %s:%i)\n",(long) x->ob_refcnt, __FILE__,__LINE__);
    71: #else
    72: # define Py_MY_INCREF(x) Py_INCREF(x);
    73: # define Py_MY_DECREF(x) Py_DECREF(x);
    74: # define Py_MY_DELETE(x) Py_DECREF(x);
    75: # define Py_MY_REFCOUNT(x)
    76: #endif
    77: 
    78: #define Py_PRINT_REFCOUNT(x) printf("* refcount for "#x" = %i (REFCOUNT at %s:%i)\n",(long) x->ob_refcnt, __FILE__,__LINE__);
    79: #define Py_DEC_REF(x) Py_XDECREF(x); x=0; /* doing this once too often doesn't hurt */
    80: 
    81: /* --- Argument passing and checking -------------------------------------- */
    82: 
    83: /* No arguments expected; also use Py_MethodListEntryNoArgs() for this
    84:    kind of fct */
    85: #define Py_NoArgsCheck() {if (!PyArg_NoArgs(args)) goto onError;}
    86: 
    87: /* For functions with old style args (Py_MethodListEntrySingleArg) */
    88: #define Py_GetArgObject(a) {a = args; if (!a) {PyErr_SetString(PyExc_TypeError,"function/method requires an argument"); goto onError;}}
    89: #define Py_GetSingleArg(format,a1) {if (!PyArg_Parse(args,format,&a1)) goto onError;}
    90: 
    91: /* For functions with new style args: */
    92: #define Py_GetArg(format,a1) {if (!PyArg_ParseTuple(args,format,&a1)) goto onError;}
    93: #define Py_Get2Args(format,a1,a2) {if (!PyArg_ParseTuple(args,format,&a1,&a2)) goto onError;}
    94: #define Py_Get3Args(format,a1,a2,a3) {if (!PyArg_ParseTuple(args,format,&a1,&a2,&a3)) goto onError;}
    95: #define Py_Get4Args(format,a1,a2,a3,a4) {if (!PyArg_ParseTuple(args,format,&a1,&a2,&a3,&a4)) goto onError;}
    96: #define Py_Get5Args(format,a1,a2,a3,a4,a5) {if (!PyArg_ParseTuple(args,format,&a1,&a2,&a3,&a4,&a5)) goto onError;}
    97: #define Py_Get6Args(format,a1,a2,a3,a4,a5,a6) {if (!PyArg_ParseTuple(args,format,&a1,&a2,&a3,&a4,&a5,&a6)) goto onError;}
    98: #define Py_Get7Args(format,a1,a2,a3,a4,a5,a6,a7) {if (!PyArg_ParseTuple(args,format,&a1,&a2,&a3,&a4,&a5,&a6,&a7)) goto onError;}
    99: #define Py_Get8Args(format,a1,a2,a3,a4,a5,a6,a7,a8) {if (!PyArg_ParseTuple(args,format,&a1,&a2,&a3,&a4,&a5,&a6,&a7,&a8)) goto onError;}
   100: 
   101: /* For functions with keywords -- the first macro parameter must be
   102:    the keywords array given as e.g.
   103: 
   104:    static char *keywords[] = {"first","second","third", 0};
   105: 
   106:    with an entry for every argument (in the correct order). The
   107:    functions must be included in the method list using
   108:    Py_MethodWithKeywordsListEntry() and be declared as
   109:    Py_C_Function_WithKeywords(). */
   110: #define Py_KeywordGetArg(keywords,format,a1) {if (!PyArg_ParseTupleAndKeywords(args,kw,format,keywords,&a1)) goto onError;}
   111: #define Py_KeywordGet2Args(keywords,format,a1,a2) {if (!PyArg_ParseTupleAndKeywords(args,kw,format,keywords,&a1,&a2)) goto onError;}
   112: #define Py_KeywordGet3Args(keywords,format,a1,a2,a3) {if (!PyArg_ParseTupleAndKeywords(args,kw,format,keywords,&a1,&a2,&a3)) goto onError;}
   113: #define Py_KeywordGet4Args(keywords,format,a1,a2,a3,a4) {if (!PyArg_ParseTupleAndKeywords(args,kw,format,keywords,&a1,&a2,&a3,&a4)) goto onError;}
   114: #define Py_KeywordGet5Args(keywords,format,a1,a2,a3,a4,a5) {if (!PyArg_ParseTupleAndKeywords(args,kw,format,keywords,&a1,&a2,&a3,&a4,&a5)) goto onError;}
   115: #define Py_KeywordGet6Args(keywords,format,a1,a2,a3,a4,a5,a6) {if (!PyArg_ParseTupleAndKeywords(args,kw,format,keywords,&a1,&a2,&a3,&a4,&a5,&a6)) goto onError;}
   116: #define Py_KeywordGet7Args(keywords,format,a1,a2,a3,a4,a5,a6,a7) {if (!PyArg_ParseTupleAndKeywords(args,kw,format,keywords,&a1,&a2,&a3,&a4,&a5,&a6,&a7)) goto onError;}
   117: #define Py_KeywordGet8Args(keywords,format,a1,a2,a3,a4,a5,a6,a7,a8) {if (!PyArg_ParseTupleAndKeywords(args,kw,format,keywords,&a1,&a2,&a3,&a4,&a5,&a6,&a7,&a8)) goto onError;}
   118: 
   119: /* --- Returning values to Python ----------------------------------------- */
   120: 
   121: /* XXX don't always work: every time you have an 'O' in the BuildValue format
   122:        string, you need to DECREF the variable *after* the tuple has been
   123:        built !!!
   124: */
   125: 
   126: #define Py_ReturnNone() {Py_MY_INCREF(Py_None);return Py_None;}
   127: #define Py_ReturnTrue() {Py_MY_INCREF(Py_True);return Py_True;}
   128: #define Py_ReturnFalse() {Py_MY_INCREF(Py_False);return Py_False;}
   129: #define Py_ReturnArg(format,a1) return Py_BuildValue(format,a1);
   130: #define Py_Return Py_ReturnArg
   131: #define Py_Return2Args(format,a1,a2) return Py_BuildValue(format,a1,a2);
   132: #define Py_Return2 Py_Return2Args
   133: #define Py_Return3Args(format,a1,a2,a3) return Py_BuildValue(format,a1,a2,a3);
   134: #define Py_Return3 Py_Return3Args
   135: #define Py_Return4Args(format,a1,a2,a3) return Py_BuildValue(format,a1,a2,a3,a4);
   136: #define Py_Return5Args(format,a1,a2,a3) return Py_BuildValue(format,a1,a2,a3,a4,a5);
   137: #define Py_Return6Args(format,a1,a2,a3) return Py_BuildValue(format,a1,a2,a3,a4,a5,a6);
   138: #define Py_Return7Args(format,a1,a2,a3) return Py_BuildValue(format,a1,a2,a3,a4,a5,a6,a7);
   139: 
   140: /* Build values */
   141: 
   142: #define Py_BuildNone() Py_NONE
   143: #define Py_Build(format,x) Py_BuildValue(format,x)
   144: #define Py_Build2(format,x,y) Py_BuildValue(format,x,y)
   145: #define Py_Build3(format,x,y,z) Py_BuildValue(format,x,y,z)
   146: 
   147: /* --- Declaring Python builtin functions/methods ------------------------- */
   148: 
   149: /* Declare C function/method fct, having docstring docstr; may use vargargs */
   150: #define Py_C_Function(fct,docstr) \
   151:         static char fct##_docstring[] = docstr;\
   152:         static PyObject *fct(PyObject *self, PyObject *args)
   153: 
   154: /* Declare C function/method fct, having keywords keywordsarray and a
   155:    docstring docstr; may use vargargs & keywords */
   156: #define Py_C_Function_WithKeywords(fct,docstr) \
   157:         static char fct##_docstring[] = docstr;\
   158:         static PyObject *fct(PyObject *self, PyObject *args, PyObject *kw)
   159: 
   160: /* these declare: self -- instance pointer for methods, NULL for functions
   161:                   args -- argument tuple
   162:                   kw   -- keywords dict (if applicable)
   163:    plus as statics:
   164:                   <function name>_docstring -- the docstring as given
   165:                   <function name>_keywords  -- the keyword array as given
   166: 
   167:    note: use the Py_GetArg macros for functions without keywords,
   168:              and Py_KeywordGetArg macros for functions with keywords
   169: */
   170: 
   171: /* --- Method list entries for builtin functions/methods ------------------ */
   172: 
   173: /* Add a C function/method cname to the module dict as pyname; no
   174:    doc-string */
   175: #define Py_MethodListEntryAny(pyname,cname) {pyname,(PyCFunction)cname,METH_VARARGS}
   176: 
   177: /* Add a C function/method cname to the module dict as pyname; the
   178:    function can use varargs */
   179: #define Py_MethodListEntry(pyname,cname) {pyname,(PyCFunction)cname,METH_VARARGS,cname##_docstring}
   180: 
   181: /* Add a C function/method cname to the module dict as pyname; the
   182:    function takes no args */
   183: #define Py_MethodListEntryNoArgs(pyname,cname) {pyname,(PyCFunction)cname,0,cname##_docstring}
   184: 
   185: /* Add a C function/method cname to the module dict as pyname; the
   186:    function takes one argument: the object is passed in directly
   187:    (without wrapping it into a tuple first), i.e. don't use
   188:    the Py_GetArg-macros or PyArg_ParseTuple(). */
   189: #define Py_MethodListEntrySingleArg(pyname,cname) {pyname,(PyCFunction)cname,0,cname##_docstring}
   190: 
   191: /* Add a C function/method that uses keywords to the module dict */
   192: #define Py_MethodWithKeywordsListEntry(pyname,cname) {pyname,(PyCFunction)cname,METH_VARARGS | METH_KEYWORDS,cname##_docstring}
   193: 
   194: 
   195: /* --- Text macros -------------------------------------------------------- */
   196: 
   197: /* Check a given slice and apply the usual rules for negative indices */
   198: #define Py_CheckBufferSlice(textlen,start,stop) {       \
   199:             if (stop > textlen)                         \
   200:                 stop = textlen;                         \
   201:             else {                                      \
   202:                 if (stop < 0)                           \
   203:                     stop += textlen;                    \
   204:                 if (stop < 0)                           \
   205:                     stop = 0;                           \
   206:             }                                           \
   207:             if (start < 0) {                            \
   208:                 start += textlen;                       \
   209:                 if (start < 0)                          \
   210:                     start = 0;                          \
   211:             }                                           \
   212:             if (stop < start)                           \
   213:                 start = stop;                           \
   214:         }
   215: 
   216: /* Dito for string objects */
   217: #define Py_CheckSlice(textobj,start,stop) \
   218:         Py_CheckBufferSlice(PyString_GET_SIZE(textobj),start,stop)
   219: 
   220: /* This assumes that fixed is a constant char array; the strcmp
   221:    function is only called in case the attribute name length exceeds
   222:    10 characters and the first 10 characters match; optimizing
   223:    compilers should eliminate any unused parts of this comparison
   224:    automatically */
   225: #define Py_StringsCompareEqual(var,fixed)                               \
   226:      (var[0] == fixed[0] &&                                             \
   227:       (var[0] == 0 ||                                                   \
   228:        (sizeof(fixed) >= 1 && var[1] == fixed[1] &&                     \
   229:         (var[1] == 0 ||                                                 \
   230:          (sizeof(fixed) >= 2 && var[2] == fixed[2] &&                   \
   231:           (var[2] == 0 ||                                               \
   232:            (sizeof(fixed) >= 3 && var[3] == fixed[3] &&                 \
   233:             (var[3] == 0 ||                                             \
   234:              (sizeof(fixed) >= 4 && var[4] == fixed[4] &&               \
   235:               (var[4] == 0 ||                                           \
   236:                (sizeof(fixed) >= 5 && var[5] == fixed[5] &&             \
   237:                 (var[5] == 0 ||                                         \
   238:                  (sizeof(fixed) >= 6 && var[6] == fixed[6] &&           \
   239:                   (var[6] == 0 ||                                       \
   240:                    (sizeof(fixed) >= 7 && var[7] == fixed[7] &&         \
   241:                     (var[7] == 0 ||                                     \
   242:                      (sizeof(fixed) >= 8 && var[8] == fixed[8] &&       \
   243:                       (var[8] == 0 ||                                   \
   244:                        (sizeof(fixed) >= 9 && var[9] == fixed[9] &&     \
   245:                         (var[9] == 0 ||                                 \
   246:                          (sizeof(fixed) >= 10 &&                        \
   247:                           strcmp(&var[10],&fixed[10]) == 0              \
   248:                          )))))))))))))))))))))
   249: 
   250: /* --- Macros for getattr ------------------------------------------------- */
   251: 
   252: /* This assumes that name is a constant char array; the strcmp
   253:    function is only called in case the attribute name length exceeds
   254:    10 characters and the first 10 characters match */
   255: #define Py_WantAttr(var,name) Py_StringsCompareEqual(var,name)
   256: 
   257: /* --- Module init helpers ------------------------------------------------ */
   258: 
   259: /* Helper for startup type object initialization */
   260: 
   261: #define PyType_Init(x) x.ob_type = &PyType_Type
   262: 
   263: /* Error reporting for module init functions */
   264: 
   265: #define Py_ReportModuleInitError(modname) {                     \
   266:     PyObject *exc_type, *exc_value, *exc_tb;                    \
   267:     PyObject *str_type, *str_value;                             \
   268:                                                                 \
   269:     /* Fetch error objects and convert them to strings */       \
   270:     PyErr_Fetch(&exc_type, &exc_value, &exc_tb);                \
   271:     if (exc_type && exc_value) {                                \
   272:         str_type = PyObject_Str(exc_type);                      \
   273:         str_value = PyObject_Str(exc_value);                    \
   274:     }                                                           \
   275:     else {                                                      \
   276:         str_type = NULL;                                        \
   277:         str_value = NULL;                                       \
   278:     }                                                           \
   279:     /* Try to format a more informative error message using the \
   280:        original error */                                        \
   281:     if (str_type && str_value &&                                \
   282:         PyString_Check(str_type) && PyString_Check(str_value))  \
   283:         PyErr_Format(                                           \
   284:                 PyExc_ImportError,                              \
   285:                 "initialization of module "modname" failed "    \
   286:                 "(%s: %s)",                                     \
   287:                 PyString_AS_STRING(str_type),                   \
   288:                 PyString_AS_STRING(str_value));                 \
   289:     else                                                        \
   290:         PyErr_SetString(                                        \
   291:                 PyExc_ImportError,                              \
   292:                 "initialization of module "modname" failed ");  \
   293:     Py_XDECREF(str_type);                                       \
   294:     Py_XDECREF(str_value);                                      \
   295:     Py_XDECREF(exc_type);                                       \
   296:     Py_XDECREF(exc_value);                                      \
   297:     Py_XDECREF(exc_tb);                                         \
   298: }
   299: 
   300: /* --- SWIG addons -------------------------------------------------------- */
   301: 
   302: /* Throw this error after having set the correct Python exception
   303:    using e.g. PyErr_SetString(); */
   304: #define mxSWIGError "mxSWIGError"
   305: 
   306: /* EOF */
   307: #endif
   308: 
End C section to interscript/core/mx.h[1]