Show More
@@ -9,7 +9,6 b'' | |||
|
9 | 9 | |
|
10 | 10 | #define PY_SSIZE_T_CLEAN |
|
11 | 11 | #include <Python.h> |
|
12 | #include <string.h> | |
|
13 | 12 | #include "util.h" |
|
14 | 13 | |
|
15 | 14 | /* |
@@ -33,19 +32,23 b' static inline Py_ssize_t _finddir(PyObje' | |||
|
33 | 32 | { |
|
34 | 33 | const char *s = PyString_AS_STRING(path); |
|
35 | 34 | |
|
36 | const char *ret = strchr(s + pos, '/'); | |
|
37 | return (ret != NULL) ? (ret - s) : -1; | |
|
35 | while (pos != -1) { | |
|
36 | if (s[pos] == '/') | |
|
37 | break; | |
|
38 | pos -= 1; | |
|
39 | } | |
|
40 | ||
|
41 | return pos; | |
|
38 | 42 | } |
|
39 | 43 | |
|
40 | 44 | static int _addpath(PyObject *dirs, PyObject *path) |
|
41 | 45 | { |
|
42 | char *cpath = PyString_AS_STRING(path); | |
|
43 |
Py_ssize_t |
|
|
44 | Py_ssize_t pos = -1; | |
|
46 | const char *cpath = PyString_AS_STRING(path); | |
|
47 | Py_ssize_t pos = PyString_GET_SIZE(path); | |
|
45 | 48 | PyObject *key = NULL; |
|
46 | 49 | int ret = -1; |
|
47 | 50 | |
|
48 |
while ((pos = _finddir(path, pos |
|
|
51 | while ((pos = _finddir(path, pos - 1)) != -1) { | |
|
49 | 52 | PyObject *val; |
|
50 | 53 | |
|
51 | 54 | /* It's likely that every prefix already has an entry |
@@ -53,18 +56,10 b' static int _addpath(PyObject *dirs, PyOb' | |||
|
53 | 56 | deallocating a string for each prefix we check. */ |
|
54 | 57 | if (key != NULL) |
|
55 | 58 | ((PyStringObject *)key)->ob_shash = -1; |
|
56 | else if (pos != 0) { | |
|
57 | /* pos >= 1, which means that len >= 2. This is | |
|
58 | guaranteed to produce a non-interned string. */ | |
|
59 | key = PyString_FromStringAndSize(cpath, len); | |
|
60 | if (key == NULL) | |
|
61 | goto bail; | |
|
62 | } else { | |
|
63 | /* pos == 0, which means we need to increment the dir | |
|
64 | count for the empty string. We need to make sure we | |
|
65 | don't muck around with interned strings, so throw it | |
|
66 | away later. */ | |
|
67 | key = PyString_FromString(""); | |
|
59 | else { | |
|
60 | /* Force Python to not reuse a small shared string. */ | |
|
61 | key = PyString_FromStringAndSize(cpath, | |
|
62 | pos < 2 ? 2 : pos); | |
|
68 | 63 | if (key == NULL) |
|
69 | 64 | goto bail; |
|
70 | 65 | } |
@@ -74,10 +69,6 b' static int _addpath(PyObject *dirs, PyOb' | |||
|
74 | 69 | val = PyDict_GetItem(dirs, key); |
|
75 | 70 | if (val != NULL) { |
|
76 | 71 | PyInt_AS_LONG(val) += 1; |
|
77 | if (pos != 0) | |
|
78 | PyString_AS_STRING(key)[pos] = '/'; | |
|
79 | else | |
|
80 | key = NULL; | |
|
81 | 72 | continue; |
|
82 | 73 | } |
|
83 | 74 | |
@@ -92,9 +83,6 b' static int _addpath(PyObject *dirs, PyOb' | |||
|
92 | 83 | Py_DECREF(val); |
|
93 | 84 | if (ret == -1) |
|
94 | 85 | goto bail; |
|
95 | ||
|
96 | /* Clear the key out since we've already exposed it to Python | |
|
97 | and can't mutate it further. */ | |
|
98 | 86 | Py_CLEAR(key); |
|
99 | 87 | } |
|
100 | 88 | ret = 0; |
@@ -107,11 +95,11 b' bail:' | |||
|
107 | 95 | |
|
108 | 96 | static int _delpath(PyObject *dirs, PyObject *path) |
|
109 | 97 | { |
|
110 |
Py_ssize_t pos = |
|
|
98 | Py_ssize_t pos = PyString_GET_SIZE(path); | |
|
111 | 99 | PyObject *key = NULL; |
|
112 | 100 | int ret = -1; |
|
113 | 101 | |
|
114 |
while ((pos = _finddir(path, pos |
|
|
102 | while ((pos = _finddir(path, pos - 1)) != -1) { | |
|
115 | 103 | PyObject *val; |
|
116 | 104 | |
|
117 | 105 | key = PyString_FromStringAndSize(PyString_AS_STRING(path), pos); |
General Comments 0
You need to be logged in to leave comments.
Login now