8000 gh-118263: Generalize `path_t` for C level optimizations by nineteendo · Pull Request #118355 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-118263: Generalize path_t for C level optimizations #118355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 31 commits into from
May 24, 2024
Merged
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ae2e171
Add `null_embeddable`
nineteendo Apr 27, 2024
0625bf1
Add `make_wide`
nineteendo Apr 28, 2024
28b4b6e
📜🤖 Added by blurb_it.
blurb-it[bot] Apr 28, 2024
19209c6
Fix typo & add type casts
nineteendo Apr 28, 2024
7ca6036
Add test for fast paths
nineteendo Apr 28, 2024
55c1883
Build tuple faster
nineteendo Apr 29, 2024
a9b7bdd
Merge branch 'main' into generalize-path_t
nineteendo Apr 29, 2024
42b2ca1
Merge branch 'main' into generalize-path_t
nineteendo Apr 30, 2024
5b44245
Resolve merge conflicts
nineteendo Apr 30, 2024
1c93617
Support `make_wide=False` on Windows
nineteendo May 3, 2024
dc36f2e
Update generated code
nineteendo May 3, 2024
0a9d497
Add `suppress`
nineteendo May 4, 2024
14d711e
Fix `allow_fd`
nineteendo May 4, 2024
89e83fa
Allow paths of any length
nineteendo May 4, 2024
d6a174e
Update generated files
nineteendo May 4, 2024
4e8b713
Simplify bytes conversion
nineteendo May 4, 2024
0777a31
Remove leftovers
nineteendo May 4, 2024
46059d1
Update conditions to use `PyBytes_Check()`
nineteendo May 4, 2024
f95da67
Fix access violation attempt 1
nineteendo May 4, 2024
e8ab7d7
Fix segmentation fault
nineteendo May 4, 2024
3b21f5d
Fix segmentation fault attempt 2
nineteendo May 4, 2024
7b3a785
Fix access violation attempt 1
nineteendo May 4, 2024
a94c852
Clear ValueError
nineteendo May 4, 2024
5f717c9
Apply suggestions from code review
nineteendo May 4, 2024
b76774b
Same docstring for python fallback
nineteendo May 4, 2024
f2537a1
Revert newline
nineteendo May 4, 2024
aa0df5c
Merge branch 'main' into generalize-path_t
nineteendo May 22, 2024
8f67225
Merge branch 'main' into generalize-path_t
nineteendo May 22, 2024
360326d
Fix pointers
nineteendo May 22, 2024
3514bdb
Return false in case of value error
nineteendo May 22, 2024
e362054
Add null check
nineteendo May 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Clear ValueError
Co-authored-by: Eryk Sun <eryksun@gmail.com>
  • Loading branch information
nineteendo and eryksun committed May 4, 2024
commit a94c852894a5c64219a819e1f867e9e6730f761f
94 changes: 41 additions & 53 deletions Modules/posixmodule.c
10000
Original file line number Diff line number Diff line change
Expand Up @@ -1112,8 +1112,8 @@ get_posix_state(PyObject *module)
* embedded null characters and have any length.
* path.make_wide
* If nonzero, the path is decoded to Unicode, encoded to bytes otherwise.
* path.suppress
* If nonzero, errors are suppressed.
* path.suppress_value_error
* If nonzero, ValueErrors are suppressed.
* path.allow_fd
* If nonzero, the path is permitted to be a file handle
* (a signed int) instead of a string.
Expand Down Expand Up @@ -1142,8 +1142,8 @@ get_posix_state(PyObject *module)
* unspecified, path_converter will never get called.
* So if you set allow_fd, you *MUST* initialize path.fd = -1
* yourself!
* path.error
* If nonzero, an error occurred.
* path.value_error
* If nonzero, a ValueError occurred.
* path.length
* The length of the path in characters, if specified as
* a string.
Expand Down Expand Up @@ -1182,32 +1182,32 @@ typedef struct {
int nullable;
int nonstrict;
int make_wide;
int suppress;
int suppress_value_error;
int allow_fd;
// Output fields
const wchar_t *wide;
const char *narrow;
int fd;
int error;
int value_error;
Py_ssize_t length;
PyObject *object;
PyObject *cleanup;
} path_t;

