Get a temporary directory where socket files will be created. To prevent additional imports, pass a pre-imported 'tempfile' module.
(tempfile)
| 153 | current_process._config['tempdir'] = None |
| 154 | |
| 155 | def _get_base_temp_dir(tempfile): |
| 156 | """Get a temporary directory where socket files will be created. |
| 157 | |
| 158 | To prevent additional imports, pass a pre-imported 'tempfile' module. |
| 159 | """ |
| 160 | if os.name == 'nt': |
| 161 | return None |
| 162 | # Most of the time, the default temporary directory is /tmp. Thus, |
| 163 | # listener sockets files "$TMPDIR/pymp-XXXXXXXX/sock-XXXXXXXX" do |
| 164 | # not have a path length exceeding SUN_PATH_MAX. |
| 165 | # |
| 166 | # If users specify their own temporary directory, we may be unable |
| 167 | # to create those files. Therefore, we fall back to the system-wide |
| 168 | # temporary directory /tmp, assumed to exist on POSIX systems. |
| 169 | # |
| 170 | # See https://github.com/python/cpython/issues/132124. |
| 171 | base_tempdir = tempfile.gettempdir() |
| 172 | # Files created in a temporary directory are suffixed by a string |
| 173 | # generated by tempfile._RandomNameSequence, which, by design, |
| 174 | # is 8 characters long. |
| 175 | # |
| 176 | # Thus, the socket file path length (without NULL terminator) will be: |
| 177 | # |
| 178 | # len(base_tempdir + '/pymp-XXXXXXXX' + '/sock-XXXXXXXX') |
| 179 | sun_path_len = len(base_tempdir) + 14 + 14 |
| 180 | # Strict inequality to account for the NULL terminator. |
| 181 | # See https://github.com/python/cpython/issues/140734. |
| 182 | if sun_path_len < _SUN_PATH_MAX: |
| 183 | return base_tempdir |
| 184 | # Fallback to the default system-wide temporary directory. |
| 185 | # This ignores user-defined environment variables. |
| 186 | # |
| 187 | # On POSIX systems, /tmp MUST be writable by any application [1]. |
| 188 | # We however emit a warning if this is not the case to prevent |
| 189 | # obscure errors later in the execution. |
| 190 | # |
| 191 | # On some legacy systems, /var/tmp and /usr/tmp can be present |
| 192 | # and will be used instead. |
| 193 | # |
| 194 | # [1]: https://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch03s18.html |
| 195 | dirlist = ['/tmp', '/var/tmp', '/usr/tmp'] |
| 196 | try: |
| 197 | base_system_tempdir = tempfile._get_default_tempdir(dirlist) |
| 198 | except FileNotFoundError: |
| 199 | warn("Process-wide temporary directory %s will not be usable for " |
| 200 | "creating socket files and no usable system-wide temporary " |
| 201 | "directory was found in %s", base_tempdir, dirlist) |
| 202 | # At this point, the system-wide temporary directory is not usable |
| 203 | # but we may assume that the user-defined one is, even if we will |
| 204 | # not be able to write socket files out there. |
| 205 | return base_tempdir |
| 206 | warn("Ignoring user-defined temporary directory: %s", base_tempdir) |
| 207 | # at most max(map(len, dirlist)) + 14 + 14 = 36 characters |
| 208 | assert len(base_system_tempdir) + 14 + 14 < _SUN_PATH_MAX |
| 209 | return base_system_tempdir |
| 210 | |
| 211 | def get_temp_dir(): |
| 212 | # get name of a temp directory which will be automatically cleaned up |
no test coverage detected
searching dependent graphs…