1# Copyright 2021, Kay Hayen, mailto:kay.hayen@gmail.com 2# 3# Part of "Nuitka", an optimizing Python compiler that is compatible and 4# integrates with CPython, but also works on its own. 5# 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18""" Node the calls to the 'hash' built-in. 19 20This is a specific thing, which must be calculated at run time, but we can 21predict things about its type, and the fact that it won't raise an exception 22for some types, so it is still useful. Also calls to it can be accelerated 23slightly. 24""" 25 26from .ExpressionBases import ExpressionChildHavingBase 27 28 29class ExpressionBuiltinHash(ExpressionChildHavingBase): 30 kind = "EXPRESSION_BUILTIN_HASH" 31 32 named_child = "value" 33 34 def __init__(self, value, source_ref): 35 ExpressionChildHavingBase.__init__(self, value=value, source_ref=source_ref) 36 37 def computeExpression(self, trace_collection): 38 value = self.subnode_value 39 40 # TODO: Have a computation slot for hashing and specialize for known cases. 41 if not value.isKnownToBeHashable(): 42 trace_collection.onExceptionRaiseExit(BaseException) 43 44 # TODO: Static raise if it's known not to be hashable. 45 46 return self, None, None 47 48 def mayRaiseException(self, exception_type): 49 return ( 50 self.subnode_value.mayRaiseException(exception_type) 51 or not self.subnode_value.isKnownToBeHashable() 52 ) 53 54 def mayRaiseExceptionOperation(self): 55 return not self.subnode_value.isKnownToBeHashable() 56