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 6974773
In Process

The Archive Base Latest Questions

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

So I have put together a few Python C-extensions and although their respective behaviors

  • 0

So I have put together a few Python C-extensions and although their respective behaviors are verified, I’d like to verify this by some refcount debugging.

How can I verify that I have inserted the INC/DEC refcounts properly? I want to do this verification from a black box perspective, that is, how should a function’s input/output/memory look if all refcounts have been done properly?

My first approach would be to use sys.getrefcount(obj) and check all the input/output objects’ refcounts to see they are ok. Secondly, I could also perhaps check the memory used to see that there is no memory leak.

But I have never really done this before, so what’s the easiest and most correct way to do this?

Please note:
I don’t want to use any Cython style libraries, I’d rather do the C nitty gritty myself to learn the fundamentals.

Solution: So as suggested I built a debug enabled Python interpreter. I thought it would be tricky, but it wasn’t. As per this recipe I added those exact lines to Objects/object.c. After that all I had to do in the downloaded Python source directory was:

./configure --with-pydebug --prefix=/usr/local/python/
make
make install

With regards to building the C-extension to this new interpreter, all that had to be done on that front was to point the include_dirs tag in the Extension in setup.py to '/usr/local/python/include/python2.7', and then running the build and install command with the new interpreter. Once that was done I could add just add _Py_CountReferences(stderr); lines in the c code and the refcount would be dumped to stderr (console). Very good to see what changes the refcount, and how it increments/decrements as the code progresses.

  • 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-27T17:16:54+00:00Added an answer on May 27, 2026 at 5:16 pm

    One suggestion: Compile the Python interpreter in debug mode (–with-pydebug). Use this interpreter to compile and test your extension. The interpreter displays the count of the total number of objects and this total shouldn’t increase as you repeated run your tests. Make sure to test all the possible exit paths of your code – I’ve forgotten to DECREF objects when an exception occurred.

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

Sidebar

Related Questions

For example, I have a few python scripts, that link together and some libraries
I have put together a script which is very much like the flickr photostream
I have put together this little script which uses a list of words to
I have a few thousand word files which some of my colleagues have put
I have put together the following mootools script window.addEvent('domready', function() { var shouts =
Greetings: I have put together a RESTful web service in .NET 3.5 that takes
I am a powershell novice. After days of searching.... I have put together a
Hello I have problem to put together animations of two separate objects to second
I have a utility that I put together that uses the .NET Framework to
I have MANY small Test Projects where I put together just enough code to

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.