Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
number of tests, all because of the codecs/_multibytecodecs issue described
here (it's not a Py3K issue, just something Py3K discovers):
http://mail.python.org/pipermail/python-dev/2006-April/064051.html

Hye-Shik Chang promised to look for a fix, so no need to fix it here. The
tests that are expected to break are:

test_codecencodings_cn
test_codecencodings_hk
test_codecencodings_jp
test_codecencodings_kr
test_codecencodings_tw
test_codecs
test_multibytecodec

This merge fixes an actual test failure (test_weakref) in this branch,
though, so I believe merging is the right thing to do anyway.
diff --git a/Python/Python-ast.c b/Python/Python-ast.c
index 3f8345e..249f70e 100644
--- a/Python/Python-ast.c
+++ b/Python/Python-ast.c
@@ -331,6 +331,8 @@
         "type",
         "name",
         "body",
+        "lineno",
+        "col_offset",
 };
 static PyTypeObject *arguments_type;
 static PyObject* ast2obj_arguments(void*);
@@ -381,10 +383,10 @@
 
 static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
 {
-    int i;
+    int i, result;
     PyObject *s, *l = PyList_New(num_fields);
     if (!l) return 0;
-    for(i=0; i < num_fields; i++) {
+    for(i = 0; i < num_fields; i++) {
         s = PyString_FromString(attrs[i]);
         if (!s) {
             Py_DECREF(l);
@@ -392,7 +394,9 @@
         }
         PyList_SET_ITEM(l, i, s);
     }
-    return PyObject_SetAttrString((PyObject*)type, "_attributes", l) >=0;
+    result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0;
+    Py_DECREF(l);
+    return result;
 }
 
 static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
@@ -432,9 +436,9 @@
     return PyInt_FromLong(b);
 }
 
-static int initialized;
 static int init_types(void)
 {
+        static int initialized;
         if (initialized) return 1;
         AST_type = make_type("AST", &PyBaseObject_Type, NULL, 0);
         mod_type = make_type("mod", AST_type, NULL, 0);
@@ -710,7 +714,7 @@
                                        comprehension_fields, 3);
         if (!comprehension_type) return 0;
         excepthandler_type = make_type("excepthandler", AST_type,
-                                       excepthandler_fields, 3);
+                                       excepthandler_fields, 5);
         if (!excepthandler_type) return 0;
         arguments_type = make_type("arguments", AST_type, arguments_fields, 4);
         if (!arguments_type) return 0;
@@ -1499,8 +1503,8 @@
 }
 
 expr_ty
-Compare(expr_ty left, asdl_seq * ops, asdl_seq * comparators, int lineno, int
-        col_offset, PyArena *arena)
+Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
+        int col_offset, PyArena *arena)
 {
         expr_ty p;
         if (!left) {
@@ -1841,7 +1845,8 @@
 }
 
 excepthandler_ty
-excepthandler(expr_ty type, expr_ty name, asdl_seq * body, PyArena *arena)
+excepthandler(expr_ty type, expr_ty name, asdl_seq * body, int lineno, int
+              col_offset, PyArena *arena)
 {
         excepthandler_ty p;
         p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
@@ -1852,6 +1857,8 @@
         p->type = type;
         p->name = name;
         p->body = body;
+        p->lineno = lineno;
+        p->col_offset = col_offset;
         return p;
 }
 
@@ -2915,6 +2922,16 @@
         if (PyObject_SetAttrString(result, "body", value) == -1)
                 goto failed;
         Py_DECREF(value);
+        value = ast2obj_int(o->lineno);
+        if (!value) goto failed;
+        if (PyObject_SetAttrString(result, "lineno", value) == -1)
+                goto failed;
+        Py_DECREF(value);
+        value = ast2obj_int(o->col_offset);
+        if (!value) goto failed;
+        if (PyObject_SetAttrString(result, "col_offset", value) == -1)
+                goto failed;
+        Py_DECREF(value);
         return result;
 failed:
         Py_XDECREF(value);
@@ -3033,146 +3050,146 @@
                 return;
         if (PyModule_AddStringConstant(m, "__version__", "42753") < 0)
                 return;
-        if(PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return;
-        if(PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type)
-           < 0) return;
-        if(PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) <
-           0) return;
-        if(PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return;
-        if(PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return;
-        if(PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type)
-           < 0) return;
-        if(PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "Print", (PyObject*)Print_type) < 0) return;
-        if(PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return;
-        if(PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return;
-        if(PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return;
-        if(PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return;
-        if(PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return;
-        if(PyDict_SetItemString(d, "TryExcept", (PyObject*)TryExcept_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "TryFinally", (PyObject*)TryFinally_type) <
-           0) return;
-        if(PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) <
-           0) return;
-        if(PyDict_SetItemString(d, "Exec", (PyObject*)Exec_type) < 0) return;
-        if(PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return;
-        if(PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return;
-        if(PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return;
-        if(PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return;
-        if(PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return;
-        if(PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return;
-        if(PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return;
-        if(PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "GeneratorExp",
-           (PyObject*)GeneratorExp_type) < 0) return;
-        if(PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return;
-        if(PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return;
-        if(PyDict_SetItemString(d, "Repr", (PyObject*)Repr_type) < 0) return;
-        if(PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return;
-        if(PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return;
-        if(PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return;
-        if(PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return;
-        if(PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return;
-        if(PyDict_SetItemString(d, "expr_context",
-           (PyObject*)expr_context_type) < 0) return;
-        if(PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return;
-        if(PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return;
-        if(PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return;
-        if(PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return;
-        if(PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return;
-        if(PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return;
-        if(PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return;
-        if(PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return;
-        if(PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return;
-        if(PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return;
-        if(PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return;
-        if(PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return;
-        if(PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return;
-        if(PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return;
-        if(PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return;
-        if(PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return;
-        if(PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return;
-        if(PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return;
-        if(PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return;
-        if(PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return;
-        if(PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return;
-        if(PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return;
-        if(PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return;
-        if(PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return;
-        if(PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return;
-        if(PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return;
-        if(PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return;
-        if(PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return;
-        if(PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return;
-        if(PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return;
-        if(PyDict_SetItemString(d, "comprehension",
-           (PyObject*)comprehension_type) < 0) return;
-        if(PyDict_SetItemString(d, "excepthandler",
-           (PyObject*)excepthandler_type) < 0) return;
-        if(PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0)
-           return;
-        if(PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return;
+        if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return;
+        if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0)
+            return;
+        if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type)
+            < 0) return;
+        if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) <
+            0) return;
+        if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return;
+        if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return;
+        if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type)
+            < 0) return;
+        if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
+            return;
+        if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0)
+            return;
+        if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0)
+            return;
+        if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0)
+            return;
+        if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) <
+            0) return;
+        if (PyDict_SetItemString(d, "Print", (PyObject*)Print_type) < 0) return;
+        if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return;
+        if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return;
+        if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return;
+        if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return;
+        if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return;
+        if (PyDict_SetItemString(d, "TryExcept", (PyObject*)TryExcept_type) <
+            0) return;
+        if (PyDict_SetItemString(d, "TryFinally", (PyObject*)TryFinally_type) <
+            0) return;
+        if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0)
+            return;
+        if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
+            return;
+        if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) <
+            0) return;
+        if (PyDict_SetItemString(d, "Exec", (PyObject*)Exec_type) < 0) return;
+        if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0)
+            return;
+        if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return;
+        if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return;
+        if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return;
+        if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
+            return;
+        if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return;
+        if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0)
+            return;
+        if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return;
+        if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0)
+            return;
+        if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0)
+            return;
+        if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return;
+        if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return;
+        if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
+            return;
+        if (PyDict_SetItemString(d, "GeneratorExp",
+            (PyObject*)GeneratorExp_type) < 0) return;
+        if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return;
+        if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0)
+            return;
+        if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return;
+        if (PyDict_SetItemString(d, "Repr", (PyObject*)Repr_type) < 0) return;
+        if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return;
+        if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return;
+        if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) <
+            0) return;
+        if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) <
+            0) return;
+        if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return;
+        if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return;
+        if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return;
+        if (PyDict_SetItemString(d, "expr_context",
+            (PyObject*)expr_context_type) < 0) return;
+        if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return;
+        if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return;
+        if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return;
+        if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0)
+            return;
+        if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
+            return;
+        if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return;
+        if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return;
+        if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
+            return;
+        if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return;
+        if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
+            return;
+        if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return;
+        if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0)
+            return;
+        if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return;
+        if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return;
+        if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
+            return;
+        if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return;
+        if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return;
+        if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return;
+        if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return;
+        if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return;
+        if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return;
+        if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0)
+            return;
+        if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0)
+            return;
+        if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return;
+        if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0)
+            return;
+        if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0)
+            return;
+        if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
+            return;
+        if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0)
+            return;
+        if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0)
+            return;
+        if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return;
+        if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return;
+        if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return;
+        if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return;
+        if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return;
+        if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return;
+        if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return;
+        if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return;
+        if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return;
+        if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return;
+        if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return;
+        if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return;
+        if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return;
+        if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return;
+        if (PyDict_SetItemString(d, "comprehension",
+            (PyObject*)comprehension_type) < 0) return;
+        if (PyDict_SetItemString(d, "excepthandler",
+            (PyObject*)excepthandler_type) < 0) return;
+        if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) <
+            0) return;
+        if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0)
+            return;
+        if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return;
 }
 
 
diff --git a/Python/asdl.c b/Python/asdl.c
index 225df6e..72329b9 100644
--- a/Python/asdl.c
+++ b/Python/asdl.c
@@ -8,7 +8,24 @@
 	size_t n = sizeof(asdl_seq) +
 			(size ? (sizeof(void *) * (size - 1)) : 0);
 
-    seq = (asdl_seq *)PyArena_Malloc(arena, n);
+	seq = (asdl_seq *)PyArena_Malloc(arena, n);
+	if (!seq) {
+		PyErr_NoMemory();
+		return NULL;
+	}
+	memset(seq, 0, n);
+	seq->size = size;
+	return seq;
+}
+
+asdl_int_seq *
+asdl_int_seq_new(int size, PyArena *arena)
+{
+	asdl_int_seq *seq = NULL;
+	size_t n = sizeof(asdl_seq) +
+			(size ? (sizeof(int) * (size - 1)) : 0);
+
+	seq = (asdl_int_seq *)PyArena_Malloc(arena, n);
 	if (!seq) {
 		PyErr_NoMemory();
 		return NULL;
diff --git a/Python/ast.c b/Python/ast.c
index bb1774b..0b3b485 100644
--- a/Python/ast.c
+++ b/Python/ast.c
@@ -31,7 +31,7 @@
 static expr_ty ast_for_expr(struct compiling *, const node *);
 static stmt_ty ast_for_stmt(struct compiling *, const node *);
 static asdl_seq *ast_for_suite(struct compiling *, const node *);
-static asdl_seq *ast_for_exprlist(struct compiling *, const node *, int);
+static asdl_seq *ast_for_exprlist(struct compiling *, const node *, expr_context_ty);
 static expr_ty ast_for_testlist(struct compiling *, const node *);
 static expr_ty ast_for_testlist_gexp(struct compiling *, const node *);
 
@@ -191,6 +191,10 @@
 
     if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
         c.c_encoding = "utf-8";
+        if (TYPE(n) == encoding_decl) {
+                ast_error(n, "encoding declaration in Unicode string");
+                goto error;
+        }
     } else if (TYPE(n) == encoding_decl) {
         c.c_encoding = STR(n);
         n = CHILD(n, 0);
@@ -243,7 +247,8 @@
                 stmts = asdl_seq_new(1, arena);
                 if (!stmts)
 		    goto error;
-                asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset, arena));
+                asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
+                                            arena));
                 return Interactive(stmts, arena);
             }
             else {
@@ -311,7 +316,7 @@
         case PERCENT:
             return Mod;
         default:
-            return 0;
+            return (operator_ty)0;
     }
 }
 
@@ -419,7 +424,7 @@
 	int i;
 
 	for (i = 0; i < asdl_seq_LEN(s); i++) {
-	    if (!set_context(asdl_seq_GET(s, i), ctx, n))
+	    if (!set_context((expr_ty)asdl_seq_GET(s, i), ctx, n))
 		return 0;
 	}
     }
@@ -460,7 +465,7 @@
                 return Mult;
         default:
             PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
-            return 0;
+            return (operator_ty)0;
     }
 }
 
@@ -494,7 +499,7 @@
             default:
                 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
                              STR(n));
-                return 0;
+                return (cmpop_ty)0;
 	}
     }
     else if (NCH(n) == 2) {
@@ -508,12 +513,12 @@
             default:
                 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
                              STR(CHILD(n, 0)), STR(CHILD(n, 1)));
-                return 0;
+                return (cmpop_ty)0;
 	}
     }
     PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
                  NCH(n));
-    return 0;
+    return (cmpop_ty)0;
 }
 
 static asdl_seq *
@@ -564,8 +569,8 @@
 	    		ast_error(child, "assignment to None");
 		    	return NULL;
 		    }   
-            arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child), child->n_col_offset,
-                       c->c_arena);
+            arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
+                       child->n_col_offset, c->c_arena);
 	    }
         else {
             arg = compiler_complex_args(c, CHILD(CHILD(n, 2*i), 1));
@@ -641,17 +646,25 @@
 		    goto error;
 		}
                 if (NCH(ch) == 3) {
-                    asdl_seq_SET(args, k++, 
-                                    compiler_complex_args(c, CHILD(ch, 1))); 
-		}
-                else if (TYPE(CHILD(ch, 0)) == NAME) {
+		    ch = CHILD(ch, 1);
+		    /* def foo((x)): is not complex, special case. */
+		    if (NCH(ch) != 1) {
+			/* We have complex arguments, setup for unpacking. */
+			asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
+		    } else {
+			/* def foo((x)): setup for checking NAME below. */
+			ch = CHILD(ch, 0);
+		    }
+                }
+                if (TYPE(CHILD(ch, 0)) == NAME) {
 		    expr_ty name;
 		    if (!strcmp(STR(CHILD(ch, 0)), "None")) {
 			    ast_error(CHILD(ch, 0), "assignment to None");
 			    goto error;
 		    }
                     name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
-                                Param, LINENO(ch), ch->n_col_offset, c->c_arena);
+                                Param, LINENO(ch), ch->n_col_offset,
+                                c->c_arena);
                     if (!name)
                         goto error;
                     asdl_seq_SET(args, k++, name);
@@ -743,7 +756,8 @@
 	name_expr = NULL;
     }
     else if (NCH(n) == 5) { /* Call with no arguments */
-	d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
+	d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n),
+                 n->n_col_offset, c->c_arena);
 	if (!d)
 	    return NULL;
 	name_expr = NULL;
@@ -815,7 +829,8 @@
     if (!body)
 	return NULL;
 
-    return FunctionDef(name, args, body, decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
+    return FunctionDef(name, args, body, decorator_seq, LINENO(n),
+                       n->n_col_offset, c->c_arena);
 }
 
 static expr_ty
@@ -861,7 +876,8 @@
     orelse = ast_for_expr(c, CHILD(n, 4));
     if (!orelse)
 	return NULL;
-    return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, c->c_arena);
+    return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
+                 c->c_arena);
 }
 
 /* Count the number of 'for' loop in a list comprehension.
@@ -969,10 +985,11 @@
             return NULL;
 
 	if (asdl_seq_LEN(t) == 1)
-	    lc = comprehension(asdl_seq_GET(t, 0), expression, NULL,
+	    lc = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, NULL,
                                c->c_arena);
 	else
-	    lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset, c->c_arena),
+	    lc = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
+                                     c->c_arena),
                                expression, NULL, c->c_arena);
         if (!lc)
             return NULL;
@@ -1114,10 +1131,11 @@
             return NULL;
 
         if (asdl_seq_LEN(t) == 1)
-            ge = comprehension(asdl_seq_GET(t, 0), expression,
+            ge = comprehension((expr_ty)asdl_seq_GET(t, 0), expression,
                                NULL, c->c_arena);
         else
-            ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset, c->c_arena),
+            ge = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset,
+                                     c->c_arena),
                                expression, NULL, c->c_arena);
 
         if (!ge)
@@ -1317,16 +1335,20 @@
 
     ch = CHILD(n, NCH(n) - 1);
     if (TYPE(ch) == sliceop) {
-	if (NCH(ch) == 1)
-            /* XXX: If only 1 child, then should just be a colon.  Should we
-               just skip assigning and just get to the return? */
-	    ch = CHILD(ch, 0);
-	else
-	    ch = CHILD(ch, 1);
-	if (TYPE(ch) == test) {
-	    step = ast_for_expr(c, ch);
+        if (NCH(ch) == 1) {
+            /* No expression, so step is None */
+            ch = CHILD(ch, 0);
+            step = Name(new_identifier("None", c->c_arena), Load,
+                        LINENO(ch), ch->n_col_offset, c->c_arena);
             if (!step)
                 return NULL;
+        } else {
+            ch = CHILD(ch, 1);
+            if (TYPE(ch) == test) {
+                step = ast_for_expr(c, ch);
+                if (!step)
+                    return NULL;
+            }
         }
     }
 
@@ -1343,7 +1365,7 @@
 
 	int i, nops;
 	expr_ty expr1, expr2, result;
-        operator_ty operator;
+        operator_ty newoperator;
 
         expr1 = ast_for_expr(c, CHILD(n, 0));
         if (!expr1)
@@ -1353,11 +1375,12 @@
         if (!expr2)
             return NULL;
 
-        operator = get_operator(CHILD(n, 1));
-        if (!operator)
+        newoperator = get_operator(CHILD(n, 1));
+        if (!newoperator)
             return NULL;
 
-	result = BinOp(expr1, operator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
+	result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
+                       c->c_arena);
 	if (!result)
             return NULL;
 
@@ -1366,16 +1389,17 @@
 		expr_ty tmp_result, tmp;
 		const node* next_oper = CHILD(n, i * 2 + 1);
 
-		operator = get_operator(next_oper);
-                if (!operator)
+		newoperator = get_operator(next_oper);
+                if (!newoperator)
                     return NULL;
 
                 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
                 if (!tmp)
                     return NULL;
 
-                tmp_result = BinOp(result, operator, tmp, 
-				   LINENO(next_oper), next_oper->n_col_offset, c->c_arena);
+                tmp_result = BinOp(result, newoperator, tmp, 
+				   LINENO(next_oper), next_oper->n_col_offset,
+                                   c->c_arena);
 		if (!tmp) 
 			return NULL;
 		result = tmp_result;
@@ -1393,7 +1417,8 @@
     REQ(n, trailer);
     if (TYPE(CHILD(n, 0)) == LPAR) {
         if (NCH(n) == 2)
-            return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
+            return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),
+                        n->n_col_offset, c->c_arena);
         else
             return ast_for_call(c, CHILD(n, 1), left_expr);
     }
@@ -1409,7 +1434,8 @@
             slice_ty slc = ast_for_slice(c, CHILD(n, 0));
             if (!slc)
                 return NULL;
-            return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset, c->c_arena);
+            return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
+                             c->c_arena);
         }
         else {
             /* The grammar is ambiguous here. The ambiguity is resolved 
@@ -1550,7 +1576,8 @@
                 asdl_seq_SET(seq, i / 2, e);
             }
             if (!strcmp(STR(CHILD(n, 1)), "and"))
-                return BoolOp(And, seq, LINENO(n), n->n_col_offset, c->c_arena);
+                return BoolOp(And, seq, LINENO(n), n->n_col_offset,
+                              c->c_arena);
             assert(!strcmp(STR(CHILD(n, 1)), "or"));
             return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
         case not_test:
@@ -1563,7 +1590,8 @@
                 if (!expression)
                     return NULL;
 
-                return UnaryOp(Not, expression, LINENO(n), n->n_col_offset, c->c_arena);
+                return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
+                               c->c_arena);
             }
         case comparison:
             if (NCH(n) == 1) {
@@ -1572,8 +1600,9 @@
             }
             else {
                 expr_ty expression;
-                asdl_seq *ops, *cmps;
-                ops = asdl_seq_new(NCH(n) / 2, c->c_arena);
+                asdl_int_seq *ops;
+		asdl_seq *cmps;
+                ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena);
                 if (!ops)
                     return NULL;
                 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena);
@@ -1581,11 +1610,10 @@
                     return NULL;
                 }
                 for (i = 1; i < NCH(n); i += 2) {
-                    /* XXX cmpop_ty is just an enum */
-                    cmpop_ty operator;
+                    cmpop_ty newoperator;
 
-                    operator = ast_for_comp_op(CHILD(n, i));
-                    if (!operator) {
+                    newoperator = ast_for_comp_op(CHILD(n, i));
+                    if (!newoperator) {
                         return NULL;
 		    }
 
@@ -1594,7 +1622,7 @@
                         return NULL;
 		    }
                         
-                    asdl_seq_SET(ops, i / 2, (void *)(Py_uintptr_t)operator);
+                    asdl_seq_SET(ops, i / 2, newoperator);
                     asdl_seq_SET(cmps, i / 2, expression);
                 }
                 expression = ast_for_expr(c, CHILD(n, 0));
@@ -1602,7 +1630,8 @@
                     return NULL;
 		}
                     
-                return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset, c->c_arena);
+                return Compare(expression, ops, cmps, LINENO(n),
+                               n->n_col_offset, c->c_arena);
             }
             break;
 
@@ -1853,7 +1882,7 @@
     }
     else if (TYPE(CHILD(n, 1)) == augassign) {
         expr_ty expr1, expr2;
-        operator_ty operator;
+        operator_ty newoperator;
 	node *ch = CHILD(n, 0);
 
 	if (TYPE(ch) == testlist)
@@ -1895,11 +1924,11 @@
         if (!expr2)
             return NULL;
 
-        operator = ast_for_augassign(CHILD(n, 1));
-        if (!operator)
+        newoperator = ast_for_augassign(CHILD(n, 1));
+        if (!newoperator)
             return NULL;
 
-	return AugAssign(expr1, operator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
+	return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
     }
     else {
 	int i;
@@ -1973,7 +2002,7 @@
 }
 
 static asdl_seq *
-ast_for_exprlist(struct compiling *c, const node *n, int context)
+ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
 {
     asdl_seq *seq;
     int i;
@@ -2512,8 +2541,8 @@
 	    int off = 5 + (n_elif - i - 1) * 4;
             expr_ty expression;
             asdl_seq *suite_seq;
-	    asdl_seq *new = asdl_seq_new(1, c->c_arena);
-	    if (!new)
+	    asdl_seq *newobj = asdl_seq_new(1, c->c_arena);
+	    if (!newobj)
 		return NULL;
             expression = ast_for_expr(c, CHILD(n, off));
             if (!expression)
@@ -2522,10 +2551,10 @@
             if (!suite_seq)
                 return NULL;
 
-	    asdl_seq_SET(new, 0,
+	    asdl_seq_SET(newobj, 0,
 			 If(expression, suite_seq, orelse, 
 			    LINENO(CHILD(n, off)), CHILD(n, off)->n_col_offset, c->c_arena));
-	    orelse = new;
+	    orelse = newobj;
 	}
 	return If(ast_for_expr(c, CHILD(n, 1)),
 		  ast_for_suite(c, CHILD(n, 3)),
@@ -2597,7 +2626,7 @@
     if (!_target)
         return NULL;
     if (asdl_seq_LEN(_target) == 1)
-	target = asdl_seq_GET(_target, 0);
+	target = (expr_ty)asdl_seq_GET(_target, 0);
     else
 	target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena);
 
@@ -2608,7 +2637,8 @@
     if (!suite_seq)
         return NULL;
 
-    return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset, c->c_arena);
+    return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset,
+               c->c_arena);
 }
 
 static excepthandler_ty
@@ -2623,7 +2653,8 @@
         if (!suite_seq)
             return NULL;
 
-	return excepthandler(NULL, NULL, suite_seq, c->c_arena);
+	return excepthandler(NULL, NULL, suite_seq, LINENO(exc),
+                             exc->n_col_offset, c->c_arena);
     }
     else if (NCH(exc) == 2) {
         expr_ty expression;
@@ -2636,7 +2667,8 @@
         if (!suite_seq)
             return NULL;
 
-	return excepthandler(expression, NULL, suite_seq, c->c_arena);
+	return excepthandler(expression, NULL, suite_seq, LINENO(exc),
+                             exc->n_col_offset, c->c_arena);
     }
     else if (NCH(exc) == 4) {
         asdl_seq *suite_seq;
@@ -2653,7 +2685,8 @@
         if (!suite_seq)
             return NULL;
 
-	return excepthandler(expression, e, suite_seq, c->c_arena);
+	return excepthandler(expression, e, suite_seq, LINENO(exc),
+                             exc->n_col_offset, c->c_arena);
     }
 
     PyErr_Format(PyExc_SystemError,
@@ -2722,7 +2755,8 @@
             asdl_seq_SET(handlers, i, e);
         }
 
-	except_st = TryExcept(body, handlers, orelse, LINENO(n), n->n_col_offset, c->c_arena);
+	except_st = TryExcept(body, handlers, orelse, LINENO(n),
+                              n->n_col_offset, c->c_arena);
         if (!finally)
 	    return except_st;
 
@@ -2797,16 +2831,16 @@
         s = ast_for_suite(c, CHILD(n, 3));
         if (!s)
             return NULL;
-	return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n), n->n_col_offset,
-                        c->c_arena);
+	return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
+                        n->n_col_offset, c->c_arena);
     }
     /* check for empty base list */
     if (TYPE(CHILD(n,3)) == RPAR) {
 	s = ast_for_suite(c, CHILD(n,5));
 	if (!s)
 		return NULL;
-	return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n), n->n_col_offset,
-                        c->c_arena);
+	return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, s, LINENO(n),
+                        n->n_col_offset, c->c_arena);
     }
 
     /* else handle the base class list */
@@ -2817,8 +2851,8 @@
     s = ast_for_suite(c, CHILD(n, 6));
     if (!s)
         return NULL;
-    return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n), n->n_col_offset,
-                    c->c_arena);
+    return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), bases, s, LINENO(n),
+                    n->n_col_offset, c->c_arena);
 }
 
 static stmt_ty
@@ -3090,7 +3124,8 @@
 #ifndef Py_USING_UNICODE
 			/* This should not happen - we never see any other
 			   encoding. */
-			Py_FatalError("cannot deal with encodings in this build.");
+			Py_FatalError(
+                            "cannot deal with encodings in this build.");
 #else
 			PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL);
 			if (u == NULL)
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 342c2db..914e0d1 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -31,23 +31,25 @@
 static PyObject *filtertuple (PyObject *, PyObject *);
 
 static PyObject *
-builtin___import__(PyObject *self, PyObject *args)
+builtin___import__(PyObject *self, PyObject *args, PyObject *kwds)
 {
+	static char *kwlist[] = {"name", "globals", "locals", "fromlist",
+				 "level", 0};
 	char *name;
 	PyObject *globals = NULL;
 	PyObject *locals = NULL;
 	PyObject *fromlist = NULL;
 	int level = -1;
 
-	if (!PyArg_ParseTuple(args, "s|OOOi:__import__",
-			&name, &globals, &locals, &fromlist, &level))
+	if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|OOOi:__import__",
+			kwlist, &name, &globals, &locals, &fromlist, &level))
 		return NULL;
 	return PyImport_ImportModuleLevel(name, globals, locals,
 					  fromlist, level);
 }
 
 PyDoc_STRVAR(import_doc,
-"__import__(name, globals, locals, fromlist) -> module\n\
+"__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module\n\
 \n\
 Import a module.  The globals are only used to determine the context;\n\
 they are not modified.  The locals are currently unused.  The fromlist\n\
@@ -55,7 +57,10 @@
 empty list to emulate ``import name''.\n\
 When importing a module from a package, note that __import__('A.B', ...)\n\
 returns package A when fromlist is empty, but its submodule B when\n\
-fromlist is not empty.");
+fromlist is not empty.  Level is used to determine whether to perform \n\
+absolute or relative imports.  -1 is the original strategy of attempting\n\
+both absolute and relative imports, 0 is absolute, a positive number\n\
+is the number of parent directories to search relative to the current module.");
 
 
 static PyObject *
@@ -1704,32 +1709,34 @@
 
 
 static PyObject *
-builtin_round(PyObject *self, PyObject *args)
+builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
 {
-	double x;
+	double number;
 	double f;
 	int ndigits = 0;
 	int i;
+	static char *kwlist[] = {"number", "ndigits", 0};
 
-	if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
-			return NULL;
+	if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|i:round",
+                kwlist, &number, &ndigits))
+                return NULL;
 	f = 1.0;
 	i = abs(ndigits);
 	while  (--i >= 0)
 		f = f*10.0;
 	if (ndigits < 0)
-		x /= f;
+		number /= f;
 	else
-		x *= f;
-	if (x >= 0.0)
-		x = floor(x + 0.5);
+		number *= f;
+	if (number >= 0.0)
+		number = floor(number + 0.5);
 	else
-		x = ceil(x - 0.5);
+		number = ceil(number - 0.5);
 	if (ndigits < 0)
-		x *= f;
+		number *= f;
 	else
-		x /= f;
-	return PyFloat_FromDouble(x);
+		number /= f;
+	return PyFloat_FromDouble(number);
 }
 
 PyDoc_STRVAR(round_doc,
@@ -2042,7 +2049,7 @@
 
 
 static PyMethodDef builtin_methods[] = {
- 	{"__import__",	builtin___import__, METH_VARARGS, import_doc},
+ 	{"__import__",	(PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
  	{"abs",		builtin_abs,        METH_O, abs_doc},
  	{"all",		builtin_all,        METH_O, all_doc},
  	{"any",		builtin_any,        METH_O, any_doc},
@@ -2079,7 +2086,7 @@
  	{"reduce",	builtin_reduce,     METH_VARARGS, reduce_doc},
  	{"reload",	builtin_reload,     METH_O, reload_doc},
  	{"repr",	builtin_repr,       METH_O, repr_doc},
- 	{"round",	builtin_round,      METH_VARARGS, round_doc},
+ 	{"round",	(PyCFunction)builtin_round,      METH_VARARGS | METH_KEYWORDS, round_doc},
  	{"setattr",	builtin_setattr,    METH_VARARGS, setattr_doc},
  	{"sorted",	(PyCFunction)builtin_sorted,     METH_VARARGS | METH_KEYWORDS, sorted_doc},
  	{"sum",		builtin_sum,        METH_VARARGS, sum_doc},
diff --git a/Python/ceval.c b/Python/ceval.c
index 6f76781..6c8afba 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -507,7 +507,7 @@
 }
 
 PyObject *
-PyEval_EvalFrameEx(PyFrameObject *f, int throw)
+PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
 {
 #ifdef DXPAIRS
 	int lastopcode = 0;
@@ -756,7 +756,7 @@
 	x = Py_None;	/* Not a reference, just anything non-NULL */
 	w = NULL;
 
-	if (throw) { /* support for generator.throw() */
+	if (throwflag) { /* support for generator.throw() */
 		why = WHY_EXCEPTION;
 		goto on_error;
 	}
@@ -2153,6 +2153,9 @@
 		case SETUP_LOOP:
 		case SETUP_EXCEPT:
 		case SETUP_FINALLY:
+			/* NOTE: If you add any new block-setup opcodes that are not try/except/finally
+			   handlers, you may need to update the PyGen_NeedsFinalizing() function. */
+
 			PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
 					   STACK_LEVEL());
 			continue;
@@ -3180,132 +3183,29 @@
 		      PyFrameObject *frame, int *instr_lb, int *instr_ub,
 		      int *instr_prev)
 {
-	/* The theory of SET_LINENO-less tracing.
-
-	   In a nutshell, we use the co_lnotab field of the code object
-	   to tell when execution has moved onto a different line.
-
-	   As mentioned above, the basic idea is so set things up so
-	   that
-
-	         *instr_lb <= frame->f_lasti < *instr_ub
-
-	   is true so long as execution does not change lines.
-
-	   This is all fairly simple.  Digging the information out of
-	   co_lnotab takes some work, but is conceptually clear.
-
-	   Somewhat harder to explain is why we don't *always* call the
-	   line trace function when the above test fails.
-
-	   Consider this code:
-
-	   1: def f(a):
-	   2:     if a:
-	   3:        print 1
-	   4:     else:
-	   5:        print 2
-
-	   which compiles to this:
-
-	   2           0 LOAD_FAST                0 (a)
-		       3 JUMP_IF_FALSE            9 (to 15)
-		       6 POP_TOP
-
-	   3           7 LOAD_CONST               1 (1)
-		      10 PRINT_ITEM
-		      11 PRINT_NEWLINE
-		      12 JUMP_FORWARD             6 (to 21)
-		 >>   15 POP_TOP
-
-	   5          16 LOAD_CONST               2 (2)
-		      19 PRINT_ITEM
-		      20 PRINT_NEWLINE
-		 >>   21 LOAD_CONST               0 (None)
-		      24 RETURN_VALUE
-
-	   If 'a' is false, execution will jump to instruction at offset
-	   15 and the co_lnotab will claim that execution has moved to
-	   line 3.  This is at best misleading.  In this case we could
-	   associate the POP_TOP with line 4, but that doesn't make
-	   sense in all cases (I think).
-
-	   What we do is only call the line trace function if the co_lnotab
-	   indicates we have jumped to the *start* of a line, i.e. if the
-	   current instruction offset matches the offset given for the
-	   start of a line by the co_lnotab.
-
-	   This also takes care of the situation where 'a' is true.
-	   Execution will jump from instruction offset 12 to offset 21.
-	   Then the co_lnotab would imply that execution has moved to line
-	   5, which is again misleading.
-
-	   Why do we set f_lineno when tracing?  Well, consider the code
-	   above when 'a' is true.  If stepping through this with 'n' in
-	   pdb, you would stop at line 1 with a "call" type event, then
-	   line events on lines 2 and 3, then a "return" type event -- but
-	   you would be shown line 5 during this event.  This is a change
-	   from the behaviour in 2.2 and before, and I've found it
-	   confusing in practice.  By setting and using f_lineno when
-	   tracing, one can report a line number different from that
-	   suggested by f_lasti on this one occasion where it's desirable.
-	*/
-
 	int result = 0;
 
+        /* If the last instruction executed isn't in the current
+           instruction window, reset the window.  If the last
+           instruction happens to fall at the start of a line or if it
+           represents a jump backwards, call the trace function.
+        */
 	if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
-		PyCodeObject* co = frame->f_code;
-		int size, addr, line;
-		unsigned char* p;
+                int line;
+                PyAddrPair bounds;
 
-		size = PyString_GET_SIZE(co->co_lnotab) / 2;
-		p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
-
-		addr = 0;
-		line = co->co_firstlineno;
-
-		/* possible optimization: if f->f_lasti == instr_ub
-		   (likely to be a common case) then we already know
-		   instr_lb -- if we stored the matching value of p
-		   somwhere we could skip the first while loop. */
-
-		/* see comments in compile.c for the description of
-		   co_lnotab.  A point to remember: increments to p
-		   should come in pairs -- although we don't care about
-		   the line increments here, treating them as byte
-		   increments gets confusing, to say the least. */
-
-		while (size > 0) {
-			if (addr + *p > frame->f_lasti)
-				break;
-			addr += *p++;
-			if (*p) *instr_lb = addr;
-			line += *p++;
-			--size;
-		}
-
-		if (addr == frame->f_lasti) {
+                line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
+                                              &bounds);
+                if (line >= 0) {
 			frame->f_lineno = line;
 			result = call_trace(func, obj, frame,
 					    PyTrace_LINE, Py_None);
-		}
-
-		if (size > 0) {
-			while (--size >= 0) {
-				addr += *p++;
-				if (*p++)
-					break;
-			}
-			*instr_ub = addr;
-		}
-		else {
-			*instr_ub = INT_MAX;
-		}
+                }
+                *instr_lb = bounds.ap_lower;
+                *instr_ub = bounds.ap_upper;
 	}
 	else if (frame->f_lasti <= *instr_prev) {
-		/* jumping back in the same line forces a trace event */
-		result = call_trace(func, obj, frame,
-				    PyTrace_LINE, Py_None);
+		result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
 	}
 	*instr_prev = frame->f_lasti;
 	return result;
@@ -3623,9 +3523,9 @@
 		Py_DECREF(func);
 	}
 
-	/* Clear the stack of the function object and the arguments,
-	   in case they weren't consumed already.
-	   XXX(twouters) when are they not consumed already?
+	/* Clear the stack of the function object.  Also removes
+           the arguments in case they weren't consumed already
+           (fast_function() and err_args() leave them on the stack).
 	 */
 	while ((*pp_stack) > pfunc) {
 		w = EXT_POP(*pp_stack);
@@ -3899,7 +3799,7 @@
 	if (v != NULL) {
 		Py_ssize_t x;
 		if (PyInt_Check(v)) {
-			x = PyInt_AsLong(v);
+			x = PyInt_AsSsize_t(v);
 		} 
 		else if (v->ob_type->tp_as_number &&
 			 PyType_HasFeature(v->ob_type, Py_TPFLAGS_HAVE_INDEX)
@@ -4302,8 +4202,8 @@
 		/* Now we own the last reference to 'v', so we can resize it
 		 * in-place.
 		 */
-		int v_len = PyString_GET_SIZE(v);
-		int w_len = PyString_GET_SIZE(w);
+		Py_ssize_t v_len = PyString_GET_SIZE(v);
+		Py_ssize_t w_len = PyString_GET_SIZE(w);
 		if (_PyString_Resize(&v, v_len + w_len) != 0) {
 			/* XXX if _PyString_Resize() fails, 'v' has been
 			 * deallocated so it cannot be put back into 'variable'.
diff --git a/Python/codecs.c b/Python/codecs.c
index 0e8c374..2124824 100644
--- a/Python/codecs.c
+++ b/Python/codecs.c
@@ -56,12 +56,12 @@
     char *p;
     PyObject *v;
     
-	if (len > INT_MAX) {
-		PyErr_SetString(PyExc_OverflowError, "string is too large");
-		return NULL;
-	}
+    if (len > PY_SSIZE_T_MAX) {
+	PyErr_SetString(PyExc_OverflowError, "string is too large");
+	return NULL;
+    }
 	
-    v = PyString_FromStringAndSize(NULL, (int)len);
+    v = PyString_FromStringAndSize(NULL, len);
     if (v == NULL)
 	return NULL;
     p = PyString_AS_STRING(v);
@@ -200,24 +200,65 @@
     return args;
 }
 
-/* Build a codec by calling factory(stream[,errors]) or just
-   factory(errors) depending on whether the given parameters are
-   non-NULL. */
+/* Helper function to get a codec item */
 
 static
-PyObject *build_stream_codec(PyObject *factory,
-			     PyObject *stream,
-			     const char *errors)
+PyObject *codec_getitem(const char *encoding, int index)
 {
-    PyObject *args, *codec;
+    PyObject *codecs;
+    PyObject *v;
 
-    args = args_tuple(stream, errors);
-    if (args == NULL)
+    codecs = _PyCodec_Lookup(encoding);
+    if (codecs == NULL)
 	return NULL;
-    
-    codec = PyEval_CallObject(factory, args);
-    Py_DECREF(args);
-    return codec;
+    v = PyTuple_GET_ITEM(codecs, index);
+    Py_DECREF(codecs);
+    Py_INCREF(v);
+    return v;
+}
+
+/* Helper function to create an incremental codec. */
+
+static
+PyObject *codec_getincrementalcodec(const char *encoding,
+				    const char *errors,
+				    const char *attrname)
+{
+    PyObject *codecs, *ret, *inccodec;
+
+    codecs = _PyCodec_Lookup(encoding);
+    if (codecs == NULL)
+	return NULL;
+    inccodec = PyObject_GetAttrString(codecs, attrname);
+    Py_DECREF(codecs);
+    if (inccodec == NULL)
+	return NULL;
+    if (errors)
+	ret = PyObject_CallFunction(inccodec, "s", errors);
+    else
+	ret = PyObject_CallFunction(inccodec, NULL);
+    Py_DECREF(inccodec);
+    return ret;
+}
+
+/* Helper function to create a stream codec. */
+
+static
+PyObject *codec_getstreamcodec(const char *encoding,
+			       PyObject *stream,
+			       const char *errors,
+			       const int index)
+{
+    PyObject *codecs, *streamcodec;
+
+    codecs = _PyCodec_Lookup(encoding);
+    if (codecs == NULL)
+	return NULL;
+
+    streamcodec = PyEval_CallFunction(
+	PyTuple_GET_ITEM(codecs, index), "Os", stream, errors);
+    Py_DECREF(codecs);
+    return streamcodec;
 }
 
 /* Convenience APIs to query the Codec registry. 
@@ -228,120 +269,38 @@
 
 PyObject *PyCodec_Encoder(const char *encoding)
 {
-    PyObject *codecs;
-    PyObject *v;
-
-    codecs = _PyCodec_Lookup(encoding);
-    if (codecs == NULL)
-	goto onError;
-    v = PyTuple_GET_ITEM(codecs,0);
-    Py_DECREF(codecs);
-    Py_INCREF(v);
-    return v;
-
- onError:
-    return NULL;
+    return codec_getitem(encoding, 0);
 }
 
 PyObject *PyCodec_Decoder(const char *encoding)
 {
-    PyObject *codecs;
-    PyObject *v;
-
-    codecs = _PyCodec_Lookup(encoding);
-    if (codecs == NULL)
-	goto onError;
-    v = PyTuple_GET_ITEM(codecs,1);
-    Py_DECREF(codecs);
-    Py_INCREF(v);
-    return v;
-
- onError:
-    return NULL;
+    return codec_getitem(encoding, 1);
 }
 
 PyObject *PyCodec_IncrementalEncoder(const char *encoding,
 				     const char *errors)
 {
-    PyObject *codecs, *ret, *encoder;
-
-    codecs = _PyCodec_Lookup(encoding);
-    if (codecs == NULL)
-	goto onError;
-    encoder = PyObject_GetAttrString(codecs, "incrementalencoder");
-    if (encoder == NULL) {
-	Py_DECREF(codecs);
-	return NULL;
-    }
-    if (errors)
-	ret = PyObject_CallFunction(encoder, "O", errors);
-    else
-	ret = PyObject_CallFunction(encoder, NULL);
-    Py_DECREF(encoder);
-    Py_DECREF(codecs);
-    return ret;
-
- onError:
-    return NULL;
+    return codec_getincrementalcodec(encoding, errors, "incrementalencoder");
 }
 
 PyObject *PyCodec_IncrementalDecoder(const char *encoding,
 				     const char *errors)
 {
-    PyObject *codecs, *ret, *decoder;
-
-    codecs = _PyCodec_Lookup(encoding);
-    if (codecs == NULL)
-	goto onError;
-    decoder = PyObject_GetAttrString(codecs, "incrementaldecoder");
-    if (decoder == NULL) {
-	Py_DECREF(codecs);
-	return NULL;
-    }
-    if (errors)
-	ret = PyObject_CallFunction(decoder, "O", errors);
-    else
-	ret = PyObject_CallFunction(decoder, NULL);
-    Py_DECREF(decoder);
-    Py_DECREF(codecs);
-    return ret;
-
- onError:
-    return NULL;
+    return codec_getincrementalcodec(encoding, errors, "incrementaldecoder");
 }
 
 PyObject *PyCodec_StreamReader(const char *encoding,
 			       PyObject *stream,
 			       const char *errors)
 {
-    PyObject *codecs, *ret;
-
-    codecs = _PyCodec_Lookup(encoding);
-    if (codecs == NULL)
-	goto onError;
-    ret = build_stream_codec(PyTuple_GET_ITEM(codecs,2),stream,errors);
-    Py_DECREF(codecs);
-    return ret;
-
- onError:
-    return NULL;
+    return codec_getstreamcodec(encoding, stream, errors, 2);
 }
 
 PyObject *PyCodec_StreamWriter(const char *encoding,
 			       PyObject *stream,
 			       const char *errors)
 {
-    PyObject *codecs, *ret;
-
-    codecs = _PyCodec_Lookup(encoding);
-    if (codecs == NULL)
-	goto onError;
-    ret = build_stream_codec(PyTuple_GET_ITEM(codecs,3),stream,errors);
-    Py_DECREF(codecs);
-    return ret;
-
- onError:
-    return NULL;
+    return codec_getstreamcodec(encoding, stream, errors, 3);
 }
 
 /* Encode an object (e.g. an Unicode object) using the given encoding
diff --git a/Python/compile.c b/Python/compile.c
index b92fb62..12b190a 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -58,8 +58,9 @@
 };
 
 typedef struct basicblock_ {
-	/* next block in the list of blocks for a unit (don't confuse with
-	 * b_next) */
+    /* Each basicblock in a compilation unit is linked via b_list in the
+       reverse order that the block are allocated.  b_list points to the next
+       block, not to be confused with b_next, which is next by control flow. */
 	struct basicblock_ *b_list;
 	/* number of instructions used */
 	int b_iused;
@@ -114,7 +115,9 @@
 	PyObject *u_private;	/* for private name mangling */
 
 	int u_argcount;	   /* number of arguments for block */ 
-	basicblock *u_blocks; /* pointer to list of blocks */
+    /* Pointer to the most recently allocated block.  By following b_list
+       members, you can reach all early allocated blocks. */
+	basicblock *u_blocks;
 	basicblock *u_curblock; /* pointer to current block */
 	int u_tmpname;	   /* temporary variables for list comps */
 
@@ -194,19 +197,19 @@
 static PyObject *__doc__;
 
 PyObject *
-_Py_Mangle(PyObject *private, PyObject *ident)
+_Py_Mangle(PyObject *privateobj, PyObject *ident)
 {
 	/* Name mangling: __private becomes _classname__private.
 	   This is independent from how the name is used. */
 	const char *p, *name = PyString_AsString(ident);
 	char *buffer;
 	size_t nlen, plen;
-	if (private == NULL || name == NULL || name[0] != '_' ||
+	if (privateobj == NULL || name == NULL || name[0] != '_' ||
             name[1] != '_') {
 		Py_INCREF(ident);
 		return ident;
 	}
-	p = PyString_AsString(private);
+	p = PyString_AsString(privateobj);
 	nlen = strlen(name);
 	if (name[nlen-1] == '_' && name[nlen-2] == '_') {
 		Py_INCREF(ident);
@@ -311,7 +314,7 @@
 	if (c->c_st)
 		PySymtable_Free(c->c_st);
 	if (c->c_future)
-		PyMem_Free(c->c_future);
+		PyObject_Free(c->c_future);
 	Py_DECREF(c->c_stack);
 }
 
@@ -319,7 +322,9 @@
 list2dict(PyObject *list)
 {
 	Py_ssize_t i, n;
-	PyObject *v, *k, *dict = PyDict_New();
+	PyObject *v, *k;
+	PyObject *dict = PyDict_New();
+	if (!dict) return NULL;
 
 	n = PyList_Size(list);
 	for (i = 0; i < n; i++) {
@@ -602,7 +607,7 @@
 static unsigned int *
 markblocks(unsigned char *code, int len)
 {
-	unsigned int *blocks = PyMem_Malloc(len*sizeof(int));
+	unsigned int *blocks = (unsigned int *)PyMem_Malloc(len*sizeof(int));
 	int i,j, opcode, blockcnt = 0;
 
 	if (blocks == NULL)
@@ -683,10 +688,11 @@
 		goto exitUnchanged;
 
 	/* Make a modifiable copy of the code string */
-	codestr = PyMem_Malloc(codelen);
+	codestr = (unsigned char *)PyMem_Malloc(codelen);
 	if (codestr == NULL)
 		goto exitUnchanged;
-	codestr = memcpy(codestr, PyString_AS_STRING(code), codelen);
+	codestr = (unsigned char *)memcpy(codestr, 
+                                        PyString_AS_STRING(code), codelen);
 
 	/* Verify that RETURN_VALUE terminates the codestring.	This allows
 	   the various transformation patterns to look ahead several
@@ -697,7 +703,7 @@
 		goto exitUnchanged;
 
 	/* Mapping to new jump targets after NOPs are removed */
-	addrmap = PyMem_Malloc(codelen * sizeof(int));
+	addrmap = (int *)PyMem_Malloc(codelen * sizeof(int));
 	if (addrmap == NULL)
 		goto exitUnchanged;
 
@@ -1077,7 +1083,8 @@
 {
 	struct compiler_unit *u;
 
-	u = PyObject_Malloc(sizeof(struct compiler_unit));
+	u = (struct compiler_unit *)PyObject_Malloc(sizeof(
+                                                struct compiler_unit));
 	if (!u) {
 		PyErr_NoMemory();
 		return 0;
@@ -1187,7 +1194,7 @@
 		return NULL;
 	}
 	memset((void *)b, 0, sizeof(basicblock));
-	assert (b->b_next == NULL);
+    /* Extend the singly linked list of blocks with new block. */
 	b->b_list = u->u_blocks;
 	u->u_blocks = b;
 	return b;
@@ -1233,8 +1240,8 @@
 {
 	assert(b != NULL);
 	if (b->b_instr == NULL) {
-		b->b_instr = PyObject_Malloc(sizeof(struct instr) *
-					     DEFAULT_BLOCK_SIZE);
+		b->b_instr = (struct instr *)PyObject_Malloc(
+                                 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
 		if (b->b_instr == NULL) {
 			PyErr_NoMemory();
 			return -1;
@@ -1252,7 +1259,8 @@
 			return -1;
 		}
 		b->b_ialloc <<= 1;
-		b->b_instr = PyObject_Realloc((void *)b->b_instr, newsize);
+		b->b_instr = (struct instr *)PyObject_Realloc(
+                                                (void *)b->b_instr, newsize);
 		if (b->b_instr == NULL)
 			return -1;
 		memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
@@ -1260,6 +1268,13 @@
 	return b->b_iused++;
 }
 
+/* Set the i_lineno member of the instruction at offse off if the
+   line number for the current expression/statement (?) has not
+   already been set.  If it has been set, the call has no effect.
+
+   Every time a new node is b
+   */
+
 static void
 compiler_set_lineno(struct compiler *c, int off)
 {
@@ -1600,7 +1615,6 @@
 	off = compiler_next_instr(c, c->u->u_curblock);
 	if (off < 0)
 		return 0;
-	compiler_set_lineno(c, off);
 	i = &c->u->u_curblock->b_instr[off];
 	i->i_opcode = opcode;
 	i->i_target = b;
@@ -1609,6 +1623,7 @@
 		i->i_jabs = 1;
 	else
 		i->i_jrel = 1;
+	compiler_set_lineno(c, off);
 	return 1;
 }
 
@@ -1695,7 +1710,7 @@
 	int _i; \
 	asdl_seq *seq = (SEQ); /* avoid variable capture */ \
 	for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
-		TYPE ## _ty elt = asdl_seq_GET(seq, _i); \
+		TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
 		if (!compiler_visit_ ## TYPE((C), elt)) \
 			return 0; \
 	} \
@@ -1705,7 +1720,7 @@
 	int _i; \
 	asdl_seq *seq = (SEQ); /* avoid variable capture */ \
 	for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
-		TYPE ## _ty elt = asdl_seq_GET(seq, _i); \
+		TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
 		if (!compiler_visit_ ## TYPE((C), elt)) { \
 			compiler_exit_scope(c); \
 			return 0; \
@@ -1731,7 +1746,7 @@
 
 	if (!asdl_seq_LEN(stmts))
 		return 1;
-	st = asdl_seq_GET(stmts, 0);
+	st = (stmt_ty)asdl_seq_GET(stmts, 0);
 	if (compiler_isdocstring(st)) {
 		i = 1;
 		VISIT(c, expr, st->v.Expr.value);
@@ -1739,7 +1754,7 @@
 			return 0;
 	}
 	for (; i < asdl_seq_LEN(stmts); i++)
-	    VISIT(c, stmt, asdl_seq_GET(stmts, i));
+	    VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
 	return 1;
 }
 
@@ -1765,7 +1780,8 @@
 		break;
 	case Interactive_kind:
 		c->c_interactive = 1;
-		VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
+		VISIT_SEQ_IN_SCOPE(c, stmt, 
+                                        mod->v.Interactive.body);
 		break;
 	case Expression_kind:
 		VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
@@ -1882,7 +1898,7 @@
 		return 1;
 
 	for (i = 0; i < asdl_seq_LEN(decos); i++) {
-		VISIT(c, expr, asdl_seq_GET(decos, i));
+		VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
 	}
 	return 1;
 }
@@ -1894,7 +1910,7 @@
 	int n = asdl_seq_LEN(args->args);
 	/* Correctly handle nested argument lists */
 	for (i = 0; i < n; i++) {
-		expr_ty arg = asdl_seq_GET(args->args, i);
+		expr_ty arg = (expr_ty)asdl_seq_GET(args->args, i);
 		if (arg->kind == Tuple_kind) {
 			PyObject *id = PyString_FromFormat(".%d", i);
 			if (id == NULL) {
@@ -1931,7 +1947,7 @@
 				  s->lineno))
 		return 0;
 
-	st = asdl_seq_GET(s->v.FunctionDef.body, 0);
+	st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
 	docstring = compiler_isdocstring(st);
 	if (docstring)
 	    first_const = st->v.Expr.value->v.Str.s;
@@ -1947,7 +1963,7 @@
 	n = asdl_seq_LEN(s->v.FunctionDef.body);
 	/* if there was a docstring, we need to skip the first statement */
 	for (i = docstring; i < n; i++) {
-		stmt_ty s2 = asdl_seq_GET(s->v.FunctionDef.body, i);
+		stmt_ty s2 = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
 		if (i == 0 && s2->kind == Expr_kind &&
 		    s2->v.Expr.value->kind == Str_kind)
 			continue;
@@ -2221,7 +2237,7 @@
 		ADDOP(c, POP_BLOCK);
 	}
 	compiler_pop_fblock(c, LOOP, loop);
-	if (orelse != NULL)
+	if (orelse != NULL) /* what if orelse is just pass? */
 		VISIT_SEQ(c, stmt, s->v.While.orelse);
 	compiler_use_next_block(c, end);
 
@@ -2375,10 +2391,12 @@
 	n = asdl_seq_LEN(s->v.TryExcept.handlers);
 	compiler_use_next_block(c, except);
 	for (i = 0; i < n; i++) {
-		excepthandler_ty handler = asdl_seq_GET(
+		excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
 						s->v.TryExcept.handlers, i);
 		if (!handler->type && i < n-1)
 		    return compiler_error(c, "default 'except:' must be last");
+        c->u->u_lineno_set = false;
+        c->u->u_lineno = handler->lineno;
 		except = compiler_new_block(c);
 		if (except == NULL)
 			return 0;
@@ -2453,7 +2471,7 @@
 	int i, n = asdl_seq_LEN(s->v.Import.names);
 
 	for (i = 0; i < n; i++) {
-		alias_ty alias = asdl_seq_GET(s->v.Import.names, i);
+		alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
 		int r;
 		PyObject *level;
 
@@ -2508,7 +2526,7 @@
 
 	/* build up the names */
 	for (i = 0; i < n; i++) {
-		alias_ty alias = asdl_seq_GET(s->v.ImportFrom.names, i);
+		alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
 		Py_INCREF(alias->name);
 		PyTuple_SET_ITEM(names, i, alias->name);
 	}
@@ -2531,7 +2549,7 @@
 	Py_DECREF(names);
 	ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
 	for (i = 0; i < n; i++) {
-		alias_ty alias = asdl_seq_GET(s->v.ImportFrom.names, i);
+		alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
 		identifier store_name;
 
 		if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
@@ -2592,8 +2610,10 @@
 {
 	int i, n;
 
+    /* Always assign a lineno to the next instruction for a stmt. */
 	c->u->u_lineno = s->lineno;
 	c->u->u_lineno_set = false;
+
 	switch (s->kind) {
 	case FunctionDef_kind:
 		return compiler_function(c, s);
@@ -2962,11 +2982,11 @@
 	s = e->v.BoolOp.values;
 	n = asdl_seq_LEN(s) - 1;
 	for (i = 0; i < n; ++i) {
-		VISIT(c, expr, asdl_seq_GET(s, i));
+		VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
 		ADDOP_JREL(c, jumpi, end);
 		ADDOP(c, POP_TOP)
 	}
-	VISIT(c, expr, asdl_seq_GET(s, n));
+	VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
 	compiler_use_next_block(c, end);
 	return 1;
 }
@@ -3013,24 +3033,25 @@
 		cleanup = compiler_new_block(c);
 		if (cleanup == NULL)
 		    return 0;
-		VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, 0));
+		VISIT(c, expr, 
+                        (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
 	}
 	for (i = 1; i < n; i++) {
 		ADDOP(c, DUP_TOP);
 		ADDOP(c, ROT_THREE);
-		/* XXX We're casting a void* to cmpop_ty in the next stmt. */
 		ADDOP_I(c, COMPARE_OP,
-			cmpop((cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i - 1)));
+			cmpop((cmpop_ty)(asdl_seq_GET(
+                                                  e->v.Compare.ops, i - 1))));
 		ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
 		NEXT_BLOCK(c);
 		ADDOP(c, POP_TOP);
 		if (i < (n - 1))
-		    VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, i));
+		    VISIT(c, expr, 
+                            (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
 	}
-	VISIT(c, expr, asdl_seq_GET(e->v.Compare.comparators, n - 1));
+	VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
 	ADDOP_I(c, COMPARE_OP,
-		/* XXX We're casting a void* to cmpop_ty in the next stmt. */
-	       cmpop((cmpop_ty)asdl_seq_GET(e->v.Compare.ops, n - 1)));
+	       cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
 	if (n > 1) {
 		basicblock *end = compiler_new_block(c);
 		if (end == NULL)
@@ -3043,6 +3064,7 @@
 	}
 	return 1;
 }
+#undef CMPCAST
 
 static int
 compiler_call(struct compiler *c, expr_ty e)
@@ -3102,7 +3124,7 @@
 		anchor == NULL)
 	    return 0;
 
-	l = asdl_seq_GET(generators, gen_index);
+	l = (comprehension_ty)asdl_seq_GET(generators, gen_index);
 	VISIT(c, expr, l->iter);
 	ADDOP(c, GET_ITER);
 	compiler_use_next_block(c, start);
@@ -3113,7 +3135,7 @@
 	/* XXX this needs to be cleaned up...a lot! */
 	n = asdl_seq_LEN(l->ifs);
 	for (i = 0; i < n; i++) {
-		expr_ty e = asdl_seq_GET(l->ifs, i);
+		expr_ty e = (expr_ty)asdl_seq_GET(l->ifs, i);
 		VISIT(c, expr, e);
 		ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
 		NEXT_BLOCK(c);
@@ -3198,7 +3220,7 @@
 	    anchor == NULL || end == NULL)
 		return 0;
 
-	ge = asdl_seq_GET(generators, gen_index);
+	ge = (comprehension_ty)asdl_seq_GET(generators, gen_index);
 	ADDOP_JREL(c, SETUP_LOOP, end);
 	if (!compiler_push_fblock(c, LOOP, start))
 		return 0;
@@ -3221,7 +3243,7 @@
 	/* XXX this needs to be cleaned up...a lot! */
 	n = asdl_seq_LEN(ge->ifs);
 	for (i = 0; i < n; i++) {
-		expr_ty e = asdl_seq_GET(ge->ifs, i);
+		expr_ty e = (expr_ty)asdl_seq_GET(ge->ifs, i);
 		VISIT(c, expr, e);
 		ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
 		NEXT_BLOCK(c);
@@ -3462,6 +3484,9 @@
 {
 	int i, n;
 
+    /* If expr e has a different line number than the last expr/stmt,
+       set a new line number for the next instruction.
+       */
 	if (e->lineno > c->u->u_lineno) {
 		c->u->u_lineno = e->lineno;
 		c->u->u_lineno_set = false;
@@ -3490,9 +3515,11 @@
 		   It wants the stack to look like (value) (dict) (key) */
 		for (i = 0; i < n; i++) {
 			ADDOP(c, DUP_TOP);
-			VISIT(c, expr, asdl_seq_GET(e->v.Dict.values, i));
+			VISIT(c, expr, 
+                                (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
 			ADDOP(c, ROT_TWO);
-			VISIT(c, expr, asdl_seq_GET(e->v.Dict.keys, i));
+			VISIT(c, expr, 
+                                (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
 			ADDOP(c, STORE_SUBSCR);
 		}
 		break;
@@ -3859,7 +3886,8 @@
 		if (ctx != AugStore) {
 			int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
 			for (i = 0; i < n; i++) {
-				slice_ty sub = asdl_seq_GET(s->v.ExtSlice.dims, i);
+				slice_ty sub = (slice_ty)asdl_seq_GET(
+                                        s->v.ExtSlice.dims, i);
 				if (!compiler_visit_nested_slice(c, sub, ctx))
 					return 0;
 			}
@@ -4048,7 +4076,7 @@
 {
 	int d_bytecode, d_lineno;
 	int len;
-	char *lnotab;
+	unsigned char *lnotab;
 
 	d_bytecode = a->a_offset - a->a_lineno_off;
 	d_lineno = i->i_lineno - a->a_lineno;
@@ -4071,7 +4099,8 @@
 			if (_PyString_Resize(&a->a_lnotab, len) < 0)
 				return 0;
 		}
-		lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
+		lnotab = (unsigned char *)
+			   PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
 		for (j = 0; j < ncodes; j++) {
 			*lnotab++ = 255;
 			*lnotab++ = 0;
@@ -4092,7 +4121,8 @@
 			if (_PyString_Resize(&a->a_lnotab, len) < 0)
 				return 0;
 		}
-		lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
+		lnotab = (unsigned char *)
+			   PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
 		*lnotab++ = 255;
 		*lnotab++ = d_bytecode;
 		d_bytecode = 0;
@@ -4109,7 +4139,8 @@
 		if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
 			return 0;
 	}
-	lnotab = PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
+	lnotab = (unsigned char *)
+			PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
 
 	a->a_lnotab_off += 2;
 	if (d_bytecode) {
diff --git a/Python/dynload_win.c b/Python/dynload_win.c
index 6e8f822..36746e2 100644
--- a/Python/dynload_win.c
+++ b/Python/dynload_win.c
@@ -11,10 +11,16 @@
 const struct filedescr _PyImport_DynLoadFiletab[] = {
 #ifdef _DEBUG
 	{"_d.pyd", "rb", C_EXTENSION},
+	/* Temporarily disable .dll, to avoid conflicts between sqlite3.dll
+	   and the sqlite3 package. If this needs to be reverted for 2.5,
+	   some other solution for the naming conflict must be found.
 	{"_d.dll", "rb", C_EXTENSION},
+	*/
 #else
 	{".pyd", "rb", C_EXTENSION},
+	/* Likewise
 	{".dll", "rb", C_EXTENSION},
+	*/
 #endif
 	{0, 0}
 };
diff --git a/Python/errors.c b/Python/errors.c
index 67f86ed..2ae062f 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -16,6 +16,11 @@
 
 #include <ctype.h>
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
 void
 PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)
 {
@@ -609,6 +614,7 @@
 				PyFile_WriteString(": ", f);
 				PyFile_WriteObject(v, f, 0);
 			}
+			Py_XDECREF(moduleName);
 		}
 		PyFile_WriteString(" in ", f);
 		PyFile_WriteObject(obj, f, 0);
@@ -796,3 +802,8 @@
 	}
 	return NULL;
 }
+
+#ifdef __cplusplus
+}
+#endif
+
diff --git a/Python/exceptions.c b/Python/exceptions.c
index b146c97..5c824e6 100644
--- a/Python/exceptions.c
+++ b/Python/exceptions.c
@@ -14,6 +14,7 @@
  * Copyright (c) 1998-2000 by Secret Labs AB.  All rights reserved.
  */
 
+#define PY_SSIZE_T_CLEAN
 #include "Python.h"
 #include "osdefs.h"
 
@@ -893,7 +894,7 @@
 	    if (have_filename)
 		bufsize += PyString_GET_SIZE(filename);
 
-	    buffer = PyMem_MALLOC(bufsize);
+	    buffer = (char *)PyMem_MALLOC(bufsize);
 	    if (buffer != NULL) {
 		if (have_filename && have_lineno)
 		    PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
@@ -1450,8 +1451,8 @@
 	assert(length < INT_MAX);
 	assert(start < INT_MAX);
 	assert(end < INT_MAX);
-    return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#iis",
-	encoding, object, (int)length, (int)start, (int)end, reason);
+    return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#nns",
+	encoding, object, length, start, end, reason);
 }
 
 
@@ -1565,7 +1566,7 @@
 	const Py_UNICODE *object, Py_ssize_t length,
 	Py_ssize_t start, Py_ssize_t end, const char *reason)
 {
-    return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#iis",
+    return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
 	object, length, start, end, reason);
 }
 #endif
diff --git a/Python/future.c b/Python/future.c
index 1902f1d..d23fad6 100644
--- a/Python/future.c
+++ b/Python/future.c
@@ -19,7 +19,7 @@
 
 	names = s->v.ImportFrom.names;
 	for (i = 0; i < asdl_seq_LEN(names); i++) {
-                alias_ty name = asdl_seq_GET(names, i);
+                alias_ty name = (alias_ty)asdl_seq_GET(names, i);
 		const char *feature = PyString_AsString(name->name);
 		if (!feature)
 			return 0;
@@ -29,7 +29,7 @@
 			continue;
 		} else if (strcmp(feature, FUTURE_DIVISION) == 0) {
 			continue;
-		} else if (strcmp(feature, FUTURE_ABSIMPORT) == 0) {
+		} else if (strcmp(feature, FUTURE_ABSOLUTE_IMPORT) == 0) {
 			continue;
 		} else if (strcmp(feature, FUTURE_WITH_STATEMENT) == 0) {
 			continue;
@@ -73,7 +73,7 @@
 	   
 
 	for (i = 0; i < asdl_seq_LEN(mod->v.Module.body); i++) {
-		stmt_ty s = asdl_seq_GET(mod->v.Module.body, i);
+		stmt_ty s = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i);
 
 		if (done && s->lineno > prev_line)
 			return 1;
@@ -120,14 +120,14 @@
 {
 	PyFutureFeatures *ff;
 
-	ff = (PyFutureFeatures *)PyMem_Malloc(sizeof(PyFutureFeatures));
+	ff = (PyFutureFeatures *)PyObject_Malloc(sizeof(PyFutureFeatures));
 	if (ff == NULL)
 		return NULL;
 	ff->ff_features = 0;
 	ff->ff_lineno = -1;
 
 	if (!future_parse(ff, mod, filename)) {
-		PyMem_Free((void *)ff);
+		PyObject_Free(ff);
 		return NULL;
 	}
 	return ff;
diff --git a/Python/getargs.c b/Python/getargs.c
index fac0b6f..b0715ba 100644
--- a/Python/getargs.c
+++ b/Python/getargs.c
@@ -6,6 +6,9 @@
 #include <ctype.h>
 
 
+#ifdef __cplusplus
+extern "C" { 
+#endif
 int PyArg_Parse(PyObject *, const char *, ...);
 int PyArg_ParseTuple(PyObject *, const char *, ...);
 int PyArg_VaParse(PyObject *, const char *, va_list);
@@ -15,6 +18,18 @@
 int PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *,
 				const char *, char **, va_list);
 
+#ifdef HAVE_DECLSPEC_DLL
+/* Export functions */
+PyAPI_FUNC(int) _PyArg_Parse_SizeT(PyObject *, char *, ...);
+PyAPI_FUNC(int) _PyArg_ParseTuple_SizeT(PyObject *, char *, ...);
+PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
+                                                  const char *, char **, ...);
+PyAPI_FUNC(PyObject *) _Py_BuildValue_SizeT(const char *, ...);
+PyAPI_FUNC(int) _PyArg_VaParse_SizeT(PyObject *, char *, va_list);
+PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
+                                              const char *, char **, va_list);
+#endif
+
 #define FLAG_COMPAT 1
 #define FLAG_SIZE_T 2
 
@@ -631,8 +646,8 @@
 		unsigned int ival;
 		if (float_argument_error(arg))
 			return converterr("integer<I>", arg, msgbuf, bufsize);
-		ival = PyInt_AsUnsignedLongMask(arg);
-		if (ival == -1 && PyErr_Occurred())
+		ival = (unsigned int)PyInt_AsUnsignedLongMask(arg);
+		if (ival == (unsigned int)-1 && PyErr_Occurred())
 			return converterr("integer<I>", arg, msgbuf, bufsize);
 		else
 			*p = ival;
@@ -645,10 +660,10 @@
 		Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
 		Py_ssize_t ival;
 		if (float_argument_error(arg))
-			return converterr("integer<i>", arg, msgbuf, bufsize);
+			return converterr("integer<n>", arg, msgbuf, bufsize);
 		ival = PyInt_AsSsize_t(arg);
 		if (ival == -1 && PyErr_Occurred())
-			return converterr("integer<i>", arg, msgbuf, bufsize);
+			return converterr("integer<n>", arg, msgbuf, bufsize);
 		*p = ival;
 		break;
 	}
@@ -1040,11 +1055,8 @@
 				STORE_SIZE(PyUnicode_GET_SIZE(arg));
 			}
 			else {
-			char *buf;
-			Py_ssize_t count = convertbuffer(arg, p, &buf);
-			if (count < 0)
-				return converterr(buf, arg, msgbuf, bufsize);
-			STORE_SIZE(count/(sizeof(Py_UNICODE))); 
+				return converterr("cannot convert raw buffers",
+						  arg, msgbuf, bufsize);
 			}
 			format++;
 		} else {
@@ -1743,3 +1755,6 @@
 			funcname);
 	return 0;
 }
+#ifdef __cplusplus
+};
+#endif
diff --git a/Python/getmtime.c b/Python/getmtime.c
index f0ac899..54edb53 100644
--- a/Python/getmtime.c
+++ b/Python/getmtime.c
@@ -6,6 +6,10 @@
 #include "Python.h"
 #include "pyconfig.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 time_t
 PyOS_GetLastModificationTime(char *path, FILE *fp)
 {
@@ -15,3 +19,8 @@
 	else
 		return st.st_mtime;
 }
+
+#ifdef __cplusplus
+}
+#endif
+
diff --git a/Python/getopt.c b/Python/getopt.c
index d80f607..5429fac5 100644
--- a/Python/getopt.c
+++ b/Python/getopt.c
@@ -27,6 +27,10 @@
 #include <stdio.h>
 #include <string.h>
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 int _PyOS_opterr = 1;          /* generate error messages */
 int _PyOS_optind = 1;          /* index into argv array   */
 char *_PyOS_optarg = NULL;     /* optional argument       */
@@ -81,3 +85,8 @@
 
 	return option;
 }
+
+#ifdef __cplusplus
+}
+#endif
+
diff --git a/Python/graminit.c b/Python/graminit.c
index 40f1770..a6ac124 100644
--- a/Python/graminit.c
+++ b/Python/graminit.c
@@ -1675,7 +1675,7 @@
 	{91, 1},
 };
 static arc arcs_77_1[1] = {
-	{26, 2},
+	{105, 2},
 };
 static arc arcs_77_2[2] = {
 	{162, 3},
@@ -1732,7 +1732,7 @@
 	{91, 1},
 };
 static arc arcs_80_1[1] = {
-	{26, 2},
+	{105, 2},
 };
 static arc arcs_80_2[2] = {
 	{164, 3},
diff --git a/Python/import.c b/Python/import.c
index b64594d..daae15f 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -17,6 +17,9 @@
 #ifdef HAVE_FCNTL_H
 #include <fcntl.h>
 #endif
+#ifdef __cplusplus
+extern "C" { 
+#endif
 
 extern time_t PyOS_GetLastModificationTime(char *, FILE *);
 						/* In getmtime.c */
@@ -40,6 +43,7 @@
        Python 1.5:   20121
        Python 1.5.1: 20121
        Python 1.5.2: 20121
+       Python 1.6:   50428
        Python 2.0:   50823
        Python 2.0.1: 50823
        Python 2.1:   60202
@@ -1217,12 +1221,12 @@
 #endif
 		if (!PyString_Check(v))
 			continue;
-		len = PyString_Size(v);
+		len = PyString_GET_SIZE(v);
 		if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
 			Py_XDECREF(copy);
 			continue; /* Too long */
 		}
-		strcpy(buf, PyString_AsString(v));
+		strcpy(buf, PyString_AS_STRING(v));
 		if (strlen(buf) != len) {
 			Py_XDECREF(copy);
 			continue; /* v contains '\0' */
@@ -1934,6 +1938,16 @@
 		}
 		tail = next;
 	}
+	if (tail == Py_None) {
+		/* If tail is Py_None, both get_parent and load_next found
+		   an empty module name: someone called __import__("") or
+		   doctored faulty bytecode */
+		Py_DECREF(tail);
+		Py_DECREF(head);
+		PyErr_SetString(PyExc_ValueError,
+				"Empty module name");
+		return NULL;
+	}
 
 	if (fromlist != NULL) {
 		if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
@@ -2094,7 +2108,8 @@
 	PyObject *result;
 
 	if (strlen(name) == 0) {
-		/* empty module name only happens in 'from . import' */
+		/* completely empty module name should only happen in
+		   'from . import' (or '__import__("")')*/
 		Py_INCREF(mod);
 		*p_name = NULL;
 		return mod;
@@ -2936,3 +2951,7 @@
 
 	return PyImport_ExtendInittab(newtab);
 }
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/Python/mactoolboxglue.c b/Python/mactoolboxglue.c
index 7f0627e..0aa2cfd 100644
--- a/Python/mactoolboxglue.c
+++ b/Python/mactoolboxglue.c
@@ -25,6 +25,7 @@
 
 #include "Python.h"
 #include "pymactoolbox.h"
+#include <arpa/inet.h>	/* for ntohl, htonl */
 
 
 /* Like strerror() but for Mac OS error numbers */
@@ -156,12 +157,14 @@
 int
 PyMac_GetOSType(PyObject *v, OSType *pr)
 {
+	uint32_t tmp;
 	if (!PyString_Check(v) || PyString_Size(v) != 4) {
 		PyErr_SetString(PyExc_TypeError,
 			"OSType arg must be string of 4 chars");
 		return 0;
 	}
-	memcpy((char *)pr, PyString_AsString(v), 4);
+	memcpy((char *)&tmp, PyString_AsString(v), 4);
+	*pr = (OSType)ntohl(tmp);
 	return 1;
 }
 
@@ -169,7 +172,8 @@
 PyObject *
 PyMac_BuildOSType(OSType t)
 {
-	return PyString_FromStringAndSize((char *)&t, 4);
+	uint32_t tmp = htonl((uint32_t)t);
+	return PyString_FromStringAndSize((char *)&tmp, 4);
 }
 
 /* Convert an NumVersion value to a 4-element tuple */
diff --git a/Python/modsupport.c b/Python/modsupport.c
index cb6bdfd..e291014 100644
--- a/Python/modsupport.c
+++ b/Python/modsupport.c
@@ -3,8 +3,11 @@
 
 #include "Python.h"
 
+#define FLAG_SIZE_T 1
 typedef double va_double;
 
+static PyObject *va_build_value(const char *, va_list, int);
+
 /* Package context -- the full module name for package imports */
 char *_Py_PackageContext = NULL;
 
@@ -146,14 +149,14 @@
 /* Generic function to create a value -- the inverse of getargs() */
 /* After an original idea and first implementation by Steven Miale */
 
-static PyObject *do_mktuple(const char**, va_list *, int, int);
-static PyObject *do_mklist(const char**, va_list *, int, int);
-static PyObject *do_mkdict(const char**, va_list *, int, int);
-static PyObject *do_mkvalue(const char**, va_list *);
+static PyObject *do_mktuple(const char**, va_list *, int, int, int);
+static PyObject *do_mklist(const char**, va_list *, int, int, int);
+static PyObject *do_mkdict(const char**, va_list *, int, int, int);
+static PyObject *do_mkvalue(const char**, va_list *, int);
 
 
 static PyObject *
-do_mkdict(const char **p_format, va_list *p_va, int endchar, int n)
+do_mkdict(const char **p_format, va_list *p_va, int endchar, int n, int flags)
 {
 	PyObject *d;
 	int i;
@@ -167,13 +170,13 @@
 	for (i = 0; i < n; i+= 2) {
 		PyObject *k, *v;
 		int err;
-		k = do_mkvalue(p_format, p_va);
+		k = do_mkvalue(p_format, p_va, flags);
 		if (k == NULL) {
 			itemfailed = 1;
 			Py_INCREF(Py_None);
 			k = Py_None;
 		}
-		v = do_mkvalue(p_format, p_va);
+		v = do_mkvalue(p_format, p_va, flags);
 		if (v == NULL) {
 			itemfailed = 1;
 			Py_INCREF(Py_None);
@@ -199,7 +202,7 @@
 }
 
 static PyObject *
-do_mklist(const char **p_format, va_list *p_va, int endchar, int n)
+do_mklist(const char **p_format, va_list *p_va, int endchar, int n, int flags)
 {
 	PyObject *v;
 	int i;
@@ -212,13 +215,13 @@
 	/* Note that we can't bail immediately on error as this will leak
 	   refcounts on any 'N' arguments. */
 	for (i = 0; i < n; i++) {
-		PyObject *w = do_mkvalue(p_format, p_va);
+		PyObject *w = do_mkvalue(p_format, p_va, flags);
 		if (w == NULL) {
 			itemfailed = 1;
 			Py_INCREF(Py_None);
 			w = Py_None;
 		}
-		PyList_SetItem(v, i, w);
+		PyList_SET_ITEM(v, i, w);
 	}
 
 	if (itemfailed) {
@@ -232,7 +235,6 @@
 				"Unmatched paren in format");
 		return NULL;
 	}
-
 	if (endchar)
 		++*p_format;
 	return v;
@@ -250,7 +252,7 @@
 #endif
 
 static PyObject *
-do_mktuple(const char **p_format, va_list *p_va, int endchar, int n)
+do_mktuple(const char **p_format, va_list *p_va, int endchar, int n, int flags)
 {
 	PyObject *v;
 	int i;
@@ -262,45 +264,46 @@
 	/* Note that we can't bail immediately on error as this will leak
 	   refcounts on any 'N' arguments. */
 	for (i = 0; i < n; i++) {
-		PyObject *w = do_mkvalue(p_format, p_va);
+		PyObject *w = do_mkvalue(p_format, p_va, flags);
 		if (w == NULL) {
 			itemfailed = 1;
 			Py_INCREF(Py_None);
 			w = Py_None;
 		}
-		PyTuple_SetItem(v, i, w);
+		PyTuple_SET_ITEM(v, i, w);
 	}
-	if (v != NULL && **p_format != endchar) {
+	if (itemfailed) {
+		/* do_mkvalue() should have already set an error */
 		Py_DECREF(v);
-		v = NULL;
+		return NULL;
+	}
+	if (**p_format != endchar) {
+		Py_DECREF(v);
 		PyErr_SetString(PyExc_SystemError,
 				"Unmatched paren in format");
+		return NULL;
 	}
-	else if (endchar)
+	if (endchar)
 		++*p_format;
-	if (itemfailed) {
-		Py_DECREF(v);
-		v = NULL;
-	}
 	return v;
 }
 
 static PyObject *
-do_mkvalue(const char **p_format, va_list *p_va)
+do_mkvalue(const char **p_format, va_list *p_va, int flags)
 {
 	for (;;) {
 		switch (*(*p_format)++) {
 		case '(':
 			return do_mktuple(p_format, p_va, ')',
-					  countformat(*p_format, ')'));
+					  countformat(*p_format, ')'), flags);
 
 		case '[':
 			return do_mklist(p_format, p_va, ']',
-					 countformat(*p_format, ']'));
+					 countformat(*p_format, ']'), flags);
 
 		case '{':
 			return do_mkdict(p_format, p_va, '}',
-					 countformat(*p_format, '}'));
+					 countformat(*p_format, '}'), flags);
 
 		case 'b':
 		case 'B':
@@ -351,10 +354,13 @@
 		{
 			PyObject *v;
 			Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
-			int n;
+			Py_ssize_t n;	
 			if (**p_format == '#') {
 				++*p_format;
-				n = va_arg(*p_va, int);
+				if (flags & FLAG_SIZE_T)
+					n = va_arg(*p_va, Py_ssize_t);
+				else
+					n = va_arg(*p_va, int);
 			}
 			else
 				n = -1;
@@ -393,10 +399,13 @@
 		{
 			PyObject *v;
 			char *str = va_arg(*p_va, char *);
-			int n;
+			Py_ssize_t n;
 			if (**p_format == '#') {
 				++*p_format;
-				n = va_arg(*p_va, int);
+				if (flags & FLAG_SIZE_T)
+					n = va_arg(*p_va, Py_ssize_t);
+				else
+					n = va_arg(*p_va, int);
 			}
 			else
 				n = -1;
@@ -407,7 +416,7 @@
 			else {
 				if (n < 0) {
 					size_t m = strlen(str);
-					if (m > INT_MAX) {
+					if (m > PY_SSIZE_T_MAX) {
 						PyErr_SetString(PyExc_OverflowError,
 							"string too long for Python string");
 						return NULL;
@@ -472,7 +481,18 @@
 	va_list va;
 	PyObject* retval;
 	va_start(va, format);
-	retval = Py_VaBuildValue(format, va);
+	retval = va_build_value(format, va, 0);
+	va_end(va);
+	return retval;
+}
+
+PyObject *
+_Py_BuildValue_SizeT(const char *format, ...)
+{
+	va_list va;
+	PyObject* retval;
+	va_start(va, format);
+	retval = va_build_value(format, va, FLAG_SIZE_T);
 	va_end(va);
 	return retval;
 }
@@ -480,6 +500,18 @@
 PyObject *
 Py_VaBuildValue(const char *format, va_list va)
 {
+	return va_build_value(format, va, 0);
+}
+
+PyObject *
+_Py_VaBuildValue_SizeT(const char *format, va_list va)
+{
+	return va_build_value(format, va, FLAG_SIZE_T);
+}
+
+static PyObject *
+va_build_value(const char *format, va_list va, int flags)
+{
 	const char *f = format;
 	int n = countformat(f, '\0');
 	va_list lva;
@@ -501,8 +533,8 @@
 		return Py_None;
 	}
 	if (n == 1)
-		return do_mkvalue(&f, &lva);
-	return do_mktuple(&f, &lva, '\0', n);
+		return do_mkvalue(&f, &lva, flags);
+	return do_mktuple(&f, &lva, '\0', n, flags);
 }
 
 
diff --git a/Python/pyarena.c b/Python/pyarena.c
index 242ca1d..f11a905 100644
--- a/Python/pyarena.c
+++ b/Python/pyarena.c
@@ -6,9 +6,16 @@
    Measurements with standard library modules suggest the average
    allocation is about 20 bytes and that most compiles use a single
    block.
+
+   TODO(jhylton): Think about a realloc API, maybe just for the last
+   allocation?
 */
 
 #define DEFAULT_BLOCK_SIZE 8192
+#define ALIGNMENT		8
+#define ALIGNMENT_MASK		(ALIGNMENT - 1)
+#define ROUNDUP(x)		(((x) + ALIGNMENT_MASK) & ~ALIGNMENT_MASK)
+
 typedef struct _block {
 	/* Total number of bytes owned by this block available to pass out.
 	 * Read-only after initialization.  The first such byte starts at
@@ -39,9 +46,25 @@
 */
 
 struct _arena {
+        /* Pointer to the first block allocated for the arena, never NULL.
+           It is used only to find the first block when the arena is
+           being freed.
+         */
 	block *a_head;
+
+        /* Pointer to the block currently used for allocation.  It's
+           ab_next field should be NULL.  If it is not-null after a
+           call to block_alloc(), it means a new block has been allocated
+           and a_cur should be reset to point it.
+         */
 	block *a_cur;
+
+        /* A Python list object containing references to all the PyObject
+           pointers associated with this area.  They will be DECREFed
+           when the arena is freed.
+        */
         PyObject *a_objects;
+
 #if defined(Py_DEBUG)
         /* Debug output */
         size_t total_allocs;
@@ -63,7 +86,8 @@
 	b->ab_size = size;
 	b->ab_mem = (void *)(b + 1);
 	b->ab_next = NULL;
-	b->ab_offset = 0;
+	b->ab_offset = ROUNDUP((Py_uintptr_t)(b->ab_mem)) - 
+	  (Py_uintptr_t)(b->ab_mem);
 	return b;
 }
 
@@ -81,19 +105,20 @@
 {
 	void *p;
 	assert(b);
+	size = ROUNDUP(size);
 	if (b->ab_offset + size > b->ab_size) {
 		/* If we need to allocate more memory than will fit in
 		   the default block, allocate a one-off block that is
 		   exactly the right size. */
 		/* TODO(jhylton): Think about space waste at end of block */
-		block *new = block_new(
+		block *newbl = block_new(
 				size < DEFAULT_BLOCK_SIZE ?
 				DEFAULT_BLOCK_SIZE : size);
-		if (!new)
+		if (!newbl)
 			return NULL;
 		assert(!b->ab_next);
-		b->ab_next = new;
-		b = new;
+		b->ab_next = newbl;
+		b = newbl;
 	}
 
 	assert(b->ab_offset + size <= b->ab_size);
@@ -134,6 +159,7 @@
 void
 PyArena_Free(PyArena *arena)
 {
+        int r;
 	assert(arena);
 #if defined(Py_DEBUG)
         /*
@@ -145,7 +171,17 @@
         */
 #endif
 	block_free(arena->a_head);
+	/* This property normally holds, except when the code being compiled
+	   is sys.getobjects(0), in which case there will be two references.
         assert(arena->a_objects->ob_refcnt == 1);
+	*/
+
+        /* Clear all the elements from the list.  This is necessary
+           to guarantee that they will be DECREFed. */
+        r = PyList_SetSlice(arena->a_objects,
+                            0, PyList_GET_SIZE(arena->a_objects), NULL);
+        assert(r == 0);
+        assert(PyList_GET_SIZE(arena->a_objects) == 0);
         Py_DECREF(arena->a_objects);
 	free(arena);
 }
diff --git a/Python/pystate.c b/Python/pystate.c
index 867334e..b8f460f 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -23,13 +23,6 @@
 #endif
 
 
-#define ZAP(x) { \
-	PyObject *tmp = (PyObject *)(x); \
-	(x) = NULL; \
-	Py_XDECREF(tmp); \
-}
-
-
 #ifdef WITH_THREAD
 #include "pythread.h"
 static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */
@@ -37,6 +30,10 @@
 #define HEAD_LOCK() PyThread_acquire_lock(head_mutex, WAIT_LOCK)
 #define HEAD_UNLOCK() PyThread_release_lock(head_mutex)
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 /* The single PyInterpreterState used by this process'
    GILState implementation
 */
@@ -102,12 +99,12 @@
 	for (p = interp->tstate_head; p != NULL; p = p->next)
 		PyThreadState_Clear(p);
 	HEAD_UNLOCK();
-	ZAP(interp->codec_search_path);
-	ZAP(interp->codec_search_cache);
-	ZAP(interp->codec_error_registry);
-	ZAP(interp->modules);
-	ZAP(interp->sysdict);
-	ZAP(interp->builtins);
+	Py_CLEAR(interp->codec_search_path);
+	Py_CLEAR(interp->codec_search_cache);
+	Py_CLEAR(interp->codec_error_registry);
+	Py_CLEAR(interp->modules);
+	Py_CLEAR(interp->sysdict);
+	Py_CLEAR(interp->builtins);
 }
 
 
@@ -211,23 +208,23 @@
 		fprintf(stderr,
 		  "PyThreadState_Clear: warning: thread still has a frame\n");
 
-	ZAP(tstate->frame);
+	Py_CLEAR(tstate->frame);
 
-	ZAP(tstate->dict);
-	ZAP(tstate->async_exc);
+	Py_CLEAR(tstate->dict);
+	Py_CLEAR(tstate->async_exc);
 
-	ZAP(tstate->curexc_type);
-	ZAP(tstate->curexc_value);
-	ZAP(tstate->curexc_traceback);
+	Py_CLEAR(tstate->curexc_type);
+	Py_CLEAR(tstate->curexc_value);
+	Py_CLEAR(tstate->curexc_traceback);
 
-	ZAP(tstate->exc_type);
-	ZAP(tstate->exc_value);
-	ZAP(tstate->exc_traceback);
+	Py_CLEAR(tstate->exc_type);
+	Py_CLEAR(tstate->exc_value);
+	Py_CLEAR(tstate->exc_traceback);
 
 	tstate->c_profilefunc = NULL;
 	tstate->c_tracefunc = NULL;
-	ZAP(tstate->c_profileobj);
-	ZAP(tstate->c_traceobj);
+	Py_CLEAR(tstate->c_profileobj);
+	Py_CLEAR(tstate->c_traceobj);
 }
 
 
@@ -297,23 +294,23 @@
 
 
 PyThreadState *
-PyThreadState_Swap(PyThreadState *new)
+PyThreadState_Swap(PyThreadState *newts)
 {
-	PyThreadState *old = _PyThreadState_Current;
+	PyThreadState *oldts = _PyThreadState_Current;
 
-	_PyThreadState_Current = new;
+	_PyThreadState_Current = newts;
 	/* It should not be possible for more than one thread state
 	   to be used for a thread.  Check this the best we can in debug
 	   builds.
 	*/
 #if defined(Py_DEBUG) && defined(WITH_THREAD)
-	if (new) {
+	if (newts) {
 		PyThreadState *check = PyGILState_GetThisThreadState();
-		if (check && check->interp == new->interp && check != new)
+		if (check && check->interp == newts->interp && check != newts)
 			Py_FatalError("Invalid thread state for this thread");
 	}
 #endif
-	return old;
+	return oldts;
 }
 
 /* An extension mechanism to store arbitrary additional per-thread state.
@@ -356,7 +353,7 @@
 	for (p = interp->tstate_head; p != NULL; p = p->next) {
 		if (p->thread_id != id)
 			continue;
-		ZAP(p->async_exc);
+		Py_CLEAR(p->async_exc);
 		Py_XINCREF(exc);
 		p->async_exc = exc;
 		count += 1;
@@ -491,7 +488,7 @@
 	   called Py_Initialize() and usually PyEval_InitThreads().
 	*/
 	assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
-	tcur = PyThread_get_key_value(autoTLSkey);
+	tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);
 	if (tcur == NULL) {
 		/* Create a new thread state for this thread */
 		tcur = PyThreadState_New(autoInterpreterState);
@@ -518,7 +515,8 @@
 void
 PyGILState_Release(PyGILState_STATE oldstate)
 {
-	PyThreadState *tcur = PyThread_get_key_value(autoTLSkey);
+	PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
+                                                                autoTLSkey);
 	if (tcur == NULL)
 		Py_FatalError("auto-releasing thread-state, "
 		              "but no thread-state for this thread");
@@ -551,4 +549,11 @@
 	else if (oldstate == PyGILState_UNLOCKED)
 		PyEval_SaveThread();
 }
+
+#ifdef __cplusplus
+}
+#endif
+
 #endif /* WITH_THREAD */
+
+
diff --git a/Python/pystrtod.c b/Python/pystrtod.c
index 83e792d..db4cad1 100644
--- a/Python/pystrtod.c
+++ b/Python/pystrtod.c
@@ -101,7 +101,7 @@
 		char *copy, *c;
 
 		/* We need to convert the '.' to the locale specific decimal point */
-		copy = malloc(end - nptr + 1 + decimal_point_len);
+		copy = (char *)malloc(end - nptr + 1 + decimal_point_len);
 
 		c = copy;
 		memcpy(c, nptr, decimal_point_pos - nptr);
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 2c5400d..99d6d9d 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -30,14 +30,15 @@
 #endif
 
 #ifndef Py_REF_DEBUG
-#  define PRINT_TOTAL_REFS()
+#define PRINT_TOTAL_REFS()
 #else /* Py_REF_DEBUG */
-#  if defined(MS_WIN64)
-#    define PRINT_TOTAL_REFS() fprintf(stderr, "[%Id refs]\n", _Py_RefTotal);
-#  else /* ! MS_WIN64 */
-#    define PRINT_TOTAL_REFS() fprintf(stderr, "[%ld refs]\n", \
-			Py_SAFE_DOWNCAST(_Py_RefTotal, Py_ssize_t, long));
-#  endif /* MS_WIN64 */
+#define PRINT_TOTAL_REFS() fprintf(stderr,				\
+				   "[%" PY_FORMAT_SIZE_T "d refs]\n",	\
+				   _Py_GetRefTotal())
+#endif
+
+#ifdef __cplusplus
+extern "C" {
 #endif
 
 extern char *Py_GetPath(void);
@@ -280,6 +281,16 @@
 		}
 		Py_XDECREF(sys_isatty);
 
+		sys_stream = PySys_GetObject("stderr");
+		sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
+		if (!sys_isatty)
+			PyErr_Clear();
+		if(sys_isatty && PyObject_IsTrue(sys_isatty)) {
+			if (!PyFile_SetEncoding(sys_stream, codeset))
+				Py_FatalError("Cannot set codeset of stderr");
+		}
+		Py_XDECREF(sys_isatty);
+
 		if (!Py_FileSystemDefaultEncoding)
 			Py_FileSystemDefaultEncoding = codeset;
 		else
@@ -296,7 +307,7 @@
 
 
 #ifdef COUNT_ALLOCS
-extern void dump_counts(void);
+extern void dump_counts(FILE*);
 #endif
 
 /* Undo the effect of Py_Initialize().
@@ -358,6 +369,13 @@
 	 * XXX I haven't seen a real-life report of either of these.
 	 */
 	PyGC_Collect();
+#ifdef COUNT_ALLOCS
+	/* With COUNT_ALLOCS, it helps to run GC multiple times:
+	   each collection might release some types from the type
+	   list, so they become garbage. */
+	while (PyGC_Collect() > 0)
+		/* nothing */;
+#endif
 
 	/* Destroy all modules */
 	PyImport_Cleanup();
@@ -386,10 +404,10 @@
 
 	/* Debugging stuff */
 #ifdef COUNT_ALLOCS
-	dump_counts();
+	dump_counts(stdout);
 #endif
 
-	PRINT_TOTAL_REFS()
+	PRINT_TOTAL_REFS();
 
 #ifdef Py_TRACE_REFS
 	/* Display all objects still alive -- this can invoke arbitrary
@@ -679,7 +697,7 @@
 	}
 	for (;;) {
 		ret = PyRun_InteractiveOneFlags(fp, filename, flags);
-		PRINT_TOTAL_REFS()
+		PRINT_TOTAL_REFS();
 		if (ret == E_EOF)
 			return 0;
 		/*
@@ -1451,7 +1469,7 @@
 	v = Py_BuildValue("(ziiz)", err->filename,
 			  err->lineno, err->offset, err->text);
 	if (err->text != NULL) {
-		PyMem_DEL(err->text);
+		PyObject_FREE(err->text);
 		err->text = NULL;
 	}
 	w = NULL;
@@ -1666,16 +1684,113 @@
 /* Deprecated C API functions still provided for binary compatiblity */
 
 #undef PyParser_SimpleParseFile
-#undef PyParser_SimpleParseString
-
-node *
+PyAPI_FUNC(node *)
 PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
 {
 	return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
 }
 
-node *
+#undef PyParser_SimpleParseString
+PyAPI_FUNC(node *)
 PyParser_SimpleParseString(const char *str, int start)
 {
 	return PyParser_SimpleParseStringFlags(str, start, 0);
 }
+
+#undef PyRun_AnyFile
+PyAPI_FUNC(int)
+PyRun_AnyFile(FILE *fp, const char *name)
+{
+	return PyRun_AnyFileExFlags(fp, name, 0, NULL);
+}
+
+#undef PyRun_AnyFileEx
+PyAPI_FUNC(int)
+PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
+{
+	return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
+}
+
+#undef PyRun_AnyFileFlags
+PyAPI_FUNC(int)
+PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
+{
+	return PyRun_AnyFileExFlags(fp, name, 0, flags);
+}
+
+#undef PyRun_File
+PyAPI_FUNC(PyObject *)
+PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
+{
+        return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
+}
+
+#undef PyRun_FileEx
+PyAPI_FUNC(PyObject *)
+PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
+{
+        return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
+}
+
+#undef PyRun_FileFlags
+PyAPI_FUNC(PyObject *)
+PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
+		PyCompilerFlags *flags)
+{
+        return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
+}
+
+#undef PyRun_SimpleFile
+PyAPI_FUNC(int)
+PyRun_SimpleFile(FILE *f, const char *p)
+{
+	return PyRun_SimpleFileExFlags(f, p, 0, NULL);
+}
+
+#undef PyRun_SimpleFileEx
+PyAPI_FUNC(int)
+PyRun_SimpleFileEx(FILE *f, const char *p, int c)
+{
+	return PyRun_SimpleFileExFlags(f, p, c, NULL);
+}
+
+
+#undef PyRun_String
+PyAPI_FUNC(PyObject *)
+PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
+{
+	return PyRun_StringFlags(str, s, g, l, NULL);
+}
+
+#undef PyRun_SimpleString
+PyAPI_FUNC(int)
+PyRun_SimpleString(const char *s)
+{
+	return PyRun_SimpleStringFlags(s, NULL);
+}
+
+#undef Py_CompileString
+PyAPI_FUNC(PyObject *)
+Py_CompileString(const char *str, const char *p, int s)
+{
+	return Py_CompileStringFlags(str, p, s, NULL);
+}
+
+#undef PyRun_InteractiveOne
+PyAPI_FUNC(int)
+PyRun_InteractiveOne(FILE *f, const char *p)
+{
+	return PyRun_InteractiveOneFlags(f, p, NULL);
+}
+
+#undef PyRun_InteractiveLoop
+PyAPI_FUNC(int)
+PyRun_InteractiveLoop(FILE *f, const char *p)
+{
+	return PyRun_InteractiveLoopFlags(f, p, NULL);
+}
+
+#ifdef __cplusplus
+}
+#endif
+
diff --git a/Python/symtable.c b/Python/symtable.c
index c8eab58..184723d 100644
--- a/Python/symtable.c
+++ b/Python/symtable.c
@@ -227,7 +227,8 @@
 	case Module_kind:
 		seq = mod->v.Module.body;
 		for (i = 0; i < asdl_seq_LEN(seq); i++)
-			if (!symtable_visit_stmt(st, asdl_seq_GET(seq, i)))
+			if (!symtable_visit_stmt(st, 
+                                    (stmt_ty)asdl_seq_GET(seq, i)))
 				goto error;
 		break;
 	case Expression_kind:
@@ -237,7 +238,8 @@
 	case Interactive_kind:
 		seq = mod->v.Interactive.body;
 		for (i = 0; i < asdl_seq_LEN(seq); i++)
-			if (!symtable_visit_stmt(st, asdl_seq_GET(seq, i)))
+			if (!symtable_visit_stmt(st, 
+                                    (stmt_ty)asdl_seq_GET(seq, i)))
 				goto error;
 		break;
 	case Suite_kind:
@@ -506,7 +508,7 @@
 */
 static int
 update_symbols(PyObject *symbols, PyObject *scope, 
-               PyObject *bound, PyObject *free, int class)
+               PyObject *bound, PyObject *free, int classflag)
 {
 	PyObject *name, *v, *u, *w, *free_value = NULL;
 	Py_ssize_t pos = 0;
@@ -541,7 +543,7 @@
 			   the class that has the same name as a local
 			   or global in the class scope.
 			*/
-			if  (class && 
+			if  (classflag && 
 			     PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) {
 				long i = PyInt_AS_LONG(o) | DEF_FREE_CLASS;
 				o = PyInt_FromLong(i);
@@ -851,7 +853,7 @@
 	int i; \
 	asdl_seq *seq = (SEQ); /* avoid variable capture */ \
 	for (i = 0; i < asdl_seq_LEN(seq); i++) { \
-		TYPE ## _ty elt = asdl_seq_GET(seq, i); \
+		TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
 		if (!symtable_visit_ ## TYPE((ST), elt)) \
 			return 0; \
 	} \
@@ -861,7 +863,7 @@
 	int i; \
 	asdl_seq *seq = (SEQ); /* avoid variable capture */ \
 	for (i = 0; i < asdl_seq_LEN(seq); i++) { \
-		TYPE ## _ty elt = asdl_seq_GET(seq, i); \
+		TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
 		if (!symtable_visit_ ## TYPE((ST), elt)) { \
 			symtable_exit_block((ST), (S)); \
 			return 0; \
@@ -873,7 +875,7 @@
 	int i; \
 	asdl_seq *seq = (SEQ); /* avoid variable capture */ \
 	for (i = (START); i < asdl_seq_LEN(seq); i++) { \
-		TYPE ## _ty elt = asdl_seq_GET(seq, i); \
+		TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
 		if (!symtable_visit_ ## TYPE((ST), elt)) \
 			return 0; \
 	} \
@@ -883,7 +885,7 @@
 	int i; \
 	asdl_seq *seq = (SEQ); /* avoid variable capture */ \
 	for (i = (START); i < asdl_seq_LEN(seq); i++) { \
-		TYPE ## _ty elt = asdl_seq_GET(seq, i); \
+		TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
 		if (!symtable_visit_ ## TYPE((ST), elt)) { \
 			symtable_exit_block((ST), (S)); \
 			return 0; \
@@ -1036,7 +1038,7 @@
 		int i;
 		asdl_seq *seq = s->v.Global.names;
 		for (i = 0; i < asdl_seq_LEN(seq); i++) {
-			identifier name = asdl_seq_GET(seq, i);
+			identifier name = (identifier)asdl_seq_GET(seq, i);
 			char *c_name = PyString_AS_STRING(name);
 			long cur = symtable_lookup(st, name);
 			if (cur < 0)
@@ -1200,7 +1202,7 @@
 	
         /* go through all the toplevel arguments first */
 	for (i = 0; i < asdl_seq_LEN(args); i++) {
-		expr_ty arg = asdl_seq_GET(args, i);
+		expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
 		if (arg->kind == Name_kind) {
 			assert(arg->v.Name.ctx == Param ||
                                (arg->v.Name.ctx == Store && !toplevel));
@@ -1236,7 +1238,7 @@
 {
 	int i;
 	for (i = 0; i < asdl_seq_LEN(args); i++) {
-		expr_ty arg = asdl_seq_GET(args, i);
+		expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
 		if (arg->kind == Tuple_kind &&
 		    !symtable_visit_params(st, arg->v.Tuple.elts, 0))
 			return 0;
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 875f73f..c6eb91f 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -600,10 +600,9 @@
 static PyObject *
 sys_gettotalrefcount(PyObject *self)
 {
-	return PyInt_FromSsize_t(_Py_RefTotal);
+	return PyInt_FromSsize_t(_Py_GetRefTotal());
 }
-
-#endif /* Py_TRACE_REFS */
+#endif /* Py_REF_DEBUG */
 
 PyDoc_STRVAR(getrefcount_doc,
 "getrefcount(object) -> integer\n\
@@ -697,6 +696,10 @@
 10. Number of stack pops performed by call_function()"
 );
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 #ifdef Py_TRACE_REFS
 /* Defined in objects.c because it uses static globals if that file */
 extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
@@ -707,6 +710,10 @@
 extern PyObject *_Py_GetDXProfile(PyObject *,  PyObject *);
 #endif
 
+#ifdef __cplusplus
+}
+#endif
+
 static PyMethodDef sys_methods[] = {
 	/* Might as well keep this in alphabetic order */
 	{"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS,
@@ -1065,6 +1072,11 @@
 		if (!PyFile_SetEncoding(sysout, buf))
 			return NULL;
 	}
+	if(isatty(_fileno(stderr))) {
+		sprintf(buf, "cp%d", GetConsoleOutputCP());
+		if (!PyFile_SetEncoding(syserr, buf))
+			return NULL;
+	}
 #endif
 
 	PyDict_SetItemString(sysdict, "stdin", sysin);
@@ -1406,7 +1418,7 @@
 			PyErr_Clear();
 			fputs(buffer, fp);
 		}
-		if (written < 0 || written >= sizeof(buffer)) {
+		if (written < 0 || (size_t)written >= sizeof(buffer)) {
 			const char *truncated = "... truncated";
 			if (PyFile_WriteString(truncated, file) != 0) {
 				PyErr_Clear();
diff --git a/Python/thread_pthread.h b/Python/thread_pthread.h
index 781467f..c29a61c 100644
--- a/Python/thread_pthread.h
+++ b/Python/thread_pthread.h
@@ -26,6 +26,16 @@
 #endif
 #endif
 
+/* Before FreeBSD 5.4, system scope threads was very limited resource
+   in default setting.  So the process scope is preferred to get
+   enough number of threads to work. */
+#ifdef __FreeBSD__
+#include <osreldate.h>
+#if __FreeBSD_version >= 500000 && __FreeBSD_version < 504101
+#undef PTHREAD_SYSTEM_SCHED_SUPPORTED
+#endif
+#endif
+
 #if !defined(pthread_attr_default)
 #  define pthread_attr_default ((pthread_attr_t *)NULL)
 #endif
@@ -138,7 +148,7 @@
 #ifdef THREAD_STACK_SIZE
 	pthread_attr_setstacksize(&attrs, THREAD_STACK_SIZE);
 #endif
-#if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED) && !defined(__FreeBSD__)
+#if defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
         pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
 #endif
 
diff --git a/Python/traceback.c b/Python/traceback.c
index 6c11cf5..cfbd833 100644
--- a/Python/traceback.c
+++ b/Python/traceback.c
@@ -39,24 +39,16 @@
 static int
 tb_traverse(PyTracebackObject *tb, visitproc visit, void *arg)
 {
-	int err = 0;
-	if (tb->tb_next) {
-		err = visit((PyObject *)tb->tb_next, arg);
-		if (err)
-			return err;
-	}
-	if (tb->tb_frame) 
-		err = visit((PyObject *)tb->tb_frame, arg);
-	return err;
+	Py_VISIT(tb->tb_next);
+	Py_VISIT(tb->tb_frame);
+	return 0;
 }
 
 static void
 tb_clear(PyTracebackObject *tb)
 {
-	Py_XDECREF(tb->tb_next);
-	Py_XDECREF(tb->tb_frame);
-	tb->tb_next = NULL;
-	tb->tb_frame = NULL;
+	Py_CLEAR(tb->tb_next);
+	Py_CLEAR(tb->tb_frame);
 }
 
 PyTypeObject PyTraceBack_Type = {
@@ -165,7 +157,7 @@
 				}
 				if (PyString_Check(v)) {
 					size_t len;
-					len = PyString_Size(v);
+					len = PyString_GET_SIZE(v);
 					if (len + 1 + taillen >= MAXPATHLEN)
 						continue; /* Too long */
 					strcpy(namebuf, PyString_AsString(v));