classification
Title: logging module fails with unclear error when supplied a (Posix)Path
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: berker.peksag, martin.panter, python-dev, r.david.murray, rhendrikse, vinay.sajip
Priority: normal Keywords:

Created on 2016-07-12 09:14 by rhendrikse, last changed 2016-07-24 16:11 by berker.peksag. This issue is now closed.

Messages (9)
msg270233 - (view) Author: Richard (rhendrikse) Date: 2016-07-12 09:14
No idea if I should be reporting this here, but it came with the default installation, so here goes:

On a mac, I supplied a basicConfig object to the logging class that contains a PosixPath instance for the "filename" attribute.
consequently, it fails with the error message "'PosixPath' object has no attribute 'startswith'" without specifying where the issue lies.

offending value:
STATUSLOG_PATH   = Path('~/logFiles/Reseller/').expanduser()

Relevant part of the traceback (which didn't print by default):
  File "/Users/rhendrikse/Documents/PythonScripts/Reseller/Reseller/application/application.py", line 41, in __setup_logger
    logging.basicConfig(level=logging.DEBUG, format=DEFS.RUNLOG_FORMAT, filename=DEFS.RUNLOG_PATH)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/logging/__init__.py", line 1744, in basicConfig
    h = FileHandler(filename, mode)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/logging/__init__.py", line 998, in __init__
    self.baseFilename = os.path.abspath(filename)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/posixpath.py", line 357, in abspath
    if not isabs(path):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/posixpath.py", line 64, in isabs
    return s.startswith(sep)
AttributeError: 'PosixPath' object has no attribute 'startswith'
msg270234 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2016-07-12 09:24
logging module doesn't support Path objects. You need to wrap

    STATUSLOG_PATH = Path('~/logFiles/Reseller/').expanduser()

with str().

We could possibly add PEP 519 support to the logging module for Python 3.6. What do you think, Vinay?
msg270235 - (view) Author: Richard (rhendrikse) Date: 2016-07-12 09:46
Yeah, figured as much. But thanks:)

I'm kind of new to Python and was having some problems determining whether this is as it should be, or if it should be improved.
After all, I could not find any documentation that states what the permitted variable-types are (for pretty much any function, actually), yet print() does cast the Path()s to a string...

Kind of used to the documentation format on php.net :P
msg270239 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2016-07-12 12:19
> We could possibly add PEP 519 support to the logging module for Python 3.6

Seems like a reasonable enhancement for 3.6.
msg270244 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-07-12 13:32
It will be interesting to see how much logging itself needs to be aware of PEP 519 once the support is added to posixpath.  ideally, to meet the goals of PEP 519, the answer would be "not at all".

@richard: python uses duck typing: if it quacks like a duck, pretend it is a duck.  That means that the code (generally, there are exceptions) doesn't check for types, but just handles the object the way it normally would until it fails to quack, at which point you get an exception.  Sometimes we add code to turn such exceptions into clearer error messages, when the existing error *hides* what is really going on.  This would not be one of those cases though, even if supporting Path wasn't on the long term agenda: the existing error tells you that the module expects it to act like a string (support startswith, and other string methods).
msg270950 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2016-07-21 17:49
> the answer would be "not at all".

Not sure about that. If all logging did were to pass the path to lower levels such as open or abspath, that may be true. However, some handlers do path computations (e.g. the rotating file handlers) and so logging would need to have a minimal awareness of PEP 519 or else call str() on any putative filename passed in, and be completely unaware of it (assuming that str() on a Path would return the same as __fspath__().
msg271007 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-07-22 18:02
New changeset fada654c5f72 by Vinay Sajip in branch 'default':
Closes #27493: accepted Path objects in file handlers for logging.
https://hg.python.org/cpython/rev/fada654c5f72
msg271047 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-07-23 03:08
Windows fails the test:

http://buildbot.python.org/all/builders/AMD64%20Windows7%20SP1%203.x/builds/7989/steps/test/logs/stdio
======================================================================
ERROR: test_path_objects (test.test_logging.HandlerTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\buildbot.python.org\3.x.kloth-win64\build\lib\test\test_logging.py", line 599, in test_path_objects
    os.unlink(fn)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\Buildbot\\AppData\\Local\\Temp\\tmplffg6d26'
msg271052 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-07-23 04:15
New changeset 5424252ce174 by Berker Peksag in branch 'default':
Issue #27493: Fix test_path_objects under Windows
https://hg.python.org/cpython/rev/5424252ce174
History
Date User Action Args
2016-07-24 16:11:09berker.peksagsetstatus: open -> closed
2016-07-23 04:15:16python-devsetmessages: + msg271052
2016-07-23 03:08:14martin.pantersetstatus: closed -> open
nosy: + martin.panter
messages: + msg271047

2016-07-22 18:02:59python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg271007

resolution: fixed
stage: needs patch -> resolved
2016-07-21 17:49:17vinay.sajipsetmessages: + msg270950
2016-07-12 13:32:00r.david.murraysetnosy: + r.david.murray
messages: + msg270244
2016-07-12 12:19:16vinay.sajipsettype: enhancement
stage: needs patch
messages: + msg270239
versions: + Python 3.6, - Python 3.5
2016-07-12 09:46:16rhendriksesetmessages: + msg270235
2016-07-12 09:24:11berker.peksagsetnosy: + berker.peksag, vinay.sajip
messages: + msg270234
2016-07-12 09:14:44rhendriksecreate