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
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("}")
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
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
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
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
# Pyrex Parser
#
-import os, re, codecs
+import os, re
from string import join, replace
from types import ListType, TupleType
from Scanning import PyrexScanner
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)
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"
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
"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) ###
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,
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))
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,
# 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()
# 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):
import Options
import Naming
import PyrexTypes
-from PyrexTypes import *
+from PyrexTypes import py_object_type
import TypeSlots
from TypeSlots import \
pyfunction_signature, pymethod_signature, \
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)
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,
# 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
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.
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.
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)
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)
# 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
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)
# 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:
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.
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)
]
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