[issue38235] Docs of logging module says argument is named "lvl". TypeError.

2019-09-20 Thread Daniel Andersson


New submission from Daniel Andersson :

How to reproduce:

>>> import logging
>>> logging.disable(lvl=logging.ERROR)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: disable() got an unexpected keyword argument 'lvl'

The correct keyword argument name is `level` as can be seen in the Python code 
Lib/logging/__init__.py:
---> def disable(level=CRITICAL):

The documentation uses `lvl`, see Doc/library/logging.rst:
---> .. function:: disable(lvl=CRITICAL)

The solution would be to rename the argument from `lvl` to `level` in the 
documentation.

I also noticed some more cases in the logging module docs where `lvl` is used 
(and `level` is used in the Python code):

* logging.Logger.isEnabledFor(lvl)
* logging.Logger.log(lvl, msg, *args, **kwargs)
* logging.Logger.makeRecord(name, lvl, fn, lno, msg, args, exc_info, func=None, 
extra=None, sinfo=None)
* logging.addLevelName(lvl, levelName)
* logging.getLevelName(lvl)

Maybe there are some historical reasons for this that I'm not aware about.

I also found an inconsistency. In the `Handler` class the docs does use `level`:
* logging.Handler.setLevel(level)

I can understand that the English in the documentation might be clearer when 
written as:
"Associates level `lvl` with text `levelName`"
instead of,
"Associates level `level` with text `levelName`"
- avoids the double-"level".

But at the same time, I usually trust the documentation blindly and was 
surprised by this error.

In the five listed examples above, `lvl` is only used as a positional argument. 
Maybe it is more OK to deviate from the actual name in the code in this case 
compared to keyword arguments, as in logging.disable.

--
assignee: docs@python
components: Documentation, Library (Lib)
messages: 352866
nosy: docs@python, penlect, vinay.sajip
priority: normal
severity: normal
status: open
title: Docs of logging module says argument is named "lvl". TypeError.
versions: Python 2.7, Python 3.5, Python 3.6, Python 3.7, Python 3.8, Python 3.9

___
Python tracker 
<https://bugs.python.org/issue38235>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44410] Exception in AsyncMock side_effect cases incorrect refcount

2021-06-13 Thread Daniel Andersson


New submission from Daniel Andersson :

Dear maintainers,

I discovered an unexpected behavior when the `side_effect` of an `AsyncMock` 
includes an exception. The test case below fails but I expected it to pass:

```
import sys
import unittest
from unittest.mock import AsyncMock


class A:
async def foobar(self):
while True:
try:
return await self.mock()
except Exception:
continue


class TestA(unittest.IsolatedAsyncioTestCase):
async def test_refcount(self):
a = A()
a.mock = AsyncMock(side_effect=[Exception(), None])
refc = sys.getrefcount(a)
await a.foobar()
self.assertEqual(refc, sys.getrefcount(a))


if __name__ == "__main__":
unittest.main()
```

If `side_effect=[Exception(), None]` is changed to `side_effect=[None, None]` 
the test case pass.

I discovered this in a bigger codebase while debugging why a weakref.finalize 
did not trigger as expected.

--
components: asyncio
messages: 395752
nosy: asvetlov, penlect, yselivanov
priority: normal
severity: normal
status: open
title: Exception in AsyncMock side_effect cases incorrect refcount
type: behavior
versions: Python 3.9

___
Python tracker 
<https://bugs.python.org/issue44410>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue38781] Clear buffer in MemoryHandler flush

2019-11-12 Thread Daniel Andersson


New submission from Daniel Andersson :

The `logging.handlers.MemoryHandler` has a method `flush` which clears the 
buffer by assigning an empty list literal:

self.buffer = []

This forces the buffer to be a list instance.
My suggestion is to clear the buffer like this instead:

self.buffer.clear()

In this way it would be possible to implement a custom buffer or use the 
`collections.deque` when subclassing `MemoryHandler`. At the moment you must 
copy-past the `flush` method and modify it accordingly in the subclass:

```
def flush(self):
# (Docstring skipped)
self.acquire()
try:
if self.target:
for record in self.buffer:
self.target.handle(record)
self.buffer = []  # <-- change to `self.buffer.clear()`
finally:
self.release()
```

Example where this change is useful
===
This example creates a DequeMemoryHandler which uses the `collections.deque` as 
a buffer. Only the latest `capacity` number of records will stored in the 
buffer. The buffer is then flushed if and only if a record has a level greater 
or equal to `logging.ERROR`.

```
import collections
import logging
from logging.handlers import MemoryHandler


class DequeMemoryHandler(MemoryHandler):

def __init__(self, capacity, *args, **kwargs):
super().__init__(capacity, *args, **kwargs)
self.buffer = collections.deque(maxlen=capacity)

def shouldFlush(self, record):
return record.levelno >= self.flushLevel


handler = DequeMemoryHandler(capacity=5, target=logging.StreamHandler())
logging.basicConfig(level=logging.INFO, handlers=[handler])

for i in range(1, 21):
logging.info('Spam %d', i)
if i % 10 == 0:
logging.error(f'---> Eggs {i}')
```


Desired output (with the change):
-
Spam 7
Spam 8
Spam 9
Spam 10
---> Eggs 10
Spam 17
Spam 18
Spam 19
Spam 20
---> Eggs 20


Actual output (without the change):
---
Spam 7
Spam 8
Spam 9
Spam 10
---> Eggs 10
Spam 11
Spam 12
Spam 13
Spam 14
Spam 15
Spam 16
Spam 17
Spam 18
Spam 19
Spam 20
---> Eggs 20

--
components: Library (Lib)
messages: 356489
nosy: penlect, vinay.sajip
priority: normal
severity: normal
status: open
title: Clear buffer in MemoryHandler flush
type: enhancement
versions: Python 3.9

___
Python tracker 
<https://bugs.python.org/issue38781>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue38781] Clear buffer in MemoryHandler flush

2019-11-12 Thread Daniel Andersson


Change by Daniel Andersson :


--
keywords: +patch
pull_requests: +16642
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/17132

___
Python tracker 
<https://bugs.python.org/issue38781>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue38781] Clear buffer in MemoryHandler flush

2019-11-13 Thread Daniel Andersson


Daniel Andersson  added the comment:

The suggested change has been merged. I'm closing this issue. Thank you Vinay 
Sajip for reviewing.

--
resolution:  -> fixed
stage: patch review -> resolved
status: open -> closed

___
Python tracker 
<https://bugs.python.org/issue38781>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21297] skipinitialspace in the csv module only skips spaces, not "whitespace" in general

2014-04-18 Thread Daniel Andersson

New submission from Daniel Andersson:

Regarding the `skipinitialspace` parameter to the different CSV reader dialects 
in the `csv` module, the official documentation asserts:

When True, whitespace immediately following the delimiter is ignored.

and the `help(csv)` style module documentation says:

* skipinitialspace - specifies how to interpret whitespace which
  immediately follows a delimiter.  It defaults to False, which
  means that whitespace immediately following a delimiter is part
  of the following field.

"Whitespace" is a bit too general in both cases (at least a red herring in the 
second case), since it only skips spaces and not e.g. tabs [1].

In `Modules/_csv.c`, it more correctly describes the parameter. At line 81:

int skipinitialspace;   /* ignore spaces following delimiter? */

and the actual implementation at line 638:

else if (c == ' ' && dialect->skipinitialspace)
/* ignore space at start of field */
;

No-one will probably assume that the whole UTF-8 spectrum of "whitespace" is 
skipped, but at least I initially assumed that the tab character was included.

[1]: http://en.wikipedia.org/wiki/Whitespace_character

--
assignee: docs@python
components: Documentation, Library (Lib)
messages: 216780
nosy: Daniel.Andersson, docs@python
priority: normal
severity: normal
status: open
title: skipinitialspace in the csv module only skips spaces, not "whitespace" 
in general
type: behavior
versions: Python 2.7, Python 3.1, Python 3.2, Python 3.3, Python 3.4, Python 3.5

___
Python tracker 
<http://bugs.python.org/issue21297>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21297] csv.skipinitialspace only skips spaces, not "whitespace" in general

2014-04-20 Thread Daniel Andersson

Daniel Andersson added the comment:

No, multiple spaces are ignored as advertised (according to actual tests; not 
just reading the code), but only spaces (U+0020) and not e.g. tabs (U+0009), 
which are also included in the term "whitespace", along with several other 
characters.

In light of your followup question, the internal comment at `Modules/_csv.c`, 
line 639:

/* ignore space at start of field */

could perhaps be clarified to say "spaces" instead of "space", but the code 
context makes it quite clear, and it does not face the users anyway. The main 
point of this issue is meant to be the wording in the module docstring and the 
official docs regarding "whitespace" contra "space".

--

___
Python tracker 
<http://bugs.python.org/issue21297>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com