From 6cc61605a91c8ebf16318a07439a206118d81614 Mon Sep 17 00:00:00 2001 From: Lisandro Dalcin Date: Wed, 28 Apr 2010 16:18:43 -0300 Subject: [PATCH] introduce CYTHON_UNUSED macro to annotate functions and parametes - 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 | 9 +++++---- Cython/Compiler/Nodes.py | 28 ++++++++++++++++++++++++++-- tests/run/numpy_cimport.pyx | 7 +++++++ 3 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 tests/run/numpy_cimport.pyx diff --git a/Cython/Compiler/ModuleNode.py b/Cython/Compiler/ModuleNode.py index 641c0805..967c2af8 100644 --- a/Cython/Compiler/ModuleNode.py +++ b/Cython/Compiler/ModuleNode.py @@ -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 diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py index 00ac4463..ec8cc0d5 100644 --- a/Cython/Compiler/Nodes.py +++ b/Cython/Compiler/Nodes.py @@ -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 index 00000000..51ce5196 --- /dev/null +++ b/tests/run/numpy_cimport.pyx @@ -0,0 +1,7 @@ +""" +>>> import sys +>>> 'numpy' in sys.modules +True +""" +cimport numpy as np +include "numpy_common.pxi" -- 2.26.2