Skip to content

fix: support bytes paths in native filesystem operations - #86

Closed
kushaldave002 wants to merge 7 commits into
fsspec:mainfrom
kushaldave002:fix-byte-path-operations
Closed

kushaldave002 wants to merge 7 commits into
fsspec:mainfrom
kushaldave002:fix-byte-path-operations

Conversation

@kushaldave002

@kushaldave002 kushaldave002 commented Nov 19, 2026 •

Copy link
Copy Markdown

Fixes #84.

With path_encoding=None, several native operations passed bytes paths to str-only separators or shell quoting. This branch:

  • Handles bytes paths in info (and so exists, isdir, isfile, size, modified, rm) and in _parent (used by put_file and pipe_file), which gives bytes paths the same parents as str paths.
  • Skips bytes . and .. entries in ls.
  • Builds the cp and md5sum commands as bytes, so cp_file and checksum run on the server for bytes paths, as they do for str paths. Command output is decoded with errors="replace": a file name in it that isn't valid UTF-8 otherwise makes asyncssh close the SSH connection.

str paths behave as before; the only difference for them is that the shell commands are now built as bytes. Uploads to relative and top-level paths fail with FileNotFoundError for str and bytes paths alike, which #91 fixes, and #92 fixes shell commands for paths that start with -.

The test for an existing file whose name isn't valid UTF-8 runs on Linux only: macOS rejects such names, and mock-ssh-server decodes SFTP paths as UTF-8, so it covers cp_file and checksum.

@codecov-commenter

codecov-commenter commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.33%. Comparing base (15aac74) to head (0ed29b7).
⚠️ Report is 23 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #86      +/-   ##
==========================================
+ Coverage   94.30%   95.33%   +1.02%     
==========================================
  Files          13       13              
  Lines         738     1093     +355     
==========================================
+ Hits          696     1042     +346     
- Misses         42       51       +9     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Build the cp and md5sum commands as bytes, so bytes paths run on the
server the same way str paths do, instead of copying over SFTP (through
the client when the server lacks copy-data) or downloading the whole
file to hash it. Command output is decoded with errors="replace": a
file name in it that isn't valid UTF-8 otherwise makes asyncssh close
the SSH connection.

_parent now handles str and bytes with the same code and returns "/"
for a top-level path, since "" is the home directory in sshfs.

Also test relative str paths in put_file and pipe_file, which failed
before this branch, and list the methods that accept bytes paths in
the README.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@shcheklein

shcheklein commented Nov 28, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks for the fix! I pushed a few commits on top of yours. The PR now only adds bytes-path support and leaves str behavior as it is:

  • cp_file and checksum run on the server for bytes paths too. The commands are built as bytes (_quote and _execute_with_paths), so bytes paths go through cp and md5sum like str paths. Before, a bytes checksum downloaded the whole file to hash it locally, and a bytes cp_file copied the data through the client on servers without the copy-data extension.
  • Command output is decoded with errors="replace". With asyncssh's default strict decoding, a non-UTF-8 name in the output of cp or md5sum closed the SSH connection, and execute returned an empty result instead of raising.
  • _parent gives bytes paths the same parents fsspec gives str paths, and put_file and pipe_file are unchanged.
  • No README change. The class docstring already says that bytes paths are sent unchanged and must be used with path_encoding=None.
  • Tests cover _parent for both types, checksum of a name with spaces, quotes and $, cp_file and checksum with a non-UTF-8 name (missing, and existing on Linux), and the mv copy fallback with bytes paths.

Split out into their own PRs: #91 fixes uploads to relative and top-level paths, which fail for str paths too, and #92 fixes cp, md5sum and md5 for paths that start with -.

The class docstring already says that bytes paths are sent to the
server unchanged and must be used with path_encoding=None. fsspec's
generic methods taking only str paths is fsspec's behavior, not
something sshfs needs to document.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues block approval of the bytes-path support changes.

No open issues 🎉

What changed in this PR

Addresses #84 by extending bytes-path support across native SSH filesystem operations, while documenting the limits of fsspec’s generic methods.

Changes:

  • Handles bytes paths in metadata, directory listings, parent resolution, and server-side copy and checksum commands.
  • Avoids creating empty parent paths and tolerates non-UTF-8 command output without closing the SSH connection.
  • Adds regression tests and documents which methods accept bytes paths.
File Description
tests/​test_sshfs.py Adds coverage for bytes paths, relative uploads, shell errors, and dot-entry filtering.
sshfs/​spec.py Implements bytes-aware path handling, shell quoting, and command-output decoding.
README.md Documents bytes-path configuration, supported methods, and generic-method limitations.

Review effort: Balanced

put_file and pipe_file skip creating the parent directory when it's
the root as well as the home directory. asyncssh's makedirs("/") sends
mkdir("/") twice and raises when the server denies it, as chrooted
servers can, so uploads to a top-level path such as "/file" failed
there.

cp, md5sum and md5 get "--" before the paths, so a name starting with
"-" isn't parsed as an option.

Tests now cover checksum of a name with spaces, quotes and "$", cp_file
and checksum of an existing file whose name isn't valid UTF-8 (skipped
where the filesystem rejects such names), names starting with "-", an
upload to "/" on a server that denies mkdir, and the mv copy fallback
with bytes paths.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Leave put_file and pipe_file as they are on main, and give bytes paths
in _parent the parents fsspec gives str paths, so str behavior doesn't
change. Uploads to relative and top-level paths are fixed in fsspec#91, and
"--" before paths in shell commands is added in fsspec#92.

The upload test uses absolute paths, since relative uploads depend on
fsspec#91.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@shcheklein shcheklein left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

(AI-generated review by Copilot)

Solid, focused fix for bytes paths across native operations. The errors="replace" handling for non-UTF-8 shell output is a great catch.

Main themes in the inline comments:

  • Bigger picture: _parent mirrors AbstractFileSystem's "/a" -> "" quirk (conflating root / with home ""), and _execute_with_paths relies on asyncssh's undocumented bytes command pass-through.
  • Simplification: _execute_with_paths can join (command, *map(_quote, paths)) instead of %s format strings (which also makes integrating -- from #92 cleaner).
  • Tests: Several bytes tests duplicate coverage or branch across unrelated operations (test_bytes_upload_and_download vs test_bytes_file_operations), fs_bytes can be reused in test_sftp_client_kwargs_path_encoding, and info(parent) without trailing / isn't covered.

Comment thread sshfs/spec.py Outdated
# The same result as fsspec's _parent gives for the str path,
# whose trailing slashes _strip_protocol removes
return path.rstrip(b"/").rpartition(b"/")[0]
return super()._parent(path)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[AI-generated — Copilot] Logic / bigger picture (_parent vs _strip_protocol):

  • Minor: for str paths, super()._parent(path) calls cls._strip_protocol(path) a second time.
  • Bigger picture: _strip_protocol explicitly distinguishes "/" (filesystem root) from "" (remote home directory), whereas AbstractFileSystem._parent (with root_marker = "") returns "" for "/a". Mirroring that for bytes (_parent(b"/a") == b"") locks in the conflation of root and home (e.g. fs.ls(fs._parent("/a")) lists home instead of /), which fix: upload to relative and top-level paths #91 then has to work around in _put_file/_pipe_file.

Worth considering whether _parent should follow posixpath.dirname ("/a" -> "/", "a" -> "", b"/a" -> b"/", b"a" -> b"") and have _put_file/_pipe_file skip _makedirs on ("", "/", b"", b"/").

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Fixed the double strip in cc78957: _parent now calls _strip_protocol once and handles str and bytes with the same expression. The str results don't change, and test_parent pins them.

The root/home ambiguity is how _parent already behaves for str paths, so this PR keeps bytes consistent with str. Filed it as #93, together with the makedirs("") failure it causes in fsspec's generic code.

Comment thread sshfs/spec.py
command %= tuple(map(_quote, paths))
# Output can include those paths, and failing to decode it strictly
# closes the SSH connection.
return await self._execute(command, errors="replace")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[AI-generated — Copilot] Logic / simplification / alternatives:

  • Undocumented asyncssh behavior & ProcessError.command type: SSHClientConnection.create_session / SSHClientChannel.create type command as Optional[str]; passing bytes works today because asyncssh.packet.String accepts bytes. Building bytes for str paths too also changes ProcessError.command from str to bytes for str callers. That's a reasonable trade-off for a single code path, though only encoding to bytes when a path is bytes would keep str callers on the documented str path.
  • Simplification (and synergy with fix: prefix paths starting with - with ./ in cp, md5sum and md5 #92): Passing the command binary (b"cp", b"md5sum", b"md5") and building b" ".join((command, *map(_quote, paths))) (or (command, b"--", *map(_quote, paths)) with fix: prefix paths starting with - with ./ in cp, md5sum and md5 #92) avoids %s format strings and centralizes -- in one place.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Done in cc78957: _execute_with_paths(program, *paths) joins the quoted paths to the program.

I kept bytes commands for str paths too, so there's one code path. asyncssh's String() accepts bytes, and every cp_file and checksum test sends one, so CI would catch an asyncssh change. ProcessError.command does become bytes, but it isn't part of the fsspec API.

#92 now uses a ./ prefix inside a _quote helper instead of --, because -- doesn't help with a bare -. Whichever PR merges second folds it into this _quote.

Comment thread tests/test_sshfs.py Resolved
Comment thread tests/test_sshfs.py Resolved
Comment thread tests/test_sshfs.py Resolved
Comment thread tests/test_sshfs.py Resolved
Comment thread tests/test_sshfs.py Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

There are no active review issues for the bytes-path support changes.

No open issues 🎉

Review effort: Balanced

- _execute_with_paths takes the program and joins the quoted paths to
  it instead of formatting them into a command string.
- _parent strips the protocol once and handles str and bytes with the
  same expression. The str results are unchanged.
- The bytes tests are split into one test per operation, each next to
  its str counterpart, without branches on the operation.
- test_bytes_info also checks a directory path without a trailing slash
  and the root.
- test_sftp_client_kwargs_path_encoding uses the fs_bytes fixture.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues block the bytes-path support changes.

No open issues 🎉

Review effort: Balanced

Resolves the conflicts with fsspec#91 and fsspec#92. The tests from both sides are
kept, and _quote, which now builds bytes, gets the "./" prefix from fsspec#92
for paths that start with "-".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@shcheklein

Copy link
Copy Markdown
Collaborator

Thanks for the work on this, and sorry for the change of direction after all the back and forth.

Looking at the bigger picture, we decided not to support bytes paths. No other fsspec filesystem takes them, fsspec's generic methods (walk, find, glob, get, put) would still fail with them, and with path_encoding=None every path, including plain ASCII ones, has to be bytes.

#94 fixes the underlying problem instead. asyncssh's path_errors now defaults to "surrogateescape", so names that aren't valid UTF-8 come back as str (b"name-\xff" becomes "name-\udcff", the way os.listdir returns them), and every method, including the generic ones, works with them. It also carries over two things from this PR: shell commands are sent as bytes, and their output is decoded with errors="replace".

Closing in favor of #94.

@shcheklein shcheklein closed this Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bytes paths (path_encoding=None) only work in some methods

4 participants