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

  • Home
  • SEARCH
  • 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 509739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:02:30+00:00 2026-05-13T07:02:30+00:00

This is a combination of my two recent questions: [1] Python instance method in

  • 0

This is a combination of my two recent questions:
[1] Python instance method in C
[2] How to redirect stderr in Python?

I would like to log the output of both stdout and stderr from a python script.

The thing I want to ask is, to create a new type according to [1] seems fairly complicated. Does it simplifies the things if there was no need to expose the new type to Python, i.e. it would only exist in C?

I mean, when Python prints something it goes to “Objects/fileobject.c” and there in “PyFile_WriteObject” it check whether it is possible to write to its argument:

writer = PyObject_GetAttrString(f, "write");
if (writer == NULL)
...

Also, it is possible to get stdout and stderr like this:

PyObject* out = PySys_GetObject("stdout");
PyObject* err = PySys_GetObject("stderr");

My question is then, is it somehow possible to construct necessary PyObject which satisfies the above ‘PyObject_GetAttrString(f, “write”)’ and is callable so I can write:

PySys_SetObject("stdout", <my writer object / class / type / ?>);

http://docs.python.org/c-api/sys.html?highlight=pysys_setobject#PySys_SetObject

This way, there would be no need to expose the new “writer type” to the rest of Python script so I thought it might be a bit simpler to write the code…?

  • 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-13T07:02:31+00:00Added an answer on May 13, 2026 at 7:02 am

    Just make a module object (you’re doing that anyway, if you’re using the C API!-) and make it have a suitable write function — that module object will be suitable as the second argument to PySys_SetObject.

    In my answer to your other question I pointed you to xxmodule.c, an example file in Python’s C sources, which is a module with a lot of examples including types and functions of various kinds — you can work from there even if (mysteriously to me) you consider the “make a new type” part too difficult;-).

    Edit: here’s a trivial working example (aview.py):

    #include "Python.h"
    #include <stdio.h>
    
    static PyObject *
    aview_write(PyObject *self, PyObject *args)
    {
        const char *what;
        if (!PyArg_ParseTuple(args, "s", &what))
            return NULL;
        printf("==%s==", what);
        return Py_BuildValue("");
    }
    
    static PyMethodDef a_methods[] = {
        {"write", aview_write, METH_VARARGS, "Write something."},
        {NULL, NULL, 0, NULL}
    };
    
    PyMODINIT_FUNC
    initaview(void)
    {
        PyObject *m = Py_InitModule("aview", a_methods);
        if (m == NULL) return;
        PySys_SetObject("stdout", m);
    }
    

    Once this aview module is properly installed:

    $ python
    Python 2.5.4 (r254:67917, Dec 23 2008, 14:57:27) 
    [GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import aview
    >>> print 'ciao'
    ==ciao====
    ==>>> 
    

    …any string emitted to standard output is written with == signs around it (and this print calls .write twice: with 'ciao', and then again with a newline).

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

Sidebar

Ask A Question

Stats

  • Questions 297k
  • Answers 297k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Async tracking was only released 2 months ago. They're just… May 13, 2026 at 7:20 pm
  • Editorial Team
    Editorial Team added an answer Try this: $params['group'] = array('NewsItem.id'); May 13, 2026 at 7:20 pm
  • Editorial Team
    Editorial Team added an answer Your for loop is outside a function body. May 13, 2026 at 7:20 pm

Related Questions

As an exercise, and mostly for my own amusement, I'm implementing a backtracking packrat
I'm asking this question because I haven't seen it documented anywhere. We are using
According to Sergey Ryazanov, his Impossibly Fast C++ Delegates are not comparable: My delegates
I've been working on a project that can't be described as 'small' anymore (40+
I'm building a JavaScript library for customers to use to embed content my company's

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.