Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Python3:mercurial中的字节与str_Python 3.x_Mercurial - Fatal编程技术网

Python 3.x Python3:mercurial中的字节与str

Python 3.x Python3:mercurial中的字节与str,python-3.x,mercurial,Python 3.x,Mercurial,以下代码中的mercurial中的bytes与str有一个例子 from mercurial import hg schemes = { 'bundle': hg.bundlerepo, } import sys print(sys.getdefaultencoding()) print('\nhg.schemes:') print(hg.schemes) print('\nschemes:') print(schemes) 输出如下所示: utf-8 hg.scheme

以下代码中的
mercurial
中的
bytes
str
有一个例子

from mercurial import hg

schemes = {
    'bundle': hg.bundlerepo,
}


import sys

print(sys.getdefaultencoding())

print('\nhg.schemes:')
print(hg.schemes)

print('\nschemes:')
print(schemes)
输出如下所示:

utf-8

hg.schemes:
{b'bundle': <module 'mercurial.bundlerepo' from '/usr/local/lib/python3.7/site-packages/mercurial/bundlerepo.py'>, b'union': <module 'mercurial.unionrepo' from '/usr/local/lib/python3.7/site-packages/mercurial/unionrepo.py'>, b'file': <function _local at 0x1044547a0>, b'http': <module 'mercurial.httppeer' from '/usr/local/lib/python3.7/site-packages/mercurial/httppeer.py'>, b'https': <module 'mercurial.httppeer' from '/usr/local/lib/python3.7/site-packages/mercurial/httppeer.py'>, b'ssh': <module 'mercurial.sshpeer' from '/usr/local/lib/python3.7/site-packages/mercurial/sshpeer.py'>, b'static-http': <module 'mercurial.statichttprepo' from '/usr/local/lib/python3.7/site-packages/mercurial/statichttprepo.py'>}

schemes:
{'bundle': <module 'mercurial.bundlerepo' from '/usr/local/lib/python3.7/site-packages/mercurial/bundlerepo.py'>, 'union': <module 'mercurial.unionrepo' from '/usr/local/lib/python3.7/site-packages/mercurial/unionrepo.py'>, 'file': <function _local at 0x1044547a0>, 'http': <module 'mercurial.httppeer' from '/usr/local/lib/python3.7/site-packages/mercurial/httppeer.py'>, 'https': <module 'mercurial.httppeer' from '/usr/local/lib/python3.7/site-packages/mercurial/httppeer.py'>, 'ssh': <module 'mercurial.sshpeer' from '/usr/local/lib/python3.7/site-packages/mercurial/sshpeer.py'>, 'static-http': <module 'mercurial.statichttprepo' from '/usr/local/lib/python3.7/site-packages/mercurial/statichttprepo.py'>}


下面的信息很好地解释了这个案例

以下注释来自
类hgloader(importlib.machine.SourceFileLoader)
,参考

转换源代码的自定义模块加载程序。 当源代码转换为代码对象时,我们转换 某些模式与Python3兼容。这允许我们编写代码 这是Python2的本机版本,并且与Python3兼容,不带 使代码过于丑陋。 我们通过在解析和编译之间转换令牌流来实现这一点。 实现转换会使缓存假设失效 由内置导入器创建。内置导入器在 已保存字节码文件,指示Python/字节码版本。如果 版本更改时,缓存的字节码被忽略 转换可以随时更改。这意味着我们需要检查 缓存的字节码是通过当前转换生成的 或者缓存的字节码和什么之间可能不匹配 将从此类生成。 我们通过包装``get\u数据来补充字节码缓存层`` 和“set_data”。当 ``SourceFileLoader``检索并保存字节码缓存文件, 我们只需在文件上添加一个额外的头 只要此文件中的版本在语义更改时更改, 当转换更改时,缓存的字节码应无效。 添加的标题的格式为“HG”。这是一个文本 ``HG``带有2个二进制字节,表示转换版本。 """ 后面的摘要来自

总之,hack是Python的源代码转换模块加载程序。Python3可以使用它导入Python2源文件,同时将某些原语转换为它们的Python3等价物。它有点像2to3,只是在导入期间在运行时执行。黑客攻击的主要目标是促进Mercurial向Python 3的移植,同时推迟在规范源代码表示中对端口进行最具侵入性(因此也是最烦人的)的操作

有关更多信息,您可以参考以下内容:

schemes = {
    'bundle': bundlerepo,
    'union': unionrepo,
    'file': _local,
    'http': httppeer,
    'https': httppeer,
    'ssh': sshpeer,
    'static-http': statichttprepo,
}
    """Custom module loader that transforms source code.

    When the source code is converted to a code object, we transform
    certain patterns to be Python 3 compatible. This allows us to write code
    that is natively Python 2 and compatible with Python 3 without
    making the code excessively ugly.

    We do this by transforming the token stream between parse and compile.
    Implementing transformations invalidates caching assumptions made
    by the built-in importer. The built-in importer stores a header on
    saved bytecode files indicating the Python/bytecode version. If the
    version changes, the cached bytecode is ignored. The Mercurial
    transformations could change at any time. This means we need to check
    that cached bytecode was generated with the current transformation
    code or there could be a mismatch between cached bytecode and what
    would be generated from this class.

    We supplement the bytecode caching layer by wrapping ``get_data``
    and ``set_data``. These functions are called when the
    ``SourceFileLoader`` retrieves and saves bytecode cache files,
    respectively. We simply add an additional header on the file. As
    long as the version in this file is changed when semantics change,
    cached bytecode should be invalidated when transformations change.

    The added header has the form ``HG<VERSION>``. That is a literal
    ``HG`` with 2 binary bytes indicating the transformation version.
    """