Skip to content

Conversation

@Joppie
Copy link

@Joppie Joppie commented Sep 26, 2019

No description provided.

@Joppie
Copy link
Author

Joppie commented Sep 26, 2019

This pull request addresses issue #1282

@Joppie
Copy link
Author

Joppie commented Sep 26, 2019

It appears the SyslogHandlerTests test class is meant to verify that both ascii and unicode strings are handeled correctly. However, in test_emit_ascii_noerror the expression 'hello' is used, which is not a bytes (ascii) type under python3. Using the expression b'hello' will ensure that this is an ascii string under both python2.7 and python3. Making this change will cause the test to fail under python3.

@Joppie
Copy link
Author

Joppie commented Sep 26, 2019

I'm not sold that this is the correct way to address this problem, but I thought I'd go ahead and show a potential solution. One thing of note, because the fmt string in the base Logger class is '%(message)s', this means the the output messages will always be unicode strings under python3. Under python2, they will be ascii strings when the input messages is ascii, and unicode strings when the input mesage is unicode. I'm not sure what the desired behavior should be.

@mnaberez
Copy link
Member

cc: @vsajip

@vsajip
Copy link
Contributor

vsajip commented Sep 27, 2019

Actually, the issue addressed appears ro be #1282

Copy link
Contributor

@vsajip vsajip left a comment

Choose a reason for hiding this comment

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

There should be another test that verifies that whether you create a LogRecord with a text msg or with a bytes msg, the message key in the resulting value of asdict() always contains text, not bytes.

It's also worth testing what happens if the as_string() is applied in the LogRecord constructor - messages should always be text, even on Python 2!

msg = self.msg % self.kw
msg = as_string(self.msg) % self.kw
else:
msg = self.msg
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not as_string(self.msg) here, too? asdict() should be returning text here rather than bytes for the message key, whether or not there are arguments.

message = params['message']
for line in message.rstrip('\n').split('\n'):
params['message'] = line
split_char = b'\n' if isinstance(message, bytes) else '\n'
Copy link
Contributor

Choose a reason for hiding this comment

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

Since in the return value from asdict() the message should always be text, this shouldn't ne needed, right?

def test_emit_ascii_noerror(self):
handler = self._makeOne()
record = self._makeLogRecord('hello!')
record = self._makeLogRecord(b'hello!')
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't the test verify both text and bytes inputs here, rather than just bytes?

@vsajip
Copy link
Contributor

vsajip commented Sep 27, 2019

See also #1284 for the alternative approach I suggested.

@Joppie
Copy link
Author

Joppie commented Sep 27, 2019

I was unsure of the desired behavior for when messages, formats, etc should be ascii/byte strings or Unicode strings in both python2 and python3. This pull request was mainly to provide some context for issue #1282. #1284 seems like a better fix, so I'm closing this request.

@Joppie Joppie closed this Sep 27, 2019
@vsajip
Copy link
Contributor

vsajip commented Sep 27, 2019

Generally, I believe messages, formats etc. should logically be Unicode (unicode on Python 2 and str on Python 3), as they are meant to be text for human consumption. Generally, bytes should only be used when communicating between processes or when doing I/O (file, socket). This "purist" approach sometimes has to be modified for reasons of legacy code.

@vsajip
Copy link
Contributor

vsajip commented Sep 28, 2019

Just to confirm - did you try the #1284 fix in your environment? Did messages show up in syslog with that fix applied?

@Joppie
Copy link
Author

Joppie commented Sep 30, 2019

I just tested it and it does fix the issue in the environment I am using. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

3 participants