cython/cython

co_filename is relative rather than absolute

开放

#2,543 创建于 2018年8月10日

 (6 条评论) (0 个反应) (0 位负责人)Python (1,517 个派生)batch import
Python Semanticshelp wanted

仓库指标

星标
 (8,663 个星标)
PR 合并指标
 (平均合并 45天 12小时) (30 天内合并 52 个 PR)

描述

I managed to get inspect working, mostly, with cython files using binding=True. However, now I ran into another issue where co_filename is set to the relative path of the pyx file at the time of the compile. This means that as soon as you you change directory, we cannot find the source file using co_filename as it doesn't map to the right path.

Looking at the generated c file, the problem is with the filename passed to __Pyx_PyCode_New. The value seems to be generated here: https://github.com/cython/cython/blob/master/Cython/Compiler/ExprNodes.py#L9396. By changing file_path = StringEncoding.bytes_literal(func.pos[0].get_filenametable_entry().encode('utf8'), 'utf8') to file_path = StringEncoding.bytes_literal(os.path.abspath(func.pos[0].get_filenametable_entry()).encode('utf8'), 'utf8'), the abs path was saved in the generated c file. Although I'm not sure this is the correct fix, or whether there are other implications to saving the full path.

My kv_cython_test.pyx file contains

#cython: binding=True

from kivy.uix.widget import Widget
from kivy.lang.compiler import KV, KVCtx

class MyWidget(Widget):

    def apply_kv(self):
        with KVCtx():
            self.x @= self.y

and is right next to the setup.py file that contains

from distutils.core import setup
from Cython.Build import cythonize
from os.path import join, dirname, abspath

setup(
    ext_modules=cythonize(abspath(join(dirname(__file__), "kv_cython_test.pyx")))
)

贡献者指南