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
5f1b69b3
Commit
5f1b69b3
authored
Aug 05, 2015
by
Boxiang Sun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add __hash__ to complex, and fix the hash problem in long and str
parent
03e4ab92
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
37 additions
and
1 deletion
+37
-1
src/runtime/complex.cpp
src/runtime/complex.cpp
+28
-0
src/runtime/long.cpp
src/runtime/long.cpp
+1
-1
src/runtime/str.cpp
src/runtime/str.cpp
+8
-0
No files found.
src/runtime/complex.cpp
View file @
5f1b69b3
...
...
@@ -228,6 +228,33 @@ static void _addFunc(const char* name, ConcreteCompilerType* rtn_type, void* com
complex_cls
->
giveAttr
(
name
,
new
BoxedFunction
(
cl
));
}
Box
*
complexHash
(
BoxedComplex
*
self
)
{
if
(
!
isSubclass
(
self
->
cls
,
complex_cls
))
raiseExcHelper
(
TypeError
,
"descriptor '__hash__' requires a 'complex' object but received a '%s'"
,
getTypeName
(
self
));
long
hashreal
,
hashimag
,
combined
;
hashreal
=
_Py_HashDouble
(
self
->
real
);
if
(
hashreal
==
-
1
)
{
throwCAPIException
();
}
hashimag
=
_Py_HashDouble
(
self
->
imag
);
if
(
hashimag
==
-
1
)
{
throwCAPIException
();
}
/* Note: if the imaginary part is 0, hashimag is 0 now,
* so the following returns hashreal unchanged. This is
* important because numbers of different types that
* compare equal must have the same hash value, so that
* hash(x + 0*j) must equal hash(x).
*/
combined
=
hashreal
+
1000003
*
hashimag
;
if
(
combined
==
-
1
)
combined
=
-
2
;
return
boxInt
(
combined
);
}
Box
*
complexStr
(
BoxedComplex
*
self
)
{
assert
(
self
->
cls
==
complex_cls
);
return
boxString
(
complexFmt
(
self
->
real
,
self
->
imag
,
12
,
'g'
));
...
...
@@ -336,6 +363,7 @@ void setupComplex() {
(
void
*
)
complexDiv
);
complex_cls
->
giveAttr
(
"__pos__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
complexPos
,
BOXED_COMPLEX
,
1
)));
complex_cls
->
giveAttr
(
"__hash__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
complexHash
,
BOXED_INT
,
1
)));
complex_cls
->
giveAttr
(
"__str__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
complexStr
,
STR
,
1
)));
complex_cls
->
giveAttr
(
"__repr__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
complexRepr
,
STR
,
1
)));
complex_cls
->
giveAttr
(
"real"
,
...
...
src/runtime/long.cpp
View file @
5f1b69b3
...
...
@@ -661,7 +661,7 @@ BoxedLong* _longNew(Box* val, Box* _base) {
int
r
=
mpz_init_set_str
(
rtn
->
n
,
s
.
data
(),
10
);
RELEASE_ASSERT
(
r
==
0
,
""
);
}
else
if
(
val
->
cls
==
float_cls
)
{
mpz_init_set_
si
(
rtn
->
n
,
static_cast
<
BoxedFloat
*>
(
val
)
->
d
);
mpz_init_set_
d
(
rtn
->
n
,
static_cast
<
BoxedFloat
*>
(
val
)
->
d
);
}
else
{
static
BoxedString
*
long_str
=
internStringImmortal
(
"__long__"
);
CallattrFlags
callattr_flags
{.
cls_only
=
true
,
.
null_on_nonexistent
=
true
,
.
argspec
=
ArgPassSpec
(
0
)
};
...
...
src/runtime/str.cpp
View file @
5f1b69b3
...
...
@@ -1526,6 +1526,10 @@ extern "C" size_t unicodeHashUnboxed(PyUnicodeObject* self) {
return
self
->
hash
;
Py_ssize_t
len
=
PyUnicode_GET_SIZE
(
self
);
if
(
len
==
0
)
return
0
;
Py_UNICODE
*
p
=
PyUnicode_AS_UNICODE
(
self
);
pyston
::
StringHash
<
Py_UNICODE
>
H
;
return
H
(
p
,
len
);
...
...
@@ -1534,6 +1538,10 @@ extern "C" size_t unicodeHashUnboxed(PyUnicodeObject* self) {
extern
"C"
Box
*
strHash
(
BoxedString
*
self
)
{
assert
(
PyString_Check
(
self
));
// CPython set the hash empty string to 0 manually
if
(
self
->
size
()
==
0
)
return
boxInt
(
0
);
StringHash
<
char
>
H
;
return
boxInt
(
H
(
self
->
data
(),
self
->
size
()));
}
...
...
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