Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6964115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:53:02+00:00 2026-05-27T15:53:02+00:00

This question is a follow-up to my previous question , where I tried to

  • 0

This question is a follow-up to my previous question, where I tried to compile python-yenc for Python3. After being told there wasn’t a quick fix, I decided to take up the challange and rewrite it completely.

The only thing I can’t figure out is how to use PyArg_ParseTupleAndKeywords with io-objects. Here is the relevant code:

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;

which obviously yields

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

How can I pass _io.BufferedReader-objects to my function?

Thanks,
Martijn

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:53:03+00:00Added an answer on May 27, 2026 at 3:53 pm

    Mzialla, you should not be using “O!” in PyArg_ParseTupleAndKeywords. Doing so means that you cannot pass any object other than the specified type. I believe the typical approach to interfacing with files in Python 3.2 extensions is not to assume any particular type, but program against the “protocol”.

    So you should do something like:

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

    And after that, you have two options: either you interact with the stream object using its Python methods, or you obtain the stream’s file descriptor (PyObject_AsFileDescriptor) and operate on that using the OS-level read/write functions (or equivalent).

    For the Python-method approach, you should obtain the “read” method, and invoke that instead of fread. Something along the following lines (untested):

    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);
        ...
    }
    

    Then you would need to do something similar for the write side.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a follow-up question to my previous post: Python/Scipy Interpolation (map_coordinates) Let's say
This question is a follow up with a previous question Previous Question The previous
This is a follow up question to a previous question I asked about calculating
This is a follow on from my previous question although this is about something
This is a follow-up question related to my previous post . Below is a
This is really a follow on question to a previous one , where I
This post if a follow-up question to mt previous post: Android RESTful Web application
This is a follow up from my previous question I have this code basically
This is a follow-up to my previous question . Suppose I am writing the
This is a follow-up to my previous question I would like to find a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.