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