D3955: mail: modernize check for Python-with-TLS
This revision was automatically updated to reflect the committed changes. Closed by commit rHG569d662816de: mail: modernize check for Python-with-TLS (authored by durin42, committed by ). REPOSITORY rHG Mercurial CHANGES SINCE LAST UPDATE https://phab.mercurial-scm.org/D3955?vs=10195=10247 REVISION DETAIL https://phab.mercurial-scm.org/D3955 AFFECTED FILES mercurial/mail.py CHANGE DETAILS diff --git a/mercurial/mail.py b/mercurial/mail.py --- a/mercurial/mail.py +++ b/mercurial/mail.py @@ -82,14 +82,23 @@ self.file = smtplib.SSLFakeFile(new_socket) return new_socket +def _pyhastls(): +"""Returns true iff Python has TLS support, false otherwise.""" +try: +import ssl +getattr(ssl, 'HAS_TLS', False) +return True +except ImportError: +return False + def _smtp(ui): '''build an smtp connection and return a function to send mail''' local_hostname = ui.config('smtp', 'local_hostname') tls = ui.config('smtp', 'tls') # backward compatible: when tls = true, we use starttls. starttls = tls == 'starttls' or stringutil.parsebool(tls) smtps = tls == 'smtps' -if (starttls or smtps) and not util.safehasattr(socket, 'ssl'): +if (starttls or smtps) and not _pyhastls(): raise error.Abort(_("can't use TLS: Python SSL support not installed")) mailhost = ui.config('smtp', 'host') if not mailhost: To: durin42, #hg-reviewers, indygreg Cc: indygreg, yuja, mercurial-devel ___ Mercurial-devel mailing list Mercurial-devel@mercurial-scm.org https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
D3955: mail: modernize check for Python-with-TLS
indygreg accepted this revision. indygreg added a comment. This revision is now accepted and ready to land. I think this utility function should live in `sslutil.py`. INLINE COMMENTS > mail.py:85-92 > +def _pyhastls(): > +"""Returns true iff Python has TLS support, false otherwise.""" > +try: > +import ssl > +getattr(ssl, 'HAS_TLS', False) > +return True > +except ImportError: We may want to consider moving this to `sslutil.py` and refactoring `sslutil.py` to not crash and burn if the `ssl` module doesn't work (which I'm pretty sure it will do today). REPOSITORY rHG Mercurial REVISION DETAIL https://phab.mercurial-scm.org/D3955 To: durin42, #hg-reviewers, indygreg Cc: indygreg, yuja, mercurial-devel ___ Mercurial-devel mailing list Mercurial-devel@mercurial-scm.org https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
D3955: mail: modernize check for Python-with-TLS
durin42 updated this revision to Diff 10195. REPOSITORY rHG Mercurial CHANGES SINCE LAST UPDATE https://phab.mercurial-scm.org/D3955?vs=10191=10195 REVISION DETAIL https://phab.mercurial-scm.org/D3955 AFFECTED FILES mercurial/mail.py CHANGE DETAILS diff --git a/mercurial/mail.py b/mercurial/mail.py --- a/mercurial/mail.py +++ b/mercurial/mail.py @@ -82,14 +82,23 @@ self.file = smtplib.SSLFakeFile(new_socket) return new_socket +def _pyhastls(): +"""Returns true iff Python has TLS support, false otherwise.""" +try: +import ssl +getattr(ssl, 'HAS_TLS', False) +return True +except ImportError: +return False + def _smtp(ui): '''build an smtp connection and return a function to send mail''' local_hostname = ui.config('smtp', 'local_hostname') tls = ui.config('smtp', 'tls') # backward compatible: when tls = true, we use starttls. starttls = tls == 'starttls' or stringutil.parsebool(tls) smtps = tls == 'smtps' -if (starttls or smtps) and not util.safehasattr(socket, 'ssl'): +if (starttls or smtps) and not _pyhastls(): raise error.Abort(_("can't use TLS: Python SSL support not installed")) mailhost = ui.config('smtp', 'host') if not mailhost: To: durin42, #hg-reviewers Cc: yuja, mercurial-devel ___ Mercurial-devel mailing list Mercurial-devel@mercurial-scm.org https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
D3955: mail: modernize check for Python-with-TLS
durin42 updated this revision to Diff 10191. REPOSITORY rHG Mercurial CHANGES SINCE LAST UPDATE https://phab.mercurial-scm.org/D3955?vs=9611=10191 REVISION DETAIL https://phab.mercurial-scm.org/D3955 AFFECTED FILES mercurial/mail.py CHANGE DETAILS diff --git a/mercurial/mail.py b/mercurial/mail.py --- a/mercurial/mail.py +++ b/mercurial/mail.py @@ -82,14 +82,23 @@ self.file = smtplib.SSLFakeFile(new_socket) return new_socket +def _pyhastls(): +"""Returns true iff Python has TLS support, false otherwise.""" +try: +import ssl +getattr(ssl, 'HAS_TLS', False) +return True +except ImportError: +return False + def _smtp(ui): '''build an smtp connection and return a function to send mail''' local_hostname = ui.config('smtp', 'local_hostname') tls = ui.config('smtp', 'tls') # backward compatible: when tls = true, we use starttls. starttls = tls == 'starttls' or stringutil.parsebool(tls) smtps = tls == 'smtps' -if (starttls or smtps) and not util.safehasattr(socket, 'ssl'): +if (starttls or smtps) and not _pyhastls(): raise error.Abort(_("can't use TLS: Python SSL support not installed")) mailhost = ui.config('smtp', 'host') if not mailhost: To: durin42, #hg-reviewers Cc: yuja, mercurial-devel ___ Mercurial-devel mailing list Mercurial-devel@mercurial-scm.org https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
D3955: mail: modernize check for Python-with-TLS
yuja added a comment. > > Maybe we can simply remove the check since we've dropped support for > > Python 2.5. > > No, we're looking to see if the ssl module is importable - if it's not, that means Python was compiled without TLS support (which is an option, even on 3.x and 2.7). I'm just sniffing for this particular attribute to force the import. That's true, but I don't think we've ever had support for Python 2.6+ without OpenSSL. Some commands would work thanks to our demandimport, but it also means some stdlib modules (e.g. httlib) would be broken because `import ssl` doesn't fail. REPOSITORY rHG Mercurial REVISION DETAIL https://phab.mercurial-scm.org/D3955 To: durin42, #hg-reviewers Cc: yuja, mercurial-devel ___ Mercurial-devel mailing list Mercurial-devel@mercurial-scm.org https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Re: D3955: mail: modernize check for Python-with-TLS
> > Maybe we can simply remove the check since we've dropped support for > > Python 2.5. > > No, we're looking to see if the ssl module is importable - if it's not, > that means Python was compiled without TLS support (which is an option, even > on 3.x and 2.7). I'm just sniffing for this particular attribute to force the > import. That's true, but I don't think we've ever had support for Python 2.6+ without OpenSSL. Some commands would work thanks to our demandimport, but it also means some stdlib modules (e.g. httlib) would be broken because `import ssl` doesn't fail. ___ Mercurial-devel mailing list Mercurial-devel@mercurial-scm.org https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
D3955: mail: modernize check for Python-with-TLS
durin42 added a comment. > Maybe we can simply remove the check since we've dropped support for > Python 2.5. No, we're looking to see if the ssl module is importable - if it's not, that means Python was compiled without TLS support (which is an option, even on 3.x and 2.7). I'm just sniffing for this particular attribute to force the import. Maybe I should add a comment? REPOSITORY rHG Mercurial REVISION DETAIL https://phab.mercurial-scm.org/D3955 To: durin42, #hg-reviewers Cc: yuja, mercurial-devel ___ Mercurial-devel mailing list Mercurial-devel@mercurial-scm.org https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Re: D3955: mail: modernize check for Python-with-TLS
> +def _pyhastls(): > +"""Returns true iff Python has TLS support, false otherwise.""" > +try: > +import ssl > +getattr(ssl, 'HAS_TLS', False) > +return True > +except ImportError: > +return False Maybe we can simply remove the check since we've dropped support for Python 2.5. ___ Mercurial-devel mailing list Mercurial-devel@mercurial-scm.org https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
D3955: mail: modernize check for Python-with-TLS
yuja added a comment. > +def _pyhastls(): > +"""Returns true iff Python has TLS support, false otherwise.""" > +try: > +import ssl > +getattr(ssl, 'HAS_TLS', False) > +return True > +except ImportError: > +return False Maybe we can simply remove the check since we've dropped support for Python 2.5. REPOSITORY rHG Mercurial REVISION DETAIL https://phab.mercurial-scm.org/D3955 To: durin42, #hg-reviewers Cc: yuja, mercurial-devel ___ Mercurial-devel mailing list Mercurial-devel@mercurial-scm.org https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
D3955: mail: modernize check for Python-with-TLS
durin42 created this revision. Herald added a subscriber: mercurial-devel. Herald added a reviewer: hg-reviewers. REVISION SUMMARY We used to be going indirectly through the socket module, but now we just check for the ssl module. REPOSITORY rHG Mercurial REVISION DETAIL https://phab.mercurial-scm.org/D3955 AFFECTED FILES mercurial/mail.py CHANGE DETAILS diff --git a/mercurial/mail.py b/mercurial/mail.py --- a/mercurial/mail.py +++ b/mercurial/mail.py @@ -82,14 +82,23 @@ self.file = smtplib.SSLFakeFile(new_socket) return new_socket +def _pyhastls(): +"""Returns true iff Python has TLS support, false otherwise.""" +try: +import ssl +getattr(ssl, 'HAS_TLS', False) +return True +except ImportError: +return False + def _smtp(ui): '''build an smtp connection and return a function to send mail''' local_hostname = ui.config('smtp', 'local_hostname') tls = ui.config('smtp', 'tls') # backward compatible: when tls = true, we use starttls. starttls = tls == 'starttls' or stringutil.parsebool(tls) smtps = tls == 'smtps' -if (starttls or smtps) and not util.safehasattr(socket, 'ssl'): +if (starttls or smtps) and not _pyhastls(): raise error.Abort(_("can't use TLS: Python SSL support not installed")) mailhost = ui.config('smtp', 'host') if not mailhost: To: durin42, #hg-reviewers Cc: mercurial-devel ___ Mercurial-devel mailing list Mercurial-devel@mercurial-scm.org https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel