Debian Codegraph @

Pyro5/callcontext.py

raw
1 """
2 Deals with the context variables of a Pyro call.
3
4 Pyro - Python Remote Objects. Copyright by Irmen de Jong ([email protected]).
5 """
6
7 import threading
8 from . import errors
9
10
11 class _CallContext(threading.local):
12 """call context thread local"""
13 def __init__(self):
14 # per-thread initialization
15 self.client = None
16 self.client_sock_addr = None
17 self.seq = 0
18 self.msg_flags = 0
19 self.serializer_id = 0
20 self.annotations = {}
21 self.response_annotations = {}
22 self.correlation_id = None
23
24 def to_global(self):
25 return dict(self.__dict__)
26
27 def from_global(self, values):
28 self.client = values["client"]
29 self.seq = values["seq"]
30 self.msg_flags = values["msg_flags"]
31 self.serializer_id = values["serializer_id"]
32 self.annotations = values["annotations"]
33 self.response_annotations = values["response_annotations"]
34 self.correlation_id = values["correlation_id"]
35 self.client_sock_addr = values["client_sock_addr"]
36
37 def track_resource(self, resource):
38 """keep a weak reference to the resource to be tracked for this connection"""
39 if self.client:
40 self.client.tracked_resources.add(resource)
41 else:
42 raise errors.PyroError("cannot track resource on a connectionless call")
43
44 def untrack_resource(self, resource):
45 """no longer track the resource for this connection"""
46 if self.client:
47 self.client.tracked_resources.discard(resource)
48 else:
49 raise errors.PyroError("cannot untrack resource on a connectionless call")
50
51
52 current_context = _CallContext()
53 """the thread-local context object for the current Pyro call"""