Petr Viktorin
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # PyTime API error handling See https://github.com/capi-workgroup/decisions/issues/15 - Add pros/cons. - Add yourself to the proposal(s) if you think we should consider them. - Remove yourself from proposals if you don't want them (e.g. there's a better proposal) - Remove proposals with no names left (i.e. ones no one wants). This is *not* a place for discussion; use GitHub/Discord/Discourse for that. ## Naming Bikeshed the name too: ### `Unchecked` suffix cons: - Not true; the result is checked ### `NoGIL` suffix (@encukou) ### `Unlocked` suffix ### `NoException` suffix (@vstinner) ### `Raw` suffix (Guido, @encukou) ### `PyOS_` prefix (Steve) ## Examples of errors * RuntimeError: QueryPerformanceFrequency() returns an invalid frequency. * RuntimeError: mach_timebase_info() returns an invalid value. * PermissionError: clock_gettime() fails with EACCESS or EPERM. Can happen in a sandbox. * OverflowError: fail to convert clock result to PyTime_t. For example, overflow on converting 64-bit time_t seconds (in timeval or timespec) to nanoseconds. * (unlikely) OSError: other clock_gettime() errors. ## "Unchecked" variants; return -1 on error (@zooba, @encukou, @vstinner) ``int PyTime_TimeUnchecked(PyTime_t *result)`` (To get exception details, the caller needs to acquire the GIL and re-try with `PyTime_Time`.) Example logging an unraisable exception with the GIL if getting the time fails: ```c // Function called without holding the GIL PyTime_t get_time(void) { PyTime_t t; if (PyTime_TimeUnchecked(&t) < 0) { // Get the GIL to call PyTime_Time() PyGILState_STATE state = PyGILState_Ensure(); if (PyTime_Time(&t) < 0) { // Handle the exception: // log an unraisable exception. PyErr_FormatUnraisable("failed to get time"); t = 0; } PyGILState_Release(gstate); } return t; } ``` ### Pros - doesn't require the GIL - maybe: doesn't require initialized runtime - doesn't conflate valid return values with error codes - doesn't require caller to check for errors - allows caller to check for errors ### Cons - No information about reason for failure - No clear existing use case - Doesn't distinguish overflow from other errors ## "Unchecked" variants; return errno (@zooba) ``int PyTime_Time(PyTime_t *result)`` ### Pros - doesn't require the GIL ### Cons - An *errno* is not always available. - No clear existing use case. - If we don't use errno, we need to maintain our own list of error codes which can cause some maintaince burden for the stable ABI. ## Require the GIL (current main branch) (@encukou, @zooba) ### Pros - simple API (no new functions) ### Cons - Having to acquire the GIL just to get the time in a cross-platform way seems … excessive. ## Rejected APIs ### Return PyTime_t with error argument API: ``PyTime_t PyTime_TimeUnchecked(int *error)`` * Set error to 0 and return time on success. * Set error to 1 and return 0 on error. * error can be NULL. On error, call again PyTime_Time() to get a regular Python exception with a nice type and error message. Example: ```c // Function called without holding the GIL PyTime_t benchmark(void (*func)(void)) { PyTime_t t1 = PyTime_TimeUnchecked(NULL); func(); PyTime_t t2 = PyTime_TimeUnchecked(NULL); return t2 - 1; } ``` Example: ```c // Function called without holding the GIL void get_time(void) { int failed; PyTime_t t = PyTime_TimeUnchecked(&failed); if (failed) { // Get the GIL to call PyTime_Time() PyGILState_STATE state = PyGILState_Ensure(); if (PyTime_Time(&t) < 0) { // Handle the exception: log an unraisable // exception. PyErr_FormatUnraisable("failed to get time"); } else { failed = 0; } PyGILState_Release(gstate); } if (!failed) { printf("current time is %lld\n", (long long)t); } } ``` #### Pros - return value is directly time: it's more convenient (see the benchmark example). - doesn't require caller to check for errors. - errors are not silently ignored, unless the caller pass NULL on purpose. ### Cons - caller doesn't know the error cause, only that the function failed. - No clear existing use case ### Log unraisable exception API: ``PyTime_t PyTime_TimeUnchecked(void)`` **Change** PyTime to not report errors to the caller, but **log** errors as an **unraisable exception** (logged by `sys.unraisablehook`, to stderr by default) and return 0 on error. Acquire the GIL to raise an exception and then log it, then release the GIL. So the common case remains efficient: don't touch the GIL. The caller doesn't need to hold the GIL. #### Pros - doesn't require caller to check for errors #### Cons - Errors can pass silently - No clear existing use case - Acquires the GIL internally (in the unlikely error case)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully