AST factory function for building scalar constants from numbers or string literals parsed by the AST. The constants are stored and cast such that they do not impose another cast operation on the virtual machine. This is the opposite behavoir to NumExpr2 where a float64 const
(self: NumExpr, node: ast.AST)
| 1122 | return register |
| 1123 | |
| 1124 | def _const(self: NumExpr, node: ast.AST) -> NumReg: |
| 1125 | ''' |
| 1126 | AST factory function for building scalar constants from numbers or string |
| 1127 | literals parsed by the AST. |
| 1128 | |
| 1129 | The constants are stored and cast such that they do not impose another cast |
| 1130 | operation on the virtual machine. This is the opposite behavoir to NumExpr2 |
| 1131 | where a float64 const could upcast an enormous array. |
| 1132 | ''' |
| 1133 | |
| 1134 | # It's easier to just use ndim==0 numpy arrays, since PyArrayScalar |
| 1135 | # isn't in the API anymore. |
| 1136 | node_n = node.n |
| 1137 | if np.iscomplex(node_n): |
| 1138 | constArr = np.complex64(node_n) |
| 1139 | elif isinstance(node_n, int): |
| 1140 | constArr = np.asarray(node_n, dtype=int) |
| 1141 | elif isinstance(node_n, float): |
| 1142 | constArr = np.asarray(node_n, dtype=float) |
| 1143 | elif type(node_n) == str or type(node_n) == bytes: |
| 1144 | constArr = np.asarray(node_n) |
| 1145 | else: |
| 1146 | raise TypeError( |
| 1147 | 'Unknown constant type for NE3 literal: {} of type {}'.format( |
| 1148 | node_n, type(node_n))) |
| 1149 | |
| 1150 | # Const arrays shouldn't be weak references as they are part of the |
| 1151 | # program and unmutable. |
| 1152 | constNo = next(self._regCount) |
| 1153 | self.registers[constNo] = register = NumReg(constNo, constNo, constArr, |
| 1154 | constArr.dtype.char, _KIND_SCALAR) |
| 1155 | return register |
| 1156 | |
| 1157 | |
| 1158 | def _attribute(self: NumExpr, node: ast.AST) -> NumReg: |
nothing calls this directly
no test coverage detected
searching dependent graphs…