author | Pierre-Yves David <pierre-yves.david@octobus.net> |
Wed, 21 Feb 2024 10:41:09 +0100 | |
changeset 51409 | 2f39c7aeb549 |
parent 50303 | 0d3690f8ce2a |
permissions | -rw-r--r-- |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
1 |
/* |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
2 |
dirs.c - dynamic directory diddling for dirstates |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
3 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
4 |
Copyright 2013 Facebook |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
5 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
6 |
This software may be used and distributed according to the terms of |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
7 |
the GNU General Public License, incorporated herein by reference. |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
8 |
*/ |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
9 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
10 |
#define PY_SSIZE_T_CLEAN |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
11 |
#include <Python.h> |
43658
0796e266d26b
dirs: resolve fuzzer OOM situation by disallowing deep directory hierarchies
Augie Fackler <augie@google.com>
parents:
43494
diff
changeset
|
12 |
#include <string.h> |
34438
b90e8da190da
cext: reorder #include
Gregory Szorc <gregory.szorc@gmail.com>
parents:
32372
diff
changeset
|
13 |
|
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
14 |
#include "util.h" |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
15 |
|
50303
0d3690f8ce2a
cext: fix for PyLong refactoring in CPython 3.12
Mads Kiilerich <mads@kiilerich.com>
parents:
48819
diff
changeset
|
16 |
#if PY_VERSION_HEX >= 0x030C00A5 |
0d3690f8ce2a
cext: fix for PyLong refactoring in CPython 3.12
Mads Kiilerich <mads@kiilerich.com>
parents:
48819
diff
changeset
|
17 |
#define PYLONG_VALUE(o) ((PyLongObject *)o)->long_value.ob_digit[0] |
0d3690f8ce2a
cext: fix for PyLong refactoring in CPython 3.12
Mads Kiilerich <mads@kiilerich.com>
parents:
48819
diff
changeset
|
18 |
#else |
43838
a47ccdcce4f9
dirs: fix out-of-bounds access in Py3
Martin von Zweigbergk <martinvonz@google.com>
parents:
43658
diff
changeset
|
19 |
#define PYLONG_VALUE(o) ((PyLongObject *)o)->ob_digit[0] |
50303
0d3690f8ce2a
cext: fix for PyLong refactoring in CPython 3.12
Mads Kiilerich <mads@kiilerich.com>
parents:
48819
diff
changeset
|
20 |
#endif |
30106
cb3048746dae
dirs: port PyInt code to work on Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30105
diff
changeset
|
21 |
|
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
22 |
/* |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
23 |
* This is a multiset of directory names, built from the files that |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
24 |
* appear in a dirstate or manifest. |
18901
66d3aebe2d95
dirs: use mutable integers internally
Bryan O'Sullivan <bryano@fb.com>
parents:
18900
diff
changeset
|
25 |
* |
66d3aebe2d95
dirs: use mutable integers internally
Bryan O'Sullivan <bryano@fb.com>
parents:
18900
diff
changeset
|
26 |
* A few implementation notes: |
66d3aebe2d95
dirs: use mutable integers internally
Bryan O'Sullivan <bryano@fb.com>
parents:
18900
diff
changeset
|
27 |
* |
66d3aebe2d95
dirs: use mutable integers internally
Bryan O'Sullivan <bryano@fb.com>
parents:
18900
diff
changeset
|
28 |
* We modify Python integers for refcounting, but those integers are |
66d3aebe2d95
dirs: use mutable integers internally
Bryan O'Sullivan <bryano@fb.com>
parents:
18900
diff
changeset
|
29 |
* never visible to Python code. |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
30 |
*/ |
43232
be178b5d91c8
dirs: tag a struct as not being formattable
Augie Fackler <augie@google.com>
parents:
43229
diff
changeset
|
31 |
/* clang-format off */ |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
32 |
typedef struct { |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
33 |
PyObject_HEAD |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
34 |
PyObject *dict; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
35 |
} dirsObject; |
43232
be178b5d91c8
dirs: tag a struct as not being formattable
Augie Fackler <augie@google.com>
parents:
43229
diff
changeset
|
36 |
/* clang-format on */ |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
37 |
|
25093
fe3a72a3e7ca
dirs.c: pass C string, not Python string, to _finddir()
Martin von Zweigbergk <martinvonz@google.com>
parents:
25092
diff
changeset
|
38 |
static inline Py_ssize_t _finddir(const char *path, Py_ssize_t pos) |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
39 |
{ |
25015
b3a68fb8b859
dirs: back out forward-searching in finddirs()
Martin von Zweigbergk <martinvonz@google.com>
parents:
24651
diff
changeset
|
40 |
while (pos != -1) { |
25093
fe3a72a3e7ca
dirs.c: pass C string, not Python string, to _finddir()
Martin von Zweigbergk <martinvonz@google.com>
parents:
25092
diff
changeset
|
41 |
if (path[pos] == '/') |
25015
b3a68fb8b859
dirs: back out forward-searching in finddirs()
Martin von Zweigbergk <martinvonz@google.com>
parents:
24651
diff
changeset
|
42 |
break; |
b3a68fb8b859
dirs: back out forward-searching in finddirs()
Martin von Zweigbergk <martinvonz@google.com>
parents:
24651
diff
changeset
|
43 |
pos -= 1; |
b3a68fb8b859
dirs: back out forward-searching in finddirs()
Martin von Zweigbergk <martinvonz@google.com>
parents:
24651
diff
changeset
|
44 |
} |
42343
d8e55c0c642c
util: make util.dirs() and util.finddirs() include root directory (API)
Martin von Zweigbergk <martinvonz@google.com>
parents:
34438
diff
changeset
|
45 |
if (pos == -1) { |
43233
ea62d7b06c12
dirs: give formatting oversight to clang-format
Augie Fackler <augie@google.com>
parents:
43232
diff
changeset
|
46 |
return 0; |
42343
d8e55c0c642c
util: make util.dirs() and util.finddirs() include root directory (API)
Martin von Zweigbergk <martinvonz@google.com>
parents:
34438
diff
changeset
|
47 |
} |
25015
b3a68fb8b859
dirs: back out forward-searching in finddirs()
Martin von Zweigbergk <martinvonz@google.com>
parents:
24651
diff
changeset
|
48 |
|
b3a68fb8b859
dirs: back out forward-searching in finddirs()
Martin von Zweigbergk <martinvonz@google.com>
parents:
24651
diff
changeset
|
49 |
return pos; |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
50 |
} |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
51 |
|
43658
0796e266d26b
dirs: resolve fuzzer OOM situation by disallowing deep directory hierarchies
Augie Fackler <augie@google.com>
parents:
43494
diff
changeset
|
52 |
/* Mercurial will fail to run on directory hierarchies deeper than |
0796e266d26b
dirs: resolve fuzzer OOM situation by disallowing deep directory hierarchies
Augie Fackler <augie@google.com>
parents:
43494
diff
changeset
|
53 |
* this constant, so we should try and keep this constant as big as |
0796e266d26b
dirs: resolve fuzzer OOM situation by disallowing deep directory hierarchies
Augie Fackler <augie@google.com>
parents:
43494
diff
changeset
|
54 |
* possible. |
0796e266d26b
dirs: resolve fuzzer OOM situation by disallowing deep directory hierarchies
Augie Fackler <augie@google.com>
parents:
43494
diff
changeset
|
55 |
*/ |
0796e266d26b
dirs: resolve fuzzer OOM situation by disallowing deep directory hierarchies
Augie Fackler <augie@google.com>
parents:
43494
diff
changeset
|
56 |
#define MAX_DIRS_DEPTH 2048 |
0796e266d26b
dirs: resolve fuzzer OOM situation by disallowing deep directory hierarchies
Augie Fackler <augie@google.com>
parents:
43494
diff
changeset
|
57 |
|
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
58 |
static int _addpath(PyObject *dirs, PyObject *path) |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
59 |
{ |
30105
b2f90d8878ac
dirs: convert PyString to PyBytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30104
diff
changeset
|
60 |
const char *cpath = PyBytes_AS_STRING(path); |
b2f90d8878ac
dirs: convert PyString to PyBytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30104
diff
changeset
|
61 |
Py_ssize_t pos = PyBytes_GET_SIZE(path); |
18901
66d3aebe2d95
dirs: use mutable integers internally
Bryan O'Sullivan <bryano@fb.com>
parents:
18900
diff
changeset
|
62 |
PyObject *key = NULL; |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
63 |
int ret = -1; |
43658
0796e266d26b
dirs: resolve fuzzer OOM situation by disallowing deep directory hierarchies
Augie Fackler <augie@google.com>
parents:
43494
diff
changeset
|
64 |
size_t num_slashes = 0; |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
65 |
|
30107
da08f4707282
dirs: document performance reasons for bypassing Python C API
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30106
diff
changeset
|
66 |
/* This loop is super critical for performance. That's why we inline |
43233
ea62d7b06c12
dirs: give formatting oversight to clang-format
Augie Fackler <augie@google.com>
parents:
43232
diff
changeset
|
67 |
* access to Python structs instead of going through a supported API. |
ea62d7b06c12
dirs: give formatting oversight to clang-format
Augie Fackler <augie@google.com>
parents:
43232
diff
changeset
|
68 |
* The implementation, therefore, is heavily dependent on CPython |
ea62d7b06c12
dirs: give formatting oversight to clang-format
Augie Fackler <augie@google.com>
parents:
43232
diff
changeset
|
69 |
* implementation details. We also commit violations of the Python |
ea62d7b06c12
dirs: give formatting oversight to clang-format
Augie Fackler <augie@google.com>
parents:
43232
diff
changeset
|
70 |
* "protocol" such as mutating immutable objects. But since we only |
ea62d7b06c12
dirs: give formatting oversight to clang-format
Augie Fackler <augie@google.com>
parents:
43232
diff
changeset
|
71 |
* mutate objects created in this function or in other well-defined |
ea62d7b06c12
dirs: give formatting oversight to clang-format
Augie Fackler <augie@google.com>
parents:
43232
diff
changeset
|
72 |
* locations, the references are known so these violations should go |
ea62d7b06c12
dirs: give formatting oversight to clang-format
Augie Fackler <augie@google.com>
parents:
43232
diff
changeset
|
73 |
* unnoticed. */ |
25093
fe3a72a3e7ca
dirs.c: pass C string, not Python string, to _finddir()
Martin von Zweigbergk <martinvonz@google.com>
parents:
25092
diff
changeset
|
74 |
while ((pos = _finddir(cpath, pos - 1)) != -1) { |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
75 |
PyObject *val; |
43658
0796e266d26b
dirs: resolve fuzzer OOM situation by disallowing deep directory hierarchies
Augie Fackler <augie@google.com>
parents:
43494
diff
changeset
|
76 |
++num_slashes; |
0796e266d26b
dirs: resolve fuzzer OOM situation by disallowing deep directory hierarchies
Augie Fackler <augie@google.com>
parents:
43494
diff
changeset
|
77 |
if (num_slashes > MAX_DIRS_DEPTH) { |
0796e266d26b
dirs: resolve fuzzer OOM situation by disallowing deep directory hierarchies
Augie Fackler <augie@google.com>
parents:
43494
diff
changeset
|
78 |
PyErr_SetString(PyExc_ValueError, |
0796e266d26b
dirs: resolve fuzzer OOM situation by disallowing deep directory hierarchies
Augie Fackler <augie@google.com>
parents:
43494
diff
changeset
|
79 |
"Directory hierarchy too deep."); |
0796e266d26b
dirs: resolve fuzzer OOM situation by disallowing deep directory hierarchies
Augie Fackler <augie@google.com>
parents:
43494
diff
changeset
|
80 |
goto bail; |
0796e266d26b
dirs: resolve fuzzer OOM situation by disallowing deep directory hierarchies
Augie Fackler <augie@google.com>
parents:
43494
diff
changeset
|
81 |
} |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
82 |
|
43494
5d40317d42b7
dirs: reject consecutive slashes in paths
Augie Fackler <augie@google.com>
parents:
43233
diff
changeset
|
83 |
/* Sniff for trailing slashes, a marker of an invalid input. */ |
5d40317d42b7
dirs: reject consecutive slashes in paths
Augie Fackler <augie@google.com>
parents:
43233
diff
changeset
|
84 |
if (pos > 0 && cpath[pos - 1] == '/') { |
5d40317d42b7
dirs: reject consecutive slashes in paths
Augie Fackler <augie@google.com>
parents:
43233
diff
changeset
|
85 |
PyErr_SetString( |
5d40317d42b7
dirs: reject consecutive slashes in paths
Augie Fackler <augie@google.com>
parents:
43233
diff
changeset
|
86 |
PyExc_ValueError, |
5d40317d42b7
dirs: reject consecutive slashes in paths
Augie Fackler <augie@google.com>
parents:
43233
diff
changeset
|
87 |
"found invalid consecutive slashes in path"); |
5d40317d42b7
dirs: reject consecutive slashes in paths
Augie Fackler <augie@google.com>
parents:
43233
diff
changeset
|
88 |
goto bail; |
5d40317d42b7
dirs: reject consecutive slashes in paths
Augie Fackler <augie@google.com>
parents:
43233
diff
changeset
|
89 |
} |
5d40317d42b7
dirs: reject consecutive slashes in paths
Augie Fackler <augie@google.com>
parents:
43233
diff
changeset
|
90 |
|
43229
9fa941faef94
dirs: remove mutable string optimization at all
Yuya Nishihara <yuya@tcha.org>
parents:
43149
diff
changeset
|
91 |
key = PyBytes_FromStringAndSize(cpath, pos); |
9fa941faef94
dirs: remove mutable string optimization at all
Yuya Nishihara <yuya@tcha.org>
parents:
43149
diff
changeset
|
92 |
if (key == NULL) |
9fa941faef94
dirs: remove mutable string optimization at all
Yuya Nishihara <yuya@tcha.org>
parents:
43149
diff
changeset
|
93 |
goto bail; |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
94 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
95 |
val = PyDict_GetItem(dirs, key); |
18901
66d3aebe2d95
dirs: use mutable integers internally
Bryan O'Sullivan <bryano@fb.com>
parents:
18900
diff
changeset
|
96 |
if (val != NULL) { |
30106
cb3048746dae
dirs: port PyInt code to work on Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30105
diff
changeset
|
97 |
PYLONG_VALUE(val) += 1; |
43229
9fa941faef94
dirs: remove mutable string optimization at all
Yuya Nishihara <yuya@tcha.org>
parents:
43149
diff
changeset
|
98 |
Py_CLEAR(key); |
25016
42e89b87ca79
dirs: speed up by storing number of direct children per dir
Martin von Zweigbergk <martinvonz@google.com>
parents:
25015
diff
changeset
|
99 |
break; |
18901
66d3aebe2d95
dirs: use mutable integers internally
Bryan O'Sullivan <bryano@fb.com>
parents:
18900
diff
changeset
|
100 |
} |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
101 |
|
18901
66d3aebe2d95
dirs: use mutable integers internally
Bryan O'Sullivan <bryano@fb.com>
parents:
18900
diff
changeset
|
102 |
/* Force Python to not reuse a small shared int. */ |
30106
cb3048746dae
dirs: port PyInt code to work on Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30105
diff
changeset
|
103 |
val = PyLong_FromLong(0x1eadbeef); |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
104 |
|
18901
66d3aebe2d95
dirs: use mutable integers internally
Bryan O'Sullivan <bryano@fb.com>
parents:
18900
diff
changeset
|
105 |
if (val == NULL) |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
106 |
goto bail; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
107 |
|
30106
cb3048746dae
dirs: port PyInt code to work on Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30105
diff
changeset
|
108 |
PYLONG_VALUE(val) = 1; |
18901
66d3aebe2d95
dirs: use mutable integers internally
Bryan O'Sullivan <bryano@fb.com>
parents:
18900
diff
changeset
|
109 |
ret = PyDict_SetItem(dirs, key, val); |
66d3aebe2d95
dirs: use mutable integers internally
Bryan O'Sullivan <bryano@fb.com>
parents:
18900
diff
changeset
|
110 |
Py_DECREF(val); |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
111 |
if (ret == -1) |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
112 |
goto bail; |
24651
67241ee427cf
dirs._addpath: reinstate use of Py_CLEAR
Siddharth Agarwal <sid0@fb.com>
parents:
24624
diff
changeset
|
113 |
Py_CLEAR(key); |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
114 |
} |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
115 |
ret = 0; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
116 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
117 |
bail: |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
118 |
Py_XDECREF(key); |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
119 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
120 |
return ret; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
121 |
} |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
122 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
123 |
static int _delpath(PyObject *dirs, PyObject *path) |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
124 |
{ |
30105
b2f90d8878ac
dirs: convert PyString to PyBytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30104
diff
changeset
|
125 |
char *cpath = PyBytes_AS_STRING(path); |
b2f90d8878ac
dirs: convert PyString to PyBytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30104
diff
changeset
|
126 |
Py_ssize_t pos = PyBytes_GET_SIZE(path); |
18901
66d3aebe2d95
dirs: use mutable integers internally
Bryan O'Sullivan <bryano@fb.com>
parents:
18900
diff
changeset
|
127 |
PyObject *key = NULL; |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
128 |
int ret = -1; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
129 |
|
25093
fe3a72a3e7ca
dirs.c: pass C string, not Python string, to _finddir()
Martin von Zweigbergk <martinvonz@google.com>
parents:
25092
diff
changeset
|
130 |
while ((pos = _finddir(cpath, pos - 1)) != -1) { |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
131 |
PyObject *val; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
132 |
|
30105
b2f90d8878ac
dirs: convert PyString to PyBytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30104
diff
changeset
|
133 |
key = PyBytes_FromStringAndSize(cpath, pos); |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
134 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
135 |
if (key == NULL) |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
136 |
goto bail; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
137 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
138 |
val = PyDict_GetItem(dirs, key); |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
139 |
if (val == NULL) { |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
140 |
PyErr_SetString(PyExc_ValueError, |
43233
ea62d7b06c12
dirs: give formatting oversight to clang-format
Augie Fackler <augie@google.com>
parents:
43232
diff
changeset
|
141 |
"expected a value, found none"); |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
142 |
goto bail; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
143 |
} |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
144 |
|
30106
cb3048746dae
dirs: port PyInt code to work on Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30105
diff
changeset
|
145 |
if (--PYLONG_VALUE(val) <= 0) { |
25016
42e89b87ca79
dirs: speed up by storing number of direct children per dir
Martin von Zweigbergk <martinvonz@google.com>
parents:
25015
diff
changeset
|
146 |
if (PyDict_DelItem(dirs, key) == -1) |
42e89b87ca79
dirs: speed up by storing number of direct children per dir
Martin von Zweigbergk <martinvonz@google.com>
parents:
25015
diff
changeset
|
147 |
goto bail; |
42e89b87ca79
dirs: speed up by storing number of direct children per dir
Martin von Zweigbergk <martinvonz@google.com>
parents:
25015
diff
changeset
|
148 |
} else |
42e89b87ca79
dirs: speed up by storing number of direct children per dir
Martin von Zweigbergk <martinvonz@google.com>
parents:
25015
diff
changeset
|
149 |
break; |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
150 |
Py_CLEAR(key); |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
151 |
} |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
152 |
ret = 0; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
153 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
154 |
bail: |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
155 |
Py_XDECREF(key); |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
156 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
157 |
return ret; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
158 |
} |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
159 |
|
47944
e02f9af7aed1
pathutil: replace the `skip` argument of `dirs` with a boolean
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47539
diff
changeset
|
160 |
static int dirs_fromdict(PyObject *dirs, PyObject *source, bool only_tracked) |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
161 |
{ |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
162 |
PyObject *key, *value; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
163 |
Py_ssize_t pos = 0; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
164 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
165 |
while (PyDict_Next(source, &pos, &key, &value)) { |
30105
b2f90d8878ac
dirs: convert PyString to PyBytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30104
diff
changeset
|
166 |
if (!PyBytes_Check(key)) { |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
167 |
PyErr_SetString(PyExc_TypeError, "expected string key"); |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
168 |
return -1; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
169 |
} |
47944
e02f9af7aed1
pathutil: replace the `skip` argument of `dirs` with a boolean
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47539
diff
changeset
|
170 |
if (only_tracked) { |
21809
e250b8300e6e
parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents:
18902
diff
changeset
|
171 |
if (!dirstate_tuple_check(value)) { |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
172 |
PyErr_SetString(PyExc_TypeError, |
43233
ea62d7b06c12
dirs: give formatting oversight to clang-format
Augie Fackler <augie@google.com>
parents:
43232
diff
changeset
|
173 |
"expected a dirstate tuple"); |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
174 |
return -1; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
175 |
} |
47948
83f0e93ec34b
dirstate-item: move the C implementation to the same logic
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47944
diff
changeset
|
176 |
if (!(((dirstateItemObject *)value)->flags & |
83f0e93ec34b
dirstate-item: move the C implementation to the same logic
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47944
diff
changeset
|
177 |
dirstate_flag_wc_tracked)) |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
178 |
continue; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
179 |
} |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
180 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
181 |
if (_addpath(dirs, key) == -1) |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
182 |
return -1; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
183 |
} |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
184 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
185 |
return 0; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
186 |
} |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
187 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
188 |
static int dirs_fromiter(PyObject *dirs, PyObject *source) |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
189 |
{ |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
190 |
PyObject *iter, *item = NULL; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
191 |
int ret; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
192 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
193 |
iter = PyObject_GetIter(source); |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
194 |
if (iter == NULL) |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
195 |
return -1; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
196 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
197 |
while ((item = PyIter_Next(iter)) != NULL) { |
30105
b2f90d8878ac
dirs: convert PyString to PyBytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30104
diff
changeset
|
198 |
if (!PyBytes_Check(item)) { |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
199 |
PyErr_SetString(PyExc_TypeError, "expected string"); |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
200 |
break; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
201 |
} |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
202 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
203 |
if (_addpath(dirs, item) == -1) |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
204 |
break; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
205 |
Py_CLEAR(item); |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
206 |
} |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
207 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
208 |
ret = PyErr_Occurred() ? -1 : 0; |
23960
bca4b6f126f2
dirs: fix leak of iterator in dirs_fromiter
Augie Fackler <augie@google.com>
parents:
21809
diff
changeset
|
209 |
Py_DECREF(iter); |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
210 |
Py_XDECREF(item); |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
211 |
return ret; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
212 |
} |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
213 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
214 |
/* |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
215 |
* Calculate a refcounted set of directory names for the files in a |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
216 |
* dirstate. |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
217 |
*/ |
47944
e02f9af7aed1
pathutil: replace the `skip` argument of `dirs` with a boolean
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47539
diff
changeset
|
218 |
static int dirs_init(dirsObject *self, PyObject *args, PyObject *kwargs) |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
219 |
{ |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
220 |
PyObject *dirs = NULL, *source = NULL; |
47944
e02f9af7aed1
pathutil: replace the `skip` argument of `dirs` with a boolean
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47539
diff
changeset
|
221 |
int only_tracked = 0; |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
222 |
int ret = -1; |
47944
e02f9af7aed1
pathutil: replace the `skip` argument of `dirs` with a boolean
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47539
diff
changeset
|
223 |
static char *keywords_name[] = {"map", "only_tracked", NULL}; |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
224 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
225 |
self->dict = NULL; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
226 |
|
47944
e02f9af7aed1
pathutil: replace the `skip` argument of `dirs` with a boolean
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47539
diff
changeset
|
227 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oi:__init__", |
e02f9af7aed1
pathutil: replace the `skip` argument of `dirs` with a boolean
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47539
diff
changeset
|
228 |
keywords_name, &source, &only_tracked)) |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
229 |
return -1; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
230 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
231 |
dirs = PyDict_New(); |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
232 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
233 |
if (dirs == NULL) |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
234 |
return -1; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
235 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
236 |
if (source == NULL) |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
237 |
ret = 0; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
238 |
else if (PyDict_Check(source)) |
47944
e02f9af7aed1
pathutil: replace the `skip` argument of `dirs` with a boolean
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47539
diff
changeset
|
239 |
ret = dirs_fromdict(dirs, source, (bool)only_tracked); |
e02f9af7aed1
pathutil: replace the `skip` argument of `dirs` with a boolean
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47539
diff
changeset
|
240 |
else if (only_tracked) |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
241 |
PyErr_SetString(PyExc_ValueError, |
47944
e02f9af7aed1
pathutil: replace the `skip` argument of `dirs` with a boolean
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
47539
diff
changeset
|
242 |
"`only_tracked` is only supported " |
43233
ea62d7b06c12
dirs: give formatting oversight to clang-format
Augie Fackler <augie@google.com>
parents:
43232
diff
changeset
|
243 |
"with a dict source"); |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
244 |
else |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
245 |
ret = dirs_fromiter(dirs, source); |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
246 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
247 |
if (ret == -1) |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
248 |
Py_XDECREF(dirs); |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
249 |
else |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
250 |
self->dict = dirs; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
251 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
252 |
return ret; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
253 |
} |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
254 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
255 |
PyObject *dirs_addpath(dirsObject *self, PyObject *args) |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
256 |
{ |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
257 |
PyObject *path; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
258 |
|
30105
b2f90d8878ac
dirs: convert PyString to PyBytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30104
diff
changeset
|
259 |
if (!PyArg_ParseTuple(args, "O!:addpath", &PyBytes_Type, &path)) |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
260 |
return NULL; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
261 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
262 |
if (_addpath(self->dict, path) == -1) |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
263 |
return NULL; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
264 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
265 |
Py_RETURN_NONE; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
266 |
} |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
267 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
268 |
static PyObject *dirs_delpath(dirsObject *self, PyObject *args) |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
269 |
{ |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
270 |
PyObject *path; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
271 |
|
30105
b2f90d8878ac
dirs: convert PyString to PyBytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30104
diff
changeset
|
272 |
if (!PyArg_ParseTuple(args, "O!:delpath", &PyBytes_Type, &path)) |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
273 |
return NULL; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
274 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
275 |
if (_delpath(self->dict, path) == -1) |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
276 |
return NULL; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
277 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
278 |
Py_RETURN_NONE; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
279 |
} |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
280 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
281 |
static int dirs_contains(dirsObject *self, PyObject *value) |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
282 |
{ |
30105
b2f90d8878ac
dirs: convert PyString to PyBytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
30104
diff
changeset
|
283 |
return PyBytes_Check(value) ? PyDict_Contains(self->dict, value) : 0; |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
284 |
} |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
285 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
286 |
static void dirs_dealloc(dirsObject *self) |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
287 |
{ |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
288 |
Py_XDECREF(self->dict); |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
289 |
PyObject_Del(self); |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
290 |
} |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
291 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
292 |
static PyObject *dirs_iter(dirsObject *self) |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
293 |
{ |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
294 |
return PyObject_GetIter(self->dict); |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
295 |
} |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
296 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
297 |
static PySequenceMethods dirs_sequence_methods; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
298 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
299 |
static PyMethodDef dirs_methods[] = { |
43233
ea62d7b06c12
dirs: give formatting oversight to clang-format
Augie Fackler <augie@google.com>
parents:
43232
diff
changeset
|
300 |
{"addpath", (PyCFunction)dirs_addpath, METH_VARARGS, "add a path"}, |
ea62d7b06c12
dirs: give formatting oversight to clang-format
Augie Fackler <augie@google.com>
parents:
43232
diff
changeset
|
301 |
{"delpath", (PyCFunction)dirs_delpath, METH_VARARGS, "remove a path"}, |
ea62d7b06c12
dirs: give formatting oversight to clang-format
Augie Fackler <augie@google.com>
parents:
43232
diff
changeset
|
302 |
{NULL} /* Sentinel */ |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
303 |
}; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
304 |
|
43233
ea62d7b06c12
dirs: give formatting oversight to clang-format
Augie Fackler <augie@google.com>
parents:
43232
diff
changeset
|
305 |
static PyTypeObject dirsType = {PyVarObject_HEAD_INIT(NULL, 0)}; |
18900
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
306 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
307 |
void dirs_module_init(PyObject *mod) |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
308 |
{ |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
309 |
dirs_sequence_methods.sq_contains = (objobjproc)dirs_contains; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
310 |
dirsType.tp_name = "parsers.dirs"; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
311 |
dirsType.tp_new = PyType_GenericNew; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
312 |
dirsType.tp_basicsize = sizeof(dirsObject); |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
313 |
dirsType.tp_dealloc = (destructor)dirs_dealloc; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
314 |
dirsType.tp_as_sequence = &dirs_sequence_methods; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
315 |
dirsType.tp_flags = Py_TPFLAGS_DEFAULT; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
316 |
dirsType.tp_doc = "dirs"; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
317 |
dirsType.tp_iter = (getiterfunc)dirs_iter; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
318 |
dirsType.tp_methods = dirs_methods; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
319 |
dirsType.tp_init = (initproc)dirs_init; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
320 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
321 |
if (PyType_Ready(&dirsType) < 0) |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
322 |
return; |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
323 |
Py_INCREF(&dirsType); |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
324 |
|
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
325 |
PyModule_AddObject(mod, "dirs", (PyObject *)&dirsType); |
02ee846b246a
scmutil: rewrite dirs in C, use if available
Bryan O'Sullivan <bryano@fb.com>
parents:
diff
changeset
|
326 |
} |