GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-22276: Fix pathlib.Path.glob not to ignore trailing path separator by e-kwsm · Pull Request #10349 · python/cpython

Conversation

@e-kwsm

@e-kwsm e-kwsm commented Nov 6, 2018 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

See also Issue 33392: pathlib .glob('*/') returns files as well as directories, closed as duplicate of 22276.

At present pathlib.Path.glob returns directories and FILES even if glob pattern ends with the path separator (/ in Linux), while glob.glob works as expected.
This PR fixes the behaviour.

$ uname -s
Linux
$ mkdir /tmp/test-glob
$ cd /tmp/test-glob
$ mkdir dir
$ touch file
$ echo *  # shell glob
dir file
$ echo */
dir/
>>> from glob import glob
>>> glob("*")
['file', 'dir']
>>> glob("*/")
['dir/']
>>> 
>>> from pathlib import Path
>>> list(Path(".").glob("*"))
[PosixPath('file'), PosixPath('dir')]
>>> list(Path(".").glob("*/"))
[PosixPath('file'), PosixPath('dir')]

https://bugs.python.org/issue22276

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

shenberg and barneygale reacted with thumbs up emoji
All reactions
  • 👍 2 reactions
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Nov 6, 2018
@bedevere-bot bedevere-bot added the awaiting review label Nov 6, 2018
serhiy-storchaka
serhiy-storchaka requested changes Nov 6, 2018
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed tests, the documentation update, a news and What's New entries.

And don't forget about an alternate separator on Windows.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-bot bedevere-bot added awaiting changes and removed awaiting review labels Nov 6, 2018
@bedevere-bot

bedevere-bot commented Nov 6, 2018

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka
serhiy-storchaka reviewed Nov 9, 2018
View reviewed changes
Comment thread Lib/test/test_pathlib.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/pathlib.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@e-kwsm
e-kwsm force-pushed the fix-pathlib.Path.glob branch 3 times, most recently from fdc8e41 to 187aaf0 Compare November 10, 2018 22:21
@e-kwsm

e-kwsm commented Nov 14, 2018

Copy link
Copy Markdown
Contributor Author

I didn't expect the Spanish Inquisition.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot added awaiting change review and removed awaiting changes labels Nov 14, 2018
@bedevere-bot

bedevere-bot commented Nov 14, 2018

Copy link
Copy Markdown

Nobody expects the Spanish Inquisition!

@serhiy-storchaka: please review the changes made to this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka
serhiy-storchaka reviewed Nov 15, 2018
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better to use sep and altsep (and corresponding pathlib attributes) instead of hardcoding '/' in the general code.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/whatsnew/3.8.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Misc/NEWS.d/next/Library/2018-11-11-04-41-11.bpo-22276.Tt19TW.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/library/pathlib.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@e-kwsm
e-kwsm force-pushed the fix-pathlib.Path.glob branch from 187aaf0 to dcae663 Compare November 17, 2018 19:18
@e-kwsm

e-kwsm commented Nov 17, 2018

Copy link
Copy Markdown
Contributor Author

@serhiy-storchaka Thank you for your advice.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@e-kwsm

e-kwsm commented Nov 17, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

