1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- import os
15+ import sys
1616
1717import pytest
1818
1919try :
2020 import urllib .request as urllib
21- except :
21+ except ImportError :
2222 import urllib
2323
2424from testing_support .external_fixtures import cache_outgoing_headers , insert_incoming_headers
2929
3030from newrelic .api .background_task import background_task
3131
32+ # Since Python 3.3, `urllib.URLopener()` has been deprecated in favor of
33+ # `urllib.request.urlopen`. In Python 3.14, `urllib.URLopener()` will be
34+ # removed. `urllib.request.urlopen` corresponds to the old `urllib2.urlopen`
35+
36+ SKIP_IF_PYTHON_3_14_OR_ABOVE = pytest .mark .skipif (
37+ sys .version_info [0 :2 ] >= (3 , 14 ), reason = "urllib.URLopener() is removed in Python 3.14 and above"
38+ )
39+
3240
3341@pytest .fixture (scope = "session" )
3442def metrics (server ):
@@ -44,6 +52,7 @@ def metrics(server):
4452 return scoped , rollup
4553
4654
55+ @SKIP_IF_PYTHON_3_14_OR_ABOVE
4756def test_urlopener_http_request (server , metrics ):
4857 @validate_transaction_metrics (
4958 "test_urllib:test_urlopener_http_request" ,
@@ -59,6 +68,7 @@ def _test():
5968 _test ()
6069
6170
71+ @SKIP_IF_PYTHON_3_14_OR_ABOVE
6272def test_urlopener_https_request (server , metrics ):
6373 @validate_transaction_metrics (
6474 "test_urllib:test_urlopener_https_request" ,
@@ -77,6 +87,7 @@ def _test():
7787 _test ()
7888
7989
90+ @SKIP_IF_PYTHON_3_14_OR_ABOVE
8091def test_urlopener_http_request_with_port (server ):
8192 scoped = [(f"External/localhost:{ server .port } /urllib/" , 1 )]
8293
@@ -110,6 +121,7 @@ def _test():
110121]
111122
112123
124+ @SKIP_IF_PYTHON_3_14_OR_ABOVE
113125@validate_transaction_metrics (
114126 "test_urllib:test_urlopener_file_request" ,
115127 scoped_metrics = _test_urlopener_file_request_scoped_metrics ,
@@ -123,6 +135,7 @@ def test_urlopener_file_request():
123135 opener .open (file_uri )
124136
125137
138+ @SKIP_IF_PYTHON_3_14_OR_ABOVE
126139@background_task ()
127140@cache_outgoing_headers
128141@validate_cross_process_headers
@@ -131,6 +144,7 @@ def test_urlopener_cross_process_request(server):
131144 opener .open (f"http://localhost:{ server .port } /" )
132145
133146
147+ @SKIP_IF_PYTHON_3_14_OR_ABOVE
134148@cat_enabled
135149def test_urlopener_cross_process_response (server ):
136150 _test_urlopener_cross_process_response_scoped_metrics = [
0 commit comments