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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:48:35+00:00 2026-06-13T22:48:35+00:00

Right code example: #include Python.h #include <string> extern const int someConstant; void some_function() {

  • 0

Right code example:

#include "Python.h"
#include <string>

extern const int someConstant;

void some_function()
{
  const char *begin = NULL;
  const char *end = NULL;

  std::string s(begin, end);
  const int v = someConstant;
}

static PyMethodDef _G_methods[] =
{
    {NULL, NULL, 0, NULL}        /* Sentinel */
};

PyMODINIT_FUNC initsf()
{
  PyObject *module;

  if (!(module = Py_InitModule("sf", _G_methods)))
  {
    return;
  }

  PyObject *pyerror = PyErr_NewException("fs.error", NULL, NULL);
  Py_INCREF(pyerror);


  PyModule_AddObject(module, "error", pyerror);
}

This is extension module draft. As simple as possible. It has an empty method table and initializer function copied from original docpage. It contains 2 (two) intentional errors:

  • variable someConstant declared but never defined;

  • function some_function defined, but never called;

If compiled and opened by dlopen/dlsym:

sf.so: undefined symbol: someConstant

as required. But if loaded by Python interpreter:

>>> from sf import *
Segmentation fault (core dumped)

and the most strange is python’s backtrace dumped from core-file:

#0  0x00000bd6 in ?? ()
#1  0xb775c057 in char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) () from /usr/local/lib/python2.7/dist-packages/sf.so
#2  0xb6f9abb6 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) () from /usr/lib/i386-linux-gnu/libstdc++.so.6
#3  0xb6c3fe30 in pkgInitConfig(Configuration&) () from /usr/lib/i386-linux-gnu/libapt-pkg.so.4.12
#4  0xb6cf959e in ?? () from /usr/lib/python2.7/dist-packages/apt_pkg.so
#5  0x081949c1 in PyEval_EvalFrameEx ()
#6  0x0819af70 in PyEval_EvalCodeEx ()
#7  0x0819bb03 in PyImport_ExecCodeModuleEx ()
#8  0x0814bd40 in ?? ()
#9  0x080a38c2 in ?? ()
#10 0x0814c6d4 in ?? ()
#11 0x081031ae in ?? ()
...

It seems, Python’s loader calls std::string constructor :-).

So, stack corrupted. It happens either while loading invalid module or while unloading it after error handled. It never happens if sample code is little changed. This behavior has been observed on Python 2.7.3/Linux Ubuntu 10/gcc 4.6.3 and definitely not shown on Python 2.7.1/FreeBSD 8.1/gcc 4.2.1.

Question:

  1. Is it a Python’s bug or my sample code has errors?
  • 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-06-13T22:48:36+00:00Added an answer on June 13, 2026 at 10:48 pm

    Let’s look at that stack trace again

    #0  0x00000bd6 in ?? ()
    #1  0xb775c057 in char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) () from /usr/local/lib/python2.7/dist-packages/sf.so
    #2  0xb6f9abb6 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) () from /usr/lib/i386-linux-gnu/libstdc++.so.6
    #3  0xb6c3fe30 in pkgInitConfig(Configuration&) () from /usr/lib/i386-linux-gnu/libapt-pkg.so.4.12
    #4  0xb6cf959e in ?? () from /usr/lib/python2.7/dist-packages/apt_pkg.so
    

    So a function in libapt-pkg.so calls a function in libstdc++.so which calls a function in your module.

    Your functions are never getting called. However, your code uses std::string and instantiates some functions for std::string, those functions get included into your *.so, override those used by a completely different *.so, and crash for some reason I’m not entirely sure why.

    My instincts tell me that you used gcc to create your *.so instead of g++. You won’t get an error at link time because linking shared objects doesn’t work that way. You won’t get an error at load time because libstdc++ is coincidentally already loaded.

    Are you using gcc or g++ to link? Try using g++.

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

Sidebar

Related Questions

Let's take this example code: #include <stdio.h> int main(void){ int x = 1; if(*(char
Right now I'm trying this: #include <stdio.h> int main(int argc, char *argv[]) { if
Consider following example code: #include <iostream> #include <inttypes.h> using namespace std; int f(uint32_t i)
How i can get full right name of generic type? For example: This code
I have a problem with this code right here: - (void)fetchedData:(NSData *)responseData { //parse
Example code taken from another SO Question var test = context.Tests .Include(Question.QuestionLocale) .FirstOrDefault(); If
Is there some relatively simple way to programmatically include source code lines to python
I have this code for example. #include <stdlib.h> #include <stdio.h> #define array_size 3 typedef
What's the right kind of validation to include for subordinate models? For example, let's
I have this code right here, where the $friends variable is an array with

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.