From 1718f1934c4fa709b42230b3ee5d6f803ec65df4 Mon Sep 17 00:00:00 2001 From: cslecrone Date: Fri, 31 May 2019 11:45:38 -0400 Subject: [PATCH] don't require ssl module for flask cli --- CHANGES.rst | 3 +++ flask/cli.py | 13 ++++++++++++- tests/test_cli.py | 6 ++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 6d1d6151..a727f947 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -68,6 +68,9 @@ Unreleased supported. :issue:`3214` - ``flask.testing.make_test_environ_builder()`` has been deprecated in favour of a new class ``flask.testing.EnvironBuilder``. :pr:`3232` +- The ``flask run`` command no longer fails if Python is not built + with SSL support. Using the ``--cert`` option will show an + appropriate error message. :issue:`3211` .. _#2935: https://github.com/pallets/flask/issues/2935 .. _#2957: https://github.com/pallets/flask/issues/2957 diff --git a/flask/cli.py b/flask/cli.py index 0e2aec6e..b20919da 100644 --- a/flask/cli.py +++ b/flask/cli.py @@ -16,7 +16,6 @@ import inspect import os import platform import re -import ssl import sys import traceback from functools import update_wrapper @@ -36,6 +35,11 @@ try: except ImportError: dotenv = None +try: + import ssl +except ImportError: + ssl = None + class NoAppException(click.UsageError): """Raised if an application cannot be found or loaded.""" @@ -684,6 +688,13 @@ class CertParamType(click.ParamType): self.path_type = click.Path(exists=True, dir_okay=False, resolve_path=True) def convert(self, value, param, ctx): + if ssl is None: + raise click.BadParameter( + 'Using "--cert" requires Python to be compiled with SSL support.', + ctx, + param, + ) + try: return self.path_type(value, param, ctx) except click.BadParameter: diff --git a/tests/test_cli.py b/tests/test_cli.py index 25d6afd8..97b989c2 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -611,6 +611,12 @@ def test_run_cert_import(monkeypatch): run_command.make_context("run", ["--cert", "ssl_context", "--key", __file__]) +def test_run_cert_no_ssl(monkeypatch): + monkeypatch.setattr("flask.cli.ssl", None) + with pytest.raises(click.BadParameter): + run_command.make_context("run", ["--cert", "not_here"]) + + def test_cli_blueprints(app): """Test blueprint commands register correctly to the application""" custom = Blueprint("custom", __name__, cli_group="customized")