Let us walk on the 3-isogeny graph
Loading...
Searching...
No Matches
pip._vendor.requests.cookies Namespace Reference

Data Structures

class  CookieConflictError
 
class  MockRequest
 
class  MockResponse
 
class  RequestsCookieJar
 

Functions

 extract_cookies_to_jar (jar, request, response)
 
 get_cookie_header (jar, request)
 
 remove_cookie_by_name (cookiejar, name, domain=None, path=None)
 
 _copy_cookie_jar (jar)
 
 create_cookie (name, value, **kwargs)
 
 morsel_to_cookie (morsel)
 
 cookiejar_from_dict (cookie_dict, cookiejar=None, overwrite=True)
 
 merge_cookies (cookiejar, cookies)
 

Detailed Description

requests.cookies
~~~~~~~~~~~~~~~~

Compatibility code to be able to use `cookielib.CookieJar` with requests.

requests.utils imports from here, so be careful with imports.

Function Documentation

◆ _copy_cookie_jar()

_copy_cookie_jar (   jar)
protected

Definition at line 440 of file cookies.py.

440def _copy_cookie_jar(jar):
441 if jar is None:
442 return None
443
444 if hasattr(jar, "copy"):
445 # We're dealing with an instance of RequestsCookieJar
446 return jar.copy()
447 # We're dealing with a generic CookieJar instance
448 new_jar = copy.copy(jar)
450 for cookie in jar:
452 return new_jar
453
454
for i

References i.

◆ cookiejar_from_dict()

cookiejar_from_dict (   cookie_dict,
  cookiejar = None,
  overwrite = True 
)
Returns a CookieJar from a key/value dictionary.

:param cookie_dict: Dict of key/values to insert into CookieJar.
:param cookiejar: (optional) A cookiejar to add the cookies to.
:param overwrite: (optional) If False, will not replace cookies
    already in the jar with new ones.
:rtype: CookieJar

Definition at line 521 of file cookies.py.

521def cookiejar_from_dict(cookie_dict, cookiejar=None, overwrite=True):
522 """Returns a CookieJar from a key/value dictionary.
523
524 :param cookie_dict: Dict of key/values to insert into CookieJar.
525 :param cookiejar: (optional) A cookiejar to add the cookies to.
526 :param overwrite: (optional) If False, will not replace cookies
527 already in the jar with new ones.
528 :rtype: CookieJar
529 """
530 if cookiejar is None:
531 cookiejar = RequestsCookieJar()
532
533 if cookie_dict is not None:
534 names_from_jar = [cookie.name for cookie in cookiejar]
535 for name in cookie_dict:
536 if overwrite or (name not in names_from_jar):
537 cookiejar.set_cookie(create_cookie(name, cookie_dict[name]))
538
539 return cookiejar
540
541

References pip._vendor.requests.cookies.create_cookie(), and i.

Here is the call graph for this function:

◆ create_cookie()

create_cookie (   name,
  value,
**  kwargs 
)
Make a cookie from underspecified parameters.

By default, the pair of `name` and `value` will be set for the domain ''
and sent on every request (this is sometimes called a "supercookie").

Definition at line 455 of file cookies.py.

455def create_cookie(name, value, **kwargs):
456 """Make a cookie from underspecified parameters.
457
458 By default, the pair of `name` and `value` will be set for the domain ''
459 and sent on every request (this is sometimes called a "supercookie").
460 """
461 result = {
462 "version": 0,
463 "name": name,
464 "value": value,
465 "port": None,
466 "domain": "",
467 "path": "/",
468 "secure": False,
469 "expires": None,
470 "discard": True,
471 "comment": None,
472 "comment_url": None,
473 "rest": {"HttpOnly": None},
474 "rfc2109": False,
475 }
476
477 badargs = set(kwargs) - set(result)
478 if badargs:
479 raise TypeError(
480 f"create_cookie() got unexpected keyword arguments: {list(badargs)}"
481 )
482
483 result.update(kwargs)
484 result["port_specified"] = bool(result["port"])
485 result["domain_specified"] = bool(result["domain"])
486 result["domain_initial_dot"] = result["domain"].startswith(".")
487 result["path_specified"] = bool(result["path"])
488
489 return cookielib.Cookie(**result)
490
491

References i.

Referenced by pip._vendor.requests.cookies.cookiejar_from_dict(), pip._vendor.requests.cookies.morsel_to_cookie(), and RequestsCookieJar.set().

Here is the caller graph for this function:

◆ extract_cookies_to_jar()

extract_cookies_to_jar (   jar,
  request,
  response 
)
Extract the cookies from the response into a CookieJar.

:param jar: cookielib.CookieJar (not necessarily a RequestsCookieJar)
:param request: our own requests.Request object
:param response: urllib3.HTTPResponse object

