|
9 | 9 | from packaging import version as packaging_version |
10 | 10 |
|
11 | 11 | from univers import arch |
| 12 | +from univers.config import config |
12 | 13 | from univers import debian |
13 | 14 | from univers import gem |
14 | 15 | from univers import gentoo |
@@ -85,17 +86,86 @@ class Version: |
85 | 86 |
|
86 | 87 | def __attrs_post_init__(self): |
87 | 88 | normalized_string = self.normalize(self.string) |
88 | | - if not self.is_valid(normalized_string): |
89 | | - raise InvalidVersion(f"{self.string!r} is not a valid {self.__class__!r}") |
| 89 | + |
| 90 | + # Skip validation if fallback is enabled |
| 91 | + if not config.use_libversion_fallback: |
| 92 | + if not self.is_valid(normalized_string): |
| 93 | + print("Validation - config id:", id(config), "value:", config.use_libversion_fallback) |
| 94 | + raise InvalidVersion(f"{self.string!r} is not a valid {self.__class__!r}") |
90 | 95 |
|
91 | 96 | # Set the normalized string as default value |
92 | | - |
93 | 97 | # Notes: setattr is used because this is an immutable frozen instance. |
94 | 98 | # See https://www.attrs.org/en/stable/init.html?#post-init |
95 | 99 | object.__setattr__(self, "normalized_string", normalized_string) |
96 | | - value = self.build_value(normalized_string) |
| 100 | + |
| 101 | + # Try to build value, but allow it to fail if fallback is enabled |
| 102 | + try: |
| 103 | + value = self.build_value(normalized_string) |
| 104 | + except Exception as e: |
| 105 | + if config.use_libversion_fallback: |
| 106 | + # Store the normalized string as value if building fails |
| 107 | + value = normalized_string |
| 108 | + else: |
| 109 | + raise |
| 110 | + |
97 | 111 | object.__setattr__(self, "value", value) |
98 | 112 |
|
| 113 | + def __init_subclass__(cls, **kwargs): |
| 114 | + """ |
| 115 | + Automatically wrap comparison methods in subclasses with fallback logic. |
| 116 | + """ |
| 117 | + super().__init_subclass__(**kwargs) |
| 118 | + |
| 119 | + comparison_methods = ['__lt__', '__le__', '__gt__', '__ge__', '__eq__', '__ne__'] |
| 120 | + |
| 121 | + for method_name in comparison_methods: |
| 122 | + # Only wrap if the method is defined in THIS specific class |
| 123 | + if method_name in cls.__dict__: |
| 124 | + original_method = cls.__dict__[method_name] |
| 125 | + wrapped = cls._wrap_comparison_method(original_method, method_name) |
| 126 | + setattr(cls, method_name, wrapped) |
| 127 | + |
| 128 | + @staticmethod |
| 129 | + def _wrap_comparison_method(original_method, method_name): |
| 130 | + """ |
| 131 | + Wrap a comparison method with fallback logic. |
| 132 | + |
| 133 | + Uses only standard library features (no external dependencies). |
| 134 | + """ |
| 135 | + def wrapper(self, other): |
| 136 | + try: |
| 137 | + # Try the original comparison method |
| 138 | + return original_method(self, other) |
| 139 | + except (ValueError, TypeError, AttributeError) as e: |
| 140 | + # If it fails and fallback is enabled, use libversion |
| 141 | + if config.use_libversion_fallback: |
| 142 | + try: |
| 143 | + import libversion |
| 144 | + result = libversion.version_compare2(str(self), str(other)) |
| 145 | + |
| 146 | + # Map libversion result to the appropriate comparison |
| 147 | + if method_name == '__lt__': |
| 148 | + return result < 0 |
| 149 | + elif method_name == '__le__': |
| 150 | + return result <= 0 |
| 151 | + elif method_name == '__gt__': |
| 152 | + return result > 0 |
| 153 | + elif method_name == '__ge__': |
| 154 | + return result >= 0 |
| 155 | + elif method_name == '__eq__': |
| 156 | + return result == 0 |
| 157 | + elif method_name == '__ne__': |
| 158 | + return result != 0 |
| 159 | + except Exception: |
| 160 | + # If fallback also fails, re-raise the original exception |
| 161 | + raise e |
| 162 | + # If fallback is disabled, re-raise the original exception |
| 163 | + raise |
| 164 | + |
| 165 | + # Preserve method name |
| 166 | + wrapper.__name__ = original_method.__name__ |
| 167 | + return wrapper |
| 168 | + |
99 | 169 | @classmethod |
100 | 170 | def is_valid(cls, string): |
101 | 171 | """ |
|
0 commit comments