o
    Zhy                     @   s  d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dl	Z	d dl
mZ d dlZd dlZd dlZd dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlZd dlZd dlZd dlmZ d dlmZ d dlm Z  d dlm!Z! d dlm"Z" d dlm#Z# d dlm$Z$ d dlm%Z% d dlm&Z& d dlm'Z' d dlm(Z( d dlm)Z) d dl*Z*d dl+Z+d dl,m-Z- d dl.m/Z/ d dl0m1Z1 dZ2e(dedZ3eeeefZ4d Z5d!d" Z6d#e3d$e3fd%d&Z7d#e8d'e)e9e&e'e9 e9e$ej: f f d(ed$e;fd)d*Z<d#ed$efd+d,Z=d#e8d$e8fd-d.Z>d#ed$dfd/d0Z?d1ed2e8d$e"d3 fd4d5Z@d6e!d3 d2e8d$e"e8 fd7d8ZAd1ed2e8d$e"e8 fd9d:ZBd$eCfd;d<ZDd1ed=e)e8ef d>e)e8ef d$dfd?d@ZEdd1ed2e8dBeCd$efdCdDZFdEed$efdFdGZGe jHfdHefdIdJZId#ed$dfdKdLZJd#edMeKd$e;fdNdOZLd#edMeKd$dfdPdQZMd1ed2e8dReCd$e"e fdSdTZNd1efdUdVZOd1ed2e8dReCdMeKd$df
dWdXZPd1ed2e8dReCdYeKdBeCd$efdZd[ZQd\e8d]ed$efd^d_ZRd`e8d#e)e8daf d$e;fdbdcZSdde8d$e%e8 fdedfZTdgdh ZUG didj djeZVG dkdl dleWZXeVjYdmd#e)e8daf dBe)e8eVf d1edne;d$ef
dodpZZdqe8dredsedte;d$e$e f
dudvZ[ej\]dwrZdxe8dye8d$e;fdzd{Z^ndxe8dye8d$e;fd|d{Z^d#ed1ed$e8fd}d~Z_de e8ef dqe8d$dfddZ`d#ed$e$e fddZaddd#edne;d$e&ee8f fddZbG dd decZddd fd#e)e8daf ded3geef d$e#d3 fddZfd#e)e8daf ded3ge;f d$e"d3 fddZgd#e)ee8f d$efddZhdeded$e$e fddZideded$e8fddZjdEed$e;fddZkdS )    N)Enum)EBADF)ELOOP)ENOENT)ENOTDIR)partial)
expanduser)
expandvars)isabs)sep)Path)PurePath)
ModuleType)Callable)Dict)Iterable)Iterator)List)Optional)Set)Tuple)Type)TypeVar)Union)assert_never)skip)PytestWarningi _AnyPurePath)bound)   i  c                 C   s    t | dd tv pt | dd tv S )Nerrnowinerror)getattr_IGNORED_ERRORS_IGNORED_WINERRORS)	exception r&   F/var/www/html/lang_env/lib/python3.10/site-packages/_pytest/pathlib.py_ignore_error=   s   r(   pathreturnc                 C   s
   |  dS )Nz.lock)joinpathr)   r&   r&   r'   get_lock_pathD   s   
r-   excinfo
start_pathc                   s  t |tr|}n|d }t |trdS t |ts,ttd| dt| d|  dS | tj	tj
tjfvrS| tjfvrQttd|  d| dt| d|  dS d	d
l dtdd
f fdd}t|}| r||jD ]}|t| ||kr{ nqm|t| | | dS )zhHandle known read-only errors during rmtree.

    The returned value is used only by our own tests.
       Fz(rm_rf) error removing 
z: z(rm_rf) unknown function z when removing z:
r   Npr*   c                    s(   t | j}t | | jB  jB  d S N)osstatst_modechmodS_IRUSRS_IWUSR)r2   moder5   r&   r'   chmod_rwr   s   z on_rm_rf_error.<locals>.chmod_rwT)
isinstanceBaseExceptionFileNotFoundErrorPermissionErrorwarningswarnr   typer4   rmdirremoveunlinkopenr5   strr   is_fileparents)funcr)   r.   r/   excr<   r2   parentr&   r;   r'   on_rm_rf_errorH   s<   



rN   c                 C   s(   t jdr|  } ttt| } | S )a0  Get the extended-length version of a path (Windows).

    On Windows, by default, the maximum length of a path (MAX_PATH) is 260
    characters, and operations on paths longer than that fail. But it is possible
    to overcome this by converting the path to "extended-length" form before
    performing the operation:
    https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#maximum-path-length-limitation

    On Windows, this function returns the extended-length absolute version of path.
    On other platforms it returns path unchanged.
    win32)sysplatform
startswithresolver   get_extended_length_path_strrH   r,   r&   r&   r'   ensure_extended_length_path   s   rU   c                 C   s<   d}d}|  ||fr| S |  dr|| dd  S ||  S )z1Convert a path to a Windows extended length path.z\\?\z\\?\UNC\z\\   N)rR   )r)   Zlong_path_prefixZunc_long_path_prefixr&   r&   r'   rT      s   
rT   c                 C   sJ   t | } tt| d}tjdkrtjt| |d dS tjt| |d dS )zNRemove the path contents recursively, even if some elements
    are read-only.)r/   )      )onexc)onerrorN)rU   r   rN   rP   version_infoshutilrmtreerH   )r)   rZ   r&   r&   r'   rm_rf   s
   
r^   rootprefixzos.DirEntry[str]c                 c   s4    |  }t| D ]}|j  |r|V  q
dS )zGFind all elements in root that begin with the prefix, case insensitive.N)lowerr4   scandirnamerR   )r_   r`   Zl_prefixxr&   r&   r'   find_prefixed   s   re   iterc                 c   s(    t |}| D ]
}|j|d V  qdS )zReturn the parts of the paths following the prefix.

    :param iter: Iterator over path names.
    :param prefix: Expected prefix of the path names.
    N)lenrc   )rf   r`   Zp_lenentryr&   r&   r'   extract_suffixes   s
   ri   c                 C   s   t t| ||S )z+Combine find_prefixes and extract_suffixes.)ri   re   )r_   r`   r&   r&   r'   find_suffixes   s   rj   c                 C   s    zt | W S  ty   Y dS w )z0Parse number path suffixes, returns -1 on error.)int
ValueError)Z	maybe_numr&   r&   r'   	parse_num   s
   
rn   targetlink_toc                 C   sP   |  |}z|  W n	 ty   Y nw z|| W dS  ty'   Y dS w )a  Helper to create the current symlink.

    It's full of race conditions that are reasonably OK to ignore
    for the context of best effort linking to the latest test run.

    The presumption being that in case of much parallelism
    the inaccuracy is going to be acceptable.
    N)r+   rF   OSError
symlink_to	Exception)r_   ro   rp   Zcurrent_symlinkr&   r&   r'   _force_symlink   s   
rt     r:   c              	   C   s   t dD ]8}tttt| |dd}|d }| | | }z|j|d W n	 ty0   Y qw t| |d | |  S t	d| d|  d	)
zKCreate a directory with an increased number as suffix for the given prefix.
   rk   defaultr0   r:   currentz*could not create numbered dir with prefix z in z after 10 tries)
rangemaxmaprn   rj   r+   mkdirrs   rt   rq   )r_   r`   r:   imax_existingZ
new_numbernew_pathr&   r&   r'   make_numbered_dir   s$   r   r2   c              
   C   s   t | }ztt|tjtjB tjB d}W n ty* } ztd|  |d}~ww t	 }t|
 }t|| t| | sHtd|S )z2Create a lock to prevent premature folder cleanup.i  zcannot create lockfile in Nz/lock path got renamed after successful creation)r-   r4   rG   rH   O_WRONLYO_CREATO_EXCLFileExistsErrorrq   getpidencodewritecloserI   )r2   	lock_pathfdepidZspidr&   r&   r'   create_cleanup_lock   s   $
r   r   c                 C   s,   t  }| |fdtdtddfdd}||S )zFRegister a cleanup function for removing a lock, by default on atexit.r   original_pidr*   Nc                 S   s8   t  }||kr
d S z|   W d S  ty   Y d S w r3   )r4   r   rF   rq   )r   r   Zcurrent_pidr&   r&   r'   cleanup_on_exit  s   z6register_cleanup_lock_removal.<locals>.cleanup_on_exit)r4   r   r   rl   )r   registerr   r   r&   r&   r'   register_cleanup_lock_removal
  s   
r   c                 C   s   t | } d}zWzt| }| j}|dt  }| | t| W n! tyD   Y W |durBz|	  W dS  tyA   Y dS w dS w W |dur\z|	  W dS  ty[   Y dS w dS |durrz|	  W w  tyq   Y w w w )z^Remove a numbered directory if its lock can be obtained and it does
    not seem to be in use.Nzgarbage-)
rU   r   rM   r+   uuiduuid4renamer^   rq   rF   )r)   r   rM   garbager&   r&   r'   maybe_delete_a_numbered_dir  s@   

r   $consider_lock_dead_if_created_beforec                 C   s   |   rdS t| }z	| sW dS W n
 ty   Y dS w z| j}W n
 ty/   Y dS w ||k rOtt |	  	 W d   dS 1 sJw   Y  dS )zGCheck if `path` is deletable based on whether the lock file is expired.FTN)

is_symlinkr-   rI   rq   r5   st_mtimers   
contextlibsuppressrF   )r)   r   lockZ	lock_timer&   r&   r'   ensure_deletable7  s*    r   c                 C   s   t | |rt|  dS dS )z8Try to cleanup a folder if we can ensure it's deletable.N)r   r   )r)   r   r&   r&   r'   try_cleanupS  s   
r   keepc           
      c   st    t ttt| |dd}|| }t| |}t|\}}ttt||}t||D ]\}}	|	|kr7t	|V  q*dS )zIList candidates for numbered directories to be removed - follows py.path.rk   rw   N)
r|   r}   rn   rj   re   	itertoolsteeri   zipr   )
r_   r`   r   r   Z
max_deleteentriesZentries2numbersrh   numberr&   r&   r'   cleanup_candidatesY  s   

r   c                 C   s.   |   D ]}| r|  s|  qd S r3   )iterdirr   rS   existsrF   )r_   Zleft_dirr&   r&   r'   cleanup_dead_symlinkse  s   r   c                 C   sN   |   sdS t| ||D ]}t|| q| dD ]}t|| qt|  dS )z-Cleanup for lock driven numbered directories.Nz	garbage-*)r   r   r   globr   )r_   r`   r   r   r)   r&   r&   r'   cleanup_numbered_dirl  s   r   lock_timeoutc                 C   s   d}t dD ]>}zt| ||}|dkrt|}t| W n ty0 }	 z|	}W Y d}	~	qd}	~	ww | j| }
tt	| |||
 |  S |dusKJ |)z>Create a numbered dir with a cleanup lock and remove old ones.Nrv   r   )
r{   r   r   r   rs   r5   r   atexitr   r   )r_   r`   r   r   r:   r   r   r2   r   rL   r   r&   r&   r'   make_numbered_dir_with_cleanupz  s.   r   inputrootpathc                 C   s*   t | } t| } t| rt| S || S r3   )r   r	   r
   r   r+   )r   r   r&   r&   r'   resolve_from_str  s
   
r   patternzos.PathLike[str]c                 C   s|   t |}tjd}|rt| vrt| v r| tt} t| vr"|j}nt|}|	 r8t
j| s8dt
j |  } t|| S )a  A port of FNMatcher from py.path.common which works with PurePath() instances.

    The difference between this algorithm and PurePath.match() is that the
    latter matches "**" glob expressions for each part of the path, while
    this algorithm uses the whole path instead.

    For example:
        "tests/foo/bar/doc/test_foo.py" matches pattern "tests/**/doc/test*.py"
        with this algorithm, but not with PurePath.match().

    This algorithm was ported to keep backward-compatibility with existing
    settings which assume paths match according this logic.

    References:
    * https://bugs.python.org/issue29249
    * https://bugs.python.org/issue34731
    win*)r   rP   rQ   rR   r   	posix_sepreplacerc   rH   is_absoluter4   r)   r
   fnmatch)r   r)   Ziswin32rc   r&   r&   r'   
fnmatch_ex  s   r   sc                    s$   |  t  fddtt D S )Nc                    s&   h | ]}t  d |d  pt qS )Nr0   )r   join).0r   partsr&   r'   	<setcomp>  s   & zparts.<locals>.<setcomp>)splitr   r{   rg   )r   r&   r   r'   r     s   
r   c              
   K   sX   zt jt| t|fi | W dS  ty+ } ztd|  W Y d}~dS d}~ww )zDMake a symlink, or skip the test in case symlinks are not supported.zsymlinks not supported: N)r4   symlinkrH   rq   r   )srcdstkwargsr   r&   r&   r'   symlink_or_skip  s   "r   c                   @   s   e Zd ZdZdZdZdZdS )
ImportModez6Possible values for `mode` parameter of `import_path`.prependappend	importlibN)__name__
__module____qualname____doc__r   r   r   r&   r&   r&   r'   r     s
    r   c                   @      e Zd ZdZdS )ImportPathMismatchErrora  Raised on import_path() if there is a mismatch of __file__'s.

    This can happen when `import_path` is called multiple times with different filenames that has
    the same basename but reside in packages
    (for example "/tests1/test_foo.py" and "/tests2/test_foo.py").
    Nr   r   r   r   r&   r&   r&   r'   r         r   ry   consider_namespace_packagesc          
      C   s  t | } t|}|  st| |tju rz
t| |d\}}W n	 ty(   Y n)w tt	 t
j| W  d   S 1 s>w   Y  t|| |dd}|durQ|S t| |}tt	 t
j| W  d   S 1 skw   Y  t|| | jdd}|du rtd| d|  |S z
t| |d\}}W n ty   | j| j}}Y nw |tju rt|t
jvrt
jt| n|tju rt|t
jd krt
jdt| nt| t| t
j| }| jd	kr|S tjd
d}|dkr?|j}|du rt||| |dr|dd }|tjd	 r |dttjd	   }z	t t| |}	W n t!y5   d}	Y nw |	s?t||| |S )aq  
    Import and return a module from the given path, which can be a file (a module) or
    a directory (a package).

    :param path:
        Path to the file to import.

    :param mode:
        Controls the underlying import mechanism that will be used:

        * ImportMode.prepend: the directory containing the module (or package, taking
          `__init__.py` files into account) will be put at the *start* of `sys.path` before
          being imported with `importlib.import_module`.

        * ImportMode.append: same as `prepend`, but the directory will be appended
          to the end of `sys.path`, if not already in `sys.path`.

        * ImportMode.importlib: uses more fine control mechanisms provided by `importlib`
          to import the module, which avoids having to muck with `sys.path` at all. It effectively
          allows having same-named test modules in different places.

    :param root:
        Used as an anchor when mode == ImportMode.importlib to obtain
        a unique name for the module being imported so it can safely be stored
        into ``sys.modules``.

    :param consider_namespace_packages:
        If True, consider namespace packages when resolving module names.

    :raises ImportPathMismatchError:
        If after importing the given `path` and the module `__file__`
        are different. Only raised in `prepend` and `append` modes.
    r   NF)insert_modulesTzCan't find module z at location r   __init__.pyZPY_IGNORE_IMPORTMISMATCH 1)z.pycz.pyork   )"r   r   r   ImportErrorr    resolve_pkg_root_and_module_nameCouldNotResolvePathErrorr   r   KeyErrorrP   modules_import_module_using_specmodule_name_from_pathrM   stemr   rH   r)   r   insertr   import_modulerc   r4   environget__file__r   endswithr   rg   _is_samer?   )
r)   r:   r_   r   pkg_rootmodule_namemodignoreZmodule_fileZis_samer&   r&   r'   import_path  s   (
 
 






r   r   module_pathmodule_locationr   c                C   s~   t jD ]}|| t|g}|dur n
qtj| t|}|dur=tj|}|t j| < |j	
| |r;tt j|  |S dS )aP  
    Tries to import a module by its canonical name, path to the .py file, and its
    parent location.

    :param insert_modules:
        If True, will call insert_missing_modules to create empty intermediate modules
        for made-up module names (when importing test files not reachable from sys.path).
        Note: we can probably drop insert_missing_modules altogether: instead of
        generating module names such as "src.tests.test_foo", which require intermediate
        empty modules, we might just as well generate unique module names like
        "src_tests_test_foo".
    N)rP   	meta_path	find_specrH   r   utilspec_from_file_locationmodule_from_specr   loaderexec_moduleinsert_missing_modules)r   r   r   r   Zmeta_importerspecr   r&   r&   r'   r   d  s   

r   r   f1f2c                 C   s   t | t |kptj| |S r3   )r   r4   r)   samefiler   r   r&   r&   r'   r     s   r   c                 C   s   t j| |S r3   )r4   r)   r   r   r&   r&   r'   r     s   c                 C   s   |  d} z| |}W n ty   | jdd }Y nw |j}t|dkr2|d dkr2|dd }tdd |D }d	|S )
z
    Return a dotted module name based on the given path, anchored on root.

    For example: path="projects/src/tests/test_foo.py" and root="/projects", the
    resulting module name will be "src.tests.test_foo".
    r   r0   NrV   rk   __init__c                 s   s    | ]	}| d dV  qdS )._N)r   )r   rd   r&   r&   r'   	<genexpr>  s    z(module_name_from_path.<locals>.<genexpr>r   )with_suffixrelative_torm   r   rg   tupler   )r)   r_   Zrelative_path
path_partsr&   r&   r'   r     s   

r   r   c                 C   s   | d}d}d}d}|rZ|| vr-ztjstt|}W n ty,   t|dd}Y nw | | }|rBt||sBt||| || |< ||	dd }}|
d d|}|sdS dS )aM  
    Used by ``import_path`` to create intermediate modules when using mode=importlib.

    When we want to import a module as "src.tests.test_foo" for example, we need
    to create empty modules "src" and "src.tests" after inserting "src.tests.test_foo",
    otherwise "src.tests.test_foo" is not importable by ``__import__``.
    r   Nr   z6Empty module created by pytest's importmode=importlib.)docrk   )r   rP   r   ModuleNotFoundErrorr   r   r   hasattrsetattr
rpartitionpopr   )r   r   module_partsZchild_modulemoduleZ
child_namer&   r&   r'   r     s2   




r   c                 C   sL   d}t | f| jD ]}| r#|d  s |S |j s! |S |}q
|S )zReturn the Python package path by looking for the last
    directory upwards which still contains an __init__.py.

    Returns None if it can not be determined.
    Nr   )r   chainrJ   is_dirrI   rc   isidentifier)r)   resultrM   r&   r&   r'   resolve_package_path  s   
r  Fr   c                C   s   t | }|durD|j}|r&|jD ]}|d  r nt|tjv r%|} nqt| d	|j
}|d dkr;|  d|}||fS td|  )a  
    Return the path to the directory of the root package that contains the
    given Python file, and its module name:

        src/
            app/
                __init__.py
                core/
                    __init__.py
                    models.py

    Passing the full path to `models.py` will yield Path("src") and "app.core.models".

    If consider_namespace_packages is True, then we additionally check upwards in the hierarchy
    until we find a directory that is reachable from sys.path, which marks it as a namespace package:

    https://packaging.python.org/en/latest/guides/packaging-namespace-packages

    Raises CouldNotResolvePathError if the given path does not belong to a package (missing any __init__.py files).
    Nr   r   rk   r   r   zCould not resolve for )r  rM   rJ   rI   rH   rP   r)   listr  r  r   r
  r   r   )r)   r   Zpkg_pathr   rM   namesr   r&   r&   r'   r     s"   

r   c                   @   r   )r   z<Custom exception raised by resolve_pkg_root_and_module_name.Nr   r&   r&   r&   r'   r     r   r   c                 C   s   | j S r3   )rc   )rh   r&   r&   r'   <lambda>$  s    r  sort_keyc                 C   s   g }t | 0}|D ]%}z|  W n ty) } zt|r$W Y d}~q
 d}~ww || q
W d   n1 s:w   Y  |j|d |S )zScan a directory recursively, in breadth-first order.

    The returned entries are sorted according to the given key.
    The default is to sort by name.
    N)key)r4   rb   rI   rq   r(   r   sort)r)   r  r   r   rh   errr&   r&   r'   rb   "  s    	rb   recursec                 c   sD    t | }|E dH  |D ]}| r||rt|j|E dH  qdS )zWalk a directory recursively, in breadth-first order.

    The `recurse` predicate determines whether a directory is recursed.

    Entries at each directory level are sorted.
    N)rb   r  visitr)   )r)   r  r   rh   r&   r&   r'   r  ;  s   	
r  c                 C   s   t tjt| S )zConvert a path to an absolute path using os.path.abspath.

    Prefer this over Path.resolve() (see #6523).
    Prefer this over Path.absolute() (not public, doesn't normalize).
    )r   r4   r)   abspathrH   r,   r&   r&   r'   absolutepathK  s   r  path1path2c                 C   s4   zt tjt| t|fW S  ty   Y dS w )zReturn the common part shared with the other path, or None if there is
    no common part.

    If one path is relative and one is absolute, returns None.
    N)r   r4   r)   
commonpathrH   rm   )r  r  r&   r&   r'   r  T  s
   r  	directorydestc                 C   sz   t | tsJ t |tsJ || krtjS t| |}|s t|S | |}||}tjjg tj	gt
|j |jR  S )zReturn a string which is a relative path from directory to dest such
    that directory/bestrelpath == dest.

    The paths must be either both absolute or both relative.

    If no such path can be determined, returns dest.
    )r=   r   r4   curdirr  rH   r  r)   r   pardirrg   r   )r   r!  baseZreldirectoryZreldestr&   r&   r'   bestrelpath`  s   


r%  c              	   C   s$   z|   W S  ttfy   Y dS w )zTLike Path.exists(), but account for input arguments that might be too long (#11394).F)r   rm   rq   )r2   r&   r&   r'   safe_exists}  s
   
r&  )ru   )lr   r   enumr   r    r   r   r   r   r   	functoolsr   importlib.utilr   r   r4   Zos.pathr   r	   r
   r   pathlibr   r   	posixpathr   r\   rP   typesr   typingr   r   r   r   r   r   r   r   r   r   r   r   rA   Z_pytest.compatr   Z_pytest.outcomesr   Z_pytest.warning_typesr   ZLOCK_TIMEOUTr   r#   r$   r(   r-   rH   r>   TracebackTypeboolrN   rU   rT   r^   re   ri   rj   rl   rn   rt   r   r   r   r   r   floatr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rQ   rR   r   r   r   r  r   rs   r   objectrb   r  r  r  r%  r&  r&   r&   r&   r'   <module>   sR  	
=




!	$


~
$)

2



	