Commit 889ffe3c authored by Stefan Behnel's avatar Stefan Behnel

fixed loads of bugs found by pychecker

parent 9382c472
......@@ -18,28 +18,6 @@ from Cython.Debugging import print_call_chain
from DebugFlags import debug_disposal_code, debug_temp_alloc, \
debug_coercion
class EncodedString(unicode):
# unicode string subclass to keep track of the original encoding.
# 'encoding' is None for unicode strings and the source encoding
# otherwise
encoding = None
def byteencode(self):
assert self.encoding is not None
return self.encode(self.encoding)
def utf8encode(self):
assert self.encoding is None
return self.encode("UTF-8")
def is_unicode(self):
return self.encoding is None
is_unicode = property(is_unicode)
# def __eq__(self, other):
# return unicode.__eq__(self, other) and \
# getattr(other, 'encoding', '') == self.encoding
class ExprNode(Node):
# subexprs [string] Class var holding names of subexpr node attrs
......@@ -2251,7 +2229,7 @@ class SequenceNode(ExprNode):
self.iterator.py_result()))
if debug_disposal_code:
print("UnpackNode.generate_assignment_code:")
print("...generating disposal code for %s" % iterator)
print("...generating disposal code for %s" % self.iterator)
self.iterator.generate_disposal_code(code)
code.putln("}")
......@@ -2279,7 +2257,7 @@ class TupleNode(SequenceNode):
def calculate_result_code(self):
if len(self.args) > 0:
error(pos, "Positive length tuples must be constructed.")
error(self.pos, "Positive length tuples must be constructed.")
else:
return Naming.empty_tuple
......
......@@ -12,7 +12,7 @@ import TypeSlots
from PyrexTypes import py_object_type, error_type, CTypedefType, CFuncType
from Symtab import ModuleScope, LocalScope, \
StructOrUnionScope, PyClassScope, CClassScope
from Cython.Utils import open_new_file, replace_suffix
from Cython.Utils import open_new_file, replace_suffix, EncodedString
import Options
import ControlFlow
......@@ -41,10 +41,10 @@ def relative_position(pos):
def embed_position(pos, docstring):
if not Options.embed_pos_in_docstring:
return docstring
pos_line = u'File: %s (starting at line %s)' % relative_position(self.pos)
pos_line = u'File: %s (starting at line %s)' % relative_position(pos)
if docstring is None:
# unicode string
return ExprNodes.EncodedString(pos_line)
return EncodedString(pos_line)
# make sure we can encode the filename in the docstring encoding
# otherwise make the docstring a unicode string
......@@ -57,9 +57,9 @@ def embed_position(pos, docstring):
if not docstring:
# reuse the string encoding of the original docstring
doc = ExprNodes.EncodedString(pos_line)
doc = EncodedString(pos_line)
else:
doc = ExprNodes.EncodedString(pos_line + u'\\n' + docstring)
doc = EncodedString(pos_line + u'\\n' + docstring)
doc.encoding = encoding
return doc
......
......@@ -2,7 +2,7 @@
# Pyrex Parser
#
import os, re, codecs
import os, re
from string import join, replace
from types import ListType, TupleType
from Scanning import PyrexScanner
......@@ -282,7 +282,7 @@ def p_call(s, function):
if not arg.is_name:
s.error("Expected an identifier before '='",
pos = arg.pos)
encoded_name = ExprNodes.EncodedString(arg.name)
encoded_name = Utils.EncodedString(arg.name)
encoded_name.encoding = s.source_encoding
keyword = ExprNodes.StringNode(arg.pos,
value = encoded_name)
......@@ -503,11 +503,11 @@ def p_name(s, name):
elif isinstance(value, float):
return ExprNodes.FloatNode(pos, value = rep)
elif isinstance(value, str):
sval = ExprNodes.EncodedString(rep[1:-1])
sval = Utils.EncodedString(rep[1:-1])
sval.encoding = value.encoding
return ExprNodes.StringNode(pos, value = sval)
elif isinstance(value, unicode):
sval = ExprNodes.EncodedString(rep[2:-1])
sval = Utils.EncodedString(rep[2:-1])
return ExprNodes.StringNode(pos, value = sval)
else:
error(pos, "Invalid type for compile-time constant: %s"
......@@ -523,12 +523,12 @@ def p_cat_string_literal(s):
while s.sy == 'BEGIN_STRING':
next_kind, next_value = p_string_literal(s)
if next_kind == 'c':
self.error(
"Cannot concatenate char literal with another string or char literal")
error(s.position(),
"Cannot concatenate char literal with another string or char literal")
elif next_kind == 'u':
kind = 'u'
strings.append(next_value)
value = ExprNodes.EncodedString( u''.join(strings) )
value = Utils.EncodedString( u''.join(strings) )
if kind != 'u':
value.encoding = s.source_encoding
return kind, value
......@@ -600,7 +600,7 @@ def p_string_literal(s):
"Unexpected token %r:%r in string literal" %
(sy, s.systring))
s.next()
value = ExprNodes.EncodedString( u''.join(chars) )
value = Utils.EncodedString( u''.join(chars) )
if kind != 'u':
value.encoding = s.source_encoding
#print "p_string_literal: value =", repr(value) ###
......@@ -915,7 +915,7 @@ def p_import_statement(s):
ExprNodes.StringNode(pos, value = "*")])
else:
name_list = None
dotted_name = ExprNodes.EncodedString(dotted_name)
dotted_name = Utils.EncodedString(dotted_name)
dotted_name.encoding = s.source_encoding
stat = Nodes.SingleAssignmentNode(pos,
lhs = ExprNodes.NameNode(pos,
......@@ -955,7 +955,7 @@ def p_from_import_statement(s):
imported_name_strings = []
items = []
for (name_pos, name, as_name) in imported_names:
encoded_name = ExprNodes.EncodedString(name)
encoded_name = Utils.EncodedString(name)
encoded_name.encoding = s.source_encoding
imported_name_strings.append(
ExprNodes.StringNode(name_pos, value = encoded_name))
......@@ -965,7 +965,7 @@ def p_from_import_statement(s):
name = as_name or name)))
import_list = ExprNodes.ListNode(
imported_names[0][0], args = imported_name_strings)
dotted_name = ExprNodes.EncodedString(dotted_name)
dotted_name = Utils.EncodedString(dotted_name)
dotted_name.encoding = s.source_encoding
return Nodes.FromImportStatNode(pos,
module = ExprNodes.ImportNode(dotted_name_pos,
......@@ -1971,7 +1971,7 @@ def p_class_statement(s):
# s.sy == 'class'
pos = s.position()
s.next()
class_name = ExprNodes.EncodedString( p_ident(s) )
class_name = Utils.EncodedString( p_ident(s) )
class_name.encoding = s.source_encoding
if s.sy == '(':
s.next()
......
......@@ -59,6 +59,7 @@ def open_pickled_lexicon(expected_hash):
# Try to open pickled lexicon file and verify that
# it matches the source file. Returns the opened
# file if successful, otherwise None. ???
global lexicon_pickle
f = None
result = None
if os.path.exists(lexicon_pickle):
......
......@@ -7,7 +7,7 @@ from Errors import warning, error, InternalError
import Options
import Naming
import PyrexTypes
from PyrexTypes import *
from PyrexTypes import py_object_type
import TypeSlots
from TypeSlots import \
pyfunction_signature, pymethod_signature, \
......@@ -299,7 +299,8 @@ class Scope:
cname = self.mangle(Naming.type_prefix, name)
entry = self.lookup_here(name)
if not entry:
type = CStructOrUnionType(name, kind, scope, typedef_flag, cname)
type = PyrexTypes.CStructOrUnionType(
name, kind, scope, typedef_flag, cname)
entry = self.declare_type(name, type, pos, cname,
visibility = visibility, defining = scope is not None)
self.sue_entries.append(entry)
......@@ -336,7 +337,7 @@ class Scope:
cname = name
else:
cname = self.mangle(Naming.type_prefix, name)
type = CEnumType(name, cname, typedef_flag)
type = PyrexTypes.CEnumType(name, cname, typedef_flag)
else:
type = PyrexTypes.c_anon_enum_type
entry = self.declare_type(name, type, pos, cname = cname,
......@@ -437,10 +438,10 @@ class Scope:
# Add an entry for a string constant.
cname = self.new_const_cname()
if value.is_unicode:
c_type = c_utf8_char_array_type
c_type = PyrexTypes.c_utf8_char_array_type
value = value.utf8encode()
else:
c_type = c_char_array_type
c_type = PyrexTypes.c_char_array_type
value = value.byteencode()
entry = Entry("", cname, c_type, init = value)
entry.used = 1
......@@ -494,16 +495,6 @@ class Scope:
genv.pynum_entries.append(entry)
return entry
def add_py_obj(self, obj, c_prefix=''):
obj.check_const()
cname = self.new_const_cname(c_prefix)
entry = Entry("", cname, py_object_type, init = value)
entry.used = 1
entry.is_interned = 1
self.const_entries.append(entry)
self.interned_objs.append(entry)
return entry
def get_py_obj(self, obj, c_prefix=''):
# Get entry for a generic constant. Returns an existing
# one if possible, otherwise creates a new one.
......@@ -513,7 +504,6 @@ class Scope:
entry = genv.add_py_num(obj, c_prefix)
genv.obj_to_entry[obj] = entry
return entry
def new_const_cname(self):
# Create a new globally-unique name for a constant.
......@@ -532,7 +522,7 @@ class Scope:
cname = "%s%d" % (Naming.pyrex_prefix, n)
entry = Entry("", cname, type)
entry.used = 1
if type.is_pyobject or type == c_py_ssize_t_type:
if type.is_pyobject or type == PyrexTypes.c_py_ssize_t_type:
entry.init = "0"
self.cname_to_entry[entry.cname] = entry
self.temp_entries.append(entry)
......@@ -844,8 +834,9 @@ class ModuleScope(Scope):
self.default_entries.append(entry)
return entry
def new_const_cname(self, prefix=''):
def new_const_cname(self):
# Create a new globally-unique name for a constant.
prefix=''
n = self.const_counter
self.const_counter = n + 1
return "%s%s_%d" % (Naming.const_prefix, prefix, n)
......@@ -877,7 +868,7 @@ class ModuleScope(Scope):
# Make a new entry if needed
#
if not entry:
type = PyExtensionType(name, typedef_flag, base_type)
type = PyrexTypes.PyExtensionType(name, typedef_flag, base_type)
type.pos = pos
if visibility == 'extern':
type.module_name = module_name
......@@ -1075,7 +1066,7 @@ class StructOrUnionScope(Scope):
if not cname:
cname = name
if type.is_cfunction:
type = CPtrType(type)
type = PyrexTypes.CPtrType(type)
entry = self.declare(name, cname, type, pos)
entry.is_variable = 1
self.var_entries.append(entry)
......@@ -1115,9 +1106,12 @@ class ClassScope(Scope):
# Don't want to add a cfunction to this scope 'cause that would mess with
# the type definition, so we just return the right entry.
self.use_utility_code(classmethod_utility_code)
entry = Entry("classmethod",
"__Pyx_Method_ClassMethod",
CFuncType(py_object_type, [CFuncTypeArg("", py_object_type, None)], 0, 0))
entry = Entry(
"classmethod",
"__Pyx_Method_ClassMethod",
PyrexTypes.CFuncType(
py_object_type,
[PyrexTypes.CFuncTypeArg("", py_object_type, None)], 0, 0))
entry.is_cfunction = 1
return entry
else:
......
......@@ -57,7 +57,7 @@ class Transform(object):
parent has with child. This method should always return the node which the parent
should use for this relation, which can either be the same node, None to remove
the node, or a different node."""
raise InternalError("Not implemented")
raise NotImplementedError("Not implemented")
class PrintTree(Transform):
"""Prints a representation of the tree to standard output.
......
......@@ -11,7 +11,7 @@ import os, sys
from Cython.Utils import replace_suffix
from Cython.Compiler.Errors import PyrexError
version = "%s.%s" % sys.version[:2]
version = "%s.%s" % sys.version_info[:2]
py_include_dirs = [
"%s/include/python%s" % (sys.prefix, version)
]
......
......@@ -54,3 +54,25 @@ def detect_file_encoding(source_filename):
def open_source_file(source_filename, mode="rU"):
encoding = detect_file_encoding(source_filename)
return codecs.open(source_filename, mode=mode, encoding=encoding)
class EncodedString(unicode):
# unicode string subclass to keep track of the original encoding.
# 'encoding' is None for unicode strings and the source encoding
# otherwise
encoding = None
def byteencode(self):
assert self.encoding is not None
return self.encode(self.encoding)
def utf8encode(self):
assert self.encoding is None
return self.encode("UTF-8")
def is_unicode(self):
return self.encoding is None
is_unicode = property(is_unicode)
# def __eq__(self, other):
# return unicode.__eq__(self, other) and \
# getattr(other, 'encoding', '') == self.encoding
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment