1
1
# Copyright (c) Microsoft Corporation. All rights reserved.
2
2
# Licensed under the MIT License.
3
+ import os
3
4
import sys
4
5
import typing
5
6
6
7
from .. import protos
7
- from ..constants import BASE_EXT_SUPPORTED_PY_MINOR_VERSION
8
8
9
9
from . import datumdef
10
10
from . import generic
11
+
11
12
from .shared_memory_data_transfer import SharedMemoryManager
13
+ from ..constants import CUSTOMER_PACKAGES_PATH
12
14
13
15
PB_TYPE = 'rpc_data'
14
16
PB_TYPE_DATA = 'data'
15
17
PB_TYPE_RPC_SHARED_MEMORY = 'rpc_shared_memory'
16
- BINDING_REGISTRY = None
17
- DEFERRED_BINDINGS_REGISTRY = None
18
- DEFERRED_BINDINGS_ENABLED = False
19
- DEFERRED_BINDINGS_CACHE = {}
18
+ # Base extension supported Python minor version
19
+ BASE_EXT_SUPPORTED_PY_MINOR_VERSION = 8
20
+
21
+ binding_registry = None
22
+ deferred_binding_registry = None
23
+ deferred_bindings_enabled = False
24
+ deferred_bindings_cache = {}
20
25
21
26
22
27
def load_binding_registry () -> None :
@@ -26,33 +31,45 @@ def load_binding_registry() -> None:
26
31
if func is None :
27
32
import azure .functions as func
28
33
29
- global BINDING_REGISTRY
30
- BINDING_REGISTRY = func .get_binding_registry ()
34
+ global binding_registry
35
+ binding_registry = func .get_binding_registry ()
31
36
32
- if BINDING_REGISTRY is None :
33
- # If the BINDING_REGISTRY is None, azure-functions hasn't been
37
+ if binding_registry is None :
38
+ # If the binding_registry is None, azure-functions hasn't been
34
39
# loaded in properly.
35
- raise AttributeError ("BINDING_REGISTRY is None." )
40
+ raise AttributeError ('binding_registry is None. Sys Path:'
41
+ f'{ sys .path } , Sys Module: { sys .modules } ,'
42
+ f'python-packages Path exists:'
43
+ f'{ os .path .exists (CUSTOMER_PACKAGES_PATH )} ' )
36
44
37
45
# The base extension supports python 3.8+
38
46
if sys .version_info .minor >= BASE_EXT_SUPPORTED_PY_MINOR_VERSION :
39
47
# Import the base extension
40
48
try :
41
49
import azure .functions .extension .base as clients
42
- global DEFERRED_BINDINGS_REGISTRY
43
- DEFERRED_BINDINGS_REGISTRY = clients .get_binding_registry ()
50
+ global deferred_binding_registry
51
+ deferred_binding_registry = clients .get_binding_registry ()
44
52
except ImportError :
45
53
# This means that the customer hasn't imported the library.
46
54
# This isn't an error.
47
55
pass
48
56
49
57
58
+ def get_deferred_binding (bind_name : str ,
59
+ pytype : typing .Optional [type ] = None ) -> object :
60
+ # This will return None if not a supported type
61
+ return deferred_binding_registry .get (bind_name )\
62
+ if (deferred_binding_registry is not None
63
+ and deferred_binding_registry .check_supported_type (
64
+ pytype )) else None
65
+
66
+
50
67
def get_binding (bind_name : str , pytype : typing .Optional [type ] = None ) -> object :
51
68
# Check if binding is deferred binding
52
69
binding = get_deferred_binding (bind_name = bind_name , pytype = pytype )
53
70
# Binding is not a deferred binding type
54
71
if binding is None :
55
- binding = BINDING_REGISTRY .get (bind_name )
72
+ binding = binding_registry .get (bind_name )
56
73
# Binding is generic
57
74
if binding is None :
58
75
binding = generic .GenericBinding
@@ -117,8 +134,8 @@ def from_incoming_proto(
117
134
118
135
try :
119
136
# if the binding is an sdk type binding
120
- if (DEFERRED_BINDINGS_REGISTRY is not None
121
- and DEFERRED_BINDINGS_REGISTRY .check_supported_type (pytype )):
137
+ if (deferred_binding_registry is not None
138
+ and deferred_binding_registry .check_supported_type (pytype )):
122
139
return deferred_bindings_decode (binding = binding ,
123
140
pb = pb ,
124
141
pytype = pytype ,
@@ -217,55 +234,37 @@ def to_outgoing_param_binding(binding: str, obj: typing.Any, *,
217
234
data = rpc_val )
218
235
219
236
220
- def get_deferred_binding (bind_name : str ,
221
- pytype : typing .Optional [type ] = None ) -> object :
222
- binding = None
223
-
224
- # Checks if pytype is a supported sdk type
225
- if (DEFERRED_BINDINGS_REGISTRY is not None
226
- and DEFERRED_BINDINGS_REGISTRY .check_supported_type (pytype )):
227
- # Returns deferred binding converter
228
- binding = DEFERRED_BINDINGS_REGISTRY .get (bind_name )
229
-
230
- # This will return None if not a supported type
231
- return binding
232
-
233
-
234
237
def deferred_bindings_decode (binding : typing .Any ,
235
238
pb : protos .ParameterBinding , * ,
236
239
pytype : typing .Optional [type ],
237
240
datum : typing .Any ,
238
241
metadata : typing .Any ):
239
242
# This cache holds deferred binding types (ie. BlobClient, ContainerClient)
240
243
# That have already been created, so that the worker can reuse the
241
- # Previously created type without creating a new one. It allows these
242
- # Types to be singleton.
243
- global DEFERRED_BINDINGS_CACHE
244
+ # Previously created type without creating a new one.
245
+ global deferred_bindings_cache
244
246
245
- # Check if the type is already in the cache
246
- # If dict is empty or key doesn't exist, deferred_binding_type is None
247
- deferred_binding_type = DEFERRED_BINDINGS_CACHE .get ((pb .name , pytype ,
247
+ # If cache is empty or key doesn't exist, deferred_binding_type is None
248
+ deferred_binding_type = deferred_bindings_cache .get ((pb .name , pytype ,
248
249
datum .value .content ),
249
250
None )
250
251
251
- # If the type is in the cache, return it
252
252
if deferred_binding_type is not None :
253
253
return deferred_binding_type
254
- # Otherwise, create the specified type and add it to the cache
255
254
else :
256
255
deferred_binding_type = binding .decode (datum , trigger_metadata = metadata ,
257
256
pytype = pytype )
258
- DEFERRED_BINDINGS_CACHE [(pb .name , pytype , datum .value .content )]\
257
+ deferred_bindings_cache [(pb .name , pytype , datum .value .content )]\
259
258
= deferred_binding_type
260
259
return deferred_binding_type
261
260
262
261
263
262
def set_deferred_bindings_flag (param_anno : type ):
264
263
# If flag hasn't already been set
265
- # If DEFERRED_BINDINGS_REGISTRY is not None
264
+ # If deferred_binding_registry is not None
266
265
# If the binding type is a deferred binding type
267
- global DEFERRED_BINDINGS_ENABLED
268
- if (not DEFERRED_BINDINGS_ENABLED
269
- and DEFERRED_BINDINGS_REGISTRY is not None
270
- and DEFERRED_BINDINGS_REGISTRY .check_supported_type (param_anno )):
271
- DEFERRED_BINDINGS_ENABLED = True
266
+ global deferred_bindings_enabled
267
+ if (not deferred_bindings_enabled
268
+ and deferred_binding_registry is not None
269
+ and deferred_binding_registry .check_supported_type (param_anno )):
270
+ deferred_bindings_enabled = True
0 commit comments