Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-28869: Set class module to caller module in ABCMeta.__new__ #14126

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

alegonz
Copy link

@alegonz alegonz commented Jun 16, 2019

Hello, this is a PR to the bug reported in bpo-28869 (https://bugs.python.org/issue28869).

Issues:

  • __module__ attribute is set differently depending on whether a metaclass is explicitly called or invoked in a class statement.
  • This causes a pickling error when dumping an instance of a class that was defined by calling ABCMeta directly, because it attempts to look for the class in abc rather than the module the class was defined in.

Solution:

If the __module__ variable is not found in the namespace variable, inspect and use the module of the caller. This follows the same pattern used in namedtuple. Values set via __init_subclass__ take precedence.

There were concerns that inspection might slow down further ABCs creation, so I also did some benchmarking and got these results:

C implementation
    Absolute difference: 0.00115500006359 ms
    Slowdown: 4.61538495086 %
Py implementation
    Absolute difference: 0.00129000000015 ms
    Slowdown: 3.67301613313 %

Notes:

  • The values reported above are the median execution times measured using timeit.repeat with 50,000 repetitions.
  • I used ABCMeta('ABC', (), {'__module__': __name__}) as a proxy for the original implementation.
  • Script was run on a laptop with Intel Core i7-3537U CPU @ 2.00GHz, 8GB RAM, running Ubuntu 16.04.

Is 3~5% considered a significant drop in performance?

I temporarily commited the script for benchmarking in the root directory for sharing purposes. This will be removed in a future commit.

Also, following the discussion in the bpo thread regarding the pickle documentation, since this issue is not limited to ABCMeta, I propose to add the following paragraph (highlighted in bold) in the "What can be pickled and unpickled?" section:

...
Note that functions (built-in and user-defined) are pickled by "fully qualified"
name reference, not by value. [2] This means that only the function name is
pickled, along with the name of the module the function is defined in. Neither
the function's code, nor any of its function attributes are pickled. Thus the
defining module must be importable in the unpickling environment, and the module
must contain the named object, otherwise an exception will be raised. [3]

When pickling instances of classes defined dynamically (i.e. classes defined by calling
type() (or any other metaclass) directly) you might need to set the __module__
attribute of the class to the module where the class was defined in. This is the case when
the class creation and the call to type() reside in different modules.

Would this be an appropriate explanation?

https://bugs.python.org/issue28869

@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA).

Our records indicate we have not received your CLA. For legal reasons we need you to sign this before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

If you have recently signed the CLA, please wait at least one business day
before our records are updated.

You can check yourself to see if the CLA has been received.

Thanks again for your contribution, we look forward to reviewing it!

@alegonz
Copy link
Author

alegonz commented Jun 16, 2019

@the-knights-who-say-ni
Done!

@@ -0,0 +1,56 @@
import csv
Copy link
Author

Choose a reason for hiding this comment

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

This file was temporarily added for sharing purposes. It will removed in a future commit.

TestLegacyAPI_Py, TestABC_Py, TestABCWithInitSubclass_Py = test_factory(abc.ABCMeta,
abc.get_cache_token)
abc.get_cache_token,
ABCToPickle_C)
TestLegacyAPI_C, TestABC_C, TestABCWithInitSubclass_C = test_factory(_py_abc.ABCMeta,
Copy link
Author

Choose a reason for hiding this comment

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

I noticed that the variables prefix do not match the module passed to the factory (they are flipped), is this a typo?

@ilevkivskyi
Copy link
Member

I would propose to close this in favor of #14166.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants