Details
Details
- Reviewers
indygreg - Group Reviewers
hg-reviewers - Commits
- rHGea62d7b06c12: dirs: give formatting oversight to clang-format
Diff Detail
Diff Detail
- Repository
- rHG Mercurial
- Lint
Lint Skipped - Unit
Unit Tests Skipped
| indygreg |
| hg-reviewers |
| Lint Skipped |
| Unit Tests Skipped |
| Path | Packages | |||
|---|---|---|---|---|
| M | contrib/clang-format-ignorelist (1 line) | |||
| M | mercurial/cext/dirs.c (32 lines) |
| Commit | Parents | Author | Summary | Date |
|---|---|---|---|---|
| 4b6e02554413 | 0b97a6463d48 | Augie Fackler | Oct 15 2019, 9:54 AM |
| # Files that just need to be migrated to the formatter. | # Files that just need to be migrated to the formatter. | ||||
| # Do not add new files here! | # Do not add new files here! | ||||
| mercurial/cext/dirs.c | |||||
| mercurial/cext/manifest.c | mercurial/cext/manifest.c | ||||
| mercurial/cext/osutil.c | mercurial/cext/osutil.c | ||||
| # Vendored code that we should never format: | # Vendored code that we should never format: | ||||
| contrib/python-zstandard/c-ext/bufferutil.c | contrib/python-zstandard/c-ext/bufferutil.c | ||||
| contrib/python-zstandard/c-ext/compressionchunker.c | contrib/python-zstandard/c-ext/compressionchunker.c | ||||
| contrib/python-zstandard/c-ext/compressiondict.c | contrib/python-zstandard/c-ext/compressiondict.c | ||||
| contrib/python-zstandard/c-ext/compressionparams.c | contrib/python-zstandard/c-ext/compressionparams.c | ||||
| contrib/python-zstandard/c-ext/compressionreader.c | contrib/python-zstandard/c-ext/compressionreader.c | ||||
| static inline Py_ssize_t _finddir(const char *path, Py_ssize_t pos) | static inline Py_ssize_t _finddir(const char *path, Py_ssize_t pos) | ||||
| { | { | ||||
| while (pos != -1) { | while (pos != -1) { | ||||
| if (path[pos] == '/') | if (path[pos] == '/') | ||||
| break; | break; | ||||
| pos -= 1; | pos -= 1; | ||||
| } | } | ||||
| if (pos == -1) { | if (pos == -1) { | ||||
| return 0; | return 0; | ||||
| } | } | ||||
| return pos; | return pos; | ||||
| } | } | ||||
| static int _addpath(PyObject *dirs, PyObject *path) | static int _addpath(PyObject *dirs, PyObject *path) | ||||
| { | { | ||||
| const char *cpath = PyBytes_AS_STRING(path); | const char *cpath = PyBytes_AS_STRING(path); | ||||
| Py_ssize_t pos = PyBytes_GET_SIZE(path); | Py_ssize_t pos = PyBytes_GET_SIZE(path); | ||||
| PyObject *key = NULL; | PyObject *key = NULL; | ||||
| int ret = -1; | int ret = -1; | ||||
| /* This loop is super critical for performance. That's why we inline | /* This loop is super critical for performance. That's why we inline | ||||
| * access to Python structs instead of going through a supported API. | * access to Python structs instead of going through a supported API. | ||||
| * The implementation, therefore, is heavily dependent on CPython | * The implementation, therefore, is heavily dependent on CPython | ||||
| * implementation details. We also commit violations of the Python | * implementation details. We also commit violations of the Python | ||||
| * "protocol" such as mutating immutable objects. But since we only | * "protocol" such as mutating immutable objects. But since we only | ||||
| * mutate objects created in this function or in other well-defined | * mutate objects created in this function or in other well-defined | ||||
| * locations, the references are known so these violations should go | * locations, the references are known so these violations should go | ||||
| * unnoticed. */ | * unnoticed. */ | ||||
| while ((pos = _finddir(cpath, pos - 1)) != -1) { | while ((pos = _finddir(cpath, pos - 1)) != -1) { | ||||
| PyObject *val; | PyObject *val; | ||||
| key = PyBytes_FromStringAndSize(cpath, pos); | key = PyBytes_FromStringAndSize(cpath, pos); | ||||
| if (key == NULL) | if (key == NULL) | ||||
| goto bail; | goto bail; | ||||
| val = PyDict_GetItem(dirs, key); | val = PyDict_GetItem(dirs, key); | ||||
| key = PyBytes_FromStringAndSize(cpath, pos); | key = PyBytes_FromStringAndSize(cpath, pos); | ||||
| if (key == NULL) | if (key == NULL) | ||||
| goto bail; | goto bail; | ||||
| val = PyDict_GetItem(dirs, key); | val = PyDict_GetItem(dirs, key); | ||||
| if (val == NULL) { | if (val == NULL) { | ||||
| PyErr_SetString(PyExc_ValueError, | PyErr_SetString(PyExc_ValueError, | ||||
| "expected a value, found none"); | "expected a value, found none"); | ||||
| goto bail; | goto bail; | ||||
| } | } | ||||
| if (--PYLONG_VALUE(val) <= 0) { | if (--PYLONG_VALUE(val) <= 0) { | ||||
| if (PyDict_DelItem(dirs, key) == -1) | if (PyDict_DelItem(dirs, key) == -1) | ||||
| goto bail; | goto bail; | ||||
| } else | } else | ||||
| break; | break; | ||||
| while (PyDict_Next(source, &pos, &key, &value)) { | while (PyDict_Next(source, &pos, &key, &value)) { | ||||
| if (!PyBytes_Check(key)) { | if (!PyBytes_Check(key)) { | ||||
| PyErr_SetString(PyExc_TypeError, "expected string key"); | PyErr_SetString(PyExc_TypeError, "expected string key"); | ||||
| return -1; | return -1; | ||||
| } | } | ||||
| if (skipchar) { | if (skipchar) { | ||||
| if (!dirstate_tuple_check(value)) { | if (!dirstate_tuple_check(value)) { | ||||
| PyErr_SetString(PyExc_TypeError, | PyErr_SetString(PyExc_TypeError, | ||||
| "expected a dirstate tuple"); | "expected a dirstate tuple"); | ||||
| return -1; | return -1; | ||||
| } | } | ||||
| if (((dirstateTupleObject *)value)->state == skipchar) | if (((dirstateTupleObject *)value)->state == skipchar) | ||||
| continue; | continue; | ||||
| } | } | ||||
| if (_addpath(dirs, key) == -1) | if (_addpath(dirs, key) == -1) | ||||
| return -1; | return -1; | ||||
| return -1; | return -1; | ||||
| if (source == NULL) | if (source == NULL) | ||||
| ret = 0; | ret = 0; | ||||
| else if (PyDict_Check(source)) | else if (PyDict_Check(source)) | ||||
| ret = dirs_fromdict(dirs, source, skipchar); | ret = dirs_fromdict(dirs, source, skipchar); | ||||
| else if (skipchar) | else if (skipchar) | ||||
| PyErr_SetString(PyExc_ValueError, | PyErr_SetString(PyExc_ValueError, | ||||
| "skip character is only supported " | "skip character is only supported " | ||||
| "with a dict source"); | "with a dict source"); | ||||
| else | else | ||||
| ret = dirs_fromiter(dirs, source); | ret = dirs_fromiter(dirs, source); | ||||
| if (ret == -1) | if (ret == -1) | ||||
| Py_XDECREF(dirs); | Py_XDECREF(dirs); | ||||
| else | else | ||||
| self->dict = dirs; | self->dict = dirs; | ||||
| static PyObject *dirs_iter(dirsObject *self) | static PyObject *dirs_iter(dirsObject *self) | ||||
| { | { | ||||
| return PyObject_GetIter(self->dict); | return PyObject_GetIter(self->dict); | ||||
| } | } | ||||
| static PySequenceMethods dirs_sequence_methods; | static PySequenceMethods dirs_sequence_methods; | ||||
| static PyMethodDef dirs_methods[] = { | static PyMethodDef dirs_methods[] = { | ||||
| {"addpath", (PyCFunction)dirs_addpath, METH_VARARGS, "add a path"}, | {"addpath", (PyCFunction)dirs_addpath, METH_VARARGS, "add a path"}, | ||||
| {"delpath", (PyCFunction)dirs_delpath, METH_VARARGS, "remove a path"}, | {"delpath", (PyCFunction)dirs_delpath, METH_VARARGS, "remove a path"}, | ||||
| {NULL} /* Sentinel */ | {NULL} /* Sentinel */ | ||||
| }; | }; | ||||
| static PyTypeObject dirsType = { PyVarObject_HEAD_INIT(NULL, 0) }; | static PyTypeObject dirsType = {PyVarObject_HEAD_INIT(NULL, 0)}; | ||||
| void dirs_module_init(PyObject *mod) | void dirs_module_init(PyObject *mod) | ||||
| { | { | ||||
| dirs_sequence_methods.sq_contains = (objobjproc)dirs_contains; | dirs_sequence_methods.sq_contains = (objobjproc)dirs_contains; | ||||
| dirsType.tp_name = "parsers.dirs"; | dirsType.tp_name = "parsers.dirs"; | ||||
| dirsType.tp_new = PyType_GenericNew; | dirsType.tp_new = PyType_GenericNew; | ||||
| dirsType.tp_basicsize = sizeof(dirsObject); | dirsType.tp_basicsize = sizeof(dirsObject); | ||||
| dirsType.tp_dealloc = (destructor)dirs_dealloc; | dirsType.tp_dealloc = (destructor)dirs_dealloc; | ||||