Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSLContext needs an argument #33771

Closed
fchapoton opened this issue Apr 29, 2022 · 22 comments
Closed

SSLContext needs an argument #33771

fchapoton opened this issue Apr 29, 2022 · 22 comments

Comments

@fchapoton
Copy link
Contributor

with python 3.10, one sees a deprecation

sage: oeis([5,21,84,330])
.../sage/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/sage/databases/oeis.py:203: DeprecationWarning: ssl.SSLContext() without protocol argument is deprecated.
  f = urlopen(url, context=SSLContext())
.../sage/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/sage/databases/oeis.py:203: DeprecationWarning: ssl.PROTOCOL_TLS is deprecated
  f = urlopen(url, context=SSLContext())
0: A002054: Binomial coefficient C(2n+1, n-1).
1: A289797: p-INVERT of the triangular numbers (A000217), where p(S) = 1 - S - S^2.
2: A284911: Number of partitions of n^2 that are the sum of n not necessarily distinct partitions of n into distinct parts.

CC: @tscrim @slel @kliem @kwankyu

Component: interfaces

Author: Frédéric Chapoton

Branch/Commit: 93e7f60

Reviewer: Thierry Monteil

Issue created by migration from https://trac.sagemath.org/ticket/33771

@fchapoton fchapoton added this to the sage-9.7 milestone Apr 29, 2022
@fchapoton
Copy link
Contributor Author

comment:1

and

git grep -c "SSLContext()" src/sage
src/sage/arith/misc.py:1
src/sage/combinat/designs/covering_design.py:1
src/sage/databases/oeis.py:1
src/sage/features/internet.py:1
src/sage/graphs/isgci.py:1
src/sage/interfaces/mathematica.py:1
src/sage/misc/messaging.py:1
src/sage/misc/package.py:1
src/sage/misc/remote_file.py:1

@fchapoton
Copy link
Contributor Author

comment:2

apparently

adding internet to --optional= shows that all context=SSLContext() ​should be replaced with context=ssl.create_default_context()

@sheerluck
Copy link
Contributor

Attachment: 33771.patch.gz

@sheerluck
Copy link
Contributor

comment:3

I tested 33771.patch​ only on python-3.10.4

@fchapoton
Copy link
Contributor Author

comment:4

thanks

are you sure that this does not work in previous Python versions ?

please make a branch

@fchapoton
Copy link
Contributor Author

comment:5
from ssl import create_default_context

works at least in Python 3.8.10

and also in python 3.5.2

@sheerluck
Copy link
Contributor

@sheerluck
Copy link
Contributor

Changed branch from u/gh-sheerluck/create_default_context to none

@sheerluck
Copy link
Contributor

comment:8
diff --git a/src/sage/arith/misc.py b/src/sage/arith/misc.py
index 9966d0b857..7e78b8a449 100644
--- a/src/sage/arith/misc.py
+++ b/src/sage/arith/misc.py
@@ -2033,7 +2033,7 @@ def xkcd(n=""):
     import contextlib
     import json
     from sage.misc.html import html
-    from ssl import SSLContext
+    from ssl import create_default_context as SSLContext
 
     from urllib.request import urlopen
     from urllib.error import HTTPError, URLError
diff --git a/src/sage/combinat/designs/covering_design.py b/src/sage/combinat/designs/covering_design.py
index a1959604a1..f6f6a6097c 100644
--- a/src/sage/combinat/designs/covering_design.py
+++ b/src/sage/combinat/designs/covering_design.py
@@ -46,7 +46,7 @@ Classes and methods
 # ****************************************************************************
 
 from urllib.request import urlopen
-from ssl import SSLContext
+from ssl import create_default_context as SSLContext
 
 from sage.misc.sage_eval import sage_eval
 from sage.structure.sage_object import SageObject
diff --git a/src/sage/databases/oeis.py b/src/sage/databases/oeis.py
index 00cbb30e6d..a551b3f102 100644
--- a/src/sage/databases/oeis.py
+++ b/src/sage/databases/oeis.py
@@ -160,7 +160,7 @@ Classes and methods
 # ****************************************************************************
 from urllib.request import urlopen
 from urllib.parse import urlencode
-from ssl import SSLContext
+from ssl import create_default_context as SSLContext
 
 from sage.structure.sage_object import SageObject
 from sage.structure.unique_representation import UniqueRepresentation
