High Sierra + Python + Postgresql错误:非法指令:4。

5

我可以通过命令行连接到Postgres,但是如果尝试使用psycopg2模块通过Python连接,则会出现以下错误。有趣的是,我刚刚尝试使用PSeqal.app连接也出现了相同的错误。

环境:

  • MacOS: 10.13.3
  • Xcode 9.2
  • Python: 3.6.4
    • 连接模块: psycopg2
  • Postgresql: 10.3

脚本:

这是我的简单连接脚本,它正在尝试通过Python连接到Postgresql:

def connect(self, params):
    """ Connect to the PostgreSQL database server """

    conn = None
    try:
        # connect to the PostgreSQL server
        logging.info('Connecting to the PostgreSQL database...')
        conn = psycopg2.connect(**params)

        # create a cursor
        self.cursor = conn.cursor()

        except (Exception, psycopg2.DatabaseError) as error:
            print(error)
        finally:
            if conn is not None:
                conn.close()
                logging.warning('Database connection closed.')

错误:

脚本输出:

$ python3 testdb.py
非法指令:4

苹果公司错误信息(部分):

Process:               Python [79534]
Path:                  /usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python
Identifier:            Python
Version:               3.6.4 (3.6.4)
Code Type:             X86-64 (Native)
Parent Process:        bash [657]
Responsible:           Python [79534]
User ID:               501

Date/Time:             2018-03-02 15:30:27.642 +1300
OS Version:            Mac OS X 10.13.3 (17D102)
Report Version:        12
Anonymous UUID:        xx


Time Awake Since Boot: 21000 seconds

System Integrity Protection: disabled

Crashed Thread:        0  Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_INSTRUCTION (SIGILL)
Exception Codes:       0x0000000000000001, 0x0000000000000000
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Illegal instruction: 4
Termination Reason:    Namespace SIGNAL, Code 0x4
Terminating Process:   exc handler [0]

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread 
0   libpq.5.10.dylib                0x00000001118e8d27 initPQExpBuffer + 32
1   libpq.5.10.dylib                0x00000001118dba93 PQconninfoParse + 43
2   _psycopg.cpython-36m-darwin.so  0x0000000111897cf1 psyco_parse_dsn + 113
3   org.python.python               0x000000010fd4681a _PyCFunction_FastCallDict + 463
4   org.python.python               0x000000010fdaad8e call_function + 489
5   org.python.python               0x000000010fda3c43 _PyEval_EvalFrameDefault + 4811
6   org.python.python               0x000000010fdab4f0 _PyEval_EvalCodeWithName + 1719
7   org.python.python               0x000000010fda293d PyEval_EvalCodeEx + 57
8   org.python.python               0x000000010fd2ec76 function_call + 339
9   org.python.python               0x000000010fd0e9f2 PyObject_Call + 101
10  org.python.python               0x000000010fda3eac _PyEval_EvalFrameDefault + 5428
11  org.python.python               0x000000010fdab4f0 _PyEval_EvalCodeWithName + 1719
12  org.python.python               0x000000010fda293d PyEval_EvalCodeEx + 57
13  org.python.python               0x000000010fd2ec76 function_call + 339
14  org.python.python               0x000000010fd0e9f2 PyObject_Call + 101
15  org.python.python               0x000000010fda3eac _PyEval_EvalFrameDefault + 5428
16  org.python.python               0x000000010fdabe93 _PyFunction_FastCall + 121
17  org.python.python               0x000000010fdaad65 call_function + 448
18  org.python.python               0x000000010fda3c43 _PyEval_EvalFrameDefault + 4811
19  org.python.python               0x000000010fdabe93 _PyFunction_FastCall + 121
20  org.python.python               0x000000010fd0eb71 _PyObject_FastCallDict + 196
21  org.python.python               0x000000010fd0ec94 _PyObject_Call_Prepend + 156
22  org.python.python               0x000000010fd0e9f2 PyObject_Call + 101
23  org.python.python               0x000000010fd598ae slot_tp_init + 57
24  org.python.python               0x000000010fd5683c type_call + 184
25  org.python.python               0x000000010fd0eb3c _PyObject_FastCallDict + 143
26  org.python.python               0x000000010fdaad5e call_function + 441
27  org.python.python               0x000000010fda3c43 _PyEval_EvalFrameDefault + 4811
28  org.python.python               0x000000010fdabe93 _PyFunction_FastCall + 121
29  org.python.python               0x000000010fd0eb71 _PyObject_FastCallDict + 196
30  org.python.python               0x000000010fd0ec94 _PyObject_Call_Prepend + 156
31  org.python.python               0x000000010fd0e9f2 PyObject_Call + 101
32  org.python.python               0x000000010fd598ae slot_tp_init + 57
33  org.python.python               0x000000010fd5683c type_call + 184
34  org.python.python               0x000000010fd0eb3c _PyObject_FastCallDict + 143
35  org.python.python               0x000000010fdaad5e call_function + 441
36  org.python.python               0x000000010fda3c43 _PyEval_EvalFrameDefault + 4811
37  org.python.python               0x000000010fdab4f0 _PyEval_EvalCodeWithName + 1719
38  org.python.python               0x000000010fda28fe PyEval_EvalCode + 42
39  org.python.python               0x000000010fdcb24e run_mod + 54
40  org.python.python               0x000000010fdca26f PyRun_FileExFlags + 160
41  org.python.python               0x000000010fdc994c PyRun_SimpleFileExFlags + 285
42  org.python.python               0x000000010fddd770 Py_Main + 3484
43  org.python.python               0x000000010fd01e1d 0x10fd00000 + 7709
44  libdyld.dylib                   0x00007fff6c260115 start + 1

Thread 0 crashed with X86 Thread State (64-bit):
rax: 0x00007f86c3db9200  rbx: 0x00007ffedfefe3a0  rcx: 0x0000000000000100  rdx: 0x0000000000010000
rdi: 0x0000000000000b93  rsi: 0x00000000ffff0001  rbp: 0x00007ffedfefe390  rsp: 0x00007ffedfefe380
r8: 0x000000006c3db930   r9: 0x000000000000000f  r10: 0x00000000ffff0000  r11: 0x00007f86c3d00000
r12: 0x0000000110665738  r13: 0x0000000111c68510  r14: 0x0000000111cb80b0  r15: 0x00007ffedfefe3a0
rip: 0x00000001118e8d27  rfl: 0x0000000000010206  cr2: 0x00000001118ef90e

Logical CPU:     2
Error Code:      0x00000000
Trap Number:     6

我已经尝试过的:

  • 从3.5.1升级Python到3.6.4
    • 重新构建了所有依赖库,包括psycopg2,以确保它们与High Sierra兼容(我希望如此)
  • 从头重新安装Postgres
  • 升级Xcode和工具

所以我有点不知道接下来该怎么办? 有人遇到过这种情况吗? 指出哪里需要注意吗? 帮助我解决这个问题的任何内容都将不胜感激,谢谢!


一个随机的想法:既然你可以通过命令行连接到Postgres(我假设是psql),那么libpq应该没问题。之前在苹果操作系统上出现过问题,因为旧版本的postgres组件仍然存在,并被新软件引用。尝试查找系统中是否存在其他libpq.so.xxx版本,并确定psychopg使用的是哪个版本。 - joop
2个回答

9

看起来psycopg2库是罪魁祸首,这个方法对我有用:

$pip3 uninstall psycopg2
...
成功卸载psycopg2-binary-2.7.4

$pip3 install psycopg2-binary --no-binary :all:
收集psycopg2-binary 正在下载psycopg2-binary-2.7.4.tar.gz (426kB) 100% |---| 430kB 649kB/s 跳过bdist_wheel以禁用二进制文件。 安装收集的软件包:psycopg2-binary 运行setup.py安装psycopg2-binary...完成
成功安装psycopg2-binary-2.7.4

$python3 test.py
正在连接到PostgreSQL数据库...
没问题。
数据库连接已关闭。

我认为原因可能与我尝试在2010年的MBP上构建psycopg2有关,它与硬件存在兼容性问题--可能是这台机器上的2.66 GHz Intel Core i7处理器。

感谢@Laurenz Albe和@joop的快速回复、指导和见解,你们帮助我缩小了答案范围,希望这个答案能在整个工作期间保持稳定。;)干杯!


或者:$ pip install --no-binary psycopg2 psycopg2。详情请见:https://stackoverflow.com/a/48710959/5802289 - J0ANMM

1
根据堆栈跟踪,错误发生在PostgreSQL客户端中,具体是在这段无害的代码中:
/*
 * initPQExpBuffer
 *
 * Initialize a PQExpBufferData struct (with previously undefined contents)
 * to describe an empty string.
 */
void
initPQExpBuffer(PQExpBuffer str)
{
    str->data = (char *) malloc(INITIAL_EXPBUFFER_SIZE);
    if (str->data == NULL)
    {
        str->data = (char *) oom_buffer;    /* see comment above */
        str->maxlen = 0;
        str->len = 0;
    }
    else
    {
        str->maxlen = INITIAL_EXPBUFFER_SIZE;
        str->len = 0;
        str->data[0] = '\0';
    }
}

我发现了thisthis两个问题,它们涉及到类似的问题。
“非法指令”应该意味着执行遇到了不存在的指令代码,这可能是编译器错误,也可能意味着代码是为不同指令集的处理器构建的。
问题很可能出现在构建PostgreSQL的过程中。您应该向构建您正在使用的二进制文件的打包人员提出此投诉。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接