0

这个问题是我之前的问题的后续问题,我尝试为 Python3 编译 python-yenc。在被告知没有快速解决方案后,我决定接受挑战并完全重写它。

我唯一不知道的是如何PyArg_ParseTupleAndKeywords与 io-objects 一起使用。以下是相关代码:

PyObject *in_file, *out_file;
static char *kwlist[] = { "infile", "outfile", NULL };

if(!PyArg_ParseTupleAndKeywords(args, kwds, "O!O!|l", kwlist,\
                     &PyBytes_Type, &in_file,\
                     &PyBytes_Type, &out_file,\
                     &bytes)) return NULL;

这显然会产生

Traceback (most recent call last):
  File "test.py", line 21, in <module>
    print(_yenc.decode_file(b), outfile=o)
TypeError: argument 1 must be bytes, not _io.BufferedReader

如何将_io.BufferedReader-objects 传递给我的函数?

谢谢,马丁

4

3 回答 3

1

我已经下载了yenc(https://bitbucket.org/dual75/yenc)并尝试了以下测试程序但没有成功。

#include <stdio.h>
#include "_yenc.h"

int main()
{
    PyObject *in_file, *out_file;
    PyObject *args;
    PyObject *kwds;
    int bytes;
    static char *kwlist[] = { "infile", "outfile", NULL };

    if(!PyArg_ParseTupleAndKeywords(args, kwds, "O!O!|l", kwlist,\
                 &PyBytes_Type, &in_file,\
                 &PyBytes_Type, &out_file,\
                 &bytes)) {
        printf("Yay!\n");
    } else {
        printf("Bad luck :( \n");
    }

    return 0;
}

我使用以下代码编译了上面的代码:

gcc test.c -I/usr/include/python2.7/

并遇到以下问题:

/tmp/ccRasW2G.o: In function `main':
/home/sangeeth/work/github/yenc/yenc/src/test.c:14: undefined reference to `PyString_Type'
/home/sangeeth/work/github/yenc/yenc/src/test.c:14: undefined reference to `PyString_Type'
/home/sangeeth/work/github/yenc/yenc/src/test.c:14: undefined reference to `PyArg_ParseTupleAndKeywords'
collect2: ld returned 1 exit status

那么,您能否分享一下重现此问题的步骤?

哦,我明白了

PyArg_ParseTupleAndKeywords (in src/_yenc.c) 

这需要两者

&PyFile_Type 

&PyString_Type 

并编译良好(python setup.py build)。那么你看到的问题是什么?

于 2011-12-19T13:28:04.493 回答
1

Mzialla,你不应该使用“O!” 在 PyArg_ParseTupleAndKeywords 中。这样做意味着您不能传递指定类型以外的任何对象。我相信在 Python 3.2 扩展中与文件交互的典型方法不是假设任何特定类型,而是针对“协议”进行编程。

因此,您应该执行以下操作:

if(!PyArg_ParseTupleAndKeywords(args, kwds, "OO|l", kwlist,\
             &in_file, &out_file, &bytes))
    ...

之后,您有两个选择:要么使用流对象的 Python 方法与流对象交互,要么获取流的文件描述符 (PyObject_AsFileDescriptor) 并使用操作系统级别的/函数(或等效函数)对其进行操作。

对于 Python 方法,您应该获取“read”方法,并调用它而不是 fread。大致如下(未经测试):

PyObject *read = PyObject_GetAttrString(in_file, "read");
if (!read) handle_error;
while(encoded < bytes || bytes == 0) {
    PyObject *bytes_obj= PyObject_CallFunction(read, "i", 1);
    if (!bytes || !PyBytes_Check(bytes_obj)) handle_error;
    char *s = PyBytes_AS_STRING(bytes_obj);
    ...
}

然后你需要为写端做类似的事情。

于 2011-12-24T11:22:41.963 回答
1

这有效:

PyObject *decode_file(PyObject *pPySelf, 
              PyObject* pPyArgs, 
              PyObject* pPyKeywords) {
  uLong     totDecodedBytes = 0;
  uInt      decodedBytes;
  uLong     bytesToRead;
  uLong     bytesToDecode = 0;
  Bool      escape = 0;
  Crc32     crc;
  PyObject *pPyInFile = NULL;
  PyObject *pPyOutFile = NULL;
  if (!PyArg_ParseTupleAndKeywords(pPyArgs,
                   pPyKeywords,
                   "OO|l",
                   argnames,
                   &pPyInFile,
                   &pPyOutFile,
                   &bytesToDecode)) {
    return NULL;
  }
  crc_init(&crc, 0xffffffffl);
  while (totDecodedBytes < bytesToDecode) {
    char      *readBuffer;
    Byte       writeBuffer[LONGBUFF];
    Py_ssize_t readBytes;
    PyObject  *pPyReadBuffer;
    bytesToRead = bytesToDecode - totDecodedBytes;
    if (bytesToRead > BLOCK) {
      bytesToRead = BLOCK;
    } else if (bytesToRead == 0) {
      break;
    }
    pPyReadBuffer = PyObject_CallMethod(pPyInFile, "read", "l", bytesToRead);
    if (NULL == pPyReadBuffer) {
      break;
    }
    PyBytes_AsStringAndSize(pPyReadBuffer, &readBuffer, &readBytes);
    decodedBytes = decode_buffer((Byte*)readBuffer,
                 writeBuffer,
                 readBytes,
                 &crc,
                 &escape);
    PyObject_CallMethod(pPyOutFile, "write", "y#", writeBuffer, decodedBytes);
    totDecodedBytes += decodedBytes;
  }
  return Py_BuildValue("(l,L)", totDecodedBytes, (long long)crc.crc);
}
于 2018-01-31T18:51:10.367 回答