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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Lib/_py_abc.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from _weakrefset import WeakSet


Expand Down Expand Up @@ -33,6 +34,18 @@ class ABCMeta(type):
_abc_invalidation_counter = 0

def __new__(mcls, name, bases, namespace, /, **kwargs):

if '__module__' not in namespace:
try:
# Set __module__ to the frame where the class is created
module = sys._getframe(1).f_globals.get('__name__', '__main__')
except (AttributeError, ValueError):
# Bypass for environments where sys._getframe is not defined (e.g. Jython)
# or sys._getframe is not defined for arguments greater than 0 (IronPython)
pass
else:
namespace['__module__'] = module

cls = super().__new__(mcls, name, bases, namespace, **kwargs)
# Compute set of abstract method names
abstracts = {name
Expand Down
13 changes: 13 additions & 0 deletions Lib/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

"""Abstract Base Classes (ABCs) according to PEP 3119."""

import sys

def abstractmethod(funcobj):
"""A decorator indicating abstract methods.
Expand Down Expand Up @@ -82,6 +83,18 @@ class ABCMeta(type):
even via super()).
"""
def __new__(mcls, name, bases, namespace, **kwargs):

if '__module__' not in namespace:
try:
# Set __module__ to the frame where the class is created
module = sys._getframe(1).f_globals.get('__name__', '__main__')
except (AttributeError, ValueError):
# Bypass for environments where sys._getframe is not defined (e.g. Jython)
# or sys._getframe is not defined for arguments greater than 0 (IronPython)
pass
else:
namespace['__module__'] = module

cls = super().__new__(mcls, name, bases, namespace, **kwargs)
_abc_init(cls)
return cls
Expand Down
33 changes: 28 additions & 5 deletions Lib/test/test_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
import unittest

import abc
import pickle
import _py_abc
from inspect import isabstract

def test_factory(abc_ABCMeta, abc_get_cache_token):
def test_factory(abc_ABCMeta, abc_get_cache_token, abc_to_pickle):
class TestLegacyAPI(unittest.TestCase):

def test_abstractproperty_basics(self):
Expand Down Expand Up @@ -469,6 +470,20 @@ class C(with_metaclass(abc_ABCMeta, A, B)):
pass
self.assertEqual(C.__class__, abc_ABCMeta)

def test_module_of_dynamically_defined_ABC_is_caller_module(self):
ABC = abc_ABCMeta('ABC', (), {})
self.assertEqual(ABC.__module__, __name__)

# user-defined __module__ is honored over the value set via caller inspection
ABC = abc_ABCMeta('ABC', (), {'__module__': 'some.module'})
self.assertEqual(ABC.__module__, 'some.module')

def test_pickling_instance_of_dynamically_defined_ABC(self):
obj = abc_to_pickle()
obj_unpickled = pickle.loads(pickle.dumps(obj))
self.assertEqual(type(obj_unpickled), type(obj))
self.assertDictEqual(obj_unpickled.__dict__, obj.__dict__)


class TestABCWithInitSubclass(unittest.TestCase):
def test_works_with_init_subclass(self):
Expand All @@ -479,15 +494,23 @@ class ReceivesClassKwargs:
def __init_subclass__(cls, **kwargs):
super().__init_subclass__()
saved_kwargs.update(kwargs)
class Receiver(ReceivesClassKwargs, abc_ABC, x=1, y=2, z=3):
class Receiver(ReceivesClassKwargs, abc_ABC, x=1, y=2, __module__='some.module'):
pass
self.assertEqual(saved_kwargs, dict(x=1, y=2, z=3))
# __module__ passed via __init_subclass__ is honored over
# the value set via caller inspection
self.assertEqual(saved_kwargs, dict(x=1, y=2, __module__='some.module'))
return TestLegacyAPI, TestABC, TestABCWithInitSubclass

# types used for pickle test
ABCToPickle_C = abc.ABCMeta('ABCToPickle_C', (), {'data': 123})
ABCToPickle_Py = _py_abc.ABCMeta('ABCToPickle_Py', (), {'data': 123})

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?

_py_abc.get_cache_token)
_py_abc.get_cache_token,
ABCToPickle_Py)

if __name__ == "__main__":
unittest.main()
56 changes: 56 additions & 0 deletions abc_bench.py
Original file line number Diff line number Diff line change
@@ -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.

import timeit


def median(x):
n = len(x)
middle = n // 2
sx = sorted(x)
if n % 2:
return sx[middle]
else:
return (sx[middle] + sx[middle - 1]) / 2


implementations = {"C": "import abc",
"Py": "import _py_abc"}
statements = {
"C": {
"master": "abc.ABCMeta('ABC_C', (), {'__module__': __name__})",
"fix": "abc.ABCMeta('ABC_C', (), {})"
},
"Py": {
"master": "_py_abc.ABCMeta('ABC_Py', (), {'__module__': __name__})",
"fix": "_py_abc.ABCMeta('ABC_Py', (), {})"
}
}

repeat = 50000
number = 1

data = {}
for imp, setup in implementations.items():
for branch, stmt in statements[imp].items():
print("timing {} - {} implementation of ABCMeta...".format(branch, imp))
times = timeit.repeat(stmt, setup=setup, repeat=repeat, number=number)
header = "{}_{}".format(branch, imp)
data[header] = times


for imp in implementations:
t_master = median(data["master_{}".format(imp)])
t_fix = median(data["fix_{}".format(imp)])

absdiff = t_fix - t_master
slowdown = (t_fix - t_master) / t_master

print("{} implementation".format(imp))
print(" Absolute difference:", 1000 * absdiff, "ms")
print(" Slowdown:", 100 * slowdown, "%")

print("Dumping to CSV file...")
with open("abc_bench.csv", "w") as csvfile:
writer = csv.writer(csvfile)
data = [[header] + times for header, times in data.items()]
for row in zip(*data):
writer.writerow(row)