Skip to content
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

Context.log_file does not allow bytes type #2384

Closed
marinelay opened this issue Apr 9, 2024 · 2 comments · Fixed by #2389
Closed

Context.log_file does not allow bytes type #2384

marinelay opened this issue Apr 9, 2024 · 2 comments · Fixed by #2389

Comments

@marinelay
Copy link
Contributor

marinelay commented Apr 9, 2024

def log_file(self, value):
r"""
Sets the target file for all logging output.
Works in a similar fashion to :attr:`log_level`.
Examples:
>>> foo_txt = tempfile.mktemp()
>>> bar_txt = tempfile.mktemp()
>>> context.log_file = foo_txt
>>> log.debug('Hello!')
>>> with context.local(log_level='ERROR'): #doctest: +ELLIPSIS
... log.info('Hello again!')
>>> with context.local(log_file=bar_txt):
... log.debug('Hello from bar!')
>>> log.info('Hello from foo!')
>>> open(foo_txt).readlines()[-3] #doctest: +ELLIPSIS
'...:DEBUG:...:Hello!\n'
>>> open(foo_txt).readlines()[-2] #doctest: +ELLIPSIS
'...:INFO:...:Hello again!\n'
>>> open(foo_txt).readlines()[-1] #doctest: +ELLIPSIS
'...:INFO:...:Hello from foo!\n'
>>> open(bar_txt).readlines()[-1] #doctest: +ELLIPSIS
'...:DEBUG:...:Hello from bar!\n'
"""
if isinstance(value, (bytes, six.text_type)):
# check if mode was specified as "[value],[mode]"
if ',' not in value:
value += ',a'
filename, mode = value.rsplit(',', 1)
value = open(filename, mode)

In the code, it seems that Context.log_file allows bytes type (at line 1034),
but it raises TypeError: a bytes-like object is required, not 'str' at line 1036 in Python 3.x.

I think it makes sense to either delete bytes type or cast it to string type.
If bytes is for Python 2.x, then it would be nice to replace it to str (because str and bytes are same in Python 2.x).

It is simplified code raising TypeError:

import tempfile
from pwnlib.context import context

foo_txt = tempfile.mktemp().encode()
context.log_file = foo_txt

Thank you!

@peace-maker
Copy link
Member

Thanks for the report. We usually deal with those by implicitly converting string arguments to bytes and only use bytes internally. There is misc.need_bytes for this. Would you be willing to propose a pull request to fix this?

@marinelay
Copy link
Contributor Author

Sure!

As I understand your comment, you use bytes internally, so you're asking me to ensure that bytes can be used without errors as well. If I've misunderstood, please let me know.

Additionally, I looked up misc.need_bytes, but I found only util.packing._need_bytes function.
I think it is a function that replaces str to bytes, whereas we need the opposite functionality for this issue.
Hence, I will use util.packing._need_text or util.packing._decode to solve this issue.

If the above is okay, I'll write PR for you!

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

Successfully merging a pull request may close this issue.

2 participants