Commit ba00ebef authored by Stefan Behnel's avatar Stefan Behnel

Remove duplicated file path handling code when generating file path tables.

parent f03092b1
......@@ -1437,12 +1437,13 @@ class GlobalState(object):
#
def lookup_filename(self, source_desc):
entry = source_desc.get_filenametable_entry()
try:
index = self.filename_table[source_desc.get_filenametable_entry()]
index = self.filename_table[entry]
except KeyError:
index = len(self.filename_list)
self.filename_list.append(source_desc)
self.filename_table[source_desc.get_filenametable_entry()] = index
self.filename_table[entry] = index
return index
def commented_file_contents(self, source_desc):
......
......@@ -9047,15 +9047,7 @@ class CodeObjectNode(ExprNode):
func_name = code.get_py_string_const(
func.name, identifier=True, is_str=False, unicode_value=func.name)
# FIXME: better way to get the module file path at module init time? Encoding to use?
file_abspath = func.pos[0].get_filenametable_entry()
# Prefer relative paths to current directory (which is most likely the project root)
# over absolute paths.
workdir = os.getcwd() + os.sep
if file_abspath.startswith(workdir):
file_path = file_abspath[len(workdir):]
else:
file_path = file_abspath
file_path = StringEncoding.bytes_literal(file_path.encode('utf8'), 'utf8')
file_path = StringEncoding.bytes_literal(func.pos[0].get_filenametable_entry().encode('utf8'), 'utf8')
file_path_const = code.get_py_string_const(file_path, identifier=False, is_str=True)
flags = []
......
......@@ -739,27 +739,15 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
code.putln_openmp("#include <omp.h>")
def generate_filename_table(self, code):
import os.path as path
full_module_path = path.join(*self.full_module_name.split('.'))
module_abspath = path.splitext(path.abspath(
self.compilation_source.source_desc.get_filenametable_entry()))[0]
root_path = module_abspath[:-len(full_module_path)]
workdir = path.abspath(os.getcwd()) + os.sep
if root_path.startswith(workdir):
# prefer relative paths to current directory (which is most likely the project root)
root_path = workdir
from os.path import isabs, basename
code.putln("")
code.putln("static const char *%s[] = {" % Naming.filetable_cname)
if code.globalstate.filename_list:
for source_desc in code.globalstate.filename_list:
file_abspath = path.abspath(source_desc.get_filenametable_entry())
if file_abspath.startswith(root_path):
filename = file_abspath[len(root_path):]
else:
filename = path.basename(file_abspath)
escaped_filename = filename.replace("\\", "\\\\").replace('"', r'\"')
file_path = source_desc.get_filenametable_entry()
if isabs(file_path):
file_path = basename(file_path) # never include absolute paths
escaped_filename = file_path.replace("\\", "\\\\").replace('"', r'\"')
code.putln('"%s",' % escaped_filename)
else:
# Some C compilers don't like an empty array
......
......@@ -10,7 +10,7 @@ cython.declare(sys=object, os=object, copy=object,
py_object_type=object, ModuleScope=object, LocalScope=object, ClosureScope=object,
StructOrUnionScope=object, PyClassScope=object,
CppClassScope=object, UtilityCode=object, EncodedString=object,
absolute_path_length=cython.Py_ssize_t, error_type=object, _py_int_types=object)
error_type=object, _py_int_types=object)
import sys, os, copy
from itertools import chain
......@@ -30,7 +30,6 @@ from . import Options
from . import DebugFlags
from ..Utils import add_metaclass
absolute_path_length = 0
if sys.version_info[0] >= 3:
_py_int_types = int
......@@ -39,25 +38,8 @@ else:
def relative_position(pos):
"""
We embed the relative filename in the generated C file, since we
don't want to have to regenerate and compile all the source code
whenever the Python install directory moves (which could happen,
e.g,. when distributing binaries.)
INPUT:
a position tuple -- (absolute filename, line number column position)
return (pos[0].get_filenametable_entry(), pos[1])
OUTPUT:
relative filename
line number
AUTHOR: William Stein
"""
global absolute_path_length
if absolute_path_length == 0:
absolute_path_length = len(os.path.abspath(os.getcwd()))
return (pos[0].get_filenametable_entry()[absolute_path_length+1:], pos[1])
def embed_position(pos, docstring):
if not Options.embed_pos_in_docstring:
......
......@@ -201,6 +201,9 @@ class FileSourceDescriptor(SourceDescriptor):
filename = Utils.decode_filename(filename)
self.path_description = path_description or filename
self.filename = filename
# Prefer relative paths to current directory (which is most likely the project root) over absolute paths.
workdir = os.path.abspath('.') + os.sep
self.file_path = filename[len(workdir):] if filename.startswith(workdir) else filename
self.set_file_type_from_name(filename)
self._cmp_name = filename
self._lines = {}
......@@ -242,7 +245,7 @@ class FileSourceDescriptor(SourceDescriptor):
return path
def get_filenametable_entry(self):
return self.filename
return self.file_path
def __eq__(self, other):
return isinstance(other, FileSourceDescriptor) and self.filename == other.filename
......
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