classification
Title: Use IntEnum and IntFlags in ssl module
Type: enhancement Stage: resolved
Components: Versions: Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: alex, christian.heimes, dstufft, ethan.furman, giampaolo.rodola, janssen, python-dev
Priority: normal Keywords: patch

Created on 2016-09-08 17:35 by christian.heimes, last changed 2017-03-07 18:49 by serhiy.storchaka. This issue is now closed.

Files
File name Uploaded Description Edit
ssl_enum.patch christian.heimes, 2016-09-08 17:35 review
convert-ssl-constants-to-enums.patch christian.heimes, 2016-09-08 18:13 review
Messages (10)
msg275076 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2016-09-08 17:35
The patch removes ssl._import_symbols and adds more enums to the ssl module. Please review the patch. I'll update documentation in the next step.

With IntFlags it is much easier to understand flags like SSLContext.options

>>> import ssl
>>> ctx = ssl.create_default_context()
>>> ssl.Options(ctx.options)
<Options.OP_ALL|OP_NO_SSLv3|OP_NO_SSLv2|OP_NO_COMPRESSION: 2197947391>
msg275096 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2016-09-08 18:13
Thanks Ethan! I have updated the patch with documentation and your fix.

Do you have an easy recipe to wrap properties like SSLContext.options to return an enum? It's not easy because super() does not support attribute proxy assignment. super().verify_mode = value raises an AttributeError.
msg275113 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2016-09-08 18:56
Can you give me a code sample?

Also, wouldn't

<Options.ALL|NO_SSLv3|NO_SSLv2|NO_COMPRESSION: 2197947391>

be more readable?  And keep the "OP_" names as aliases.
msg275119 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2016-09-08 19:15
I tried:

class SSLContext(_SSLContext):
    ...

    @property
    def options(self):
        return Options(super().options)

    @options.setter
    def options(self, value)
        super().options = value
    #          ^^^^^
    #          This fails with an AttributeError

    # it would be cool to have something like this:
    options = MagicEnumWrapper(Options)


About the shorter names, I'm worried that it might confuse people. The long names have been module constants for a long time.
msg275131 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2016-09-08 19:57
Huh.  Well, for property this works:

    @property
    def options(self):
        return Options(_SSLContext.options.__get__(self))

    @options.setter
    def options(self, value):
        _SSLContext.options.__set__(self, Options.OP_ALL)

Sure is ugly, though.

I think there's a PEP about making super() work with descriptors... Ah, PEP447 (not sure it's the same issue, though.)
msg275137 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2016-09-08 20:23
A little more research shows this is a problem with inheriting descriptors.
msg275138 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2016-09-08 20:28
See issue14965.
msg275146 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2016-09-08 20:43
Evidently the correct form is:

    super(SSLContext, SSLContext).options.__set__(self, value)

Not sure that's any better.  :(
msg275155 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2016-09-08 21:06
Too bad, but the ugly variant will suffice.

>>> import ssl
>>> ctx = ssl.create_default_context()
>>> ctx.options
<Options.OP_ALL|OP_NO_SSLv3|OP_NO_SSLv2|OP_NO_COMPRESSION: 2197947391>
>>> ctx.verify_flags
<VerifyFlags.VERIFY_X509_TRUSTED_FIRST: 32768>
>>> ctx.verify_mode
<VerifyMode.CERT_REQUIRED: 2>

Latest patch: https://github.com/tiran/cpython/commits/feature/ssl_enum
msg275472 - (view) Author: Roundup Robot (python-dev) Date: 2016-09-09 22:20
New changeset c32e9f9b00f7 by Christian Heimes in branch 'default':
Issue #28025: Convert all ssl module constants to IntEnum and IntFlags.
https://hg.python.org/cpython/rev/c32e9f9b00f7
History
Date User Action Args
2017-03-07 18:49:32serhiy.storchakasetstatus: pending -> closed
resolution: fixed
stage: commit review -> resolved
2016-09-09 22:20:29christian.heimessetstatus: open -> pending
stage: patch review -> commit review
2016-09-09 22:20:00python-devsetnosy: + python-dev
messages: + msg275472
2016-09-08 21:18:27hayposetnosy: - haypo
2016-09-08 21:06:59christian.heimessetmessages: + msg275155
2016-09-08 20:43:21ethan.furmansetmessages: + msg275146
2016-09-08 20:28:13ethan.furmansetmessages: + msg275138
2016-09-08 20:23:53ethan.furmansetmessages: + msg275137
2016-09-08 19:57:14ethan.furmansetmessages: + msg275131
2016-09-08 19:15:24christian.heimessetmessages: + msg275119
2016-09-08 18:56:29ethan.furmansetmessages: + msg275113
2016-09-08 18:13:51christian.heimessetfiles: + convert-ssl-constants-to-enums.patch

messages: + msg275096
2016-09-08 17:38:45ethan.furmansetnosy: + ethan.furman
2016-09-08 17:35:49christian.heimescreate