title: How does Python access OS API functions such as socket()?
body: <p>UPDATED Questions:</p>
  
  <ol>
  <li><p>Where does the socket object actually get created? I found this at line 4188 in <a href="https://github.com/python/cpython/blob/master/Modules/socketmodule.c" rel="nofollow noreferrer">socketmodule.c</a>, but it looks like it's called sock_new not socket?</p>
  
  <pre><code>static PyObject *sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  </code></pre></li>
  <li><p>Is there some convention for figuring out where a module like socketmodule.c is imported? In other words, when I see a "from _socket import *" who do I know what that's importing (without searching the whole repository)?</p></li>
  </ol>
  
  <hr>
  
  <p>ORIGINAL:</p>
  
  <pre><code>sock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  </code></pre>
  
  <p>I'm trying to understand how this code works specifically how/where Python actually makes the OS function call to socket():</p>
  
  <pre><code>class _socketobject(object):
  
      __doc__ = _realsocket.__doc__
  
      __slots__ = ["_sock", "__weakref__"] + list(_delegate_methods)
  
      def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, _sock=None):
          if _sock is None:
              _sock = _realsocket(family, type, proto)
          self._sock = _sock
          for method in _delegate_methods:
              setattr(self, method, getattr(_sock, method))
  </code></pre>
  
  <p>When I look up BSD sockets on Wikipedia I see this example which makes sense because the socket function is defined under types.h. In the above I see a call to realsocket which look like an OS function call, but I don't realsocket defined anywhere (I don't see anything about sockets at all in the Python27/include headers).</p>
  
  <pre><code>  /* Server code in C */
  
    #include &lt;sys/types.h&gt;
    #include &lt;sys/socket.h&gt;
    #include &lt;netinet/in.h&gt;
    #include &lt;arpa/inet.h&gt;
    #include &lt;stdio.h&gt;
    #include &lt;stdlib.h&gt;
    #include &lt;string.h&gt;
    #include &lt;unistd.h&gt;
  
    int main(void)
    {
      struct sockaddr_in stSockAddr;
      int SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  </code></pre>