a
    ûbw  ã                   @   sj   d Z ddlZddlZddlZddlZddlZddlZddlZddlZe 	e
¡Zd
dd„ZdZddd	„ZdS )a  
The Cython debugger

The current directory should contain a directory named 'cython_debug', or a
path to the cython project directory should be given (the parent directory of
cython_debug).

Additional gdb args can be provided only if a path to the project directory is
given.
é    NÚ Fc                 C   s   |s<t j | dd¡}t |¡}|s<t dtt j | ¡f ¡ t 	¡ \}}t  
|d¡}zœ| |¡ | t d¡¡ |rvnpt j | dd¡}t|ƒ}	z|	 ¡ }
W |	 ¡  n
|	 ¡  0 | d|
 ¡ | d d	d
„ |D ƒ¡¡ | t d¡¡ W | ¡  n
| ¡  0 |S )NZcython_debugzcython_debug_info_*z.%s.
No debug files were found in %s. Aborting.ÚwaV              # This is a gdb command file
            # See https://sourceware.org/gdb/onlinedocs/gdb/Command-Files.html

            set breakpoint pending on
            set print pretty on

            python
            # Activate virtualenv, if we were launched from one
            import os
            virtualenv = os.getenv('VIRTUAL_ENV')
            if virtualenv:
                path_to_activate_this_py = os.path.join(virtualenv, 'bin', 'activate_this.py')
                print("gdb command file: Activating virtualenv: %s; path_to_activate_this_py: %s" % (
                    virtualenv, path_to_activate_this_py))
                with open(path_to_activate_this_py) as f:
                    exec(f.read(), dict(__file__=path_to_activate_this_py))

            from Cython.Debugger import libcython, libpython
            end
            Úinterpreterzfile %s
Ú
c                 s   s   | ]}d | V  qdS )zcy import %s
N© )Ú.0Úfnr   r   úe/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/Cython/Debugger/Cygdb.pyÚ	<genexpr>J   ó    z$make_command_file.<locals>.<genexpr>a°                  python
                import sys
                try:
                    gdb.lookup_type('PyModuleObject')
                except RuntimeError:
                    sys.stderr.write(
                        'Python was not compiled with debug symbols (or it was '
                        'stripped). Some functionality may not work (properly).\n')
                end

                source .cygdbinit
            )ÚosÚpathÚjoinÚglobÚsysÚexitÚusageÚabspathÚtempfileÚmkstempÚfdopenÚwriteÚtextwrapÚdedentÚopenÚreadÚclose)Úpath_to_debug_infoZprefix_codeÚ	no_importÚpatternZdebug_filesÚfdÚtempfilenameÚfr   Zinterpreter_filer   r   r   r	   Úmake_command_file   s6    þ
ÿ

r#   z0Usage: cygdb [options] [PATH [-- GDB_ARGUMENTS]]c              	   C   sî  t jtd}|jddddd |jdddd	d
dd | ¡ \}}| du rdt|ƒdkr^|d
 } ntj} |du rx|dd… }| dkr„d}tj	}|j
dkrštj}|j
dkrªtj}tj|d t d|j
¡ t d||¡ t d| |¡ t| |d}t d|j||¡ t|ƒ°}t d|| ¡ ¡ t d|j¡ t |jd|g| ¡}	t d|j|	j¡ z*t d|	j¡ |	 ¡ }
t d|	j|
¡ W n tyŽ   Y n0 q˜qPt d| ¡ ¡ W d  ƒ n1 s¾0    Y  t d|¡ t |¡ t d |¡ dS )!ab  
    Start the Cython debugger. This tells gdb to import the Cython and Python
    extensions (libcython.py and libpython.py) and it enables gdb's pending
    breakpoints.

    path_to_debug_info is the path to the Cython build directory
    gdb_argv is the list of options to gdb
    no_import tells cygdb whether it should import debug information
    )r   z--gdb-executableÚgdbz$gdb executable to use [default: gdb])ÚdestÚdefaultÚhelpz	--verbosez-vÚ	verbosityÚcountr   z8Verbose mode. Multiple -v options increase the verbosity)r%   Úactionr&   r'   Né   z--Té   )Úlevelzverbosity = %rzoptions = %r; args = %rzIDone parsing command-line options. path_to_debug_info = %r, gdb_argv = %r)r   z3Launching %s with command file: %s and gdb_argv: %sz%Command file (%s) contains: """
%s"""zSpawning %s...z-commandzSpawned %s (pid %d)z#Waiting for gdb (pid %d) to exit...z3Wait for gdb (pid %d) to exit is done. Returned: %rz%Closing temp command file with fd: %szRemoving temp command file: %szRemoved temp command file: %s)ÚoptparseÚOptionParserr   Ú
add_optionÚ
parse_argsÚlenr   ÚcurdirÚloggingÚWARNr(   ÚINFOÚDEBUGÚbasicConfigÚloggerÚinfoÚdebugr#   r$   r   r   Ú
subprocessÚPopenÚpidÚwaitÚKeyboardInterruptÚfilenoÚremove)r   Zgdb_argvr   ÚparserÚoptionsÚargsZlogging_levelr!   r   ÚpÚretr   r   r	   Úmain_   sb    
þþ


ÿÿ
0
rH   )r   F)NNF)Ú__doc__r   r   r   r   r   r<   r.   r4   Ú	getLoggerÚ__name__r9   r#   r   rH   r   r   r   r	   Ú<module>   s   

D