python eval 函数中的 \r\n 与 \n

发布于 2024-07-30 02:37:17 字数 309 浏览 0 评论 0原文

为什么 eval 函数不能与 \r\n 一起使用,而可以与 \n 一起使用。 例如 eval("for i in range(5):\r\n print 'hello'") 不起作用 eval("for i in range(5):\n print 'hello'") 有效

我知道使用替换("\r","") 不会导致问题被纠正,但有人知道为什么会发生?

- 编辑 - 哦! 抱歉,确切地说,我的意思是执行。 出现回车是因为我正在通过 POST 从 HTML 文本区域读取内容(我在 Linux 机器上)。 现在清楚了,谢谢大家。

Why eval function doesn't work with \r\n but with \n. for example
eval("for i in range(5):\r\n print 'hello'") doesn't work
eval("for i in range(5):\n print 'hello'") works

I know there is not a problem cause using replace("\r","") is corrected, but someone knows why happens?

--Edit--
Oh! sorry , exactly, I meant exec. Carriage returns appeared because I'm reading from a HTML textarea via POST (I'm on a Linux box). now It is clearer, thanks to everyone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑看君怀她人 2024-08-06 02:37:17

你对“工作”有一个奇怪的定义:

>>> eval("for i in range(5):\n print 'hello'")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1
    for i in range(5):
      ^
SyntaxError: invalid syntax
>>> 

我不确定你为什么使用eval——我怀疑你的意思是exec。 在 Python 中,表达式和语句是完全不同的实体 - eval 仅处理表达式(裸表达式也是 也是一个语句,因此 exec 可以处理与它以及其他声明)。

转向 exec,并思考作为 Python 核心提交者的情况,我认为这是一个轻微的设计错误:就像(多余且无用的)空格、制表符和换页符在接受换行符之前一样被忽略,回车符也应该被忽略(同样多余且无用)。 我道歉:我想我们从来没有考虑过有人可能想要在那里放回车符——但是,那里有例如换页符没有任何意义,而且我们确实接受< em>那个...所以我认为没有理由拒绝回车(或其他 Unicode 非 ANSI 空白,现在在 Python 3 中我们接受标识符中的任意 Unicode 非 ANSI 字母数字)。

如果你关心的话,请在 Python 的问题跟踪器上打开一个问题,并且(除非其他提交者出现不可预见的反对)我想我可以通过 Python 3.2 修复它(应该在 12 到 18 个月内发布——这是一个估计 [知情猜测] ],不是承诺;-)。

You have a strange definition of "work":

>>> eval("for i in range(5):\n print 'hello'")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1
    for i in range(5):
      ^
SyntaxError: invalid syntax
>>> 

I'm not sure why you're using eval -- I suspect you mean exec. Expressions and statements are drastically different entities in Python -- eval only deals with expressions (a bare expression is also a statement, so exec can deal with it as well as other statements).

Turning to exec, and pondering the situation as a Python core committer, I think it's a minor mis-design: just like (redundant and useless) spaces, tabs and form feeds just before a NEWLINE are accepted and ignored, so should (just as redundant and useless) carriage returns be. I apologize: I think we never ever considered that somebody might want to put carriage returns there -- but then, it doesn't make any more sense to have e.g. form feeds there, and we do accept that... so I see no rationale for rejecting carriage returns (or other Unicode non-ANSI whitespace, either, now that in Python 3 we accept arbitrary Unicode non-ANSI alphanumerics in identifiers).

If you care, please open an issue on Python's issue tracker, and (barring unforeseen opposition by other commiters) I think I can get it fixed by Python 3.2 (which should be out in 12 to 18 months -- that's an estimate [informed guess], not a promise;-).

爱的故事 2024-08-06 02:37:17

你的意思是eval还是exec? 请准确发布您运行的内容,以及完整的回溯和错误消息。

问题可能是因为 Python 语法规定行以换行符 ('\n') 终止,而不是两个字符序列 '\r\n'。

一般来说,如果某处有有意义的 '\r',使用 Replace('\r\n', '\n') 会更安全。 如果你一开始就没有 '\r' 那就更好了...你如何获取文本——在 Windows 机器上读取二进制?

谈到安全性,在从可能的敌人那里获得的任何旧代码上使用 eval 或 exec 时应该小心。

Do you mean eval or exec? Please post exactly what you ran, plus the full traceback and error message.

The problem is probably because the Python grammar says that lines are terminated by newlines ('\n'), not the two-character sequence '\r\n'.

In general, it would be safer for you to use replace('\r\n', '\n') in case there's a meaningful '\r' in there somewhere. It would be better if you didn't have the '\r' there in the first place ... how are you obtaining the text -- binary read on a Windows box??

Talking about safety, you should be careful about using eval or exec on any old code obtained from a possible enemy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文