Commit 37f6b079 authored by Stefan Behnel's avatar Stefan Behnel

revert integer literal suffixing again and instead generate C hex literals if...

revert integer literal suffixing again and instead generate C hex literals if no suffix is used to make the C compiler consider unsigned types for their values (see C standard 6.4.4)
parent c1a97ed7
......@@ -1133,19 +1133,13 @@ class IntNode(ConstNode):
return FloatNode(self.pos, value=self.value, type=dst_type,
constant_result=not_a_constant)
if dst_type.is_numeric and not dst_type.is_complex:
unsigned = self.unsigned
if not unsigned and dst_type.is_int and not dst_type.signed:
# mark positive literals as 'U' when coercing them to unsigned integer types
# to extend their potential value range in the C code
if self.has_constant_result() and self.constant_result > 0:
unsigned = 'U'
node = IntNode(self.pos, value=self.value, constant_result=self.constant_result,
type=dst_type, is_c_literal=True,
unsigned=unsigned, longness=self.longness)
unsigned=self.unsigned, longness=self.longness)
return node
elif dst_type.is_pyobject:
node = IntNode(self.pos, value=self.value, constant_result=self.constant_result,
type = PyrexTypes.py_object_type, is_c_literal = False,
type=PyrexTypes.py_object_type, is_c_literal=False,
unsigned=self.unsigned, longness=self.longness)
else:
# FIXME: not setting the type here to keep it working with
......@@ -1178,11 +1172,17 @@ class IntNode(ConstNode):
def value_as_c_integer_string(self):
value = self.value
if len(value) > 2:
# convert C-incompatible Py3 oct/bin notations
if value[1] in 'oO':
value = value[0] + value[2:] # '0o123' => '0123'
elif value[1] in 'bB':
if value[0] == '0':
literal_type = value[1] # 0'o' - 0'b' - 0'x'
# 0x123 hex literals and 0123 octal literals work nicely in C
# but convert C-incompatible Py3 oct/bin notations
if literal_type in 'oO':
value = '0' + value[2:] # '0o123' => '0123'
elif literal_type in 'bB':
value = int(value[2:], 2)
elif value.isdigit() and not self.unsigned and not self.longness:
# C compilers do not consider unsigned types for decimal literals, but they do for hex
value = '0x%X' % int(value)
return str(value)
def calculate_result_code(self):
......
......@@ -761,8 +761,7 @@ def wrap_compile_time_constant(pos, value):
elif isinstance(value, bool):
return ExprNodes.BoolNode(pos, value=value)
elif isinstance(value, int):
return ExprNodes.IntNode(
pos, value=rep, constant_result=value, longness=Utils.longness_of(value))
return ExprNodes.IntNode(pos, value=rep, constant_result=value)
elif isinstance(value, float):
return ExprNodes.FloatNode(pos, value=rep, constant_result=value)
elif isinstance(value, _unicode):
......@@ -779,8 +778,7 @@ def wrap_compile_time_constant(pos, value):
# error already reported
return None
elif not _IS_PY3 and isinstance(value, long):
return ExprNodes.IntNode(
pos, value=rep.rstrip('L'), longness=Utils.longness_of(value), constant_result=value)
return ExprNodes.IntNode(pos, value=rep.rstrip('L'), constant_result=value)
error(pos, "Invalid type for compile-time constant: %r (type %s)"
% (value, value.__class__.__name__))
return None
......
......@@ -319,16 +319,6 @@ def long_literal(value):
return not -2**31 <= value < 2**31
def longness_of(value):
if isinstance(value, basestring):
value = str_to_number(value)
if -2**15 <= value < 2**15:
return ''
if -2**31 <= value < 2**31:
return 'L'
return 'LL'
@cached_function
def get_cython_cache_dir():
"""get the cython cache dir
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment