Consider whether expr is a comparison involving sys.platform. Return ALWAYS_TRUE, ALWAYS_FALSE, or TRUTH_VALUE_UNKNOWN.
(expr: Expression, platform: str)
| 224 | |
| 225 | |
| 226 | def consider_sys_platform(expr: Expression, platform: str) -> int: |
| 227 | """Consider whether expr is a comparison involving sys.platform. |
| 228 | |
| 229 | Return ALWAYS_TRUE, ALWAYS_FALSE, or TRUTH_VALUE_UNKNOWN. |
| 230 | """ |
| 231 | # Cases supported: |
| 232 | # - sys.platform == 'linux' |
| 233 | # - sys.platform != 'win32' |
| 234 | # - sys.platform.startswith('win') |
| 235 | if isinstance(expr, ComparisonExpr): |
| 236 | # Let's not yet support chained comparisons. |
| 237 | if len(expr.operators) > 1: |
| 238 | return TRUTH_VALUE_UNKNOWN |
| 239 | op = expr.operators[0] |
| 240 | if op not in ("==", "!="): |
| 241 | return TRUTH_VALUE_UNKNOWN |
| 242 | if not is_sys_attr(expr.operands[0], "platform"): |
| 243 | return TRUTH_VALUE_UNKNOWN |
| 244 | right = expr.operands[1] |
| 245 | if not isinstance(right, StrExpr): |
| 246 | return TRUTH_VALUE_UNKNOWN |
| 247 | return fixed_comparison(platform, op, right.value) |
| 248 | elif isinstance(expr, CallExpr): |
| 249 | if not isinstance(expr.callee, MemberExpr): |
| 250 | return TRUTH_VALUE_UNKNOWN |
| 251 | if len(expr.args) != 1 or not isinstance(expr.args[0], StrExpr): |
| 252 | return TRUTH_VALUE_UNKNOWN |
| 253 | if not is_sys_attr(expr.callee.expr, "platform"): |
| 254 | return TRUTH_VALUE_UNKNOWN |
| 255 | if expr.callee.name != "startswith": |
| 256 | return TRUTH_VALUE_UNKNOWN |
| 257 | if platform.startswith(expr.args[0].value): |
| 258 | return ALWAYS_TRUE |
| 259 | else: |
| 260 | return ALWAYS_FALSE |
| 261 | else: |
| 262 | return TRUTH_VALUE_UNKNOWN |
| 263 | |
| 264 | |
| 265 | Targ = TypeVar("Targ", int, str, tuple[int, ...]) |
no test coverage detected
searching dependent graphs…