#define PATH_T_INITIALIZE(function_name, argument_name, nullable, nonstrict, \
make_wide, suppress, allow_fd) \
{function_name, argument_name, nullable, nonstrict, make_wide, suppress, \
allow_fd, NULL, NULL, -1, 0, 0, NULL, NULL}
make_wide, suppress_value_error, allow_fd) \
{function_name, argument_name, nullable, nonstrict, make_wide, \
suppress_value_error, allow_fd, NULL, NULL, -1, 0, 0, NULL, NULL}
#ifdef MS_WINDOWS
#define PATH_T_INITIALIZE_P(function_name, argument_name, nullable, \
nonstrict, suppress, allow_fd) \
nonstrict, suppress_value_error, allow_fd) \
PATH_T_INITIALIZE(function_name, argument_name, nullable, nonstrict, 1, \
suppress, allow_fd)
suppress_value_error, allow_fd)
#else
#define PATH_T_INITIALIZE_P(function_name, argument_name, nullable, \
nonstrict, suppress, allow_fd) \
nonstrict, suppress_value_error, allow_fd) \
PATH_T_INITIALIZE(function_name, argument_name, nullable, nonstrict, 0, \
suppress, allow_fd)
suppress_value_error, allow_fd)
#endif

static void
Expand Down Expand Up @@ -1397,7 +1397,7 @@ path_converter(PyObject *o, void *p)
path->fd = -1;

success_exit:
path->error = 0;
path->value_error = 0;
path->length = length;
path->object = o;
return Py_CLEANUP_SUPPORTED;
Expand All @@ -1406,13 +1406,16 @@ path_converter(PyObject *o, void *p)
Py_XDECREF(o);
Py_XDECREF(bytes);
PyMem_Free(wide);
if (!path->suppress) {
if (!path->suppress_value_error ||
!PyErr_ExceptionMatches(PyExc_ValueError))
{
return 0;
}
PyErr_Clear();
path->wide = NULL;
path->narrow = NULL;
path->fd = -1;
path->error = 1;
path->value_error = 1;
path->length = 0;
path->object = NULL;
return Py_CLEANUP_SUPPORTED;
Expand Down Expand Up @@ -2926,7 +2929,8 @@ class path_t_converter(CConverter):
converter = 'path_converter'

def converter_init(self, *, allow_fd=False, make_wide=None,
nonstrict=False, nullable=False, suppress=False):
nonstrict=False, nullable=False,
suppress_value_error=False):
# right now path_t doesn't support default values.
# to support a default value, you'll need to override initialize().
if self.default not in (unspecified, None):
Expand All @@ -2938,7 +2942,7 @@ class path_t_converter(CConverter):
self.nullable = nullable
self.nonstrict = nonstrict
self.make_wide = make_wide
self.suppress = suppress
self.suppress_value_error = suppress_value_error
self.allow_fd = allow_fd

def pre_render(self):
Expand All @@ -2954,7 +2958,7 @@ class path_t_converter(CConverter):
self.name,
strify(self.nullable),
strify(self.nonstrict),
strify(self.suppress),
strify(self.suppress_value_error),
strify(self.allow_fd),
)
else:
Expand All @@ -2964,7 +2968,7 @@ class path_t_converter(CConverter):
strify(self.nullable),
strify(self.nonstrict),
strify(self.make_wide),
strify(self.suppress),
strify(self.suppress_value_error),
strify(self.allow_fd),
)

Expand Down Expand Up @@ -3045,7 +3049,7 @@ class sysconf_confname_converter(path_confname_converter):
converter="conv_sysconf_confname"

[python start generated code]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=e859625395de8933]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=577cb476e5d64960]*/

/*[clinic input]

Expand Down Expand Up @@ -5122,15 +5126,15 @@ os__path_splitroot_impl(PyObject *module, path_t *path)
/*[clinic input]
os._path_isdir

s as path: path_t(allow_fd=True, suppress=True)
s as path: path_t(allow_fd=True, suppress_value_error=True)

Return true if the pathname refers to an existing directory.

[clinic start generated code]*/

