Writing an Extension: 2 questions (so far) 
Author Message
 Writing an Extension: 2 questions (so far)

1) I have the basic skeleton working for an extension. Now I want to know on
the C side when the user does a del <module>. What entry in the PhMethodDef
structure should I add, or is there a global function that is called?

I noticed that after I do import <module> then a del <module>, my DLL is
still in memory. Is this expected. I would have expected it to
reference-counted like everything else, and cleaned up when no longer used.

2) I am writing one function which takes a string, either multibyte or
Unicode. I want to call PyArg_ParseTuple once, and have it convert the
string passed into my desired output (n this case, Unicode). Some thing
like: ParseTuple(args, "u", &ustr); But it appears that the interpreter will
not automatically do this for, or is there a way?

Thank you,
Tyler Wilson



Mon, 21 Jun 2004 23:39:35 GMT  
 Writing an Extension: 2 questions (so far)


Quote:
> 1) I have the basic skeleton working for an extension. Now I want to know
on
> the C side when the user does a del <module>. What entry in the
PhMethodDef
> structure should I add, or is there a global function that is called?

> I noticed that after I do import <module> then a del <module>, my DLL is
> still in memory. Is this expected. I would have expected it to
> reference-counted like everything else, and cleaned up when no longer
used.

Only one answer: someone else can take care of the second question.

Extension modules CANNOT be deleted or reloaded, so it isn't possible to
reclaim the memory occupied by a shared library module containing such an
extension (without terminating the interpreter, which limits you somewhat
:-).

Sorry.

Quote:
> 2) I am writing one function which takes a string, either multibyte or
> Unicode. I want to call PyArg_ParseTuple once, and have it convert the
> string passed into my desired output (n this case, Unicode). Some thing
> like: ParseTuple(args, "u", &ustr); But it appears that the interpreter
will
> not automatically do this for, or is there a way?

regards
 Steve
--
http://www.holdenweb.com/


Mon, 21 Jun 2004 23:52:48 GMT  
 Writing an Extension: 2 questions (so far)
I would expect _some_ kind of destructor-like facility at the
extension-level. In my case, I have to call some Windows init functions
(CoInitialize) on init, and then call the corresponding cleanup functions
(CoUninitialize). Should I:

1) Write the standard DLL entrypoint and handle cleanup there?

2) Add an explicit 'close' method on the module interface?

Thanks,
Tyler


Quote:


> > 1) I have the basic skeleton working for an extension. Now I want to
know
> on
> > the C side when the user does a del <module>. What entry in the
> PhMethodDef
> > structure should I add, or is there a global function that is called?

> > I noticed that after I do import <module> then a del <module>, my DLL is
> > still in memory. Is this expected. I would have expected it to
> > reference-counted like everything else, and cleaned up when no longer
> used.

> Only one answer: someone else can take care of the second question.

> Extension modules CANNOT be deleted or reloaded, so it isn't possible to
> reclaim the memory occupied by a shared library module containing such an
> extension (without terminating the interpreter, which limits you somewhat
> :-).

> Sorry.

> > 2) I am writing one function which takes a string, either multibyte or
> > Unicode. I want to call PyArg_ParseTuple once, and have it convert the
> > string passed into my desired output (n this case, Unicode). Some thing
> > like: ParseTuple(args, "u", &ustr); But it appears that the interpreter
> will
> > not automatically do this for, or is there a way?

> regards
>  Steve
> --
> http://www.holdenweb.com/



Tue, 22 Jun 2004 00:07:47 GMT  
 Writing an Extension: 2 questions (so far)

Quote:
> 2) I am writing one function which takes a string, either multibyte or
> Unicode. I want to call PyArg_ParseTuple once, and have it convert the
> string passed into my desired output (n this case, Unicode). Some thing
> like: ParseTuple(args, "u", &ustr); But it appears that the interpreter will
> not automatically do this for, or is there a way?

No. If a multibyte string is passed, you expect a conversion to a
Py_UNICODE*. Somebody needs to allocate the memory, and you would need
to release it afterwards. However, you don't need release anything if
you are passed a Unicode object. So the API you are proposing cannot
work.

Instead, I recommend to use O&, and a conversion function. This
conversion function should always convert to a PyObject* representing
a Unicode object, by:
- INCREFing the argument if it is a Unicode object, or
- invoking PyUnicode_FromEncodedObject if it is a MBCS string.
Then, your application would get a PyUnicodeObject*, which it would
need to DECREF.

Regards,
Martin



Tue, 22 Jun 2004 02:14:19 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Further Questions

2. Further newbie Haskell questions...

3. Further to my Arexx question;

4. far pointer in assembler question

5. TASM FAR JMP Question

6. Further question on AQS Guidelines for relational tests on reals

7. further simple question about redirecting output

8. Writing Extensions and Control Panels with REALbasic

9. ANN: Pyrex - a language for writing Python extension modules

10. writing a .dll as Rexx extension under OS/2 in C

11. Writing DLL extensions for OS/2 REXX

12. Good guide to writing Ruby extensions in C?

 

 
Powered by phpBB® Forum Software