@@ -207,6 +207,8 @@ def setopt(self, option: CurlOpt, value: Any) -> int:
207
207
Returns:
208
208
0 if no error, see ``CurlECode``.
209
209
"""
210
+ if self ._curl is None :
211
+ return 0 # silently ignore if curl handle is None
210
212
input_option = {
211
213
# this should be int in curl, but cffi requires pointer for void*
212
214
# it will be convert back in the glue c code.
@@ -308,18 +310,28 @@ def getinfo(self, option: CurlInfo) -> Union[bytes, int, float, list]:
308
310
0x100000 : ffi .string ,
309
311
0x200000 : int ,
310
312
0x300000 : float ,
313
+ 0x400000 : list ,
311
314
0x500000 : int ,
312
315
0x600000 : int ,
313
316
}
314
- c_value = ffi .new (ret_option [option & 0xF00000 ])
317
+
318
+ option_type = option & 0xF00000
319
+
320
+ if self ._curl is None :
321
+ if option_type == 0x100000 :
322
+ return b""
323
+ return ret_cast_option [option_type ]()
324
+
325
+ c_value = ffi .new (ret_option [option_type ])
315
326
ret = lib .curl_easy_getinfo (self ._curl , option , c_value )
316
327
self ._check_error (ret , "getinfo" , option )
317
328
# cookielist and ssl_engines starts with 0x400000, see also: const.py
318
- if option & 0xF00000 == 0x400000 :
329
+ if option_type == 0x400000 :
319
330
return slist_to_list (c_value [0 ])
320
331
if c_value [0 ] == ffi .NULL :
321
332
return b""
322
- return ret_cast_option [option & 0xF00000 ](c_value [0 ])
333
+
334
+ return ret_cast_option [option_type ](c_value [0 ])
323
335
324
336
def version (self ) -> bytes :
325
337
"""Get the underlying libcurl version."""
@@ -335,6 +347,8 @@ def impersonate(self, target: str, default_headers: bool = True) -> int:
335
347
Returns:
336
348
0 if no error.
337
349
"""
350
+ if self ._curl is None :
351
+ return 0 # silently ignore if curl handle is None
338
352
return lib .curl_easy_impersonate (
339
353
self ._curl , target .encode (), int (default_headers )
340
354
)
@@ -356,6 +370,9 @@ def perform(self, clear_headers: bool = True, clear_resolve: bool = True) -> Non
356
370
Raises:
357
371
CurlError: if the perform was not successful.
358
372
"""
373
+ if self ._curl is None :
374
+ raise CurlError ("Cannot perform request on closed handle." )
375
+
359
376
# make sure we set a cacert store
360
377
self ._ensure_cacert ()
361
378
@@ -369,12 +386,14 @@ def perform(self, clear_headers: bool = True, clear_resolve: bool = True) -> Non
369
386
self .clean_handles_and_buffers (clear_headers , clear_resolve )
370
387
371
388
def upkeep (self ) -> int :
389
+ if self ._curl is None :
390
+ return 0 # silently ignore if curl handle is None
372
391
return lib .curl_easy_upkeep (self ._curl )
373
392
374
393
def clean_handles_and_buffers (
375
394
self , clear_headers : bool = True , clear_resolve : bool = True
376
395
) -> None :
377
- """Clean up handles and buffers after ``perform`` and ``close``,
396
+ """Clean up handles and buffers after ``perform`` and ``close``,
378
397
called at the end of ``perform`` and ``close``."""
379
398
self ._write_handle = None
380
399
self ._header_handle = None
@@ -400,6 +419,8 @@ def duphandle(self) -> Curl:
400
419
401
420
This is not a full copy of entire curl object in python. For example, headers
402
421
handle is not copied, you have to set them again."""
422
+ if self ._curl is None :
423
+ raise CurlError ("Cannot duplicate closed handle." )
403
424
new_handle = lib .curl_easy_duphandle (self ._curl )
404
425
c = Curl (cacert = self ._cacert , debug = self ._debug , handle = new_handle )
405
426
return c
@@ -478,6 +499,9 @@ def ws_recv(self, n: int = 1024) -> tuple[bytes, CurlWsFrame]:
478
499
Raises:
479
500
CurlError: if failed.
480
501
"""
502
+ if self ._curl is None :
503
+ raise CurlError ("Cannot receive websocket data on closed handle." )
504
+
481
505
buffer = ffi .new ("char[]" , n )
482
506
n_recv = ffi .new ("size_t *" )
483
507
p_frame = ffi .new ("struct curl_ws_frame **" )
@@ -503,6 +527,9 @@ def ws_send(self, payload: bytes, flags: CurlWsFlag = CurlWsFlag.BINARY) -> int:
503
527
Raises:
504
528
CurlError: if failed.
505
529
"""
530
+ if self ._curl is None :
531
+ raise CurlError ("Cannot send websocket data on closed handle." )
532
+
506
533
n_sent = ffi .new ("size_t *" )
507
534
buffer = ffi .from_buffer (payload )
508
535
ret = lib .curl_ws_send (self ._curl , buffer , len (payload ), n_sent , 0 , flags )
0 commit comments