[Python] fix and improve default argument handling

1. Fix negative octals. Currently not handled correctly by `-py3`
   (unusual case, but incorrect).
2. Fix arguments of type "octal + something" (e.g. `0640 | 04`).
   Currently drops everything after the first octal. Nasty!
3. Fix bool arguments "0 + something" (e.g. `0 | 1`) are always
   "False" (unusual case, but incorrect).
4. Remove special handling of "TRUE" and "FALSE" from
   `convertValue` since there's no reason these have to match
   "true" and "false".
5. Remove the Python 2 vs. Python 3 distinction based on the
   `-py3` flag. Now the same python code is produced for default
   arguments for Python 2 and Python 3. For this, octal default
   arguments, e.g. 0644, are now wrapped as `int('644', 8)`. This
   is required, as Python 2 and Python 3 have incompatible syntax
   for octal literals.

Fixes #707
This commit is contained in:
Michael Thon 2016-06-07 02:12:12 +02:00 committed by Olly Betts
commit 80ffb169c1
4 changed files with 129 additions and 105 deletions

View file

@ -2030,6 +2030,66 @@ public:
return doc;
}
/* ------------------------------------------------------------
* convertIntegerValue()
*
* Check if string v is an integer and can be represented in
* Python. If so, return an appropriate Python representation,
* otherwise (or if we are unsure), return NIL.
* ------------------------------------------------------------ */
String *convertIntegerValue(String *v, SwigType *resolved_type) {
const char *const s = Char(v);
char *end;
String *result = NIL;
// Check if this is an integer number in any base.
long value = strtol(s, &end, 0);
if (errno == ERANGE || end == s)
return NIL;
if (*end != '\0') {
// If there is a suffix after the number, we can safely ignore any
// combination of "l" and "u", but not anything else.
for (char *p = end; *p != '\0'; ++p) {
switch (*p) {
case 'l':
case 'L':
case 'u':
case 'U':
break;
default:
return NIL;
}
}
}
// So now we are certain that we are indeed dealing with an integer
// that has a representation as long given by value.
if (Cmp(resolved_type, "bool") == 0)
// Allow integers as the default value for a bool parameter.
return NewString(value ? "True" : "False");
if (value == 0)
return NewString(SwigType_ispointer(resolved_type) ? "None" : "0");
// v may still be octal or hexadecimal:
const char *p = s;
if (*p == '+' || *p == '-')
++p;
if (*p == '0' && *(p+1) != 'x' && *(p+1) != 'X') {
// This must have been an octal number. This is the only case we
// cannot use in Python directly, since Python 2 and 3 use non-
// compatible representations.
result = NewString(*s == '-' ? "int('-" : "int('");
String *octal_string = NewStringWithSize(p, (int) (end - p));
Append(result, octal_string);
Append(result, "', 8)");
Delete(octal_string);
return result;
}
result = *end == '\0' ? v : NewStringWithSize(s, (int) (end - s));
return result;
}
/* ------------------------------------------------------------
* convertDoubleValue()
*
@ -2078,111 +2138,24 @@ public:
* convertValue()
*
* Check if string v can be a Python value literal or a
* constant. Return NIL if it isn't.
* constant. Return an equivalent Python representation,
* or NIL if it isn't, or we are unsure.
* ------------------------------------------------------------ */
String *convertValue(String *v, SwigType *type) {
const char *const s = Char(v);
char *end;
String *result = NIL;
bool fail = false;
SwigType *resolved_type = 0;
SwigType *resolved_type = SwigType_typedef_resolve_all(type);
// Check if this is a number in any base.
long value = strtol(s, &end, 0);
(void) value;
if (end != s) {
if (errno == ERANGE) {
// There was an overflow, we could try representing the value as Python
// long integer literal, but for now don't bother with it.
fail = true;
} else {
if (*end != '\0') {
// If there is a suffix after the number, we can safely ignore any
// combination of "l" and "u", but not anything else (again, stuff like
// "LL" could be handled, but we don't bother to do it currently).
bool seen_long = false;
for (char * p = end; *p != '\0'; ++p) {
switch (*p) {
case 'l':
case 'L':
// Bail out on "LL".
if (seen_long) {
fail = true;
break;
}
seen_long = true;
break;
case 'u':
case 'U':
if (value < 0)
fail = true;
break;
default:
// Except that our suffix could actually be the fractional part of
// a floating point number, so we still have to check for this.
result = convertDoubleValue(v);
}
}
}
if (!fail) {
// Allow integers as the default value for a bool parameter.
resolved_type = SwigType_typedef_resolve_all(type);
if (Cmp(resolved_type, "bool") == 0) {
result = NewString(value ? "True" : "False");
} else {
// Deal with the values starting with 0 first as they can be octal or
// hexadecimal numbers or even pointers.
if (s[0] == '0') {
if (Len(v) == 1) {
// This is just a lone 0, but it needs to be represented differently
// in Python depending on whether it's a zero or a null pointer.
if (SwigType_ispointer(resolved_type))
result = NewString("None");
else
result = v;
} else if (s[1] == 'x' || s[1] == 'X') {
// This must have been a hex number, we can use it directly in Python,
// so nothing to do here.
} else {
// This must have been an octal number, we have to change its prefix
// to be "0o" in Python 3 only (and as long as we still support Python
// 2.5, this can't be done unconditionally).
if (py3) {
if (end - s > 1) {
result = NewString("0o");
Append(result, NewStringWithSize(s + 1, (int)(end - s - 1)));
}
}
}
}
// Avoid unnecessary string allocation in the common case when we don't
// need to remove any suffix.
if (!result)
result = *end == '\0' ? v : NewStringWithSize(s, (int)(end - s));
}
}
}
}
// Check if this is a floating point number (notice that it wasn't
// necessarily parsed as a long above, consider e.g. ".123").
if (!fail && !result) {
result = convertIntegerValue(v, resolved_type);
if (!result) {
result = convertDoubleValue(v);
if (!result) {
if (Strcmp(v, "true") == 0 || Strcmp(v, "TRUE") == 0)
if (Strcmp(v, "true") == 0)
result = NewString("True");
else if (Strcmp(v, "false") == 0 || Strcmp(v, "FALSE") == 0)
else if (Strcmp(v, "false") == 0)
result = NewString("False");
else if (Strcmp(v, "NULL") == 0 || Strcmp(v, "nullptr") == 0) {
if (!resolved_type)
resolved_type = SwigType_typedef_resolve_all(type);
else if (Strcmp(v, "NULL") == 0 || Strcmp(v, "nullptr") == 0)
result = SwigType_ispointer(resolved_type) ? NewString("None") : NewString("0");
}
// This could also be an enum type, default value of which could be
// representable in Python if it doesn't include any scope (which could,
// but currently is not, translated).