[FAQTS] Python Knowledge Base Update -- July 26th, 2000

Fiona Czuczman fiona at sitegnome.com
Wed Jul 26 09:06:20 EDT 2000


Hi All,

The latest entries into http://python.faqts.com

regards,

Fiona Czuczman

Including:

- Manipulating PNG files using Win32 PIL
- How can I map the read() function of a file object?
- What is the fastest way to search a binary file for a certain byte 
pattern (**** in my case)?
- Embedding Python code in a C++ program, I get dynamic linking errors 
if I try to import any non-built in module, what's wrong?


## New Entries #################################################


-------------------------------------------------------------
Manipulating PNG files using Win32 PIL
http://www.faqts.com/knowledge-base/view.phtml/aid/5025
-------------------------------------------------------------
Fiona Czuczman
Olivier A. Dagenais

Problem:

I've writen a script to generate PNG images using PIL.  It works a treat
under Linux, but when I try to port it to MSWindows, the
PIL/PngImagePlugin spits saying that I don't have a zip encoder. I
already have a copy of pkzip, but I got hold of a copy of gzip.exe and
stuck it in C:\Windows\zip.exe but to no avail. Can anyone confirm that
I need a zip executable, or is the PIL wanting a DLL, etc?

Solution:

http://www.pythonware.com/downloads.htm

Says this, near the end of the page:

"Optional support libraries must be downloaded separately: jpeg and 
zlib."

...you'll want to follow the zlib link:

ftp://ftp.freesoftware.com/.0/infozip/zlib/zlib.html

The zlib library is used for manipulating PNG files...


-------------------------------------------------------------
How can I map the read() function of a file object?
http://www.faqts.com/knowledge-base/view.phtml/aid/5028
-------------------------------------------------------------
Fiona Czuczman
Peter Schneider-Kamp, Bjorn Pettersen

Just use a lambda like this:

map(lambda f:open(f,"r").read(), list_of_filenames)

or perhaps:

  def openAndRead(filename):
      return open(filename).read()

  map( openAndRead, listOfFilenames )


-------------------------------------------------------------
What is the fastest way to search a binary file for a certain byte pattern (**** in my case)?
http://www.faqts.com/knowledge-base/view.phtml/aid/5029
-------------------------------------------------------------
Fiona Czuczman
Bjorn Pettersen

Use:

import string
string.find( open('my_binary_file','rb').read(), '****' )


-------------------------------------------------------------
Embedding Python code in a C++ program, I get dynamic linking errors if I try to import any non-built in module, what's wrong?
http://www.faqts.com/knowledge-base/view.phtml/aid/5030
-------------------------------------------------------------
Fiona Czuczman
Ted Drain

Problem:

I am trying to learn how to embed Python code in a C++ program. I got
the example from the docs (the one that imports sys and prints out a
couple of strings -- search from initxyzzy in google). The problem I get
is that if I try to import any non-built in module, I get dynamic
linking errors. For example, adding the following line 31 works:

_______________________________________________________________________
...
     29         /* Execute some Python statements (in module __main__)
*/
     30         PyRun_SimpleString("import sys\n");
-->  31         PyRun_SimpleString("import thread\n");
     32         PyRun_SimpleString("print sys.builtin_module_names\n");
...
_______________________________________________________________________

but adding the following line instead:

_______________________________________________________________________
...
     29         /* Execute some Python statements (in module __main__)
*/
     30         PyRun_SimpleString("import sys\n");
-->  31         PyRun_SimpleString("import random\n");
     32         PyRun_SimpleString("print sys.builtin_module_names\n");
...
_______________________________________________________________________

produces:

_______________________________________________________________________
[lopes at groucho lopes]$ ./demo 
Hello, brave new world

Traceback (innermost last):
  File "<string>", line 1, in ?
  File "/usr/lib/python1.5/random.py", line 22, in ?
    import whrandom
  File "/usr/lib/python1.5/whrandom.py", line 140, in ?
    _inst = whrandom()
  File "/usr/lib/python1.5/whrandom.py", line 46, in __init__
    self.seed(x, y, z)
  File "/usr/lib/python1.5/whrandom.py", line 58, in seed
    import time
ImportError: /usr/lib/python1.5/lib-dynload/timemodule.so: undefined
symbol: PyExc_IOError
('__builtin__', '__main__', 'imp', 'marshal', 'pcre', 'posix', 'regex',
'signal', 'sys', 'thread')
['xyzzy', 'os.path', 'os', 'exceptions', '__main__', 'posix',
'whrandom', 'sys', '__builtin__', 'site', 'random', 'signal',
'UserDict', 'posixpath', 'stat']
./demo
['./demo']
_______________________________________________________________________


Solution:

If you're building w/ gcc, add the link line option:

-Wl,-E

This says to pass the -E option to the linker.  This option tells the
linker to make all statically linked symbols available to the dynamic
libraries.  Without it, the symbols linked from libpython1.5.a aren't 
seen by symbols in the dynamic libraries.







More information about the Python-list mailing list