diff --git a/src/sage/features/internet.py b/src/sage/features/internet.py
index 5c6a077bcb..85b087cf18 100644
--- a/src/sage/features/internet.py
+++ b/src/sage/features/internet.py
@@ -40,7 +40,7 @@ class Internet(Feature):
         """
         import urllib.error
         from urllib.request import Request, urlopen
-        from ssl import SSLContext
+        from ssl import create_default_context as SSLContext
 
         req = Request("https://www.sagemath.org", headers={"User-Agent": "sage-doctest"})
         try:
diff --git a/src/sage/graphs/isgci.py b/src/sage/graphs/isgci.py
index 99f90f7617..f97a070a5b 100644
--- a/src/sage/graphs/isgci.py
+++ b/src/sage/graphs/isgci.py
@@ -400,7 +400,7 @@ from sage.env import GRAPHS_DATA_DIR
 import os
 import zipfile
 from urllib.request import urlopen
-from ssl import SSLContext
+from ssl import create_default_context as SSLContext
 
 
 #*****************************************************************************
diff --git a/src/sage/interfaces/mathematica.py b/src/sage/interfaces/mathematica.py
index 83c90418c4..6620d40c80 100644
--- a/src/sage/interfaces/mathematica.py
+++ b/src/sage/interfaces/mathematica.py
@@ -1140,7 +1140,7 @@ def request_wolfram_alpha(input, verbose=False):
     from urllib.request import Request, build_opener, HTTPCookieProcessor, HTTPSHandler
     import json
     from http.cookiejar import CookieJar
-    from ssl import SSLContext
+    from ssl import create_default_context as SSLContext
 
     # we need cookies for this...
     cj = CookieJar()
diff --git a/src/sage/misc/messaging.py b/src/sage/misc/messaging.py
index 1a4b248cf2..cf0ad289c8 100644
--- a/src/sage/misc/messaging.py
+++ b/src/sage/misc/messaging.py
@@ -14,7 +14,7 @@ AUTHORS:
 
 import http.client as httplib
 from urllib.parse import urlencode
-from ssl import SSLContext
+from ssl import create_default_context as SSLContext
 
 pushover_defaults = {"token": "Eql67F14ohOZJ0AtEBJJU7FiLAk8wK"}
 
diff --git a/src/sage/misc/package.py b/src/sage/misc/package.py
index 1ca56fbd5c..30b17c97be 100644
--- a/src/sage/misc/package.py
+++ b/src/sage/misc/package.py
@@ -51,7 +51,7 @@ import sys
 from pathlib import Path
 from urllib.request import urlopen
 from urllib.error import URLError
-from ssl import SSLContext
+from ssl import create_default_context as SSLContext
 
 DEFAULT_PYPI = 'https://pypi.org/pypi'
 
diff --git a/src/sage/misc/remote_file.py b/src/sage/misc/remote_file.py
index 092640cc9e..479480831b 100644
--- a/src/sage/misc/remote_file.py
+++ b/src/sage/misc/remote_file.py
@@ -3,7 +3,7 @@
 
 import os
 from urllib.request import Request, urlopen
-from ssl import SSLContext
+from ssl import create_default_context as SSLContext
 
 
 def get_remote_file(filename, verbose=True):
-- 
2.35.1

@fchapoton
Copy link
Contributor Author

comment:9

yes, looks better. Does it work ?

@sheerluck
Copy link
Contributor

comment:10

for python-3.10.4 -- yes :)

@sagetrac-tmonteil
Copy link
Mannequin

sagetrac-tmonteil mannequin commented Apr 30, 2022

comment:11

You shouldn't give a name of the ssl library for another object of the same library, this will be very confusing. I would suggest to just import create_default_context as is.

@sheerluck
Copy link
Contributor

comment:12

Replying to @sagetrac-tmonteil:

You shouldn't give a name of the ssl library for another object of the same library, this will be very confusing. I would suggest to just import create_default_context as is.

How about this then:

diff --git a/src/sage/arith/misc.py b/src/sage/arith/misc.py
index 9966d0b857..abfb4ce047 100644
--- a/src/sage/arith/misc.py
+++ b/src/sage/arith/misc.py
@@ -2033,7 +2033,7 @@ def xkcd(n=""):
     import contextlib
     import json
     from sage.misc.html import html
-    from ssl import SSLContext
+    import ssl
 
     from urllib.request import urlopen
     from urllib.error import HTTPError, URLError
@@ -2046,7 +2046,8 @@ def xkcd(n=""):
         url = "https://xkcd.com/{}/info.0.json".format(n)
 
     try:
-        with contextlib.closing(urlopen(url, context=SSLContext())) as f:
+        default_ctx = ssl.create_default_context()
+        with contextlib.closing(urlopen(url, context=default_ctx)) as f:
             data = f.read()
     except HTTPError as error:
         if error.getcode() == 400:  # this error occurs when asking for a non valid comic number
diff --git a/src/sage/combinat/designs/covering_design.py b/src/sage/combinat/designs/covering_design.py
index a1959604a1..5eeb07e7e2 100644
--- a/src/sage/combinat/designs/covering_design.py
+++ b/src/sage/combinat/designs/covering_design.py
@@ -46,7 +46,7 @@ Classes and methods
 # ****************************************************************************
 
 from urllib.request import urlopen
-from ssl import SSLContext
+import ssl
 
 from sage.misc.sage_eval import sage_eval
 from sage.structure.sage_object import SageObject
@@ -529,7 +529,8 @@ def best_known_covering_design_www(v, k, t, verbose=False):
     if verbose:
         print("Looking up the bounds at %s" % url)
 
-    f = urlopen(url, context=SSLContext())
+    default_ctx = ssl.create_default_context()
+    f = urlopen(url, context=default_ctx)
     try:
         s = bytes_to_str(f.read())
     finally:
diff --git a/src/sage/databases/oeis.py b/src/sage/databases/oeis.py
index 00cbb30e6d..13e82707ee 100644
--- a/src/sage/databases/oeis.py
+++ b/src/sage/databases/oeis.py
@@ -160,7 +160,6 @@ Classes and methods
 # ****************************************************************************
 from urllib.request import urlopen
 from urllib.parse import urlencode
-from ssl import SSLContext
 
 from sage.structure.sage_object import SageObject
 from sage.structure.unique_representation import UniqueRepresentation
@@ -176,6 +175,7 @@ from sage.repl.preparse import preparse
 
 from collections import defaultdict
 import re
+import ssl
 
 oeis_url = 'https://oeis.org/'
 
@@ -200,7 +200,8 @@ def _fetch(url):
     """
     try:
         verbose("Fetching URL %s ..." % url, caller_name='OEIS')
-        f = urlopen(url, context=SSLContext())
+        default_ctx = ssl.create_default_context()
+        f = urlopen(url, context=default_ctx)
         result = f.read()
         f.close()
         return bytes_to_str(result)
diff --git a/src/sage/features/internet.py b/src/sage/features/internet.py
index 5c6a077bcb..794edc11f3 100644
--- a/src/sage/features/internet.py
+++ b/src/sage/features/internet.py
@@ -38,13 +38,14 @@ class Internet(Feature):
             sage: Internet()._is_present()  # random, optional - internet
             FeatureTestResult('internet', True)
         """
+        import ssl
         import urllib.error
         from urllib.request import Request, urlopen
-        from ssl import SSLContext
 
         req = Request("https://www.sagemath.org", headers={"User-Agent": "sage-doctest"})
         try:
-            urlopen(req, timeout=1, context=SSLContext())
+            default_ctx = ssl.create_default_context()
+            urlopen(req, timeout=1, context=default_ctx)
             return FeatureTestResult(self, True)
         except urllib.error.URLError:
             return FeatureTestResult(self, False)
diff --git a/src/sage/graphs/isgci.py b/src/sage/graphs/isgci.py
index 99f90f7617..e9705cf488 100644
--- a/src/sage/graphs/isgci.py
+++ b/src/sage/graphs/isgci.py
@@ -398,9 +398,9 @@ from sage.misc.unknown import Unknown
 from sage.env import GRAPHS_DATA_DIR
 
 import os
+import ssl
 import zipfile
 from urllib.request import urlopen
-from ssl import SSLContext
 
 
 #*****************************************************************************
@@ -828,7 +828,8 @@ class GraphClasses(UniqueRepresentation):
             sage: graph_classes._download_db() # Not tested -- requires internet
         """
         from sage.misc.misc import SAGE_TMP
-        u = urlopen('https://www.graphclasses.org/data.zip', context=SSLContext())
+        default_ctx = ssl.create_default_context()
+        u = urlopen('https://www.graphclasses.org/data.zip', context=default_ctx)
         localFile = open(os.path.join(SAGE_TMP, 'isgci.zip'), 'w')
         localFile.write(u.read())
         localFile.close()
