GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Specializing adaptive interpreter code object hashes are less unique · Issue #94155 · python/cpython Open Labels 3.11only security fixesonly security fixes3.12only security fixesonly security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error DescriptionIn Python 3.11 and 3.12, the hash function for a But this means that the hash of a code object no longer factors in the most unique part of a code object. Currently in 3.11 and 3.12 the hash of a code object is calculated using:
Which means it's not hard to construct code objects with identical hashes but different bytecode. For example: class A: def method(self, a, b): return a + b class B: def method(self, a, b): return a * b class C: def method(self, a, b): return a / b for cls in (A, B, C): o = cls() print(o.method(3, 5)) print(hex(hash(cls.method.__code__))) The hashes for for Is this scenario realistic? I don't know. Certainly I've never seen it. But it's at least plausible; a base class could have a tiny function that only does a little work, and a subclass could override that function and tweak the work done slightly, without relying on additional external names / closures or employing a different list of locals. It's certainly not impossible. Obviously this is low priority--there's very little code that hashes code objects. It might cause some collisions and probing when marshal dumps a module exhibiting this behavior, because marshal maintains a hash table of the objects it writes. That's the worst side-effect I can think up. We could mitigate this slightly by factoring in more values into the code object's hash. Three come immediately to mind:
With only the first two, you'd still have hash collisions from code objects defined using lambdas all on the same line: a, b, c = (lambda a, b: a + b), (lambda a, b: a * b), (lambda a, b: a / b) for l in (a, b, c): print(l(3, 5)) print(hex(hash(l.__code__))) As unlikely as it is that someone would stumble over this scenario, it's even less likely that it would cause a problem. But decreasing the likelihood of hash value collisions seems so wholesome and good, I think it's worth pursuing. Linked PRsActivity
MetadataMetadataAssigneesLabels3.11only security fixesonly security fixes3.12only security fixesonly security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error ProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |