Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
d15057c1
Commit
d15057c1
authored
Jan 10, 1995
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
parent
52d2c38b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
50 deletions
+40
-50
Objects/object.c
Objects/object.c
+33
-0
Python/bltinmodule.c
Python/bltinmodule.c
+7
-50
No files found.
Objects/object.c
View file @
d15057c1
...
@@ -319,6 +319,39 @@ testbool(v)
...
@@ -319,6 +319,39 @@ testbool(v)
return
res
;
return
res
;
}
}
/* Coerce two numeric types to the "larger" one.
Increment the reference count on each argument.
Return -1 and raise an exception if no coercion is possible
(and then no reference count is incremented).
*/
int
coerce
(
pv
,
pw
)
object
**
pv
,
**
pw
;
{
register
object
*
v
=
*
pv
;
register
object
*
w
=
*
pw
;
int
res
;
if
(
v
->
ob_type
==
w
->
ob_type
&&
!
is_instanceobject
(
v
))
{
INCREF
(
v
);
INCREF
(
w
);
return
0
;
}
if
(
v
->
ob_type
->
tp_as_number
&&
v
->
ob_type
->
tp_as_number
->
nb_coerce
)
{
res
=
(
*
v
->
ob_type
->
tp_as_number
->
nb_coerce
)(
pv
,
pw
);
if
(
res
<=
0
)
return
res
;
}
if
(
w
->
ob_type
->
tp_as_number
&&
w
->
ob_type
->
tp_as_number
->
nb_coerce
)
{
res
=
(
*
w
->
ob_type
->
tp_as_number
->
nb_coerce
)(
pw
,
pv
);
if
(
res
<=
0
)
return
res
;
}
err_setstr
(
TypeError
,
"number coercion failed"
);
return
-
1
;
}
/*
/*
NoObject is usable as a non-NULL undefined value, used by the macro None.
NoObject is usable as a non-NULL undefined value, used by the macro None.
...
...
Python/bltinmodule.c
View file @
d15057c1
...
@@ -255,32 +255,22 @@ builtin_cmp(self, args)
...
@@ -255,32 +255,22 @@ builtin_cmp(self, args)
return
newintobject
((
long
)
cmpobject
(
a
,
b
));
return
newintobject
((
long
)
cmpobject
(
a
,
b
));
}
}
static
object
*
do_coerce
(
v
,
w
)
object
*
v
,
*
w
;
{
object
*
res
;
if
(
is_instanceobject
(
v
)
||
is_instanceobject
(
w
))
return
instancebinop
(
v
,
w
,
"__coerce__"
,
"__rcoerce__"
,
do_coerce
);
if
(
coerce
(
&
v
,
&
w
)
<
0
)
return
NULL
;
res
=
mkvalue
(
"(OO)"
,
v
,
w
);
DECREF
(
v
);
DECREF
(
w
);
return
res
;
}
static
object
*
static
object
*
builtin_coerce
(
self
,
args
)
builtin_coerce
(
self
,
args
)
object
*
self
;
object
*
self
;
object
*
args
;
object
*
args
;
{
{
object
*
v
,
*
w
;
object
*
v
,
*
w
;
object
*
res
;
if
(
!
newgetargs
(
args
,
"OO:coerce"
,
&
v
,
&
w
))
if
(
!
newgetargs
(
args
,
"OO:coerce"
,
&
v
,
&
w
))
return
NULL
;
return
NULL
;
return
do_coerce
(
v
,
w
);
if
(
coerce
(
&
v
,
&
w
)
<
0
)
return
NULL
;
res
=
mkvalue
(
"(OO)"
,
v
,
w
);
DECREF
(
v
);
DECREF
(
w
);
return
res
;
}
}
static
object
*
static
object
*
...
@@ -1464,39 +1454,6 @@ initbuiltin()
...
@@ -1464,39 +1454,6 @@ initbuiltin()
(
void
)
dictinsert
(
builtin_dict
,
"None"
,
None
);
(
void
)
dictinsert
(
builtin_dict
,
"None"
,
None
);
}
}
/* Coerce two numeric types to the "larger" one.
Increment the reference count on each argument.
Return -1 and raise an exception if no coercion is possible
(and then no reference count is incremented).
*/
int
coerce
(
pv
,
pw
)
object
**
pv
,
**
pw
;
{
register
object
*
v
=
*
pv
;
register
object
*
w
=
*
pw
;
int
res
;
if
(
v
->
ob_type
==
w
->
ob_type
&&
!
is_instanceobject
(
v
))
{
INCREF
(
v
);
INCREF
(
w
);
return
0
;
}
if
(
v
->
ob_type
->
tp_as_number
&&
v
->
ob_type
->
tp_as_number
->
nb_coerce
)
{
res
=
(
*
v
->
ob_type
->
tp_as_number
->
nb_coerce
)(
pv
,
pw
);
if
(
res
<=
0
)
return
res
;
}
if
(
w
->
ob_type
->
tp_as_number
&&
w
->
ob_type
->
tp_as_number
->
nb_coerce
)
{
res
=
(
*
w
->
ob_type
->
tp_as_number
->
nb_coerce
)(
pw
,
pv
);
if
(
res
<=
0
)
return
res
;
}
err_setstr
(
TypeError
,
"number coercion failed"
);
return
-
1
;
}
/* Helper for filter(): filter a tuple through a function */
/* Helper for filter(): filter a tuple through a function */
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment