Skip to content

Add optional parameters for encoding different from utf8 (default is still utf8) - #1995

Open
hakaishi wants to merge 13 commits into
paramiko:mainfrom
hakaishi:add_encoding2
Open

hakaishi wants to merge 13 commits into
paramiko:mainfrom
hakaishi:add_encoding2

Conversation

@hakaishi

@hakaishi hakaishi commented Mar 3, 2022

Copy link
Copy Markdown

Continuation of original pull request #1716

Cloned the current main branch and added my function parameters. No much change. Fixed 2 failing tests too.

Optional parameters are "encoding" with default to utf8 and "errors" with default to "strict".

You could add a paramiko global or something to avoid the need to pass the encoding/errors parameters each time.

Best regards,
Hakaishi

@hakaishi
hakaishi force-pushed the add_encoding2 branch 4 times, most recently from 397b5aa to 131cf60 Compare March 3, 2022 14:47
Comment thread paramiko/sftp_client.py Outdated
whether to do a stat() on the file afterwards to confirm the file
size (since 1.7.7)
:param str encoding:
encoding for file paths/names and content (default: utf8)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The encoding and errors parameters are not used for the content because putfo uses binary mode.

@hakaishi hakaishi Mar 13, 2022 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I guess you refer to the following part:

with self.file(
    remotepath, "wb", encoding=encoding, errors=errors
) as fr:

Right, it's in binary mode, but that's just the content, the file path might be non uft8 though. And just below that part the stat method is also used, which takes these parameters to get the actual remote path in the remote encoding.

Actually, this remotepath over there might need encoding too. I'm not sure... I'll try and test this week. (All you need is a VM or Docker with an OS using an encoding different from utf8...)
-> remotepath.decode("utf-8", errors).encode(encoding, errors)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Update:
self.file is an object of open and open takes encoding and errors as parameter. Encoding and errors in open decodes the file name/path.

In other words: Everything should be correct here. You didn't dig deep enough, I guess.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What I mean is that the comment for the parameter should be:

encoding for file paths/names (default: utf8)

I think the current comment leads misleading. In fact, the encoding parameter is not used for the content.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I fixed that now.

Comment thread paramiko/file.py Outdated
else:

def __next__(self):
def __next__(self, encoding="utf8", errors="strict"):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The __next__ method doesn't have arguments. It is unable to use the arguments because Python iterator directly invokes it.

Instead, I suggest:

  • Add encoding and errors parameters to __init__ method and store to the instance attributes
  • Use the attributes here

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I see. I should have noticed that....

I'll try and see what to do best. Thanks.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I fixed this and merged the newest commits from the main branch.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think readline should use self.encoding and self.errors and remove added arguments, because passing two parameters to __init__.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Oops. I'll check again.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Better now?

@hakaishi

Copy link
Copy Markdown
Author

By the way... How about having a encoding parameters at SFTPClient's init instead of every method (list etc)

@hakaishi
hakaishi force-pushed the add_encoding2 branch 2 times, most recently from 07d1988 to 2779500 Compare March 13, 2022 15:00
@hakaishi

Copy link
Copy Markdown
Author

The new commit removes all encoding/errors parameters from SFTPClients methods and puts them instead into the constructor and the from_transport method.

@hakaishi

Copy link
Copy Markdown
Author

I will have to merge this manually over and over again if it won't get merged/pulled...
Depressing...
Anything speaking against a merge?

@hakaishi

hakaishi commented Apr 4, 2022

Copy link
Copy Markdown
Author

merged current main into this pull request.
Any updates why this is not getting (some) priority? - Many people in many countries will be waiting for this (including me) as many servers don't run on UTF8.

@bskinn bskinn left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I have a couple of questions, and a suggestion for additional tests.

Thanks for putting together a PR for this!

Comment thread tests/test_sftp.py Outdated
try:
folder_contents = sftp.listdir(sftp.FOLDER)
self.assertEqual(["canard.txt"], folder_contents)
TestCase().assertEqual(["canard.txt"], folder_contents)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What is your reasoning behind this replacement? This seems like it will unnecessarily create a new, empty TestCase instance for each assert, instead of just using the one that already exists in the form of self.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Hmm? I don't remember changing this myself. I'll check when I come home tomorrow night.

Thank you.

Comment thread tests/__init__.py Outdated
else:
try:
return test_method(testself)
return test_method(testself, sftp)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm relatively unfamiliar with the guts of the test suite, but what does adding the sftp here accomplish? I don't see anything obvious in any of the other changes where this is used.

Comment thread tests/test_channelfile.py
@patch("paramiko.channel.ChannelFile._set_mode")
def test_defaults_to_unbuffered_reading(self, setmode):
self.klass(Channel(None))
self.klass(Channel(None), encoding="utf8", errors="strict")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You've added encoding="utf8" cases to many tests, but I don't see any tests that probe compatibility with other encodings. Those would be good to add.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Right. In the first place this is supposed to "not break the current tests".

Adding one or two different test cases where the system does not use utf8 would be very welcome. I'll try to put something together, but I'm not yet sure at how to do it.

We have to keep in mind that there are two different problems: Encoding of file content and encoding of file paths including system messages. (Welcome message on login and error messages etc)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, I see -- with this PR, you're going for a ~zero-effect change, making sure that adding the encoding and errors arguments didn't break anything in the default utf8 use case? Thus, setting the stage for future development/testing when new encodings enter the mix?

@hakaishi hakaishi Apr 8, 2022 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes. That's right 🙂

If things break at the base level, further coding and testing will get messy and difficult.

@bitprophet bitprophet added this to the Subsequent feature release milestone Apr 8, 2022
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.

4 participants