BTW, I address bpo-33392 (Path.glob and Path.rglob) rather than [bpo-22276](https://bugs.python.org/issue) (PurePath.match) in this PR; the former is closed as duplicate of the latter.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@csabella
csabella requested a review from serhiy-storchaka May 29, 2019 13:26
@csabella
csabella requested review from pitrou and serhiy-storchaka and removed request for serhiy-storchaka May 25, 2020 14:40
@e-kwsm

e-kwsm commented Jan 5, 2021

Copy link
Copy Markdown
Contributor Author

ping

shenberg, marceloslacerda, ZashIn, and mbbyn reacted with thumbs up emoji
All reactions
  • 👍 4 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@e-kwsm
e-kwsm requested a review from brettcannon as a code owner April 2, 2022 13:51
@brettcannon

brettcannon commented Apr 4, 2022

Copy link
Copy Markdown
Member

/cc @barneygale

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@barneygale

barneygale commented Apr 5, 2022

Copy link
Copy Markdown
Contributor

Hey @e-kwsm, sorry for the long wait. Are you still interested in working on this PR? If so I can help review.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@e-kwsm

e-kwsm commented Apr 8, 2022

Copy link
Copy Markdown
Contributor Author

Are you still interested in working on this PR?

Yes.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ghost

ghost commented Apr 13, 2022 •
edited by ghost
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.
CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@arhadthedev

arhadthedev commented Apr 13, 2022

Copy link
Copy Markdown
Member

@e-kwsm The bot is broken and is being fixed so no worries.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@barneygale

barneygale commented Apr 13, 2022

Copy link
Copy Markdown
Contributor

I don't think the code should make additional is_dir() calls, as those cause a stat() under the hood. The "selector" classes already have this information, it's just a case of feeding it through to the _TerminatingSelector. What do you think @e-kwsm?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@brettcannon brettcannon closed this Apr 25, 2022
@brettcannon brettcannon reopened this Apr 25, 2022
@serhiy-storchaka

serhiy-storchaka commented Apr 26, 2022

Copy link
Copy Markdown
Member

You can also press "Update branch" to trigger testing.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka
serhiy-storchaka reviewed Apr 26, 2022
View reviewed changes
Comment thread Lib/test/test_pathlib.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/library/pathlib.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka
serhiy-storchaka self-requested a review April 26, 2022 06:08
serhiy-storchaka
serhiy-storchaka reviewed Apr 26, 2022
View reviewed changes
Comment thread Doc/library/pathlib.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/whatsnew/3.8.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Apr 26, 2022

Copy link
Copy Markdown
Member

I propose simpler patch:

diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index 4763ab54f6..a759db1684 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -281,7 +281,9 @@ def make_uri(self, path):
 def _make_selector(pattern_parts, flavour):
     pat = pattern_parts[0]
     child_parts = pattern_parts[1:]
-    if pat == '**':
+    if not pat:
+        return _TerminatingSelector()
+    elif pat == '**':
         cls = _RecursiveWildcardSelector
     elif '**' in pat:
         raise ValueError("Invalid pattern: '**' can only be an entire path component")
@@ -943,6 +945,8 @@ def glob(self, pattern):
         drv, root, pattern_parts = self._flavour.parse_parts((pattern,))
         if drv or root:
             raise NotImplementedError("Non-relative patterns are unsupported")
+        if pattern[-1] in (self._flavour.sep, self._flavour.altsep):
+            pattern_parts.append('')
         selector = _make_selector(tuple(pattern_parts), self._flavour)
         for p in selector.select_from(self):
             yield p
@@ -956,6 +960,8 @@ def rglob(self, pattern):
         drv, root, pattern_parts = self._flavour.parse_parts((pattern,))
         if drv or root:
             raise NotImplementedError("Non-relative patterns are unsupported")
+        if pattern[-1] in (self._flavour.sep, self._flavour.altsep):
+            pattern_parts.append('')
         selector = _make_selector(("**",) + tuple(pattern_parts), self._flavour)
         for p in selector.select_from(self):
             yield p
barneygale reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@brettcannon

brettcannon commented Apr 26, 2022

Copy link
Copy Markdown
Member

You can also press "Update branch" to trigger testing.

You're right, but I was also trying to get rid of the old CLA check.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@e-kwsm
bpo-22276: Fix pathlib.Path.glob not to ignore trailing path separator
937b12a
@e-kwsm
e-kwsm force-pushed the fix-pathlib.Path.glob branch from 7c7f251 to 937b12a Compare April 27, 2022 11:17
@e-kwsm

e-kwsm commented Apr 27, 2022

Copy link
Copy Markdown
Contributor Author

@serhiy-storchaka Thank you for your review. I have reflected your suggestion.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

barneygale
barneygale approved these changes Apr 27, 2022
View reviewed changes

@barneygale barneygale left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
serhiy-storchaka
serhiy-storchaka reviewed Apr 27, 2022
View reviewed changes
Comment thread Misc/NEWS.d/next/Library/2018-11-11-04-41-11.bpo-22276.Tt19TW.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@e-kwsm @serhiy-storchaka
Update Misc/NEWS.d/next/Library/2018-11-11-04-41-11.bpo-22276.Tt19TW.rst
8ecc162
Co-authored-by: Serhiy Storchaka <[email protected]>
serhiy-storchaka
serhiy-storchaka approved these changes Apr 27, 2022
View reviewed changes
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting change review labels Apr 27, 2022
@brettcannon

brettcannon commented Apr 27, 2022

Copy link
Copy Markdown
Member

@e-kwsm would you mind signing our new digital CLA (see the "details" link in the failing status check)?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@e-kwsm

e-kwsm commented Apr 28, 2022

Copy link
Copy Markdown
Contributor Author

would you mind signing our new digital CLA (see the "details" link in the failing status check)?

Done.

brettcannon reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

brettcannon
brettcannon approved these changes Apr 28, 2022
View reviewed changes
@brettcannon
brettcannon merged commit ea2f5bc into python:main Apr 28, 2022
@bedevere-bot bedevere-bot removed the awaiting merge label Apr 28, 2022
@brettcannon

brettcannon commented Apr 28, 2022

Copy link
Copy Markdown
Member

Thanks!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@e-kwsm
e-kwsm deleted the fix-pathlib.Path.glob branch April 29, 2022 09:59
@bluetech bluetech mentioned this pull request May 9, 2022
pathlib.Path.rglob("") raises IndexError in Python 3.11b1 #92550
Closed
@jocabt jocabt mannequin mentioned this pull request Apr 10, 2022
pathlib glob ignores trailing slash in pattern #66472
Closed
@phcerdan phcerdan mentioned this pull request Mar 22, 2023
glob should show only directories when used with */ fsspec/s3fs#712
Open
@barneygale barneygale mentioned this pull request Jan 30, 2024
GH-101112: Add "pattern language" section to pathlib docs #114030
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@brettcannon brettcannon brettcannon approved these changes
@barneygale barneygale barneygale approved these changes
@serhiy-storchaka serhiy-storchaka serhiy-storchaka approved these changes
@pitrou pitrou Awaiting requested review from pitrou

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

7 participants

@e-kwsm @bedevere-bot @brettcannon @barneygale @arhadthedev @serhiy-storchaka @the-knights-who-say-ni
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.