B u `ŸCã@s¨ddlZddlZddlZddlZddlmZdd„Zdd„Zdd„Zdd d „Zde_ Gd d „d e ƒZ Gdd„de ƒZ Gdd„de ƒZ Gdd„de ƒZGdd„de ƒZdS)éN)Ú iteritemscCs&t|tjƒr|S|dk r"| d¡SdS)z6Helper function to coerce a string to a bytes() objectNzutf-8)Ú isinstanceÚsixÚ binary_typeÚencode)Úval©rú4/tmp/pip-unpacked-wheel-n62dbgi3/pyomo/common/env.pyÚ _as_bytess   r cCs$t|tjƒr|S|dk r | ¡SdS)z8Helper function to coerce a string to a unicode() objectN)rrÚ text_typeÚdecode)rrrr Ú _as_unicodes  r cCs(yt |¡}dStk r"dSXdS)z6Load a CDLL library, returning bool indicating successTFN)ÚctypesÚCDLLÚOSError)ÚnameÚdllrrr Ú_attempt_ctypes_cdll$s  ré cCsŽtj |¡sdStjdkr&t d¡t_tj t|f¡}y|  |¡}Wn*tj k rnd}tj  ¡dt_YnX|r‚|t  |¡fS|dfSdS)aãLoad a DLL with a timeout On some platforms and some DLLs (notably Windows GitHub Actions with Python 3.5, 3.6, and 3.7 and the msvcr90.dll) we have observed behavior where the ctypes.CDLL() call hangs indefinitely. This uses multiprocessing to attempt the import in a subprocess (with a timeout) and then only calls the import in the main process if the subprocess succeeded. Performance note: CtypesEnviron only ever attempts to load a DLL once (the DLL reference is then held in a class attribute), and this interface only spawns the subprocess if ctypes.util.find_library actually locates the target library. This will have a measurable impact on Windows (where the DLLs exist), but not on other platforms. The default timeout of 10 is arbitrary. For simple situations, 1 seems adequate. However, more complex examples have been observed that needed timeout==5. Using a default of 10 is simply doubling that observed case. )FNNéF) rÚutilZ find_libraryÚ _load_dllÚpoolÚmultiprocessingZPoolZ apply_asyncrÚgetÚ TimeoutErrorÚ terminater)rÚtimeoutZjobÚresultrrr r-s     rc@s8eZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd S) Ú_RestorableEnvironInterfacez8Interface to track environment changes and restore statecCs|| ¡s t‚||_i|_x.tttjƒƒD]\}}|||kr(|||<q(W|j ¡}|dk rxx|D]}|tjkr`||=q`WdS)N) Ú availableÚAssertionErrorrÚ_original_stateÚlistrÚosÚenvironÚ get_env_dict)ÚselfrÚkeyrZorigEnvrrr Ú__init__]s      z$_RestorableEnvironInterface.__init__cCsBx6t|jƒD](\}}|s,||dk r4||=q |||<q Wi|_dS)N)rr")r'r(rrrr Úrestoreps   z#_RestorableEnvironInterface.restorecCs(t|tjƒr|j |¡S|j |¡SdS)N)rrr rÚwgetenvÚgetenv)r'r(rrr Ú __getitem__ys  z'_RestorableEnvironInterface.__getitem__cCs†||jkr|||j|<t|tjƒrTt|tjƒr@|j ||¡q‚|j |t|ƒ¡n.t|tjƒrt|j t|ƒ|¡n|j ||¡dS)N)r"rrr rÚ wputenv_sr Úputenv_s)r'r(rrrr Ú __setitem__s    z'_RestorableEnvironInterface.__setitem__cCsF||jkr|||j|<t|tjƒr4|j |d¡n|j |d¡dS)NÚó)r"rrr rr.r/)r'r(rrr Ú __delitem__s   z'_RestorableEnvironInterface.__delitem__N) Ú__name__Ú __module__Ú __qualname__Ú__doc__r)r*r-r0r3rrrr rZs  rc@sDeZdZdZdZdd„Zdd„Zdd„Zd d „Zd d „Z d d„Z dS)Ú _OSEnvironz:Helper class to proxy a "DLL-like" interface to os.environz os.environcCsdS)NTr)r'rrr r œsz_OSEnviron.availablecCs ttjƒS)N)Údictr$r%)r'rrr r&Ÿsz_OSEnviron.get_env_dictcCsRtjrttj |d¡ƒSytj |d¡Stk rLttj t|ƒd¡ƒSXdS)N) rÚPY2r r$r%rÚenvironbÚAttributeErrorr )r'r(rrr r,¢s z_OSEnviron.getenvcCsttj |d¡ƒS)N)r r$r%r)r'r(rrr r+®sz_OSEnviron.wgetenvcCs:|s|tjkrtj|=dStjr,|tj|<n |tj|<dS)N)r$r%rr:r;)r'r(rrrr r/´s  z_OSEnviron.putenv_scCs:|s|tjkrtj|=dStjr,|tj|<n |tj|<dS)N)r$r%rr:)r'r(rrrr r.Às  z_OSEnviron.wputenv_sN) r4r5r6r7Ú_libnamer r&r,r+r/r.rrrr r8—s  r8c@s(eZdZdZdd„Zdd„Zdd„ZdS) Ú _MsvcrtDLLzÍsr>c@s8eZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd S) Ú _Win32DLLz;Helper class to manage the interface with the Win32 runtimecCs&||_|dkrd|_nd|_d|_dS)NF)r=r?r)r'rrrr r)s z_Win32DLL.__init__cCs|jdk r|jSt|jƒ\|_|_|js.|jS|jj|_tjtjg|j_tj |j_ |jj |_ tj tj g|j _tj |j _ |jj|_tjtjtjg|j_tj|j_ |jj|_tj tj tjg|j_tj|j_ |jj|_g|j_t tj¡|j_ |jj|_t tj¡g|j_tj |j_ |jS)N)r?rr=rZSetEnvironmentVariableAr/rr@rAÚc_boolrCZSetEnvironmentVariableWr.rDZGetEnvironmentVariableAÚ _getenv_dllÚc_ulongZGetEnvironmentVariableWÚ _wgetenv_dllZGetEnvironmentStringsWÚ_envstrrFÚc_wcharZFreeEnvironmentStringsWÚ _free_envstr)r'rrr r $s0            z_Win32DLL.availablecCs<| |dd¡}|sdSt d|¡}| |||¡|jp:dS)Nró)rQrÚcreate_string_bufferÚvalue)r'r(rMÚbufrrr r,Is z_Win32DLL.getenvcCs<| |dd¡}|sdSt d|¡}| |||¡|jp:dS)Nrú)rSrÚcreate_unicode_bufferrY)r'r(rMrZrrr r+Qs z_Win32DLL.wgetenvcCsºi}| ¡}ddh}d}x’|||krªd}xT|||kr~|||7}|t||ƒ7}t||ƒdkrltdƒ‚|dkr,tdƒ‚q,W| dd ¡\}}|||<|t||ƒ7}qW| |¡|S) Nr[rWrr1zMError processing Win32 GetEnvironmentStringsW: 0-length character encounterediÿzZError processing Win32 GetEnvironmentStringsW: exceeded max environment block size (32767)rEr)rTrJrIrKrV)r'rLZ_str_bufÚ_nullÚiÚ_strr(rrrr r&Ys(    z_Win32DLL.get_env_dictN) r4r5r6r7r)r r,r+r&rrrr rOs %rOc @s¦eZdZdZedƒeeejddd„ƒƒƒedƒedƒedƒed ƒed ƒed ƒed ƒed ƒedƒg Z dd„Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zdd„ZdS) Ú CtypesEnvirona(A context manager for managing environment variables This class provides a simplified interface for consistently setting and restoring environment variables, with special handling to ensure consistency with the C Runtime Library environment on Windows platforms. `os.environ` reflects the current python environment variables, and will be passed to subprocesses. However, it does not reflect the C Runtime Library (MSVCRT) environment on Windows platforms. This can be problemmatic as DLLs loaded through the CTYPES interface will see the MSVCRT environment and not os.environ. This class provides a way to manage environment variables and pass changes to both os.environ and the MSVCRT runtime. This class implements a context manager API, so that clients can temporarily change - and then subsequently restore - the environment. .. testcode:: :hide: import os from pyomo.common.env import TemporaryEnv orig_env_val = os.environ.get('TEMP_ENV_VAR', None) .. doctest:: >>> os.environ['TEMP_ENV_VAR'] = 'original value' >>> print(os.environ['TEMP_ENV_VAR']) original value >>> with CtypesEnviron(TEMP_ENV_VAR='temporary value'): ... print(os.envion['TEMP_ENV_VAR']) temporary value >>> print(os.environ['TEMP_ENV_VAR']) original value .. testcode:: :hide: if orig_env_val is None: del os.environ['TEMP_ENV_VAR'] else: os.environ['TEMP_ENV_VAR'] = orig_env_val Úkernel32Z find_msvcrtcCsdS)Nrrrrr Ú´r2zCtypesEnviron.z!api-ms-win-crt-environment-l1-1-0ÚmsvcrtZmsvcr120Zmsvcr110Zmsvcr100Zmsvcr90Zmsvcr80Zmsvcr71Zmsvcr70cKsbttƒƒg|_|j dd„|jDƒ¡tjdk r@tj ¡dt_xt|ƒD]\}}|||<qJWdS)Ncss|]}| ¡rt|ƒVqdS)N)r r)Ú.0rrrr ú Äsz)CtypesEnviron.__init__..) rr8Ú interfacesÚextendÚDLLsrrrr)r'ÚkwdsÚkÚvrrr r)Às    zCtypesEnviron.__init__cCs|S)Nr)r'rrr Ú __enter__ÐszCtypesEnviron.__enter__cCs | ¡dS)N)r*)r'Úexc_typeÚ exc_valueÚ exc_tracebackrrr Ú__exit__ÓszCtypesEnviron.__exit__cCs xt|jƒD] }| ¡q WdS)aFRestore the environment to the original state This restores all environment variables modified through this object to the state they were in before this instance made any changes. Note that any changes made directly to os.environ outside this instance will not be detected / undone. N)Úreversedrfr*)r'Úlibrrr r*ÖszCtypesEnviron.restorecCs tj|S)z=Return the current environment variable value from os.environ)r$r%)r'r(rrr r-êszCtypesEnviron.__getitem__cCs |tjkS)z'Return True if the key is in os.environ)r$r%)r'r(rrr Ú __contains__îszCtypesEnviron.__contains__cCsx|jD] }|||<qWdS)z=Set an environment variable in all known runtime environmentsN)rf)r'r(rrrrrr r0òs zCtypesEnviron.__setitem__cCsx|jD] }||=qWdS)zBRemove an environment variable from all known runtime environmentsN)rf)r'r(rrrrr r3÷s zCtypesEnviron.__delitem__N)r4r5r6r7rOr>Úgetattrrrrhr)rlrpr*r-rsr0r3rrrr r`zs(0  r`)r)rrr$rrr r rrrÚobjectrr8r>rOr`rrrr Ú s    *=6La