编程语言
首页 > 编程语言> > 处理python脚本的绝对/相对路径时的异常处理

处理python脚本的绝对/相对路径时的异常处理

作者:互联网

我有没有shebang的python脚本(很重要).当我用相对路径运行它时,一切正常,将异常直接引发到终端:

$python -V
Python 2.6.6
$cat 1.py
import os

print os.getcwd()

a=b

print c
$python ./1.py
/home/user
Traceback (most recent call last):
  File "./1.py", line 5, in <module>
    a=b
NameError: name 'b' is not defined
$

但是仅当指定脚本的绝对路径时,才在类型为Control-C的情况下引发异常(在Traceback部分之前的代码片段中请注意^ C符号):

$python /home/user/1.py
/home/user
^CTraceback (most recent call last):
  File "/home/v-yyushyn/1.py", line 5, in <module>
    a=b
NameError: name 'b' is not defined
$

首先,我认为它的行为取决于子外壳中运行的“绝对路径”,但是经过简短的调查,我发现这两个启动都在同一外壳中运行.

是的,可以放置shebang并运行脚本而无需指定解释器.但是有趣的是,为什么在脚本的绝对/相对路径下行为会有所不同.

有任何建议吗?
谢谢

更新:python -v /home/user/1.py

# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
# /usr/lib64/python2.6/site.pyc matches /usr/lib64/python2.6/site.py
import site # precompiled from /usr/lib64/python2.6/site.pyc
# /usr/lib64/python2.6/os.pyc matches /usr/lib64/python2.6/os.py
import os # precompiled from /usr/lib64/python2.6/os.pyc
import errno # builtin
import posix # builtin
# /usr/lib64/python2.6/posixpath.pyc matches /usr/lib64/python2.6/posixpath.py
import posixpath # precompiled from /usr/lib64/python2.6/posixpath.pyc
# /usr/lib64/python2.6/stat.pyc matches /usr/lib64/python2.6/stat.py
import stat # precompiled from /usr/lib64/python2.6/stat.pyc
# /usr/lib64/python2.6/genericpath.pyc matches /usr/lib64/python2.6/genericpath.py
import genericpath # precompiled from /usr/lib64/python2.6/genericpath.pyc
# /usr/lib64/python2.6/warnings.pyc matches /usr/lib64/python2.6/warnings.py
import warnings # precompiled from /usr/lib64/python2.6/warnings.pyc
# /usr/lib64/python2.6/linecache.pyc matches /usr/lib64/python2.6/linecache.py
import linecache # precompiled from /usr/lib64/python2.6/linecache.pyc
# /usr/lib64/python2.6/types.pyc matches /usr/lib64/python2.6/types.py
import types # precompiled from /usr/lib64/python2.6/types.pyc
# /usr/lib64/python2.6/UserDict.pyc matches /usr/lib64/python2.6/UserDict.py
import UserDict # precompiled from /usr/lib64/python2.6/UserDict.pyc
# /usr/lib64/python2.6/_abcoll.pyc matches /usr/lib64/python2.6/_abcoll.py
import _abcoll # precompiled from /usr/lib64/python2.6/_abcoll.pyc
# /usr/lib64/python2.6/abc.pyc matches /usr/lib64/python2.6/abc.py
import abc # precompiled from /usr/lib64/python2.6/abc.pyc
# /usr/lib64/python2.6/copy_reg.pyc matches /usr/lib64/python2.6/copy_reg.py
import copy_reg # precompiled from /usr/lib64/python2.6/copy_reg.pyc
# /usr/lib/python2.6/site-packages/abrt_exception_handler.pyc matches /usr/lib/python2.6/site-packages/abrt_exception_handler.py
import abrt_exception_handler # precompiled from /usr/lib/python2.6/site-packages/abrt_exception_handler.pyc
import encodings # directory /usr/lib64/python2.6/encodings
# /usr/lib64/python2.6/encodings/__init__.pyc matches /usr/lib64/python2.6/encodings/__init__.py
import encodings # precompiled from /usr/lib64/python2.6/encodings/__init__.pyc
# /usr/lib64/python2.6/codecs.pyc matches /usr/lib64/python2.6/codecs.py
import codecs # precompiled from /usr/lib64/python2.6/codecs.pyc
import _codecs # builtin
# /usr/lib64/python2.6/encodings/aliases.pyc matches /usr/lib64/python2.6/encodings/aliases.py
import encodings.aliases # precompiled from /usr/lib64/python2.6/encodings/aliases.pyc
# /usr/lib64/python2.6/encodings/utf_8.pyc matches /usr/lib64/python2.6/encodings/utf_8.py
import encodings.utf_8 # precompiled from /usr/lib64/python2.6/encodings/utf_8.pyc
Python 2.6.6 (r266:84292, Sep 11 2012, 08:34:23)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
/home/user
/usr/lib64/python2.6/os.pyc
/home/user/1.py
False
<function <lambda> at 0x7fa22e784c08>
dlopen("/usr/lib64/python2.6/lib-dynload/syslog.so", 2);
import syslog # dynamically loaded from /usr/lib64/python2.6/lib-dynload/syslog.so
# /usr/lib64/python2.6/traceback.pyc matches /usr/lib64/python2.6/traceback.py
import traceback # precompiled from /usr/lib64/python2.6/traceback.pyc
# /usr/lib64/python2.6/socket.pyc matches /usr/lib64/python2.6/socket.py
import socket # precompiled from /usr/lib64/python2.6/socket.pyc
dlopen("/usr/lib64/python2.6/lib-dynload/_socketmodule.so", 2);
import _socket # dynamically loaded from /usr/lib64/python2.6/lib-dynload/_socketmodule.so
dlopen("/usr/lib64/python2.6/lib-dynload/_ssl.so", 2);
import _ssl # dynamically loaded from /usr/lib64/python2.6/lib-dynload/_ssl.so
dlopen("/usr/lib64/python2.6/lib-dynload/cStringIO.so", 2);
import cStringIO # dynamically loaded from /usr/lib64/python2.6/lib-dynload/cStringIO.so
^CTraceback (most recent call last):
  File "/home/user/1.py", line 10, in <module>
    a=b
NameError: name 'b' is not defined
# clear __builtin__._
# clear sys.path
# clear sys.argv
# clear sys.ps1
# clear sys.ps2
# clear sys.exitfunc
# clear sys.exc_type
# clear sys.exc_value
# clear sys.exc_traceback
# clear sys.last_type
# clear sys.last_value
# clear sys.last_traceback
# clear sys.path_hooks
# clear sys.path_importer_cache
# clear sys.meta_path
# clear sys.flags
# clear sys.float_info
# restore sys.stdin
# restore sys.stdout
# restore sys.stderr
# cleanup __main__
# cleanup[1] cStringIO
# cleanup[1] encodings
# cleanup[1] site
# cleanup[1] syslog
# cleanup[1] abc
# cleanup[1] _codecs
# cleanup[1] _warnings
# cleanup[1] zipimport
# cleanup[1] encodings.utf_8
# cleanup[1] codecs
# cleanup[1] socket
# cleanup[1] signal
# cleanup[1] traceback
# cleanup[1] posix
# cleanup[1] encodings.aliases
# cleanup[1] exceptions
# cleanup[1] abrt_exception_handler
# cleanup[1] _ssl
# cleanup[1] _socket
# cleanup[2] copy_reg
# cleanup[2] posixpath
# cleanup[2] errno
# cleanup[2] _abcoll
# cleanup[2] types
# cleanup[2] genericpath
# cleanup[2] stat
# cleanup[2] warnings
# cleanup[2] UserDict
# cleanup[2] os.path
# cleanup[2] linecache
# cleanup[2] os
# cleanup sys
# cleanup __builtin__
# cleanup ints: 19 unfreed ints
# cleanup floats

解决方法:

客户端安装了abrt automatic bug reporting tool.它拦截异常并将其发送到专用进程以跟踪崩溃.它是CentOS和Redhat系统上的常见附加组件(另请参见Redhat documentation)

挂起的是该进程,并且要求您使用中断来绕过它.

abrt configuration documentation;自省它在做什么,并将其调整为不挂起. abrt FAQ也可能有帮助;例如,那里列出了用于检查服务状态的命令.

hook abrt installs中可以看出,具有相对路径的脚本将被忽略;它只处理具有绝对路径的脚本中的异常(因此/home/user/1.py),这就是为什么只有那时才看到此问题的原因.

从同一代码中,我们可以看到它将消息记录到syslog中.使用默认值,因此它们将以/var/log/user.log结尾;查找那里的脚本文件名(1.py).

与abrtd守护程序的套接字连接很可能正在挂起.套接字挂起的原因可能有多种原因,including faulty network firmware.

标签:exception-handling,linux,python
来源: https://codeday.me/bug/20191121/2051950.html