bpo-30520: Implemented pickling for loggers. by vsajip · Pull Request #1956 · python/cpython (original) (raw)

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 andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation14 Commits13 Checks0 Files changed

Conversation

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})

vsajip

@vsajip

pitrou

@@ -323,6 +323,8 @@ is the module's name in the Python package namespace.
.. versionadded:: 3.2
.. versionadded:: 3.7

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should probably be a versionchanged.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@vsajip

pitrou

serhiy-storchaka

def test_pickling(self):
for name in ('', 'root', 'foo', 'foo.bar', 'baz.bar'):
logger = logging.getLogger(name)
s = pickle.dumps(logger)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please test it for all protocols:

for proto in range(pickle.HIGHEST_PROTOCOL + 1):
    s = pickle.dumps(logger, proto)
@@ -1583,6 +1590,9 @@ def __init__(self, level):
"""
Logger.__init__(self, "root", level)
def __reduce__(self):
return getLogger, ('',)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe without arguments at all?

def __reduce__(self):
# In general, only the root logger will not be accessible via its name.
# However, the root logger's class has its own __reduce__ method.
if getLogger(self.name) is not self:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implicitly creates the global logger with name self.name. Is it okay?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a problem with it.

# In general, only the root logger will not be accessible via its name.
# However, the root logger's class has its own __reduce__ method.
if getLogger(self.name) is not self:
raise pickle.PicklingError('logger cannot be pickled')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pickle module is used only here. It isn't used if pickling is not involved. May be make an exception from PEP 8 and import it locally?

@@ -4086,6 +4086,13 @@ def test_invalid_names(self):
self.assertRaises(TypeError, logging.getLogger, any)
self.assertRaises(TypeError, logging.getLogger, b'foo')
def test_pickling(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__reduce__ is used also in the copy module. Maybe add tests for copy.copy() and copy.deepcopy()? Even unpickleable loggers can be copied if add special __copy__ and __deepcopy__ methods or register the classes with copyreg.pickle().

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loggers are singletons, so copy operations shouldn't actually make copies, anyway. Seems like a separate issue (or at least use case).

@@ -4086,6 +4086,13 @@ def test_invalid_names(self):
self.assertRaises(TypeError, logging.getLogger, any)
self.assertRaises(TypeError, logging.getLogger, b'foo')
def test_pickling(self):
for name in ('', 'root', 'foo', 'foo.bar', 'baz.bar'):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is self.logger pickleable? If not, it may make sense to add an explicit test for this.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's not pickleable, as it's created using Logger.__init__(), which users are not supposed to use.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is created via Logger constructor, not via getLogger(). logging.Logger('blah') is not logging.getLogger('blah').

@vsajip

serhiy-storchaka

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@serhiy-storchaka

Ah, just please add a Misc/NEWS entry.

zooba and others added 10 commits

June 5, 2017 15:54

@zooba

…codes on Windows (#1924)

@zware

Also weakens the 'should this be run?' regex to allow all builds when .travis.yml changes.

@zware

@oz123 @doerwalter

GH-1439)

Several class attributes have been added to calendar.HTMLCalendar that allow customization of the CSS classes used in the resulting HTML. This can be done by subclasses HTMLCalendar and overwriting those class attributes (Patch by Oz Tiram).

@alex

The previous implementation had grown organically over time, as OpenSSL's API evolved.

@vsajip

@vsajip

@vsajip

@vsajip

@vsajip

@vsajip vsajip deleted the pickle-logger branch

June 6, 2017 15:34