Skip to content

site.USER_SITE is not checked for None before use #114

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

Closed
hashstat opened this issue Jul 16, 2019 · 2 comments
Closed

site.USER_SITE is not checked for None before use #114

hashstat opened this issue Jul 16, 2019 · 2 comments
Labels
bug Something isn't working

Comments

@hashstat
Copy link

hashstat commented Jul 16, 2019

I'm running into an exception in loguru/_better_exceptions.py::ExceptionFormatter._get_lib_dirs() when site.USER_SITE is used without checking for None. This is happening when using the Windows embedded Python distribution which, by default, does not initialize the site module on startup.

From the documentation for the builtin site module regarding site.USER_SITE:

Path to the user site-packages for the running Python. Can be None if getusersitepackages() hasn’t been called yet.

The issue can be reproduced by eliminating the site execution on startup using the -s and -S flags. In the following example, I created a virtual environment on my Linux box and installed loguru using pip.

$ python3 -m venv venv
$ . venv/bin/activate
(venv) $ pip install loguru
...
(venv) $ PYTHONPATH='venv/lib/python3.7/site-packages' python -sS
Python 3.7.3 (default, Jun 24 2019, 04:54:02) 
[GCC 9.1.0] on linux
>>> import loguru
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "venv/lib/python3.7/site-packages/loguru/__init__.py", line 19, in <module>
    logger.add(_sys.stderr)
  File "venv/lib/python3.7/site-packages/loguru/_logger.py", line 821, in add
    hidden_frames_filename=self.catch.__code__.co_filename,
  File "venv/lib/python3.7/site-packages/loguru/_better_exceptions.py", line 133, in __init__
    self._lib_dirs = self._get_lib_dirs()
  File "venv/lib/python3.7/site-packages/loguru/_better_exceptions.py", line 161, in _get_lib_dirs
    return [os.path.abspath(d) + os.sep for d in lib_dirs]
  File "venv/lib/python3.7/site-packages/loguru/_better_exceptions.py", line 161, in <listcomp>
    return [os.path.abspath(d) + os.sep for d in lib_dirs]
  File "/usr/lib/python3.7/posixpath.py", line 378, in abspath
    path = os.fspath(path)
TypeError: expected str, bytes or os.PathLike object, not NoneType
>>>

I'm currently working around the issue by ensuring that site is executed on startup.

@Delgan Delgan added the bug Something isn't working label Jul 16, 2019
@Delgan
Copy link
Owner

Delgan commented Jul 16, 2019

Thank you for taking the time to report and investigate this bug! It should be easily fixed, I will update loguru package next week-end. 👍

@Delgan
Copy link
Owner

Delgan commented Jul 21, 2019

I just released v0.3.2 which handles missing site.USER_SITE correctly (and others special cases that I had not thought of). 😉

@Delgan Delgan closed this as completed Jul 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants