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

  • Home
  • SEARCH
  • 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 3432116
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:24:34+00:00 2026-05-18T07:24:34+00:00

Take this code: def A(): try: B() except Exception: pass def B(): C() def

  • 0

Take this code:

def A():
   try:
      B()
   except Exception:
      pass

def B():
   C()

def C():
   print exception_handling_pointer()

A()

The function exception_handling_pointer should return me a pointer to the function where this specific exception would be checked first for being handled. I.e., in this case, I would expect the output to be sth. like:

<function A ...>

How can I implement the function exception_handling_pointer?

  • 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-18T07:24:34+00:00Added an answer on May 18, 2026 at 7:24 am

    This is a pretty silly thing to do and most people would say that it can’t be done (THC4k gives compelling evidence of this for the general cace) but it does sound fun and should be perfectly doable in many real use-cases.

    step 1. You need to step back through the frames. Get the first one with sys._getframe or inspect.currentframe (don’t tell anyone, the second seems aliased to the first). Then you can iterate through them with f.f_back

    step 2. Each one will have a f.f_lasti instruction. This is the last instruction that was executed in the frame. You’ll have to save it. Now go backwords through the bytecode – f.f_code.co_code – and look for a SETUP_EXCEPT opcode with an argument that jumps to after f.f_lasti`. The jump point is the exception handling.

    step 3. This is where it gets fuzzier. The key is that the actual comparison operation will be a COMPARE_OP with a 10 as its argument. In all cases that I’ve seen, it’s followed by a POP_JUMP_IF_FALSE. This will jump to the next except clause or the finally clause. It will be preceded by the code that loads loads the exceptions onto the stack. If there is only one, then it will be a straight LOAD_GLOBAL or a LOAD_GLOBAL or LOAD_FAST (depending if the module with the exceptions is global or local) followed by a LOAD_ATTR. If there are multiple exceptions being matched then there will be a sequence of load operations followed by a BUILD_TUPLE (idiomatic) or BUILD_LIST (some other weird or non-idiomatic situation).

    The point is that you can go through the LOAD_X instructions and compare the name to the exception that you’re matching. Note that you’re comparing name only. If they’ve reassigned the name, you’re SOL.

    step 4. Let’s assume that you found a match. Now you need the function object. The best way that I can think of to do this follows (I reserve the right to update): The f.f_code will have a co_filename attribute. You can loop through sys.modules and each one will have __name__attribute. You can compare the two keeping in mind that you should use __name__.endswith(co_filename). When you get a match, you can loop over the modules functions and compare their f.func_code.co_firstlineno attribute with the frames f.f_lineno attribute. When you get a match, you have your function. You should loop over the methods of each class in the module as well. There’s the possibility that the handling is occurring in some nested function in which case, I can’t currently think of a sensible thing to do. (It would be a whole other bytecode hack and would itself be flakey)

    step 5. Profit.

    This should give you the general idea of how to go about doing this. There are all sorts of corner cases where you wont be able to do it but in any normal use-case, you should be able to pull it off. If you write code that depends on being able to do it, it will break though. This is sort of “Do it because I can” sort of thing.

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

Sidebar

Related Questions

Take this code: <?php if (isset($_POST['action']) && !empty($_POST['action'])) { $action = $_POST['action']; } if
The questions says everything, take this example code: <ul id=css-id> <li> <something:CustomControl ID=SomeThingElse runat=server
Take this non-compiling code for instance: public string GetPath(string basefolder, string[] extraFolders) { string
For instance, take this piece of code: var person = new Person(); or for
take a look at this example code: public class Comment { private Comment() {
This one will take some explaining. What I've done is create a specific custom
Take the method System.Windows.Forms.Control.Invoke(Delegate method) Why does this give a compile time error: string
In this particular situation, there are 9 automated steps in a process that take
Take a very simple case as an example, say I have this URL: http://www.example.com/65167.html
Compiling this lines long int sz; char tmpret[128]; //take substring of c, translate in

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.