introduce CYTHON_UNUSED macro to annotate functions and parametes
authorLisandro Dalcin <dalcinl@gmail.com>
Wed, 28 Apr 2010 19:18:43 +0000 (16:18 -0300)
committerLisandro Dalcin <dalcinl@gmail.com>
Wed, 28 Apr 2010 19:18:43 +0000 (16:18 -0300)
- Silent compiler warnings about unused function/parametes
- Defined to __attribute__((__unused__)) for GCC(>3.4) and ICC
- Applied to a __{get|release}buffer__ special methods
- Applied to a couple of (self,unused) parameter pairs

Cython/Compiler/ModuleNode.py
Cython/Compiler/Nodes.py
tests/run/numpy_cimport.pyx [new file with mode: 0644]

index 641c080582d0bf2d2b9a83a0f4dc2c39f71ca97f..967c2af82897b4bf42263d23e1629561e99a2ebf 100644 (file)
@@ -1771,7 +1771,8 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
         if not Options.generate_cleanup_code:
             return
         code.globalstate.use_utility_code(register_cleanup_utility_code)
-        code.putln('static PyObject* %s(PyObject *self, PyObject *unused) {' % Naming.cleanup_cname)
+        code.putln('static PyObject *%s(CYTHON_UNUSED PyObject *self, CYTHON_UNUSED PyObject *unused) {' % 
+                   Naming.cleanup_cname)
         if Options.generate_cleanup_code >= 2:
             code.putln("/*--- Global cleanup code ---*/")
             rev_entries = list(env.var_entries)
@@ -2333,9 +2334,9 @@ bad:
 register_cleanup_utility_code = UtilityCode(
 proto = """
 static int __Pyx_RegisterCleanup(void); /*proto*/
-static PyObject* __pyx_module_cleanup(PyObject *self, PyObject *unused); /*proto*/
-static PyMethodDef cleanup_def = {__Pyx_NAMESTR("__cleanup"), (PyCFunction)&__pyx_module_cleanup, METH_NOARGS, 0};
-""",
+static PyObject* %(module_cleanup)s(CYTHON_UNUSED PyObject *self, CYTHON_UNUSED PyObject *unused); /*proto*/
+static PyMethodDef cleanup_def = {__Pyx_NAMESTR("__cleanup"), (PyCFunction)&%(module_cleanup)s, METH_NOARGS, 0};
+""" % {'module_cleanup': Naming.cleanup_cname},
 impl = """
 static int __Pyx_RegisterCleanup(void) {
     /* Don't use Py_AtExit because that has a 32-call limit 
index 00ac446326b46780f6201afbfb033de1720abc5e..ec8cc0d5e7c27846398ca970f9dbdbf63e6689ad 100644 (file)
@@ -1192,6 +1192,12 @@ class FuncDefNode(StatNode, BlockNode):
 
         is_getbuffer_slot = (self.entry.name == "__getbuffer__" and
                              self.entry.scope.is_c_class_scope)
+        is_releasebuffer_slot = (self.entry.name == "__releasebuffer__" and
+                                 self.entry.scope.is_c_class_scope)
+        is_buffer_slot = is_getbuffer_slot or is_releasebuffer_slot
+        if is_buffer_slot:
+            if 'cython_unused' not in self.modifiers:
+                self.modifiers = self.modifiers + ['cython_unused']
         
         profile = code.globalstate.directives['profile']
         if profile:
@@ -2089,14 +2095,16 @@ class DefNode(FuncDefNode):
                     arg_code_list.append(
                         arg.hdr_type.declaration_code(arg.hdr_cname))
         if not self.entry.is_special and sig.method_flags() == [TypeSlots.method_noargs]:
-            arg_code_list.append("PyObject *unused")
+            arg_code_list.append("CYTHON_UNUSED PyObject *unused")
         if sig.has_generic_args:
             arg_code_list.append(
                 "PyObject *%s, PyObject *%s"
                     % (Naming.args_cname, Naming.kwds_cname))
         arg_code = ", ".join(arg_code_list)
         dc = self.return_type.declaration_code(self.entry.func_cname)
-        header = "static %s(%s)" % (dc, arg_code)
+        mf = " ".join(self.modifiers).upper()
+        if mf: mf += " "
+        header = "static %s%s(%s)" % (mf, dc, arg_code)
         code.putln("%s; /*proto*/" % header)
         if proto_only:
             return
@@ -5033,6 +5041,7 @@ class FromImportStatNode(StatNode):
 
 utility_function_predeclarations = \
 """
+/* inline attribute */
 #ifndef CYTHON_INLINE
   #if defined(__GNUC__)
     #define CYTHON_INLINE __inline__
@@ -5045,6 +5054,21 @@ utility_function_predeclarations = \
   #endif
 #endif
 
+/* unused attribute */
+#ifndef CYTHON_UNUSED
+# if defined(__GNUC__)
+#   if !(defined(__cplusplus)) || (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
+#     define CYTHON_UNUSED __attribute__ ((__unused__)) 
+#   else
+#     define CYTHON_UNUSED
+#   endif
+# elif defined(__ICC) || defined(__INTEL_COMPILER)
+#   define CYTHON_UNUSED __attribute__ ((__unused__)) 
+# else
+#   define CYTHON_UNUSED 
+# endif
+#endif
+
 typedef struct {PyObject **p; char *s; const long n; const char* encoding; const char is_unicode; const char is_str; const char intern; } __Pyx_StringTabEntry; /*proto*/
 
 """
diff --git a/tests/run/numpy_cimport.pyx b/tests/run/numpy_cimport.pyx
new file mode 100644 (file)
index 0000000..51ce519
--- /dev/null
@@ -0,0 +1,7 @@
+"""
+>>> import sys
+>>> 'numpy' in sys.modules
+True
+"""
+cimport numpy as np
+include "numpy_common.pxi"