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
4b40c012
Commit
4b40c012
authored
May 29, 2015
by
Marius Wachtler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add int.bit_length, function.func_doc, fix '(-1)**0'
parent
a58e93a3
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
39 additions
and
7 deletions
+39
-7
src/runtime/int.cpp
src/runtime/int.cpp
+31
-4
src/runtime/types.cpp
src/runtime/types.cpp
+1
-0
test/tests/decimal_test.py
test/tests/decimal_test.py
+0
-2
test/tests/functions.py
test/tests/functions.py
+5
-0
test/tests/intmethods.py
test/tests/intmethods.py
+2
-1
No files found.
src/runtime/int.cpp
View file @
4b40c012
...
...
@@ -338,11 +338,8 @@ extern "C" Box* pow_i64_i64(i64 lhs, i64 rhs) {
if
(
rhs
<
0
)
return
boxFloat
(
pow_float_float
(
lhs
,
rhs
));
if
(
rhs
==
0
)
{
if
(
lhs
<
0
)
return
boxInt
(
-
1
);
if
(
rhs
==
0
)
return
boxInt
(
1
);
}
assert
(
rhs
>
0
);
while
(
true
)
{
...
...
@@ -1035,6 +1032,34 @@ extern "C" Box* intNew(Box* _cls, Box* val, Box* base) {
return
new
(
cls
)
BoxedInt
(
n
->
n
);
}
static
const
unsigned
char
BitLengthTable
[
32
]
=
{
0
,
1
,
2
,
2
,
3
,
3
,
3
,
3
,
4
,
4
,
4
,
4
,
4
,
4
,
4
,
4
,
5
,
5
,
5
,
5
,
5
,
5
,
5
,
5
,
5
,
5
,
5
,
5
,
5
,
5
,
5
,
5
};
static
int
bits_in_ulong
(
unsigned
long
d
)
noexcept
{
int
d_bits
=
0
;
while
(
d
>=
32
)
{
d_bits
+=
6
;
d
>>=
6
;
}
d_bits
+=
(
int
)
BitLengthTable
[
d
];
return
d_bits
;
}
extern
"C"
Box
*
intBitLength
(
BoxedInt
*
v
)
{
if
(
!
isSubclass
(
v
->
cls
,
int_cls
))
raiseExcHelper
(
TypeError
,
"descriptor 'bit_length' requires a 'int' object but received a '%s'"
,
getTypeName
(
v
));
unsigned
long
n
;
if
(
v
->
n
<
0
)
/* avoid undefined behaviour when v->n == -LONG_MAX-1 */
n
=
0U
-
(
unsigned
long
)
v
->
n
;
else
n
=
(
unsigned
long
)
v
->
n
;
return
PyInt_FromLong
(
bits_in_ulong
(
n
));
}
static
void
_addFuncIntFloatUnknown
(
const
char
*
name
,
void
*
int_func
,
void
*
float_func
,
void
*
boxed_func
)
{
std
::
vector
<
ConcreteCompilerType
*>
v_ii
,
v_if
,
v_iu
;
assert
(
BOXED_INT
);
...
...
@@ -1131,6 +1156,8 @@ void setupInt() {
ParamNames
({
""
,
"x"
,
"base"
},
""
,
""
)),
{
boxInt
(
0
),
NULL
}));
int_cls
->
giveAttr
(
"bit_length"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
intBitLength
,
BOXED_INT
,
1
)));
int_cls
->
giveAttr
(
"real"
,
new
(
pyston_getset_cls
)
BoxedGetsetDescriptor
(
intInt
,
NULL
,
NULL
));
int_cls
->
giveAttr
(
"imag"
,
new
(
pyston_getset_cls
)
BoxedGetsetDescriptor
(
int0
,
NULL
,
NULL
));
int_cls
->
giveAttr
(
"conjugate"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
intInt
,
BOXED_INT
,
1
)));
...
...
src/runtime/types.cpp
View file @
4b40c012
...
...
@@ -2616,6 +2616,7 @@ void setupRuntime() {
offsetof
(
BoxedFunction
,
modname
),
false
));
function_cls
->
giveAttr
(
"__doc__"
,
new
BoxedMemberDescriptor
(
BoxedMemberDescriptor
::
OBJECT
,
offsetof
(
BoxedFunction
,
doc
),
false
));
function_cls
->
giveAttr
(
"func_doc"
,
function_cls
->
getattr
(
"__doc__"
));
function_cls
->
giveAttr
(
"__globals__"
,
new
(
pyston_getset_cls
)
BoxedGetsetDescriptor
(
functionGlobals
,
NULL
,
NULL
));
function_cls
->
giveAttr
(
"__get__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
functionGet
,
UNKNOWN
,
3
)));
function_cls
->
giveAttr
(
"__call__"
,
...
...
test/tests/decimal_test.py
View file @
4b40c012
# expected: fail
from
decimal
import
Decimal
for
d
in
(
Decimal
(
"0.5"
),
Decimal
(
"0"
),
Decimal
(
0
),
Decimal
(
1.0
)):
...
...
test/tests/functions.py
View file @
4b40c012
def
f
():
"""very nice function"""
print
"f"
return
2
print
f
.
__call__
()
print
f
.
__doc__
print
f
.
func_doc
def
g
():
print
"g"
return
3
print
g
.
__doc__
print
type
(
f
).
__call__
(
f
)
print
type
(
f
).
__call__
(
g
)
...
...
test/tests/intmethods.py
View file @
4b40c012
...
...
@@ -12,13 +12,14 @@ for i in xrange(1, 12):
print
1
**
0
print
0
**
0
print
-
1
**
0
print
-
1
**
0
,
(
-
1
)
**
0
,
(
-
5
)
**
0
print
(
11
).
__pow__
(
5
,
50
)
print
(
11
).
__pow__
(
32
,
50
)
print
(
11
).
__index__
()
for
i
in
(
-
10
,
10
,
0
,
-
15
):
print
i
,
i
.
__hex__
(),
i
.
__oct__
()
print
i
.
bit_length
()
# Testing int.__new__:
class
C
(
int
):
...
...
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