annotate mercurial/cext/util.h @ 51723:9367571fea21

cext: correct the argument handling of `b85encode()` The type stub indicated that this argument is `Optional`, which implies None is allowed. I don't see in the documentation where that's the case for `i`[1], and trying it in `hg debugshell` resulted in the method failing with a TypeError. I guess it was typed as an `int` argument because the `p` format unit wasn't added until Python 3.3[2]. In any event, 2 clients in core (`pvec` and `obsolete`) call this with no argument supplied, and `mdiff` calls it with True. So I guess we've avoided the None arg case, and when no arg is supplied, it defaults to the 0 initialization of the `pad` variable in C. Since the `p` format unit accepts both `int` and None, as well as `bool`, I'm not bothering to bump the module version- this code is more permissive than it was, in addition to being more correct. Interestingly, when I first imported the `cext` and `pure` methods in the same manner as the previous commit, it dropped the `Optional` part of the argument type when generating `util.pyi`. No idea why. [1] https://docs.python.org/3/c-api/arg.html#numbers [2] https://docs.python.org/3/c-api/arg.html#other-objects
author Matt Harbison <matt_harbison@yahoo.com>
date Sat, 20 Jul 2024 01:55:09 -0400
parents 3aa1b7ded52c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11358
4494fb02d549 util.h: Utility macros for handling different Python APIs.
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
1 /*
4494fb02d549 util.h: Utility macros for handling different Python APIs.
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
2 util.h - utility functions for interfacing with the various python APIs.
4494fb02d549 util.h: Utility macros for handling different Python APIs.
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
3
4494fb02d549 util.h: Utility macros for handling different Python APIs.
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
4 This software may be used and distributed according to the terms of
4494fb02d549 util.h: Utility macros for handling different Python APIs.
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
5 the GNU General Public License, incorporated herein by reference.
4494fb02d549 util.h: Utility macros for handling different Python APIs.
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
6 */
4494fb02d549 util.h: Utility macros for handling different Python APIs.
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
7
4494fb02d549 util.h: Utility macros for handling different Python APIs.
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
8 #ifndef _HG_UTIL_H_
4494fb02d549 util.h: Utility macros for handling different Python APIs.
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
9 #define _HG_UTIL_H_
4494fb02d549 util.h: Utility macros for handling different Python APIs.
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
10
29444
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents: 25076
diff changeset
11 #include "compat.h"
284d742e5611 internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents: 25076
diff changeset
12
34635
3455e2e2ce9b util: add clang-format control comment around struct and format macro
Augie Fackler <augie@google.com>
parents: 33758
diff changeset
13 /* clang-format off */
21809
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 19833
diff changeset
14 typedef struct {
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 19833
diff changeset
15 PyObject_HEAD
48250
1730b2fceaa1 dirstate-v2: adds a flag to mark a file as modified
Simon Sapin <simon.sapin@octobus.net>
parents: 48232
diff changeset
16 int flags;
21809
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 19833
diff changeset
17 int mode;
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 19833
diff changeset
18 int size;
48260
269ff8978086 dirstate: store mtimes with nanosecond precision in memory
Simon Sapin <simon.sapin@octobus.net>
parents: 48252
diff changeset
19 int mtime_s;
269ff8978086 dirstate: store mtimes with nanosecond precision in memory
Simon Sapin <simon.sapin@octobus.net>
parents: 48252
diff changeset
20 int mtime_ns;
47539
84391ddf4c78 dirstate-item: rename the class to DirstateItem
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 40598
diff changeset
21 } dirstateItemObject;
34635
3455e2e2ce9b util: add clang-format control comment around struct and format macro
Augie Fackler <augie@google.com>
parents: 33758
diff changeset
22 /* clang-format on */
21809
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 19833
diff changeset
23
48266
749946b6a641 dirstate-v2: reorder flag to group related one together
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48264
diff changeset
24 static const int dirstate_flag_wc_tracked = 1 << 0;
48250
1730b2fceaa1 dirstate-v2: adds a flag to mark a file as modified
Simon Sapin <simon.sapin@octobus.net>
parents: 48232
diff changeset
25 static const int dirstate_flag_p1_tracked = 1 << 1;
1730b2fceaa1 dirstate-v2: adds a flag to mark a file as modified
Simon Sapin <simon.sapin@octobus.net>
parents: 48232
diff changeset
26 static const int dirstate_flag_p2_info = 1 << 2;
48266
749946b6a641 dirstate-v2: reorder flag to group related one together
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48264
diff changeset
27 static const int dirstate_flag_mode_exec_perm = 1 << 3;
749946b6a641 dirstate-v2: reorder flag to group related one together
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48264
diff changeset
28 static const int dirstate_flag_mode_is_symlink = 1 << 4;
749946b6a641 dirstate-v2: reorder flag to group related one together
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48264
diff changeset
29 static const int dirstate_flag_has_fallback_exec = 1 << 5;
749946b6a641 dirstate-v2: reorder flag to group related one together
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48264
diff changeset
30 static const int dirstate_flag_fallback_exec = 1 << 6;
749946b6a641 dirstate-v2: reorder flag to group related one together
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48264
diff changeset
31 static const int dirstate_flag_has_fallback_symlink = 1 << 7;
749946b6a641 dirstate-v2: reorder flag to group related one together
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48264
diff changeset
32 static const int dirstate_flag_fallback_symlink = 1 << 8;
749946b6a641 dirstate-v2: reorder flag to group related one together
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48264
diff changeset
33 static const int dirstate_flag_expected_state_is_modified = 1 << 9;
749946b6a641 dirstate-v2: reorder flag to group related one together
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48264
diff changeset
34 static const int dirstate_flag_has_meaningful_data = 1 << 10;
749946b6a641 dirstate-v2: reorder flag to group related one together
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48264
diff changeset
35 static const int dirstate_flag_has_mtime = 1 << 11;
749946b6a641 dirstate-v2: reorder flag to group related one together
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48264
diff changeset
36 static const int dirstate_flag_mtime_second_ambiguous = 1 << 12;
749946b6a641 dirstate-v2: reorder flag to group related one together
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48264
diff changeset
37 static const int dirstate_flag_directory = 1 << 13;
749946b6a641 dirstate-v2: reorder flag to group related one together
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48264
diff changeset
38 static const int dirstate_flag_all_unknown_recorded = 1 << 14;
749946b6a641 dirstate-v2: reorder flag to group related one together
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 48264
diff changeset
39 static const int dirstate_flag_all_ignored_recorded = 1 << 15;
47948
83f0e93ec34b dirstate-item: move the C implementation to the same logic
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 47539
diff changeset
40
47539
84391ddf4c78 dirstate-item: rename the class to DirstateItem
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 40598
diff changeset
41 extern PyTypeObject dirstateItemType;
84391ddf4c78 dirstate-item: rename the class to DirstateItem
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 40598
diff changeset
42 #define dirstate_tuple_check(op) (Py_TYPE(op) == &dirstateItemType)
21809
e250b8300e6e parsers: inline fields of dirstate values in C version
Siddharth Agarwal <sid0@fb.com>
parents: 19833
diff changeset
43
35775
440e8fce29e7 cext: define MIN macro only if it is not yet defined
André Sintzoff <andre.sintzoff@gmail.com>
parents: 34635
diff changeset
44 #ifndef MIN
34635
3455e2e2ce9b util: add clang-format control comment around struct and format macro
Augie Fackler <augie@google.com>
parents: 33758
diff changeset
45 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
35775
440e8fce29e7 cext: define MIN macro only if it is not yet defined
André Sintzoff <andre.sintzoff@gmail.com>
parents: 34635
diff changeset
46 #endif
24442
98042b0e19f9 manifest: move C bool polyfill into util.h
Matt Mackall <mpm@selenic.com>
parents: 24016
diff changeset
47 /* VC9 doesn't include bool and lacks stdbool.h based on my searching */
24829
8e9f8d2a2c0c util: fix the check for non-C99 compilers (issue4605)
Kevin Bullock <kbullock@ringworld.org>
parents: 24823
diff changeset
48 #if defined(_MSC_VER) || __STDC_VERSION__ < 199901L
24442
98042b0e19f9 manifest: move C bool polyfill into util.h
Matt Mackall <mpm@selenic.com>
parents: 24016
diff changeset
49 #define true 1
98042b0e19f9 manifest: move C bool polyfill into util.h
Matt Mackall <mpm@selenic.com>
parents: 24016
diff changeset
50 #define false 0
98042b0e19f9 manifest: move C bool polyfill into util.h
Matt Mackall <mpm@selenic.com>
parents: 24016
diff changeset
51 typedef unsigned char bool;
98042b0e19f9 manifest: move C bool polyfill into util.h
Matt Mackall <mpm@selenic.com>
parents: 24016
diff changeset
52 #else
98042b0e19f9 manifest: move C bool polyfill into util.h
Matt Mackall <mpm@selenic.com>
parents: 24016
diff changeset
53 #include <stdbool.h>
98042b0e19f9 manifest: move C bool polyfill into util.h
Matt Mackall <mpm@selenic.com>
parents: 24016
diff changeset
54 #endif
98042b0e19f9 manifest: move C bool polyfill into util.h
Matt Mackall <mpm@selenic.com>
parents: 24016
diff changeset
55
33756
5866ba5e9c48 cext: move _dict_new_presized() to header
Yuya Nishihara <yuya@tcha.org>
parents: 32386
diff changeset
56 static inline PyObject *_dict_new_presized(Py_ssize_t expected_size)
5866ba5e9c48 cext: move _dict_new_presized() to header
Yuya Nishihara <yuya@tcha.org>
parents: 32386
diff changeset
57 {
5866ba5e9c48 cext: move _dict_new_presized() to header
Yuya Nishihara <yuya@tcha.org>
parents: 32386
diff changeset
58 /* _PyDict_NewPresized expects a minused parameter, but it actually
5866ba5e9c48 cext: move _dict_new_presized() to header
Yuya Nishihara <yuya@tcha.org>
parents: 32386
diff changeset
59 creates a dictionary that's the nearest power of two bigger than the
5866ba5e9c48 cext: move _dict_new_presized() to header
Yuya Nishihara <yuya@tcha.org>
parents: 32386
diff changeset
60 parameter. For example, with the initial minused = 1000, the
5866ba5e9c48 cext: move _dict_new_presized() to header
Yuya Nishihara <yuya@tcha.org>
parents: 32386
diff changeset
61 dictionary created has size 1024. Of course in a lot of cases that
5866ba5e9c48 cext: move _dict_new_presized() to header
Yuya Nishihara <yuya@tcha.org>
parents: 32386
diff changeset
62 can be greater than the maximum load factor Python's dict object
5866ba5e9c48 cext: move _dict_new_presized() to header
Yuya Nishihara <yuya@tcha.org>
parents: 32386
diff changeset
63 expects (= 2/3), so as soon as we cross the threshold we'll resize
5866ba5e9c48 cext: move _dict_new_presized() to header
Yuya Nishihara <yuya@tcha.org>
parents: 32386
diff changeset
64 anyway. So create a dictionary that's at least 3/2 the size. */
5866ba5e9c48 cext: move _dict_new_presized() to header
Yuya Nishihara <yuya@tcha.org>
parents: 32386
diff changeset
65 return _PyDict_NewPresized(((1 + expected_size) / 2) * 3);
5866ba5e9c48 cext: move _dict_new_presized() to header
Yuya Nishihara <yuya@tcha.org>
parents: 32386
diff changeset
66 }
5866ba5e9c48 cext: move _dict_new_presized() to header
Yuya Nishihara <yuya@tcha.org>
parents: 32386
diff changeset
67
40598
fa33196088c4 revlog: replace PyInt_AS_LONG with a more portable helper function
Augie Fackler <augie@google.com>
parents: 36618
diff changeset
68 /* Convert a PyInt or PyLong to a long. Returns false if there is an
fa33196088c4 revlog: replace PyInt_AS_LONG with a more portable helper function
Augie Fackler <augie@google.com>
parents: 36618
diff changeset
69 error, in which case an exception will already have been set. */
fa33196088c4 revlog: replace PyInt_AS_LONG with a more portable helper function
Augie Fackler <augie@google.com>
parents: 36618
diff changeset
70 static inline bool pylong_to_long(PyObject *pylong, long *out)
fa33196088c4 revlog: replace PyInt_AS_LONG with a more portable helper function
Augie Fackler <augie@google.com>
parents: 36618
diff changeset
71 {
fa33196088c4 revlog: replace PyInt_AS_LONG with a more portable helper function
Augie Fackler <augie@google.com>
parents: 36618
diff changeset
72 *out = PyLong_AsLong(pylong);
fa33196088c4 revlog: replace PyInt_AS_LONG with a more portable helper function
Augie Fackler <augie@google.com>
parents: 36618
diff changeset
73 /* Fast path to avoid hitting PyErr_Occurred if the value was obviously
fa33196088c4 revlog: replace PyInt_AS_LONG with a more portable helper function
Augie Fackler <augie@google.com>
parents: 36618
diff changeset
74 * not an error. */
fa33196088c4 revlog: replace PyInt_AS_LONG with a more portable helper function
Augie Fackler <augie@google.com>
parents: 36618
diff changeset
75 if (*out != -1) {
fa33196088c4 revlog: replace PyInt_AS_LONG with a more portable helper function
Augie Fackler <augie@google.com>
parents: 36618
diff changeset
76 return true;
fa33196088c4 revlog: replace PyInt_AS_LONG with a more portable helper function
Augie Fackler <augie@google.com>
parents: 36618
diff changeset
77 }
fa33196088c4 revlog: replace PyInt_AS_LONG with a more portable helper function
Augie Fackler <augie@google.com>
parents: 36618
diff changeset
78 return PyErr_Occurred() == NULL;
fa33196088c4 revlog: replace PyInt_AS_LONG with a more portable helper function
Augie Fackler <augie@google.com>
parents: 36618
diff changeset
79 }
fa33196088c4 revlog: replace PyInt_AS_LONG with a more portable helper function
Augie Fackler <augie@google.com>
parents: 36618
diff changeset
80
11358
4494fb02d549 util.h: Utility macros for handling different Python APIs.
Renato Cunha <renatoc@gmail.com>
parents:
diff changeset
81 #endif /* _HG_UTIL_H_ */