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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:26:20+00:00 2026-06-15T19:26:20+00:00

I’m working with the pylibtiff library (hosted here ) and I’m trying to extend

  • 0

I’m working with the pylibtiff library (hosted here) and I’m trying to extend class TIFF(ctypes.c_void_p). I’m having trouble with the following method:

# lib has been loaded with the path to libtiff.so, or equivalent
libtiff = ctypes.cdll.LoadLibrary(lib)

class TIFF(ctypes.c_void_p):
    @classmethod
    def open(cls, filename, mode='r'):
        """ Open tiff file as TIFF.
        """
        tiff = libtiff.TIFFOpen(filename, mode)
        if tiff.value is None:
            raise TypeError ('Failed to open file '+`filename`)
        return tiff

(full class here)

Now, the expected use pattern here is that we open a file and receive a class TIFF instance, like this:

import libtiff
t = libtiff.TIFF.open('myfile.tiff')

# e.g. grab a numpy array of the data:
arr = t.read_image()

What is this magic? How is the C variable from libtiff typed as a TIFF* arriving back in pythonland and automagically becoming a class TIFF instance? presumably this has something to do with the subclassing of ctypes.c_void_p

I ask because I am trying to override a method on class TIFF:

import libtiff
class TIFFdifferent(libtiff.TIFF):
    def read_image(self, verbose=False, different=False):
        if not different:
            return libtiff.TIFF.read_image(self, verbose)

        # my different code ...

however, when I try to create a class TIFFdifferent instance, I get a class TIFF instead:

In [3]: t = libtiffdifferent.TIFFdifferent.open('file.tiff')

In [4]: arr = t.read_image(different=True)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-8bdc24ec874c> in <module>()
----> 1 arr = t.read_image(different=True)

TypeError: read_image() got an unexpected keyword argument 'different'

In [5]: t.read_image?
Type:       instancemethod
String Form:<bound method TIFF.read_image of <TIFF object at 0x10b67df80>>
File:       /Library/Python/2.7/site-packages/libtiff/libtiff_ctypes.py
Definition: t.read_image(self, verbose=False)
Docstring:
Read image from TIFF and return it as an array.


In [6]: t
Out[6]: <TIFF object at 0x10b67df80>

So, what I need to do is to override open as well – which I don’t know how to do without understanding the magic transformation from C TIFF* to python class TIFF instance, without a constructor, cast or anything else explicit.

  • 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-15T19:26:22+00:00Added an answer on June 15, 2026 at 7:26 pm

    The magic is at line 1041 of libtiff_ctypes.py:

    libtiff.TIFFOpen.restype = TIFF
    

    As described in 15.17.1.8 Return types in the ctypes docs, you don’t have to use a ctypes type as the result type; you can specify any Python callable—typically either a function, which will validate the real C return value to raise an exception, or a class, which will take the real C return value as its constructor argument.

    You may notice that the TIFF class doesn’t define an __init__ method. This is where the inheritance from c_void_p comes in: You can construct a c_void_p from a pointer; therefore, if you have a subclass of c_void_p, and it doesn’t need to do any other initialization, it can just rely on the base-class construction to take care of it.

    So, what I need to do is to override open as well – which I don’t know how to do without understanding the magic transformation from C TIFF* to python class TIFF instance, without a constructor, cast or anything else explicit.

    Well, you obviously could just do an explicit construction call in your override, even if the original version doesn’t do it:

    def myopen(*args):
        tiffpointer = libtiff.cfunc(*args)
        return TIFF(tiffpointer)
    

    But you’d only want to do this if you can’t just define libtiff.cfunc.restype (e.g., because some other code in pylibtiff relies on the fact that it return something different).

    Anyway, I’m not sure this is what you actually want to do. What you’re trying to do is write something like this:

    class TIFFDifferent(TIFF):
        …
        @classmethod
        def open(cls, filename, mode='r'):
            tiff = libtiff.TIFFOpen(filename, mode)
            if tiff.value is None:
                raise TypeError ('Failed to open file '+`filename`)
            return cls(tiff)
    

    That works, without you having to understand the magic behind restype at all. Unless you’re worried about the cost of constructing a short-lived temporary TIFF object (which, as you’ve seen, has no members and no constructor code beyond those of a c_void_p), isn’t this simple enough?

    One more possibility, which I wouldn’t normally suggest, and probably isn’t appropriate here, but might be: If pylibtiff is designed in such a way that overriding its behavior via subclasses is hard to do, you can either (a) fork the code and use your own version, or (b) monkeypatch it at runtime. As suggested in your own comment, something like this:

    class TIFFDifferent(TIFF):
        …
        @classmethod
        def open(cls, filename, mode='r'):
            stash = libtiff.TIFFOpen.restype
            libtiff.TIFFOpen.restype = cls
            tiff = libtiff.TIFFOpen(filename, mode)
            libtiff.TIFFOpen.restype = stash
            if tiff.value is None:
                raise TypeError ('Failed to open file '+`filename`)
            return tiff
    

    You’d want to wrap the stashing up in a contextmanager (or use a try:/finally:), if your constructor could ever possibly throw an exception; otherwise you’ll leave the restype patched.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.