From c68759564e1d94c302c86340086be3fcbe59fff0 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Sat, 3 Aug 2024 14:01:39 -0400 Subject: [PATCH] [PY312] Add support for ssl.OP_LEGACY_SERVER_CONNECT (#2489) --- ChangeLog | 3 +++ astroid/brain/brain_ssl.py | 11 ++++++++--- tests/brain/test_ssl.py | 21 +++++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 7bcf764e0b..c9f5a26f81 100644 --- a/ChangeLog +++ b/ChangeLog @@ -19,6 +19,9 @@ Release date: TBA * Implement inference for JoinedStr and FormattedValue +* Add support for ``ssl.OP_LEGACY_SERVER_CONNECT`` (new in Python 3.12). + + Closes pylint-dev/pylint#9849 What's New in astroid 3.2.5? diff --git a/astroid/brain/brain_ssl.py b/astroid/brain/brain_ssl.py index 42018b5bfa..23d7ee4f73 100644 --- a/astroid/brain/brain_ssl.py +++ b/astroid/brain/brain_ssl.py @@ -6,7 +6,7 @@ from astroid import parse from astroid.brain.helpers import register_module_extender -from astroid.const import PY310_PLUS +from astroid.const import PY310_PLUS, PY312_PLUS from astroid.manager import AstroidManager @@ -42,13 +42,16 @@ class Options(_IntFlag): OP_NO_COMPRESSION = 11 OP_NO_TICKET = 12 OP_NO_RENEGOTIATION = 13 - OP_ENABLE_MIDDLEBOX_COMPAT = 14""" + OP_ENABLE_MIDDLEBOX_COMPAT = 14 + """ + if PY312_PLUS: + enum += "OP_LEGACY_SERVER_CONNECT = 15" return enum def ssl_transform(): return parse( - """ + f""" # Import necessary for conversion of objects defined in C into enums from enum import IntEnum as _IntEnum, IntFlag as _IntFlag @@ -71,6 +74,8 @@ def ssl_transform(): OP_NO_TLSv1, OP_NO_TLSv1_1, OP_NO_TLSv1_2, OP_SINGLE_DH_USE, OP_SINGLE_ECDH_USE) + {"from _ssl import OP_LEGACY_SERVER_CONNECT" if PY312_PLUS else ""} + from _ssl import (ALERT_DESCRIPTION_ACCESS_DENIED, ALERT_DESCRIPTION_BAD_CERTIFICATE, ALERT_DESCRIPTION_BAD_CERTIFICATE_HASH_VALUE, ALERT_DESCRIPTION_BAD_CERTIFICATE_STATUS_RESPONSE, diff --git a/tests/brain/test_ssl.py b/tests/brain/test_ssl.py index 798bebfb72..418c589953 100644 --- a/tests/brain/test_ssl.py +++ b/tests/brain/test_ssl.py @@ -4,7 +4,10 @@ """Tests for the ssl brain.""" +import pytest + from astroid import bases, nodes, parse +from astroid.const import PY312_PLUS def test_ssl_brain() -> None: @@ -41,3 +44,21 @@ def test_ssl_brain() -> None: inferred_cert_required = next(module.body[4].value.infer()) assert isinstance(inferred_cert_required, bases.Instance) assert inferred_cert_required._proxied.name == "CERT_REQUIRED" + + +@pytest.mark.skipif(not PY312_PLUS, reason="Uses new 3.12 constant") +def test_ssl_brain_py312() -> None: + """Test ssl brain transform.""" + module = parse( + """ + import ssl + ssl.OP_LEGACY_SERVER_CONNECT + ssl.Options.OP_LEGACY_SERVER_CONNECT + """ + ) + + inferred_constant = next(module.body[1].value.infer()) + assert isinstance(inferred_constant, nodes.Const) + + inferred_instance = next(module.body[2].value.infer()) + assert isinstance(inferred_instance, bases.Instance)