Skip to content
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

Minor edits to the Descriptor HowTo Guide #24901

Merged
merged 25 commits into from
Oct 9, 2022
Merged
Changes from 22 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
58b799d
Fix wrong Sphinx markup preventing syntax highlighting
geryogam Mar 17, 2021
f77b0f9
Fix a bug in the ClassMethod Python equivalent
geryogam Mar 17, 2021
1dbd876
Fix the description of the ClassMethod Python equivalent
geryogam Mar 17, 2021
d0d301e
Raise TypeError for __get__(None, None) calls
geryogam Mar 19, 2021
e87fe37
Fix a typo
geryogam Mar 22, 2021
52a06d0
Use the existing cls variable
geryogam Mar 25, 2021
2f9971b
Allow keyword arguments in Object.__new__
geryogam Mar 31, 2021
2f335ca
Use two more super() for consistency
geryogam Mar 31, 2021
4886dbb
Add Object.__getattribute__ to raise AttributeError with the correct …
geryogam Mar 31, 2021
705c577
Allow attribute lookup from a class in Member.__get__
geryogam Mar 31, 2021
c0e6432
Allow keyword arguments in Type.__new__
geryogam Mar 31, 2021
81ec93c
Raise ValueError for slot names conflicting with class variables
geryogam Apr 1, 2021
34eef3a
Update descriptor.rst
geryogam Apr 1, 2021
ae8c622
Raise TypeError for non-empty __slots__ in subclasses of variable-len…
geryogam Apr 5, 2021
1d5fb96
Raise TypeError for non-empty __slots__ in subclasses of variable-len…
geryogam Apr 5, 2021
4671b91
Apply requested changes and remove changes on the slots emulation
geryogam May 10, 2022
0a132bf
Merge branch 'main' into patch-14
geryogam May 10, 2022
62fe4fc
Update descriptor.rst
geryogam May 10, 2022
1252f4b
Update descriptor.rst
geryogam May 10, 2022
58ae977
Update descriptor.rst
geryogam May 10, 2022
5630c64
Update descriptor.rst
geryogam May 10, 2022
bfb33f7
Merge branch 'main' into patch-14
geryogam May 10, 2022
5d45eaa
Revert the two-argument form of super() and forward variadic arguments
geryogam Oct 4, 2022
c853e6b
Merge branch 'main' into patch-14
rhettinger Oct 7, 2022
bcc9da0
Update descriptor.rst
geryogam Oct 8, 2022
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
16 changes: 9 additions & 7 deletions Doc/howto/descriptor.rst
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ afterwards, :meth:`__set_name__` will need to be called manually.
ORM example
-----------

The following code is simplified skeleton showing how data descriptors could
The following code is a simplified skeleton showing how data descriptors could
geryogam marked this conversation as resolved.
Show resolved Hide resolved
geryogam marked this conversation as resolved.
Show resolved Hide resolved
be used to implement an `object relational mapping
<https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping>`_.

Expand Down Expand Up @@ -1515,6 +1515,8 @@ by member descriptors:
def __get__(self, obj, objtype=None):
'Emulate member_get() in Objects/descrobject.c'
# Also see PyMember_GetOne() in Python/structmember.c
if obj is None:
return self
geryogam marked this conversation as resolved.
Show resolved Hide resolved
value = obj._slotvalues[self.offset]
if value is null:
raise AttributeError(self.name)
Expand Down Expand Up @@ -1543,13 +1545,13 @@ variables:
class Type(type):
'Simulate how the type metaclass adds member objects for slots'

def __new__(mcls, clsname, bases, mapping):
def __new__(mcls, clsname, bases, mapping, **kwargs):
'Emulate type_new() in Objects/typeobject.c'
# type_new() calls PyTypeReady() which calls add_methods()
slot_names = mapping.get('slot_names', [])
for offset, name in enumerate(slot_names):
mapping[name] = Member(name, clsname, offset)
return type.__new__(mcls, clsname, bases, mapping)
return super().__new__(mcls, clsname, bases, mapping)
geryogam marked this conversation as resolved.
Show resolved Hide resolved

The :meth:`object.__new__` method takes care of creating instances that have
slots instead of an instance dictionary. Here is a rough simulation in pure
Expand All @@ -1560,20 +1562,20 @@ Python:
class Object:
'Simulate how object.__new__() allocates memory for __slots__'

def __new__(cls, *args):
def __new__(cls, *args, **kwargs):
geryogam marked this conversation as resolved.
Show resolved Hide resolved
geryogam marked this conversation as resolved.
Show resolved Hide resolved
'Emulate object_new() in Objects/typeobject.c'
inst = super().__new__(cls)
if hasattr(cls, 'slot_names'):
empty_slots = [null] * len(cls.slot_names)
object.__setattr__(inst, '_slotvalues', empty_slots)
super(Object, inst).__setattr__('_slotvalues', empty_slots)
geryogam marked this conversation as resolved.
Show resolved Hide resolved
return inst

def __setattr__(self, name, value):
'Emulate _PyObject_GenericSetAttrWithDict() Objects/object.c'
cls = type(self)
if hasattr(cls, 'slot_names') and name not in cls.slot_names:
raise AttributeError(
f'{type(self).__name__!r} object has no attribute {name!r}'
f'{cls.__name__!r} object has no attribute {name!r}'
geryogam marked this conversation as resolved.
Show resolved Hide resolved
geryogam marked this conversation as resolved.
Show resolved Hide resolved
)
super().__setattr__(name, value)

Expand All @@ -1582,7 +1584,7 @@ Python:
cls = type(self)
if hasattr(cls, 'slot_names') and name not in cls.slot_names:
raise AttributeError(
f'{type(self).__name__!r} object has no attribute {name!r}'
f'{cls.__name__!r} object has no attribute {name!r}'
geryogam marked this conversation as resolved.
Show resolved Hide resolved
geryogam marked this conversation as resolved.
Show resolved Hide resolved
)
super().__delattr__(name)

Expand Down