Skip to content

Commit

Permalink
[PY312] Add support for ssl.OP_LEGACY_SERVER_CONNECT (#2489)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls authored Aug 3, 2024
1 parent ed4276b commit c687595
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
11 changes: 8 additions & 3 deletions astroid/brain/brain_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
21 changes: 21 additions & 0 deletions tests/brain/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)

0 comments on commit c687595

Please sign in to comment.