From a3536927936be86113b0a86d86684dc97494f988 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Thu, 13 Sep 2007 16:37:05 +0200 Subject: [PATCH] support method slots specific to a Python version --- Cython/Compiler/Code.py | 5 ++++- Cython/Compiler/TypeSlots.py | 20 ++++++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/Cython/Compiler/Code.py b/Cython/Compiler/Code.py index 434d35eb..b2278849 100644 --- a/Cython/Compiler/Code.py +++ b/Cython/Compiler/Code.py @@ -88,7 +88,10 @@ class CCodeWriter: F = open(file).readlines() self.input_file_contents[file] = F return F - + + def get_py_version_hex(self, pyversion): + return "0x%02X%02X%02X%02X" % (tuple(pyversion) + (0,0,0,0))[:4] + def mark_pos(self, pos): file, line, col = pos contents = self.file_contents(file) diff --git a/Cython/Compiler/TypeSlots.py b/Cython/Compiler/TypeSlots.py index a6316dca..7e021b80 100644 --- a/Cython/Compiler/TypeSlots.py +++ b/Cython/Compiler/TypeSlots.py @@ -104,17 +104,23 @@ class SlotDescriptor: # slot_name string Member name of the slot in the type object # is_initialised_dynamically Is initialised by code in the module init function - def __init__(self, slot_name, dynamic = 0): + def __init__(self, slot_name, dynamic = 0, min_python_version = None): self.slot_name = slot_name self.is_initialised_dynamically = dynamic - + self.min_python_version = min_python_version + def generate(self, scope, code): if self.is_initialised_dynamically: value = 0 else: value = self.slot_code(scope) + if self.min_python_version is not None: + code.putln("#if PY_VERSION_HEX >= " + + code.get_py_version_hex(self.min_python_version)) code.putln("%s, /*%s*/" % (value, self.slot_name)) - + if self.min_python_version is not None: + code.putln("#endif") + # Some C implementations have trouble statically # initialising a global with a pointer to an extern # function, so we initialise some of the type slots @@ -175,8 +181,10 @@ class MethodSlot(SlotDescriptor): # method_name string The __xxx__ name of the method # default string or None Default value of the slot - def __init__(self, signature, slot_name, method_name, default = None): - SlotDescriptor.__init__(self, slot_name) + def __init__(self, signature, slot_name, method_name, + default = None, min_python_version = None): + SlotDescriptor.__init__(self, slot_name, + min_python_version = min_python_version) self.signature = signature self.slot_name = slot_name self.method_name = method_name @@ -493,7 +501,7 @@ PyNumberMethods = ( MethodSlot(ibinaryfunc, "nb_inplace_true_divide", "__itruediv__"), # Added in release 2.5 - MethodSlot(unaryfunc, "nb_index", "__index__"), + MethodSlot(unaryfunc, "nb_index", "__index__", min_python_version=(2,5)), ) PySequenceMethods = ( -- 2.26.2