static PyObject *
os__path_isdir_impl(PyObject *module, path_t *path)
/*[clinic end generated code: output=0adeafd60704f710 input=3f8bf1ea0cc77e0c]*/
/*[clinic end generated code: output=0adeafd60704f710 input=2d09b8801fd2f638]*/
{
HANDLE hfile;
BOOL close_file = TRUE;
Expand All @@ -5139,12 +5143,8 @@ os__path_isdir_impl(PyObject *module, path_t *path)
BOOL slow_path = TRUE;
FILE_STAT_BASIC_INFORMATION statInfo;

if (path->error) {
if (PyErr_ExceptionMatches(PyExc_ValueError)) {
PyErr_Clear();
Py_RETURN_FALSE;
}
return NULL;
if (path->value_error) {
Py_RETURN_FALSE;
}

Py_BEGIN_ALLOW_THREADS
Expand Down Expand Up @@ -5216,15 +5216,15 @@ os__path_isdir_impl(PyObject *module, path_t *path)
/*[clinic input]
os._path_isfile

path: path_t(allow_fd=True, suppress=True)
path: path_t(allow_fd=True, suppress_value_error=True)

Test whether a path is a regular file

[clinic start generated code]*/

static PyObject *
os__path_isfile_impl(PyObject *module, path_t *path)
/*[clinic end generated code: output=4f72e7b1ada002da input=b6c69c96e1722a27]*/
/*[clinic end generated code: output=4f72e7b1ada002da input=c378f54b14ae878a]*/
{
HANDLE hfile;
BOOL close_file = TRUE;
Expand All @@ -5233,12 +5233,8 @@ os__path_isfile_impl(PyObject *module, path_t *path)
BOOL slow_path = TRUE;
FILE_STAT_BASIC_INFORMATION statInfo;

if (path->error) {
if (PyErr_ExceptionMatches(PyExc_ValueError)) {
PyErr_Clear();
Py_RETURN_FALSE;
}
return NULL;
if (path->value_error) {
Py_RETURN_FALSE;
}

Py_BEGIN_ALLOW_THREADS
Expand Down Expand Up @@ -5310,28 +5306,24 @@ os__path_isfile_impl(PyObject *module, path_t *path)
/*[clinic input]
os._path_exists

path: path_t(allow_fd=True, suppress=True)
path: path_t(allow_fd=True, suppress_value_error=True)

Test whether a path exists. Returns False for broken symbolic links

[clinic start generated code]*/

static PyObject *
os__path_exists_impl(PyObject *module, path_t *path)
/*[clinic end generated code: output=69e6089df1fe463a input=3a73be0affbbe43c]*/
/*[clinic end generated code: output=69e6089df1fe463a input=a62c424c1784c43b]*/
{
HANDLE hfile;
BOOL close_file = TRUE;
int result;
BOOL slow_path = TRUE;
FILE_STAT_BASIC_INFORMATION statInfo;

if (path->error) {
if (PyErr_ExceptionMatches(PyExc_ValueError)) {
PyErr_Clear();
Py_RETURN_FALSE;
}
return NULL;
if (path->value_error) {
Py_RETURN_FALSE;
}

Py_BEGIN_ALLOW_THREADS
Expand Down Expand Up @@ -5393,15 +5385,15 @@ os__path_exists_impl(PyObject *module, path_t *path)
/*[clinic input]
os._path_islink

path: path_t(allow_fd=True, suppress=True)
path: path_t(allow_fd=True, suppress_value_error=True)

Test whether a path is a symbolic link

[clinic start generated code]*/

static PyObject *
os__path_islink_impl(PyObject *module, path_t *path)
/*[clinic end generated code: output=109ad77ec747b3b7 input=3937a93631697d6c]*/
/*[clinic end generated code: output=109ad77ec747b3b7 input=80bd45abdecb418e]*/
{
HANDLE hfile;
BOOL close_file = TRUE;
Expand All @@ -5410,12 +5402,8 @@ os__path_islink_impl(PyObject *module, path_t *path)
BOOL slow_path = TRUE;
FILE_STAT_BASIC_INFORMATION statInfo;

if (path->error) {
if (PyErr_ExceptionMatches(PyExc_ValueError)) {
PyErr_Clear();
Py_RETURN_FALSE;
}
return NULL;
if (path->value_error) {
Py_RETURN_FALSE;
}

Py_BEGIN_ALLOW_THREADS
Expand Down
0