Skip to content

Commit 6c91563

Browse files
authored
Execute the source module w/in the app context (#76)
* Add failing test * Execute the source module w/in the app context * Update changelog
1 parent b029055 commit 6c91563

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Fixed
9+
- Execute the source module w/in the app context ([#76])
810

911
## [2.0.0] - 2020-07-01
1012
### Added
@@ -84,6 +86,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
8486
[1.0.1]: https://github.com/GoogleCloudPlatform/functions-framework-python/releases/tag/v1.0.1
8587
[1.0.0]: https://github.com/GoogleCloudPlatform/functions-framework-python/releases/tag/v1.0.0
8688

89+
[#76]: https://github.com/GoogleCloudPlatform/functions-framework-python/pull/76
8790
[#66]: https://github.com/GoogleCloudPlatform/functions-framework-python/pull/66
8891
[#61]: https://github.com/GoogleCloudPlatform/functions-framework-python/pull/61
8992
[#56]: https://github.com/GoogleCloudPlatform/functions-framework-python/pull/56

src/functions_framework/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,14 @@ def create_app(target=None, source=None, signature_type=None):
225225
# 4. Add the module to sys.modules
226226
sys.modules[name] = source_module
227227

228-
# 5. Execute the module
229-
spec.loader.exec_module(source_module)
230-
228+
# 5. Create the application
231229
app = flask.Flask(target, template_folder=template_folder)
232230
app.config["MAX_CONTENT_LENGTH"] = MAX_CONTENT_LENGTH
233231

232+
# 6. Execute the module, within the application context
233+
with app.app_context():
234+
spec.loader.exec_module(source_module)
235+
234236
# Extract the target function from the source file
235237
try:
236238
function = getattr(source_module, target)

tests/test_functions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,13 @@ def test_class_in_main_is_in_right_module():
308308
resp = client.get("/")
309309

310310
assert resp.status_code == 200
311+
312+
313+
def test_flask_current_app_is_available():
314+
source = TEST_FUNCTIONS_DIR / "flask_current_app" / "main.py"
315+
target = "function"
316+
317+
client = create_app(target, source).test_client()
318+
resp = client.get("/")
319+
320+
assert resp.status_code == 200
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Test that flask.current_app is importable and usable outside the function"""
16+
17+
from flask import current_app
18+
19+
with current_app.app_context():
20+
pass
21+
22+
23+
def function(request):
24+
return "OK"

0 commit comments

Comments
 (0)