summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2021-09-27 16:00:32 (GMT)
committerGitHub <noreply@github.com>2021-09-27 16:00:32 (GMT)
commitae7839bbe817329dd015f9195da308a0f3fbd3e2 (patch)
treeba710c468adef4718e3d1ed9747d2acdc830216b /Modules
parente5f13ce5b48b551c09fdd0faeafa6ecf860de51c (diff)
downloadcpython-ae7839bbe817329dd015f9195da308a0f3fbd3e2.zip
cpython-ae7839bbe817329dd015f9195da308a0f3fbd3e2.tar.gz
cpython-ae7839bbe817329dd015f9195da308a0f3fbd3e2.tar.bz2
bpo-45211: Move helpers from getpath.c to internal API. (gh-28550)
This accomplishes 2 things: * consolidates some common code between getpath.c and getpathp.c * makes the helpers available to code in other files FWIW, the signature of the join_relfile() function (in fileutils.c) intentionally mirrors that of Windows' PathCchCombineEx(). Note that this change is mostly moving code around. No behavior is meant to change. https://bugs.python.org/issue45211
Diffstat (limited to 'Modules')
-rw-r--r--Modules/getpath.c64
1 files changed, 8 insertions, 56 deletions
diff --git a/Modules/getpath.c b/Modules/getpath.c
index 363d62a..de1c6e3 100644
--- a/Modules/getpath.c
+++ b/Modules/getpath.c
@@ -115,11 +115,6 @@ extern "C" {
#define BUILD_LANDMARK L"Modules/Setup.local"
-#define DECODE_LOCALE_ERR(NAME, LEN) \
- ((LEN) == (size_t)-2) \
- ? _PyStatus_ERR("cannot decode " NAME) \
- : _PyStatus_NO_MEMORY()
-
#define PATHLEN_ERR() _PyStatus_ERR("path configuration: path too long")
typedef struct {
@@ -149,23 +144,6 @@ static const wchar_t delimiter[2] = {DELIM, '\0'};
static const wchar_t separator[2] = {SEP, '\0'};
-/* Get file status. Encode the path to the locale encoding. */
-static int
-_Py_wstat(const wchar_t* path, struct stat *buf)
-{
- int err;
- char *fname;
- fname = _Py_EncodeLocaleRaw(path, NULL);
- if (fname == NULL) {
- errno = EINVAL;
- return -1;
- }
- err = stat(fname, buf);
- PyMem_RawFree(fname);
- return err;
-}
-
-
static void
reduce(wchar_t *dir)
{
@@ -235,28 +213,18 @@ isdir(const wchar_t *filename)
static PyStatus
joinpath(wchar_t *path, const wchar_t *path2, size_t path_len)
{
- size_t n;
- if (!_Py_isabs(path2)) {
- n = wcslen(path);
- if (n >= path_len) {
+ if (_Py_isabs(path2)) {
+ if (wcslen(path2) >= path_len) {
return PATHLEN_ERR();
}
-
- if (n > 0 && path[n-1] != SEP) {
- path[n++] = SEP;
- }
+ wcscpy(path, path2);
}
else {
- n = 0;
- }
-
- size_t k = wcslen(path2);
- if (n + k >= path_len) {
- return PATHLEN_ERR();
+ if (_Py_add_relfile(path, path2, path_len) < 0) {
+ return PATHLEN_ERR();
+ }
+ return _PyStatus_OK();
}
- wcsncpy(path + n, path2, k);
- path[n + k] = '\0';
-
return _PyStatus_OK();
}
@@ -283,23 +251,7 @@ joinpath2(const wchar_t *path, const wchar_t *path2)
if (_Py_isabs(path2)) {
return _PyMem_RawWcsdup(path2);
}
-
- size_t len = wcslen(path);
- int add_sep = (len > 0 && path[len - 1] != SEP);
- len += add_sep;
- len += wcslen(path2);
-
- wchar_t *new_path = PyMem_RawMalloc((len + 1) * sizeof(wchar_t));
- if (new_path == NULL) {
- return NULL;
- }
-
- wcscpy(new_path, path);
- if (add_sep) {
- wcscat(new_path, separator);
- }
- wcscat(new_path, path2);
- return new_path;
+ return _Py_join_relfile(path, path2);
}