Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Pyston
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
Boxiang Sun
Pyston
Commits
94020486
Commit
94020486
authored
Feb 17, 2016
by
Boxiang Sun
1
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add __format__ function to long object
parent
83f56df0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
0 deletions
+36
-0
src/runtime/long.cpp
src/runtime/long.cpp
+34
-0
test/tests/long.py
test/tests/long.py
+2
-0
No files found.
src/runtime/long.cpp
View file @
94020486
...
...
@@ -841,6 +841,39 @@ Box* longStr(BoxedLong* v) {
return
_PyLong_Format
(
v
,
10
,
0
/* no L */
,
0
);
}
Box
*
long__format__
(
BoxedLong
*
self
,
Box
*
format_spec
)
noexcept
{
if
(
PyBytes_Check
(
format_spec
))
return
_PyLong_FormatAdvanced
(
self
,
PyBytes_AS_STRING
(
format_spec
),
PyBytes_GET_SIZE
(
format_spec
));
if
(
PyUnicode_Check
(
format_spec
))
{
/* Convert format_spec to a str */
PyObject
*
result
;
PyObject
*
str_spec
=
PyObject_Str
(
format_spec
);
if
(
str_spec
==
NULL
)
return
NULL
;
result
=
_PyLong_FormatAdvanced
(
self
,
PyBytes_AS_STRING
(
str_spec
),
PyBytes_GET_SIZE
(
str_spec
));
Py_DECREF
(
str_spec
);
return
result
;
}
PyErr_SetString
(
PyExc_TypeError
,
"__format__ requires str or unicode"
);
return
NULL
;
}
Box
*
longFormat
(
BoxedLong
*
self
,
Box
*
format_spec
)
{
if
(
!
PyLong_Check
(
self
))
raiseExcHelper
(
TypeError
,
"descriptor '__format__' requires a 'long' object but received a '%s'"
,
getTypeName
(
self
));
Box
*
res
=
long__format__
(
self
,
format_spec
);
if
(
res
==
NULL
)
throwCAPIException
();
return
res
;
}
Box
*
longBin
(
BoxedLong
*
v
)
{
if
(
!
PyLong_Check
(
v
))
raiseExcHelper
(
TypeError
,
"descriptor '__bin__' requires a 'long' object but received a '%s'"
,
getTypeName
(
v
));
...
...
@@ -1713,6 +1746,7 @@ void setupLong() {
long_cls
->
giveAttr
(
"__float__"
,
new
BoxedFunction
(
FunctionMetadata
::
create
((
void
*
)
longFloat
,
UNKNOWN
,
1
)));
long_cls
->
giveAttr
(
"__repr__"
,
new
BoxedFunction
(
FunctionMetadata
::
create
((
void
*
)
longRepr
,
STR
,
1
)));
long_cls
->
giveAttr
(
"__str__"
,
new
BoxedFunction
(
FunctionMetadata
::
create
((
void
*
)
longStr
,
STR
,
1
)));
long_cls
->
giveAttr
(
"__format__"
,
new
BoxedFunction
(
FunctionMetadata
::
create
((
void
*
)
longFormat
,
STR
,
2
)));
long_cls
->
giveAttr
(
"__bin__"
,
new
BoxedFunction
(
FunctionMetadata
::
create
((
void
*
)
longBin
,
STR
,
1
)));
long_cls
->
giveAttr
(
"__hex__"
,
new
BoxedFunction
(
FunctionMetadata
::
create
((
void
*
)
longHex
,
STR
,
1
)));
long_cls
->
giveAttr
(
"__oct__"
,
new
BoxedFunction
(
FunctionMetadata
::
create
((
void
*
)
longOct
,
STR
,
1
)));
...
...
test/tests/long.py
View file @
94020486
...
...
@@ -203,3 +203,5 @@ for x in data:
arg2
=
y
)))
except
Exception
as
e
:
print
(
e
.
message
)
print
(
long
.
__format__
(
130L
,
'd'
))
Boxiang Sun
@Daetalus
mentioned in commit
73ab425f
·
Sep 08, 2016
mentioned in commit
73ab425f
mentioned in commit 73ab425f796a51fb736323840998676ead711bef
Toggle commit list
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