pyldapsearch
pyldapsearch copied to clipboard
_printlog can't handle utf8.
Ldap can return UTF characters, but python crashes when trying to log it with _printlog.
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "C:\Users\User\AppData\Local\Programs\Python\Python313\Scripts\pyldapsearch.exe\__main__.py", line 7, in <module>
sys.exit(app())
~~~^^
File "C:\Users\User\AppData\Local\Programs\Python\Python313\Lib\site-packages\typer\main.py", line 214, in __call__
return get_command(self)(*args, **kwargs)
~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
File "C:\Users\User\AppData\Local\Programs\Python\Python313\Lib\site-packages\click\core.py", line 1161, in __call__
return self.main(*args, **kwargs)
~~~~~~~~~^^^^^^^^^^^^^^^^^
File "C:\Users\User\AppData\Local\Programs\Python\Python313\Lib\site-packages\click\core.py", line 1082, in main
rv = self.invoke(ctx)
File "C:\Users\User\AppData\Local\Programs\Python\Python313\Lib\site-packages\click\core.py", line 1443, in invoke
return ctx.invoke(self.callback, **ctx.params)
~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\AppData\Local\Programs\Python\Python313\Lib\site-packages\click\core.py", line 788, in invoke
return __callback(*args, **kwargs)
File "C:\Users\User\AppData\Local\Programs\Python\Python313\Lib\site-packages\typer\main.py", line 532, in wrapper
return callback(**use_params) # type: ignore
File "C:\Users\User\AppData\Local\Programs\Python\Python313\Lib\site-packages\pyldapsearch\__main__.py", line 470, in main
ldapsearch.query()
~~~~~~~~~~~~~~~~^^
File "C:\Users\User\AppData\Local\Programs\Python\Python313\Lib\site-packages\pyldapsearch\__main__.py", line 332, in query
self._printlog(f'{attr}: {value}')
~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\AppData\Local\Programs\Python\Python313\Lib\site-packages\pyldapsearch\__main__.py", line 301, in _printlog
f.write(f'{line}\n')
~~~~~~~^^^^^^^^^^^^^
File "C:\Users\User\AppData\Local\Programs\Python\Python313\Lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'charmap' codec can't encode character '\u0119' in position 81: character maps to <undefined>
I fixed this on my local version by modifying _printlog to perform UTF8 encoding, but this obviously this means the output is no longer ascii, which may or may not be wanted.
Old
def _printlog(self, line, log=False):
with open(self.filename, 'a') as f:
f.write(f'{line}\n')
if log:
logging.info(line)
else:
if not self.silent:
print(line)
New
def _printlog(self, line, log=False):
with open(self.filename, 'a', encoding='utf-8') as f:
f.write(f'{line}\n')
if log:
logging.info(line)
else:
if not self.silent:
print(line)