diff --git a/src/sage/interfaces/mathematica.py b/src/sage/interfaces/mathematica.py
index 83c90418c4..e77366c49a 100644
--- a/src/sage/interfaces/mathematica.py
+++ b/src/sage/interfaces/mathematica.py
@@ -1136,15 +1136,16 @@ def request_wolfram_alpha(input, verbose=False):
          'timing',
          'version']
     """
+    from http.cookiejar import CookieJar
     from urllib.parse import urlencode
     from urllib.request import Request, build_opener, HTTPCookieProcessor, HTTPSHandler
     import json
-    from http.cookiejar import CookieJar
-    from ssl import SSLContext
+    import ssl
 
     # we need cookies for this...
     cj = CookieJar()
-    opener = build_opener(HTTPCookieProcessor(cj), HTTPSHandler(context=SSLContext()))
+    default_ctx = ssl.create_default_context()
+    opener = build_opener(HTTPCookieProcessor(cj), HTTPSHandler(context=default_ctx))
     # build initial query for code
     req = Request("https://www.wolframalpha.com/input/api/v1/code")
     resp = opener.open(req)
diff --git a/src/sage/misc/messaging.py b/src/sage/misc/messaging.py
index 1a4b248cf2..eda7087416 100644
--- a/src/sage/misc/messaging.py
+++ b/src/sage/misc/messaging.py
@@ -12,9 +12,10 @@ AUTHORS:
 - Martin Albrecht (2012) - initial implementation
 """
 
+import ssl
 import http.client as httplib
 from urllib.parse import urlencode
-from ssl import SSLContext
+
 
 pushover_defaults = {"token": "Eql67F14ohOZJ0AtEBJJU7FiLAk8wK"}
 
@@ -77,7 +78,8 @@ def pushover(message, **kwds):
     request.update(pushover_defaults)
     request.update(kwds)
 
-    conn = httplib.HTTPSConnection("api.pushover.net:443", context=SSLContext())
+    default_ctx = ssl.create_default_context()
+    conn = httplib.HTTPSConnection("api.pushover.net:443", context=default_ctx)
     conn.request("POST", "/1/messages.json",
                  urlencode(request),
                  {"Content-type": "application/x-www-form-urlencoded"})
diff --git a/src/sage/misc/package.py b/src/sage/misc/package.py
index 1ca56fbd5c..a2f1dcd938 100644
--- a/src/sage/misc/package.py
+++ b/src/sage/misc/package.py
@@ -47,11 +47,11 @@ import sage.env
 import json
 import os
 import subprocess
+import ssl
 import sys
 from pathlib import Path
 from urllib.request import urlopen
 from urllib.error import URLError
-from ssl import SSLContext
 
 DEFAULT_PYPI = 'https://pypi.org/pypi'
 
@@ -110,7 +110,8 @@ def pip_remote_version(pkg, pypi_url=DEFAULT_PYPI, ignore_URLError=False):
     url = '{pypi_url}/{pkg}/json'.format(pypi_url=pypi_url, pkg=pkg)
 
     try:
-        f = urlopen(url, context=SSLContext())
+        default_ctx = ssl.create_default_context()
+        f = urlopen(url, context=default_ctx)
         text = f.read()
         f.close()
     except URLError:
diff --git a/src/sage/misc/remote_file.py b/src/sage/misc/remote_file.py
index 092640cc9e..296974ba5c 100644
--- a/src/sage/misc/remote_file.py
+++ b/src/sage/misc/remote_file.py
@@ -2,8 +2,8 @@
 
 
 import os
+import ssl
 from urllib.request import Request, urlopen
-from ssl import SSLContext
 
 
 def get_remote_file(filename, verbose=True):
@@ -43,7 +43,8 @@ def get_remote_file(filename, verbose=True):
     if verbose:
         print("Loading started")
 
-    content = urlopen(req, timeout=1, context=SSLContext())
+    default_ctx = ssl.create_default_context()
+    content = urlopen(req, timeout=1, context=default_ctx)
     with open(temp_name, 'wb') as f:
         f.write(content.read())
 

@fchapoton
Copy link
Contributor Author

Commit: 93e7f60

@fchapoton
Copy link
Contributor Author

comment:13

here is a branch, please review


New commits:

93e7f60no longer use SSLContext

@fchapoton
Copy link
Contributor Author

Author: Frédéric Chapoton

@fchapoton
Copy link
Contributor Author

Branch: u/chapoton/33771

@fchapoton
Copy link
Contributor Author

comment:14

simple one, I think

@sagetrac-tmonteil
Copy link
Mannequin

sagetrac-tmonteil mannequin commented May 7, 2022

comment:15

Looks good to me, let us wait for the patchbots to confirm.

@sagetrac-tmonteil
Copy link
Mannequin

sagetrac-tmonteil mannequin commented May 7, 2022

Reviewer: Thierry Monteil

@sagetrac-tmonteil
Copy link
Mannequin

sagetrac-tmonteil mannequin commented May 7, 2022

comment:16

Since patchbots are not running internet doctests by default, i ran them by hand.

@vbraun
Copy link
Member

vbraun commented May 22, 2022

Changed branch from u/chapoton/33771 to 93e7f60

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants