Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.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 带有编解码器头的ASCII安全文件的编码问题,具体取决于行数_Python_Windows_Python 3.x_Encoding_Python 3.5 - Fatal编程技术网

Python 带有编解码器头的ASCII安全文件的编码问题,具体取决于行数

Python 带有编解码器头的ASCII安全文件的编码问题,具体取决于行数,python,windows,python-3.x,encoding,python-3.5,Python,Windows,Python 3.x,Encoding,Python 3.5,下面是在Windows上运行的Python3.5.2的神奇bug,它扼杀了我的一天。以下文件在此系统上失败: 几乎不包含任何内容-除了编码标题外,还有一堆空行,但是当删除任何行时,即使是空行,它也会再次工作。我认为这是一个局部问题,所以我设置了显示相同行为的程序 Python是怎么回事 下面是该文件的一部分: #!/usr/bin/env python # -*- coding: cp1252 -*- """ There is nothing in this file, except tha

下面是在Windows上运行的Python3.5.2的神奇bug,它扼杀了我的一天。以下文件在此系统上失败:

几乎不包含任何内容-除了
编码
标题外,还有一堆空行,但是当删除任何行时,即使是空行,它也会再次工作。我认为这是一个局部问题,所以我设置了显示相同行为的程序

Python是怎么回事

下面是该文件的一部分:

#!/usr/bin/env python
# -*- coding: cp1252 -*-


"""
There is nothing in this file, except that it is more
than 50 lines long. Running it with Python 3.5.2 on
Windows gives the following error:

    >python encoding-problem-cp1252.py
      File "encoding-problem-cp1252.py", line 2
    SyntaxError: encoding problem: cp1252

    >python
    Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.

If you remove any lines from this file, it will
execute successfully.
"""



def restore(dump):
  """













  """
  return



def main():
  print('ok')



if __name__ == '__main__':
  main()

这看起来像是由以下原因造成的倒退。位置计算似乎假设在文件仅包含LF字符时始终存在CRLF行结尾,从而导致偏移量不正确:

fd=fileno(tok->fp);
/*由于缓冲,fd的文件偏移量可能与文件不同
*tok->fp的位置。如果在Windows上以文本模式打开tok->fp,
*它的文件位置将CRLF计数为一个字符,不能直接映射
*到fd的文件偏移量。相反,我们后退一个字节并读取
*最后一行*/
pos=ftell(tok->fp);
如果(位置==-1||
lseek(fd,(关断)(位置>0?位置-1:pos),寻道设置)==(关断)-1){
PyErr_setfromrnowithfilename(PyExc_OSError,NULL);
去清理;
}
当您将文件转换为使用Windows(CRLF)行结尾时,问题就消失了,但我可以理解,对于跨平台脚本,这不是一个实用的解决方案


我已经提交;这应该在Python本身中修复。

不在windows上运行,这很难调试;该文件在Mac OS X和Linux上运行良好。听起来你好像发现了一个bug,请在查看后提交一个,我怀疑CRLF在那里的处理。如果将文件从Unix行尾(现在的情况)转换为Windows行尾,会发生什么情况?文件变长肯定会带来缓冲问题。@MartijnPieters,当文件转换为CRLF时,错误消失了,但我不会将所有源代码转换为CRLF,因为Windows是这些脚本的辅助平台。感谢您确认我的直觉是正确的。报告的问题是
ftell(tok->fp)
返回-1。触发错误所需的换行数取决于shebang和编码规范的长度。要计算文件位置
ftell
从实际操作系统文件位置减去缓冲区中未读字节的计数。问题是缓冲区中的行尾已经转换为LF,而OS文件的位置基于未转换的行尾。因此
ftell
假定文件使用CRLF行结尾,因此每个未读LF减去2个字节。在正确的条件下,它返回-1,这看起来像是一个错误。其他人?;-)顺便说一句,我只看了这些使
ftell
return-1的具体案例。它还可以返回小于-1的位置,在这种情况下,
lseek
调用失败,因为它是
SEEK\u SET
@eryksun:oops!感谢您的分析!
#!/usr/bin/env python
# -*- coding: cp1252 -*-


"""
There is nothing in this file, except that it is more
than 50 lines long. Running it with Python 3.5.2 on
Windows gives the following error:

    >python encoding-problem-cp1252.py
      File "encoding-problem-cp1252.py", line 2
    SyntaxError: encoding problem: cp1252

    >python
    Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.

If you remove any lines from this file, it will
execute successfully.
"""



def restore(dump):
  """













  """
  return



def main():
  print('ok')



if __name__ == '__main__':
  main()