Skip to content

Commit

Permalink
Fixes #387 : Class name as dictionary key. Fixes #388 : hash collisio…
Browse files Browse the repository at this point in the history
…ns between different classes, and between classes and instances
  • Loading branch information
Pierre Quentel committed Feb 28, 2016
1 parent 5dd9593 commit 7cb7168
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 17 deletions.
10 changes: 5 additions & 5 deletions www/src/brython.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ $B.language=window.navigator.userLanguage ||window.navigator.language
$B.charset=document.characterSet ||document.inputEncoding ||"utf-8"
$B.max_int=Math.pow(2,53)-1
$B.min_int=-$B.max_int
$B.$py_next_hash=-Math.pow(2,53)
$B.$py_next_hash=Math.pow(2,53)-1
$B.$py_UUID=0
$B.lambda_magic=Math.random().toString(36).substr(2,8)
$B.callbacks={}
Expand All @@ -60,7 +60,7 @@ return $B.frames_stack[$B.frames_stack.length-1][3]}})(__BRYTHON__)
__BRYTHON__.implementation=[3,2,5,'alpha',0]
__BRYTHON__.__MAGIC__="3.2.5"
__BRYTHON__.version_info=[3,3,0,'alpha',0]
__BRYTHON__.compiled_date="2016-02-23 11:45:19.107808"
__BRYTHON__.compiled_date="2016-02-28 21:38:25.337382"
__BRYTHON__.builtin_module_names=["posix","sys","errno","time","_ajax","_browser","_html","_jsre","_multiprocessing","_posixsubprocess","_svg","_sys","builtins","dis","hashlib","javascript","json","long_int","math","modulefinder","random","_abcoll","_codecs","_collections","_csv","_functools","_imp","_io","_random","_socket","_sre","_string","_struct","_sysconfigdata","_testcapi","_thread","_warnings","_weakref"]

;(function($B){var js,$pos,res,$op
Expand Down Expand Up @@ -4287,7 +4287,7 @@ throw _b_.TypeError(msg)}}
func.$infos={__name__ : klass.__name__+'.'+attr}
return func}}}}
$ObjectDict.__gt__=$ObjectNI('__gt__','>')
$ObjectDict.__hash__=function(self){$B.$py_next_hash+=1;
$ObjectDict.__hash__=function(self){$B.$py_next_hash--;
return $B.$py_next_hash;}
$ObjectDict.__init__=function(){return _b_.None}
$ObjectDict.__le__=$ObjectNI('__le__','<=')
Expand Down Expand Up @@ -5417,10 +5417,10 @@ if(isinstance(obj,_b_.int))return obj.valueOf()
if(isinstance(obj,bool))return _b_.int(obj)
if(obj.__hash__ !==undefined){return obj.__hashvalue__=obj.__hash__()}
var hashfunc=getattr(obj,'__hash__',_b_.None)
if(hashfunc==_b_.None)return $B.$py_next_hash++
if(hashfunc==_b_.None)return $B.$py_next_hash--
if(hashfunc.$infos===undefined){return obj.__hashvalue__=hashfunc()}
if(hashfunc.$infos.__func__===_b_.object.$dict.__hash__){if(getattr(obj,'__eq__').$infos.__func__!==_b_.object.$dict.__eq__){throw _b_.TypeError("unhashable type: '"+
$B.get_class(obj).__name__+"'")}else{return $B.$py_next_hash++}}else{return obj.__hashvalue__=hashfunc()}}
$B.get_class(obj).__name__+"'")}else{return $B.$py_next_hash--}}else{return obj.__hashvalue__=hashfunc()}}
function _get_builtins_doc(){if($B.builtins_doc===undefined){
var url=$B.brython_path
if(url.charAt(url.length-1)=='/'){url=url.substr(0,url.length-1)}
Expand Down
2 changes: 1 addition & 1 deletion www/src/brython_builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ $B.min_int = -$B.max_int

// Used to compute the hash value of some objects (see
// py_builtin_functions.js)
$B.$py_next_hash = -Math.pow(2,53)
$B.$py_next_hash = Math.pow(2,53)-1

// $py_UUID guarantees a unique id. Do not use this variable
// directly, use the $B.UUID function defined in py_utils.js
Expand Down
12 changes: 6 additions & 6 deletions www/src/brython_dist.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion www/src/py_VFS.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions www/src/py_builtin_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ function hash(obj){
}
var hashfunc = getattr(obj, '__hash__', _b_.None)

if (hashfunc == _b_.None) return $B.$py_next_hash++
if (hashfunc == _b_.None) return $B.$py_next_hash--

if(hashfunc.$infos === undefined){
return obj.__hashvalue__ = hashfunc()
Expand All @@ -652,7 +652,7 @@ function hash(obj){
throw _b_.TypeError("unhashable type: '"+
$B.get_class(obj).__name__+"'")
}else{
return $B.$py_next_hash++
return $B.$py_next_hash--
}
}else{
return obj.__hashvalue__= hashfunc()
Expand Down
2 changes: 1 addition & 1 deletion www/src/py_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ $ObjectDict.__getattribute__ = function(obj,attr){
$ObjectDict.__gt__ = $ObjectNI('__gt__','>')

$ObjectDict.__hash__ = function (self) {
$B.$py_next_hash+=1;
$B.$py_next_hash--;
return $B.$py_next_hash;
}

Expand Down
2 changes: 1 addition & 1 deletion www/src/version_info.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__BRYTHON__.implementation = [3, 2, 5, 'alpha', 0]
__BRYTHON__.__MAGIC__ = "3.2.5"
__BRYTHON__.version_info = [3, 3, 0, 'alpha', 0]
__BRYTHON__.compiled_date = "2016-02-23 11:45:19.107808"
__BRYTHON__.compiled_date = "2016-02-28 21:38:25.337382"
__BRYTHON__.builtin_module_names = ["posix","sys","errno", "time","_ajax",
"_browser",
"_html",
Expand Down
27 changes: 27 additions & 0 deletions www/tests/issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,33 @@ def f():
"test" %3
assertRaises(TypeError, f)

# issues 387 and 388
class A():
pass

class B():
pass

a1 = A()
a2 = A()

assert hash(A) != hash(B)
assert hash(A) != hash(a1)
assert hash(A) != hash(a2)

class A():
pass

class B():
pass

d = {A: "class A"}

def test():
d[B]

assertRaises(KeyError, test)

# ==========================================
# Finally, report that all tests have passed
# ==========================================
Expand Down

0 comments on commit 7cb7168

Please sign in to comment.