Skip to content

Documentation example of module init function lacks error handling #84460

Closed

Description

@scoder
BPO 40279
Nosy @scoder, @caje731
PRs
  • bpo-40279 Add some error-handling to the module initialisation example #19705
  • [3.8] bpo-40279: Add some error-handling to the module initialisation docs example (GH-19705) #19710
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-04-25.05:46:25.518>
    created_at = <Date 2020-04-14.06:08:29.273>
    labels = ['easy', 'type-feature', '3.8', '3.9', 'docs']
    title = 'Documentation example of module init function lacks error handling'
    updated_at = <Date 2020-04-25.05:46:25.517>
    user = 'https://github.com/scoder'

    bugs.python.org fields:

    activity = <Date 2020-04-25.05:46:25.517>
    actor = 'scoder'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2020-04-25.05:46:25.518>
    closer = 'scoder'
    components = ['Documentation']
    creation = <Date 2020-04-14.06:08:29.273>
    creator = 'scoder'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 40279
    keywords = ['patch', 'easy', 'newcomer friendly']
    message_count = 5.0
    messages = ['366368', '367234', '367257', '367258', '367259']
    nosy_count = 3.0
    nosy_names = ['scoder', 'docs@python', 'cajetan.rodrigues']
    pr_nums = ['19705', '19710']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue40279'
    versions = ['Python 3.8', 'Python 3.9']

    Activity

    1. scoder commented on Apr 14, 2020

      @scoder
      ContributorAuthor

      The example in the docs that shows how to initialise an embedded module gives a wrong impression about error handling. Most of the functions that it calls return error codes, but those do not get looked at. Innocent users who copy and paste the example may miss some of them when adapting the code, thus ending up with an unsafe implementation.

      The example should at least make it clear where error handling is needed.

      https://docs.python.org/3/extending/extending.html#the-module-s-method-table-and-initialization-function

      int
      main(int argc, char *argv[])
      {
          wchar_t *program = Py_DecodeLocale(argv[0], NULL);
          if (program == NULL) {
              fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
              exit(1);
          }
      
          /* Add a built-in module, before Py_Initialize */
          PyImport_AppendInittab("spam", PyInit_spam);
      
          /* Pass argv[0] to the Python interpreter */
          Py_SetProgramName(program);
      
          /* Initialize the Python interpreter.  Required. */
          Py_Initialize();
      
          /* Optionally import the module; alternatively,
             import can be deferred until the embedded script
             imports it. */
          PyImport_ImportModule("spam");
      ...
      
          PyMem_RawFree(program);
          return 0;
      }
    2. added
      docsDocumentation in the Doc dir
      type-featureA feature request or enhancement
      on Apr 14, 2020
    3. caje731 commented on Apr 24, 2020

      caje731mannequin
      Mannequin

      At the risk of sounding like a jerk, I'd very much want to say that copy-pasters should get what they deserve :) But then I remember I was once a copy-paster too!

      Hello again, Stefan :)

      Thanks for watching out for the community - I'll be happy to fix this!

    4. scoder commented on Apr 25, 2020

      @scoder
      ContributorAuthor

      New changeset d4f3923 by Cajetan Rodrigues in branch 'master':
      bpo-40279: Add some error-handling to the module initialisation docs example (GH-19705)
      d4f3923

    5. scoder commented on Apr 25, 2020

      @scoder
      ContributorAuthor

      New changeset 882a7f4 by Miss Islington (bot) in branch '3.8':
      bpo-40279: Add some error-handling to the module initialisation docs example (GH-19705) (GH-19710)
      882a7f4

    6. scoder commented on Apr 25, 2020

      @scoder
      ContributorAuthor

      Hi Cajetan, thank you for your contribution! That's a nice documentation improvement.

    7. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.8 (EOL)end of life3.9 (EOL)end of lifedocsDocumentation in the Doc direasytype-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions