scripts.py 文件源码

python
阅读 32 收藏 0 点赞 0 评论 0

项目:dymo-m10-python 作者: pbrf 项目源码 文件源码
def _get_shebang(self, encoding, post_interp=b'', flags=None):
        if self.executable:
            executable = self.executable
        elif not sysconfig.is_python_build():
            executable = get_executable()
        elif hasattr(sys, 'base_prefix') and sys.prefix != sys.base_prefix:
            executable = os.path.join(
                sysconfig.get_path('scripts'),
               'python%s' % sysconfig.get_config_var('EXE'))
        else:
            executable = os.path.join(
                sysconfig.get_config_var('BINDIR'),
               'python%s%s' % (sysconfig.get_config_var('VERSION'),
                               sysconfig.get_config_var('EXE')))
        if flags:
            executable = self._get_alternate_executable(executable, flags)

        executable = fsencode(executable)
        shebang = b'#!' + executable + post_interp + b'\n'
        # Python parser starts to read a script using UTF-8 until
        # it gets a #coding:xxx cookie. The shebang has to be the
        # first line of a file, the #coding:xxx cookie cannot be
        # written before. So the shebang has to be decodable from
        # UTF-8.
        try:
            shebang.decode('utf-8')
        except UnicodeDecodeError:
            raise ValueError(
                'The shebang (%r) is not decodable from utf-8' % shebang)
        # If the script is encoded to a custom encoding (use a
        # #coding:xxx cookie), the shebang has to be decodable from
        # the script encoding too.
        if encoding != 'utf-8':
            try:
                shebang.decode(encoding)
            except UnicodeDecodeError:
                raise ValueError(
                    'The shebang (%r) is not decodable '
                    'from the script encoding (%r)' % (shebang, encoding))
        return shebang
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号