@@ -56,23 +56,18 @@ def __init_subclass__(cls):
5656 f"that's incompatible with the existing default: "
5757 f"{ c .__module__ } .{ c .__qualname__ } " )
5858
59- def __new__ (cls , msgid , msgtype , controls = None , ** kwargs ):
60- if cls is not __class__ :
61- instance = super ().__new__ (cls )
62- instance .msgid = msgid
63- instance .msgtype = msgtype
64- instance .controls = controls
65- return instance
66-
67- c = __class__ .__subclasses .get (msgtype )
68- if c :
69- return c .__new__ (c , msgid , msgtype , controls , ** kwargs )
70-
71- instance = super ().__new__ (cls , ** kwargs )
72- instance .msgid = msgid
73- instance .msgtype = msgtype
74- instance .controls = controls
75- return instance
59+ def __init__ (self , msgid , msgtype , controls = None ):
60+ self .msgid = msgid
61+ self .msgtype = msgtype
62+ self .controls = controls
63+
64+ @classmethod
65+ def from_message (cls , msgid , msgtype , controls = None , ** kwargs ):
66+ c = cls .__subclasses .get (msgtype )
67+ if c and c is not cls :
68+ return c .from_message (msgid , msgtype , controls , ** kwargs )
69+
70+ return cls (msgid , msgtype , controls , ** kwargs )
7671
7772 def __repr__ (self ):
7873 optional = ""
@@ -92,16 +87,15 @@ class Result(Response):
9287 message : str
9388 referrals : Optional [list [str ]]
9489
95- def __new__ (cls , msgid , msgtype , controls = None , * ,
96- result , matcheddn , message , referrals , ** kwargs ):
97- instance = super ().__new__ (cls , msgid , msgtype , controls , ** kwargs )
98-
99- instance .result = result
100- instance .matcheddn = matcheddn
101- instance .message = message
102- instance .referrals = referrals
90+ def __init__ (self , msgid , msgtype , controls = None , * ,
91+ result : int , matcheddn : str , message : str ,
92+ referrals : Optional [list [str ]]):
93+ super ().__init__ (msgid , msgtype , controls )
10394
104- return instance
95+ self .result = result
96+ self .matcheddn = matcheddn
97+ self .message = message
98+ self .referrals = referrals
10599
106100 def raise_for_result (self ) -> 'Result' :
107101 if self .result in _SUCCESS_CODES :
@@ -131,14 +125,12 @@ class SearchEntry(Response):
131125 dn : str
132126 attrs : dict [str , Optional [list [bytes ]]]
133127
134- def __new__ ( cls , msgid , msgtype , controls = None , * ,
135- dn : str , attrs : dict [str , Optional [list [bytes ]]], ** kwargs ):
136- instance = super ().__new__ ( cls , msgid , msgtype , controls , ** kwargs )
128+ def __init__ ( self , msgid , msgtype , controls = None , * ,
129+ dn : str , attrs : dict [str , Optional [list [bytes ]]]):
130+ super ().__init__ ( msgid , msgtype , controls )
137131
138- instance .dn = dn
139- instance .attrs = attrs
140-
141- return instance
132+ self .dn = dn
133+ self .attrs = attrs
142134
143135 def __rich_repr__ (self ):
144136 yield from super ().__rich_repr__ ()
@@ -151,13 +143,10 @@ class SearchReference(Response):
151143
152144 referrals : list [str ]
153145
154- def __new__ (cls , msgid , msgtype , controls = None , * ,
155- referrals , ** kwargs ):
156- instance = super ().__new__ (cls , msgid , msgtype , controls , ** kwargs )
157-
158- instance .referrals = referrals
146+ def __init__ (self , msgid , msgtype , controls = None , * , referrals ):
147+ super ().__init__ (msgid , msgtype , controls )
159148
160- return instance
149+ self . referrals = referrals
161150
162151 def __rich_repr__ (self ):
163152 yield from super ().__rich_repr__ ()
@@ -174,28 +163,27 @@ class IntermediateResponse(Response):
174163 name : Optional [str ]
175164 value : Optional [bytes ]
176165
177- def __new__ (cls , msgid , msgtype , controls = None , * ,
178- name = None , value = None ,
179- defaultClass : Optional [type ['IntermediateResponse' ]] = None ,
180- ** kwargs ):
181- if cls is not __class__ :
182- instance = super ().__new__ (cls , msgid , msgtype , controls , ** kwargs )
183- instance .name = name
184- instance .value = value
185- return instance
186-
166+ def __init__ (self , msgid : int , msgtype : int ,
167+ controls : ResponseControl = None , * ,
168+ name : Optional [str ] = None , value : Optional [bytes ] = None ):
169+ super ().__init__ (msgid , msgtype , controls )
170+ self .name = name
171+ self .value = value
172+
173+ if hasattr (self , 'decode' ):
174+ self .decode (value )
175+
176+ @classmethod
177+ def from_message (cls , msgid , msgtype , controls = None , * ,
178+ name = None , value = None , defaultClass :
179+ Optional [type ['IntermediateResponse' ]] = None ,
180+ ** kwargs ):
187181 c = ldap .KNOWN_INTERMEDIATE_RESPONSES .get (name , defaultClass )
188- if c :
189- instance = c .__new__ (c , msgid , msgtype , controls ,
190- name = name , value = value , ** kwargs )
191- if hasattr (instance , 'decode' ):
192- instance .decode (value )
193- return instance
194-
195- instance = super ().__new__ (cls , msgid , msgtype , controls , ** kwargs )
196- instance .name = name
197- instance .value = value
198- return instance
182+ if c and c is not cls :
183+ return c .from_message (msgid , msgtype , controls ,
184+ name = name , value = value , ** kwargs )
185+
186+ return cls (msgid , msgtype , controls , name = name , value = value , ** kwargs )
199187
200188 def __repr__ (self ):
201189 optional = ""
@@ -219,6 +207,15 @@ class BindResult(Result):
219207
220208 credentials : Optional [bytes ]
221209
210+ def __init__ (self , msgid : int , msgtype : int ,
211+ controls : ResponseControl = None , * ,
212+ result , matcheddn , message , referrals ,
213+ credentials : Optional [bytes ] = None ):
214+ super ().__init__ (msgid , msgtype , controls , result = result ,
215+ matcheddn = matcheddn , message = message ,
216+ referrals = referrals )
217+ self .credentials = credentials
218+
222219 def __rich_repr__ (self ):
223220 yield from super ().__rich_repr__ ()
224221 yield "credentials" , self .credentials , None
@@ -257,35 +254,31 @@ class ExtendedResult(Result):
257254 name : Optional [str ]
258255 value : Optional [bytes ]
259256
260- def __new__ (cls , msgid , msgtype , controls = None , * ,
261- result , matcheddn , message , referrals ,
262- name = None , value = None ,
263- defaultClass : Optional [type ['ExtendedResult' ]] = None ,
264- ** kwargs ):
265- if cls is not __class__ :
266- instance = super ().__new__ (cls , msgid , msgtype , controls ,
267- result = result , matcheddn = matcheddn ,
268- message = message , referrals = referrals )
269- instance .name = name
270- instance .value = value
271- return instance
272-
257+ def __init__ (self , msgid : int , msgtype : int ,
258+ controls : ResponseControl = None , * ,
259+ result : int , matcheddn : str , message : str ,
260+ referrals : Optional [list [str ]],
261+ name : Optional [str ] = None , value : Optional [bytes ] = None ):
262+ super ().__init__ (msgid , msgtype , controls , result = result ,
263+ matcheddn = matcheddn , message = message ,
264+ referrals = referrals )
265+ self .name = name
266+ self .value = value
267+
268+ if hasattr (self , 'decode' ):
269+ self .decode (value )
270+
271+ @classmethod
272+ def from_message (cls , msgid , msgtype , controls = None , * ,
273+ name = None , value = None , defaultClass :
274+ Optional [type ['ExtendedResult' ]] = None ,
275+ ** kwargs ):
273276 c = ldap .KNOWN_EXTENDED_RESPONSES .get (name , defaultClass )
274- if not c and msgid == ldap .RES_UNSOLICITED :
275- c = UnsolicitedNotification
276-
277- if c :
278- return c .__new__ (c , msgid , msgtype , controls ,
279- result = result , matcheddn = matcheddn ,
280- message = message , referrals = referrals ,
281- name = name , value = value , ** kwargs )
282-
283- instance = super ().__new__ (cls , msgid , msgtype , controls ,
284- result = result , matcheddn = matcheddn ,
285- message = message , referrals = referrals )
286- instance .name = name
287- instance .value = value
288- return instance
277+ if c and c is not cls :
278+ return c .from_message (msgid , msgtype , controls ,
279+ name = name , value = value , ** kwargs )
280+
281+ return cls (msgid , msgtype , controls , name = name , value = value , ** kwargs )
289282
290283 def __repr__ (self ):
291284 optional = ""
0 commit comments