Definition at line 124 of file cookies.py.

124def extract_cookies_to_jar(jar, request, response):
125 """Extract the cookies from the response into a CookieJar.
126
127 :param jar: cookielib.CookieJar (not necessarily a RequestsCookieJar)
128 :param request: our own requests.Request object
129 :param response: urllib3.HTTPResponse object
130 """
131 if not (hasattr(response, "_original_response") and response._original_response):
132 return
133 # the _original_response field is the wrapped httplib.HTTPResponse object,
134 req = MockRequest(request)
135 # pull out the HTTPMessage with the headers and put it in the mock:
136 res = MockResponse(response._original_response.msg)
137 jar.extract_cookies(res, req)
138
139

References i.

◆ get_cookie_header()

get_cookie_header (   jar,
  request 
)
Produce an appropriate Cookie header string to be sent with `request`, or None.

:rtype: str

Definition at line 140 of file cookies.py.

140def get_cookie_header(jar, request):
141 """
142 Produce an appropriate Cookie header string to be sent with `request`, or None.
143
144 :rtype: str
145 """
146 r = MockRequest(request)
148 return r.get_new_headers().get("Cookie")
149
150

References i.

◆ merge_cookies()

merge_cookies (   cookiejar,
  cookies 
)
Add cookies to cookiejar and returns a merged CookieJar.

:param cookiejar: CookieJar object to add the cookies to.
:param cookies: Dictionary or CookieJar object to be added.
:rtype: CookieJar

Definition at line 542 of file cookies.py.

542def merge_cookies(cookiejar, cookies):
543 """Add cookies to cookiejar and returns a merged CookieJar.
544
545 :param cookiejar: CookieJar object to add the cookies to.
546 :param cookies: Dictionary or CookieJar object to be added.
547 :rtype: CookieJar
548 """
549 if not isinstance(cookiejar, cookielib.CookieJar):
550 raise ValueError("You can only merge into CookieJar")
551
552 if isinstance(cookies, dict):
553 cookiejar = cookiejar_from_dict(cookies, cookiejar=cookiejar, overwrite=False)
554 elif isinstance(cookies, cookielib.CookieJar):
555 try:
556 cookiejar.update(cookies)
557 except AttributeError:
558 for cookie_in_jar in cookies:
559 cookiejar.set_cookie(cookie_in_jar)
560
561 return cookiejar

References i.

◆ morsel_to_cookie()

morsel_to_cookie (   morsel)
Convert a Morsel object into a Cookie containing the one k/v pair.

Definition at line 492 of file cookies.py.

492def morsel_to_cookie(morsel):
493 """Convert a Morsel object into a Cookie containing the one k/v pair."""
494
495 expires = None
496 if morsel["max-age"]:
497 try:
498 expires = int(time.time() + int(morsel["max-age"]))
499 except ValueError:
500 raise TypeError(f"max-age: {morsel['max-age']} must be integer")
501 elif morsel["expires"]:
502 time_template = "%a, %d-%b-%Y %H:%M:%S GMT"
503 expires = calendar.timegm(time.strptime(morsel["expires"], time_template))
504 return create_cookie(
505 comment=morsel["comment"],
506 comment_url=bool(morsel["comment"]),
507 discard=False,
508 domain=morsel["domain"],
509 expires=expires,
510 name=morsel.key,
511 path=morsel["path"],
512 port=None,
513 rest={"HttpOnly": morsel["httponly"]},
514 rfc2109=False,
515 secure=bool(morsel["secure"]),
516 value=morsel.value,
517 version=morsel["version"] or 0,
518 )
519
520

References pip._vendor.requests.cookies.create_cookie(), and i.

Referenced by RequestsCookieJar.set().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ remove_cookie_by_name()

remove_cookie_by_name (   cookiejar,
  name,
  domain = None,
  path = None 
)
Unsets a cookie by name, by default over all domains and paths.

Wraps CookieJar.clear(), is O(n).

Definition at line 151 of file cookies.py.

151def remove_cookie_by_name(cookiejar, name, domain=None, path=None):
152 """Unsets a cookie by name, by default over all domains and paths.
153
154 Wraps CookieJar.clear(), is O(n).
155 """
156 clearables = []
157 for cookie in cookiejar:
158 if cookie.name != name:
159 continue
160 if domain is not None and domain != cookie.domain:
161 continue
162 if path is not None and path != cookie.path:
163 continue
165
166 for domain, path, name in clearables:
167 cookiejar.clear(domain, path, name)
168
169

References i.

Referenced by RequestsCookieJar.__delitem__(), and RequestsCookieJar.set().

Here is the caller graph for this function: