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
22b337b8
Commit
22b337b8
authored
Aug 22, 2007
by
Alex Martelli
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement the trunc builtin for PEP 3141
parent
5d2e7acb
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
0 deletions
+36
-0
Lib/test/test_builtin.py
Lib/test/test_builtin.py
+14
-0
Python/bltinmodule.c
Python/bltinmodule.c
+22
-0
No files found.
Lib/test/test_builtin.py
View file @
22b337b8
...
...
@@ -1515,6 +1515,20 @@ class BuiltinTest(unittest.TestCase):
raise
ValueError
self
.
assertRaises
(
ValueError
,
sum
,
BadSeq
())
def
test_trunc
(
self
):
class
TestTrunc
:
def
__trunc__
(
self
):
return
23
class
TestNoTrunc
:
pass
self
.
assertEqual
(
trunc
(
TestTrunc
()),
23
)
self
.
assertRaises
(
TypeError
,
trunc
)
self
.
assertRaises
(
TypeError
,
trunc
,
1
,
2
)
self
.
assertRaises
(
TypeError
,
trunc
,
TestNoTrunc
())
def
test_tuple
(
self
):
self
.
assertEqual
(
tuple
(()),
())
t0_3
=
(
0
,
1
,
2
,
3
)
...
...
Python/bltinmodule.c
View file @
22b337b8
...
...
@@ -1486,6 +1486,27 @@ PyDoc_STRVAR(vars_doc,
Without arguments, equivalent to locals().
\n
\
With an argument, equivalent to object.__dict__."
);
static
PyObject
*
builtin_trunc
(
PyObject
*
self
,
PyObject
*
v
)
{
PyObject
*
res
;
PyObject
*
d
=
PyObject_GetAttrString
(
v
,
"__trunc__"
);
if
(
d
==
NULL
)
{
PyErr_SetString
(
PyExc_TypeError
,
"trunc() argument must have __trunc__ attribute"
);
return
NULL
;
}
res
=
PyObject_CallFunction
(
d
,
""
);
Py_DECREF
(
d
);
return
res
;
}
PyDoc_STRVAR
(
trunc_doc
,
"trunc(Real) -> Integral
\n
\
\n
\
returns the integral closest to x between 0 and x."
);
static
PyObject
*
builtin_sum
(
PyObject
*
self
,
PyObject
*
args
)
...
...
@@ -1659,6 +1680,7 @@ static PyMethodDef builtin_methods[] = {
{
"sorted"
,
(
PyCFunction
)
builtin_sorted
,
METH_VARARGS
|
METH_KEYWORDS
,
sorted_doc
},
{
"sum"
,
builtin_sum
,
METH_VARARGS
,
sum_doc
},
{
"vars"
,
builtin_vars
,
METH_VARARGS
,
vars_doc
},
{
"trunc"
,
builtin_trunc
,
METH_O
,
trunc_doc
},
{
"zip"
,
builtin_zip
,
METH_VARARGS
,
zip_doc
},
{
NULL
,
NULL
},
};
...
...
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