Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Cpython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
sip
Cpython
Commits
820ef628
Unverified
Commit
820ef628
authored
Oct 9, 2022
by
Miss Islington (bot)
Committed by
GitHub
Oct 9, 2022
Browse files
Options
Downloads
Patches
Plain Diff
[3.10] Minor edits to the Descriptor HowTo Guide (GH-24901) (GH-98114)
parent
c86ee93d
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Doc/howto/descriptor.rst
+8
-6
8 additions, 6 deletions
Doc/howto/descriptor.rst
with
8 additions
and
6 deletions
Doc/howto/descriptor.rst
+
8
−
6
View file @
820ef628
...
@@ -847,7 +847,7 @@ afterwards, :meth:`__set_name__` will need to be called manually.
...
@@ -847,7 +847,7 @@ afterwards, :meth:`__set_name__` will need to be called manually.
ORM example
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
be used to implement an `object relational mapping
be used to implement an `object relational mapping
<https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping>`_.
<https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping>`_.
...
@@ -1533,6 +1533,8 @@ by member descriptors:
...
@@ -1533,6 +1533,8 @@ by member descriptors:
def __get__(self, obj, objtype=None):
def __get__(self, obj, objtype=None):
'Emulate member_get() in Objects/descrobject.c'
'Emulate member_get() in Objects/descrobject.c'
# Also see PyMember_GetOne() in Python/structmember.c
# Also see PyMember_GetOne() in Python/structmember.c
if obj is None:
return self
value = obj._slotvalues[self.offset]
value = obj._slotvalues[self.offset]
if value is null:
if value is null:
raise AttributeError(self.name)
raise AttributeError(self.name)
...
@@ -1561,13 +1563,13 @@ variables:
...
@@ -1561,13 +1563,13 @@ variables:
class Type(type):
class Type(type):
'Simulate how the type metaclass adds member objects for slots'
'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'
'Emulate type_new() in Objects/typeobject.c'
# type_new() calls PyTypeReady() which calls add_methods()
# type_new() calls PyTypeReady() which calls add_methods()
slot_names = mapping.get('slot_names', [])
slot_names = mapping.get('slot_names', [])
for offset, name in enumerate(slot_names):
for offset, name in enumerate(slot_names):
mapping[name] = Member(name, clsname, offset)
mapping[name] = Member(name, clsname, offset)
return type.__new__(mcls, clsname, bases, mapping)
return type.__new__(mcls, clsname, bases, mapping
, **kwargs
)
The :meth:`object.__new__` method takes care of creating instances that have
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
slots instead of an instance dictionary. Here is a rough simulation in pure
...
@@ -1578,7 +1580,7 @@ Python:
...
@@ -1578,7 +1580,7 @@ Python:
class Object:
class Object:
'Simulate how object.__new__() allocates memory for __slots__'
'Simulate how object.__new__() allocates memory for __slots__'
def __new__(cls, *args):
def __new__(cls, *args
, **kwargs
):
'Emulate object_new() in Objects/typeobject.c'
'Emulate object_new() in Objects/typeobject.c'
inst = super().__new__(cls)
inst = super().__new__(cls)
if hasattr(cls, 'slot_names'):
if hasattr(cls, 'slot_names'):
...
@@ -1591,7 +1593,7 @@ Python:
...
@@ -1591,7 +1593,7 @@ Python:
cls = type(self)
cls = type(self)
if hasattr(cls, 'slot_names') and name not in cls.slot_names:
if hasattr(cls, 'slot_names') and name not in cls.slot_names:
raise AttributeError(
raise AttributeError(
f'{
type(self)
.__name__!r} object has no attribute {name!r}'
f'{
cls
.__name__!r} object has no attribute {name!r}'
)
)
super().__setattr__(name, value)
super().__setattr__(name, value)
...
@@ -1600,7 +1602,7 @@ Python:
...
@@ -1600,7 +1602,7 @@ Python:
cls = type(self)
cls = type(self)
if hasattr(cls, 'slot_names') and name not in cls.slot_names:
if hasattr(cls, 'slot_names') and name not in cls.slot_names:
raise AttributeError(
raise AttributeError(
f'{
type(self)
.__name__!r} object has no attribute {name!r}'
f'{
cls
.__name__!r} object has no attribute {name!r}'
)
)
super().__delattr__(name)
super().__delattr__(name)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment